Skip to content

Instantly share code, notes, and snippets.

@bipinkrish
Created August 4, 2022 11:25
Show Gist options
  • Save bipinkrish/506b2cc76237ae6cc56ed1d32769a801 to your computer and use it in GitHub Desktop.
Save bipinkrish/506b2cc76237ae6cc56ed1d32769a801 to your computer and use it in GitHub Desktop.
I Will Guess Your Number
# functions
def decimalToBinary(n):
return bin(n).replace("0b", "").zfill(size)
def binaryToDecimal(n):
return int(n,2)
def printNumbers(numList):
ele = 0
while ele < len(numList):
i = 0
while i < 5 and ele < len(numList):
if numList[ele] > N:
return
print(f"\t{numList[ele]}", end = "")
ele = ele + 1
i = i + 1
print()
def generateNumbers(i):
numList = []
for j in range(1,N+1):
if int(decimalToBinary(j)[-i]):
numList.append(j)
printNumbers(numList)
# main
N = input("Enter the Upper Limit 1 - ? ")
while (not N.isnumeric()) or (N == "0"):
print("\nWrong Input, Enter only Positve non-zero Number")
N = input("Enter the Upper Limit 1 - ? ")
N = int(N)
size = len(bin(N).replace("0b", ""))
print(f"Take a Number between 0 - {N}, and i will guess it in {size} or less steps")
input("Ready ? press Enter to continue... ")
print("\n")
binary = ""
for i in range(1,size+1):
generateNumbers(i)
print("\n")
take = input("is your number in this list? 0 for No & 1 for Yes : ")
while not (take == "0" or take == "1"):
print("\nWrong Input, Enter Input Again")
take = input("is your number in this list? 0 for No & 1 for Yes : ")
binary = binary + take
print("\n")
binary = binary[::-1]
number = binaryToDecimal(binary)
if number == 0 or number > N:
print(f"\t\tI said in between 1 - {N}\n")
else:
print("\t\tYour number is",number,"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment