#returns 0 if digit isnt contained, 1 if it is but wrong postion, and 2 if same postion def compare(number, check): numberString = str(number) checkString = str(check) outputString = '' currentIndexOfNumber = 0 for digit in numberString: try: indexInCheck = checkString.index(digit) except ValueError: indexInCheck = -1 if(indexInCheck == -1): outputString = outputString + '0' elif(indexInCheck == currentIndexOfNumber): outputString = outputString + '2' elif(indexInCheck >= 0): outputString = outputString + '1' else: outputString = outputString + 'X' currentIndexOfNumber = currentIndexOfNumber + 1 return outputString #run through all 000-999 options and check each comparison to the hints for firstDigit in range(0,10): for secondDigit in range(0,10): for thirdDigit in range(0,10): stringOfCurrentNumber = str(firstDigit) + str(secondDigit) + str(thirdDigit) check1 = False check2 = False check3 = False check4 = False check5 = False checkNum1 = 682 checkNum2 = 614 checkNum3 = 206 checkNum4 = 738 checkNum5 = 780 # 6 8 2 one # is correct and well placed compareVar = compare(stringOfCurrentNumber, checkNum1) if(compareVar.count('0') == 2 and compareVar.count('1') == 0 and compareVar.count('2') == 1): check1 = True # 6 1 4 one # is correct but wrong placed compareVar = compare(stringOfCurrentNumber, checkNum2) if(compareVar.count('0') == 2 and compareVar.count('1') == 1 and compareVar.count('2') == 0): check2 = True # 2 0 6 two #s are correct but wrong placed compareVar = compare(stringOfCurrentNumber, checkNum3) if(compareVar.count('0') == 1 and compareVar.count('1') == 2 and compareVar.count('2') == 0): check3 = True # 7 3 8 nothing is correct compareVar = compare(stringOfCurrentNumber, checkNum4) if(compareVar.count('0') == 3 and compareVar.count('1') == 0 and compareVar.count('2') == 0): check4 = True # 7 8 0 one # is correct but wrong placed compareVar = compare(stringOfCurrentNumber, checkNum5) if(compareVar.count('0') == 2 and compareVar.count('1') == 1 and compareVar.count('2') == 0): check5 = True if(check1 and check2 and check3 and check4 and check5): print stringOfCurrentNumber
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question