HackerRank Python Solution - Itertools Topic - Compress the String!

  • In this task, we would like for you to appreciate the usefulness of the groupby() function of itertools.
  • You are given a string S. Suppose a character 'c' occurs consecutively X times in the string. Replace these consecutive occurrences of the character 'c' with (X,c) in the string.
  • For a better understanding of the problem, check the explanation.
Input Format:

A single line of input consisting of the string S.

HackerRank Python Solution - Itertools Topic - itertools.combinations_with_replacement()

itertools.combinations_with_replacement(iterable, r):
  • This tool returns r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.
  • Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.
Sample Code:

>>> from itertools import combinations_with_replacement
>>> 
>>> print list(combinations_with_replacement('12345',2))
[('1', '1'), ('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '2'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '3'), ('3', '4'), ('3', '5'), ('4', '4'), ('4', '5'), ('5', '5')]
>>> 
>>> A = [1,1,3,3,3]
>>> print list(combinations(A,2))
[(1, 1), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (3, 3), (3, 3), (3, 3)]

HackerRank Python Solution - Itertools Topic - itertools.combinations()

itertools.combinations(iterable, r):
  • This tool returns the length subsequences of elements from the input iterable.
  • Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.
Sample Code:

>>> from itertools import combinations
>>> 
>>> print list(combinations('12345',2))
[('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')]
>>> 
>>> A = [1,1,3,3,3]
>>> print list(combinations(A,4))
[(1, 1, 3, 3), (1, 1, 3, 3), (1, 1, 3, 3), (1, 3, 3, 3), (1, 3, 3, 3)]

HackerRank Python Solution - Itertools Topic - itertools.permutations()

itertools.permutations(iterable[, r]):
  • This tool returns successive length permutations of elements in an iterable.
  • If r is not specified or is None, then r defaults to the length of the iterable, and all possible full-length permutations are generated.
  • Permutations are printed in a lexicographic sorted order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.
Sample Code:
>>> from itertools import permutations
>>> print permutations(['1','2','3'])
<itertools.permutations object at 0x02A45210>
>>> 
>>> print list(permutations(['1','2','3']))
[('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')]
>>> 
>>> print list(permutations(['1','2','3'],2))
[('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')]
>>>
>>> print list(permutations('abc',3))
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]

HackerRank Python Solution - Itertools Topic - itertools.product()

itertools.product():
  • This tool computes the cartesian product of input iterables.
  • It is equivalent to nested for-loops.
  • For example, product(A, B) returns the same as ((x,y) for x in A for y in B).
Sample Code:

>>> from itertools import product
>>>
>>> print list(product([1,2,3],repeat = 2))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
>>>
>>> print list(product([1,2,3],[3,4]))
[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
>>>
>>> A = [[1,2,3],[3,4,5]]
>>> print list(product(*A))
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
>>>
>>> B = [[1,2,3],[3,4,5],[7,8]]
>>> print list(product(*B))
[(1, 3, 7), (1, 3, 8), (1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 3, 7), (2, 3, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (3, 3, 7), (3, 3, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8)]

HackerRank Python Solution - Math Topic - Triangle Quest

  • You are given a positive integer N. Print a numerical triangle of height N-1 like the one below:
1
22
333
4444
55555
......
  • Can you do it using only arithmetic operations, a single for loop, and print statement?
  • Use no more than two lines. The first line (the for statement) is already written for you. You have to complete the print statement.
  • Note: Using anything related to strings will give a score of 0.

HackerRank Python Solution - Math Topic - Integer Come In All Sizes

  • Integers in Python can be as big as the bytes in your machine's memory. There is no limit in size as there is: 231-1 (c++ int) or 263-1 (C++ long long int).
  • As we know, the result of ab grows really fast with increasing b.
  • Let's do some calculations on very large integers.

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...