We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Some points as also mentioned by other programmers
1) You can't cancel any 0, irrespective of whatever position it is at.
2) 0's at end are allowed as long as point 1 is followed.
3) You have to 'cancel' common digits in numerator and denominator not reduce the fractions, and hence the order of digits can be any.
4) Don't repeat count fractions
100points/- python 3
importitertoolsdeflist_adder(strings,number):'''Create all possible strings with number added at all indices in string in strings'''l=len(strings[0])+1#+1, because I also have to add number at last position lis=[string[:i]+str(number)+string[i:]forstringinstringsforiinrange(l)]returnlisdefpermuter(lis1,lis2):'''It creates all possible permutations with lis1 as unordered and lis2 as ordered'''whilelis1:n=lis1.pop()lis2=list_adder(lis2,n)returnset(lis2)deflis_to_str(list1):'''Take in a sequence of numbers and returns a str of them'''return''.join([str(no)fornoinlist1])deflis_to_int(list1):'''Take in a sequence of numbers and return int formed by combining them'''returnint(''.join([str(no)fornoinlist1]))item=input().split()n=int(item[0])k=int(item[1])li1=list(itertools.combinations_with_replacement(range(1,10),r=k))#This is the list of possible combinations of numbers which are common in numerator and denominatorli2=list(itertools.product(range(10),repeat=n-k))#The ordered combinations of other digits in numerator or denominatorli2.remove((0,)*(n-k))# after cancelling common digits we should'nt be left with something like 00/00 or 00/12 etcanswers=set()#so that I don't count repeated fractionsli3=[]# it contains for a particular element in li all the possible casses of elements in li2 with the numerator or denominator as in the format (12,1234) all stored in a listforiinli1:li4=[]forjinli2:li4+=[(lis_to_int(j),int(i))foriinpermuter(list(i),[lis_to_str(j)])ifi[0]!='0']# we should'nt get number like 0123 for n=4li3.append(li4)foriinli3:foriteminitertools.combinations(i,r=2):# I just used combinations instead of permutations cause the left over numerator should be less than left over denominator and the storing order was lexicographicalifitem[0][0]<item[1][0]anditem[0][0]*item[1][1]==item[1][0]*item[0][1]:answers.add((item[0][1],item[1][1]))print(sum([a[0]forainanswers]),sum([a[1]forainanswers]))
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #33: Digit canceling fractions
You are viewing a single comment's thread. Return to all comments →
Some points as also mentioned by other programmers
1) You can't cancel any 0, irrespective of whatever position it is at.
2) 0's at end are allowed as long as point 1 is followed.
3) You have to 'cancel' common digits in numerator and denominator not reduce the fractions, and hence the order of digits can be any.
4) Don't repeat count fractions
100points/- python 3
`