python - When printing outputs an empty line appears before my outputs -


i have attempted write program asks user string , number (on same line) , prints possible combinations of string size of number. output format should be: capitals, each combination on each line, length of combination(shortest first) , in alphabetical.

my code outputs right combinations in right order places empty before outputs , i'm not sure why.

from itertools import combinations allcombo = [] s = input().strip() inputlist = s.split() k = int(inputlist[1]) s = inputlist[0]  # l in range(0, k+1):     allcombo = []      pos in combinations(s, l):          pos = sorted(pos)         pos = str(pos).translate({ord(c): none c in "[]()', "})         allcombo.append(pos)          allcombo = sorted(allcombo)       print(*allcombo, sep = '\n') 

input:

hack 2 

output:

(empty line) c h k ac ah ak ch ck hk 

also i've been coding week if show me how write properly, i'd pleased.

observe line:

for l in range(0, k+1) # notice l starting @ 0. 

now, observe line:

for pos in combinations(s, l) 

so, have following during our first iteration of inner loop:

for pos in combinations(s, 0) # empty collection during first loop. 

basically no work being performed inside loop because there nothing iterate over, , being printing empty string.

change following code:

for l in range(0, k+1) 

to this:

for l in range(1, k+1) # skips empty collection since l starts @ 1. 

and fix problem.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -