Skip to content

Instantly share code, notes, and snippets.

@VitamintK
Last active August 29, 2015 14:16
Show Gist options
  • Save VitamintK/1f90dcdcdae8fa35f179 to your computer and use it in GitHub Desktop.
Save VitamintK/1f90dcdcdae8fa35f179 to your computer and use it in GitHub Desktop.
# Kevin Cao
# March 9, 2015
# define intro function
def intro():
# give rules
print ("This is a puzzle favored by Einstein. \nYou will be asked to enter a three digit number, \nwhere the hundred's digit differs from the one's digit \nby at least two. \nThe procedure will always yield 1089.")
# define function einstein
def main():
# prompt user for number
# define digits for number
# define is_valid
def is_valid(num):
# set conditionals for number to be 3 digits and the properties of the hundreds and tens digits
if num >= 100 and num <= 1000:
if hundreds / 100 > ones:
if (hundreds / 100)- ones >= 2:
return True
else:
return False
if hundreds / 100 < ones:
if ones - (hundreds / 100) >= 2:
return True
else:
return False
else:
return False
while True:
number = int(input("Please enter a number: "))
hundreds = (number // 100) * 100
tens = ((number - hundreds) // 10) * 10
ones = number - hundreds - tens
# set error message
if is_valid(number):
break
else:
print ("The number you have provided does not abide by the rules. \nPlease provide a 3 digit number where the hundreds digit differs from the one's digit by at least 2.")
# define reverse function
def reverse(num):
# define digits for num
hundreds = (num // 100) * 100
tens = ((num - hundreds) // 10) * 10
ones = num - hundreds - tens
# reverse values
r_hundreds = hundreds // 100
r_ones = ones * 100
# reversed num
r_num = r_ones + tens + r_hundreds
# print input and reverse
return r_num
r_num = reverse(number)
# print the number and its reverse
print ("Your number is", number)
print ("The reverse of your number is", r_num)
# define difference variable
difference = 0
# define conditional for positive difference
if number > r_num:
difference = number - r_num
elif r_num > number:
difference = r_num - number
r_difference = reverse(difference)
# print the difference
print ("The difference between your first number and the reverse is", difference)
print ("The reverse of the difference is", r_difference)
# sum of difference and reversed difference
thousand_eighty_nine = difference + r_difference
# print the sum
print ("The sum of your difference and the reverse of your difference is", thousand_eighty_nine)
print ("Your answer will always be 1089 because of this method.")
# call function
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment