HackerRankPythonPython InterviewRandom

HackerRank itertools.combinations_with_replacement() solution in python

itertools.combinations_with_replacement(iterable, r)
This tool returns  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)]

Task

You are given a string .
Your task is to print all possible size  replacement combinations of the string in lexicographic sorted order.

Input Format

A single line containing the string  and integer value  separated by a space.

Constraints


The string contains only UPPERCASE characters.

Output Format

Print the combinations with their replacements of string  on separate lines.

Sample Input

HACK 2

Sample Output

AA
AC
AH
AK
CC
CH
CK
HH
HK
KK
In this itertools.combinations_with_replacement() problem we need to develop a python program that can read a string and then we need to use the itertool module and print all possible combinations of string in the lexicographically sorted order on the output screen.

Problem solution in Python 2 programming.

from itertools import combinations_with_replacement

S,k = raw_input().split()
k = int(k)

print 'n'.join(sorted(''.join(sorted(c)) for c in combinations_with_replacement(S,k)))

Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations_with_replacement
s, n = input().split()
print(*[''.join(i) for i in combinations_with_replacement(sorted(s), int(n))], sep="n")

Problem solution in pypy programming.

from itertools import combinations_with_replacement
source, combos = raw_input().split()
sstr = sorted(source)
for combo in combinations_with_replacement(sstr,int(combos)):
print ''.join(combo)

Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations_with_replacement
s,k = input().split()
print('n'.join(map(''.join,combinations_with_replacement(sorted(s),int(k)))))





Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science