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.
For those who are too much frustrated, here is my code. Sometimes, it passes all tests cases, sometimes 1 or 2 end in "timeout".
# Enter your code here. Read input from STDIN. Print output to STDOUTfromrandomimportrandrange# Contains the possibilites for each digit of the 12-digits solutionpossibilities=[[str(i)foriinrange(10)]foriinrange(12)]attempts={}n=input()# Avoid problem with test case 3ifn!="":n=int(n)else:n=int(input())# Fill an object containing the test cases in list, key is their score# Ex : attempts = { "0" : ["123456789012", "234567890123"], "1" : [...] until "3"}foriinrange(n):attempt,success=input().split()try:attempts[success].append(attempt)exceptKeyError:attempts[success]=[attempt]# If one test case has score 0, all of its digits can be removed from the possibilities# at the corresponding indexif"0"inattempts:forattemptinattempts["0"]:fori,einenumerate(attempt):ifeinpossibilities[i]:possibilities[i].remove(e)# Starting at "000000000000" or better if test cases with 0 were foundstring="".join([p[-1]forpinpossibilities])bestScore=100# While we don't have the solution (matching the same difference scores as with test cases)whilebestScore>0:intermediate_best_score,intermediate_best_string=100,""# We try to change one digit, the one who gives the best score# At each step, we check the score of each string if we change only one of its digits# Using the digits available in possibilitiesforiinrange(12):forjinpossibilities[i]:stringToTest=string[:i]+j+string[i+1:]score=0forkinrange(1,4):ifstr(k)inattempts:attempt=attempts[str(k)]fortestCaseinattempt:intermediateScore=sum([1foriinrange(12)iftestCase[i]==stringToTest[i]])score+=abs(intermediateScore-k)ifscore>=intermediate_best_score:break#togaintimeandpassthelasttestcasesifscore>=intermediate_best_score:break#togaintimeandpassthelasttestcasesifscore<intermediate_best_score:intermediate_best_score=scoreintermediate_best_string=stringToTest# if the best score does not change (ie: local mininum), we go from a random new combination, and do everything again (at least 10 * 12 = 120 score computations...)ifintermediate_best_score==bestScore:string="".join([p[randrange(len(p))]forpinpossibilities])# else, we go on with the changed digit until we get the good solutionelse:bestScore=intermediate_best_scorestring=intermediate_best_stringprint(string)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #185: Number Mind
You are viewing a single comment's thread. Return to all comments →
For those who are too much frustrated, here is my code. Sometimes, it passes all tests cases, sometimes 1 or 2 end in "timeout".