Digit of Life in Python
Question:
I’m trying to create a method that will return a digit of life when receiving input of a date of birth in the following format: YYYYMMDD. The requirements are as follows (see image below):
I have tried the following:
def split(word):
return [char for char in word]
date = input("Enter your date of birth in YYYYMMDD format: > ")
sum_list = []
def digitOfLife(date):
sum = 0
if(len(date) > 8):
print("Input data too long")
return
else:
date_list = []
for char in date:
date_list.append(int(char))
while len(date_list) >= 1:
print(len(date_list))
for num in date_list:
if sum > 9:
sum = 0
sum+=num
date_list.pop()
return sum
print(digitOfLife(date))
However, I am not getting the result that is supposed to be produced with this algorithm. I know it has something to do with my logic which adds the digits of the list after it’s been popped, but I’m not sure what it is.
Any insight would be greatly appreciated.
Answers:
while len(date_list) >= 1:
print(len(date_list))
# check if it's over 9
if sum > 9:
sum =0
num = date_list.pop()
sum += num
Check the above code maybe this would help you. And also check the documentation of pop() function from here
Pop function removes the last element from the list and returns it. Like this :
L = [1, 2, 3]
x = L.pop()
print(x) # this will return 3
***This afterwards occured me that you can also add them straightforward and apply modulo by ten.
It looks like you would only need to perform modulo 10 on cumulative additions:
def dol(N): return N if N < 10 else dol(N//10+N%10)
date = "19991229"
dol(int(date)) # 6
A .pop() inside loop may cause a problem because size of the list is changing during the iterations.
Besides .pop() removes the last element.
I would suggest the following solution:
def digitOfLife(date):
sum_ = 0
if (len(date) > 8):
print("Input data too long")
return
else:
date_list = []
for char in date:
date_list.append(int(char))
sum_ = sum(date_list) # calculate the sum of list of digits
while len(str(sum_)) > 1: # repeat while the sum has more than 1 digit
sum_ = sum(date_list)
date_list = [int(x) for x in str(sum_)]
return sum_
Also "sum" is a reserved word, so I renamed variable to sum_
Starting from your piece of code:
- you should use a for loop istead of the while loop with pop .. this will make things easier.
- then when sum > 9, you should not reset the sum to 0, but add the digits together.
This gives you the following piece of code:
def split(word):
return [char for char in word]
date = input("Enter your date of birth in YYYYMMDD format: > ")
sum_list = []
def digitOfLife(date):
sum = 0
if(len(date) > 8):
print("Input data too long")
return
else:
date_list = []
for char in date:
date_list.append(int(char))
for num in date_list:
sum+=num
if sum > 9:
sum = sum%10 +sum//10
return sum
print(digitOfLife(date))
This will output what you expect:
Enter your date of birth in YYYYMMDD format: > 19991229
6
Other improvements are still possible, but this shows you the easier change to make in your code.
Short way for digit of life:
def digitoflife(date):
if len(date) > 8:
print("date inputed is long")
else:
num = num2 = 0
for digit in date:
num += int(digit)
for i in str(num):
num2 += int(i)
return num2
date = str(input('Enter the date: '))
print("The digit of life is: ",digitoflife(date))
Code:
def bd_digit(string):
total = 0
for digit in string:
total+=int(digit)
if total >=10:
return bd_digit(str(total))
else:
return total
print(bd_digit('19991229'))
This may help you with recursion
I did the same exercise today like this.
date = '19991229'
list1=[]
for i in date:
list1.append(int(i))
date2 = str(sum(list1))
list2 = []
for i in date2:
list2.append(int(i))
total = sum(list2)
print(total)
def digitsoflife(number):
sum_ = [i for i in str(number)]
sum_ = sum(list(map(int, sum_)))
sum_total = 0
while len(str(sum_)) > 1:
for i in str(sum_):
sum_total += int(i)
sum_ = sum_total
print(sum_total)
break
else:
print(sum_)
I’m trying to create a method that will return a digit of life when receiving input of a date of birth in the following format: YYYYMMDD. The requirements are as follows (see image below):
I have tried the following:
def split(word):
return [char for char in word]
date = input("Enter your date of birth in YYYYMMDD format: > ")
sum_list = []
def digitOfLife(date):
sum = 0
if(len(date) > 8):
print("Input data too long")
return
else:
date_list = []
for char in date:
date_list.append(int(char))
while len(date_list) >= 1:
print(len(date_list))
for num in date_list:
if sum > 9:
sum = 0
sum+=num
date_list.pop()
return sum
print(digitOfLife(date))
However, I am not getting the result that is supposed to be produced with this algorithm. I know it has something to do with my logic which adds the digits of the list after it’s been popped, but I’m not sure what it is.
Any insight would be greatly appreciated.
while len(date_list) >= 1:
print(len(date_list))
# check if it's over 9
if sum > 9:
sum =0
num = date_list.pop()
sum += num
Check the above code maybe this would help you. And also check the documentation of pop() function from here
Pop function removes the last element from the list and returns it. Like this :
L = [1, 2, 3]
x = L.pop()
print(x) # this will return 3
***This afterwards occured me that you can also add them straightforward and apply modulo by ten.
It looks like you would only need to perform modulo 10 on cumulative additions:
def dol(N): return N if N < 10 else dol(N//10+N%10)
date = "19991229"
dol(int(date)) # 6
A .pop() inside loop may cause a problem because size of the list is changing during the iterations.
Besides .pop() removes the last element.
I would suggest the following solution:
def digitOfLife(date):
sum_ = 0
if (len(date) > 8):
print("Input data too long")
return
else:
date_list = []
for char in date:
date_list.append(int(char))
sum_ = sum(date_list) # calculate the sum of list of digits
while len(str(sum_)) > 1: # repeat while the sum has more than 1 digit
sum_ = sum(date_list)
date_list = [int(x) for x in str(sum_)]
return sum_
Also "sum" is a reserved word, so I renamed variable to sum_
Starting from your piece of code:
- you should use a for loop istead of the while loop with pop .. this will make things easier.
- then when sum > 9, you should not reset the sum to 0, but add the digits together.
This gives you the following piece of code:
def split(word):
return [char for char in word]
date = input("Enter your date of birth in YYYYMMDD format: > ")
sum_list = []
def digitOfLife(date):
sum = 0
if(len(date) > 8):
print("Input data too long")
return
else:
date_list = []
for char in date:
date_list.append(int(char))
for num in date_list:
sum+=num
if sum > 9:
sum = sum%10 +sum//10
return sum
print(digitOfLife(date))
This will output what you expect:
Enter your date of birth in YYYYMMDD format: > 19991229
6
Other improvements are still possible, but this shows you the easier change to make in your code.
Short way for digit of life:
def digitoflife(date):
if len(date) > 8:
print("date inputed is long")
else:
num = num2 = 0
for digit in date:
num += int(digit)
for i in str(num):
num2 += int(i)
return num2
date = str(input('Enter the date: '))
print("The digit of life is: ",digitoflife(date))
Code:
def bd_digit(string):
total = 0
for digit in string:
total+=int(digit)
if total >=10:
return bd_digit(str(total))
else:
return total
print(bd_digit('19991229'))
This may help you with recursion
I did the same exercise today like this.
date = '19991229'
list1=[]
for i in date:
list1.append(int(i))
date2 = str(sum(list1))
list2 = []
for i in date2:
list2.append(int(i))
total = sum(list2)
print(total)
def digitsoflife(number):
sum_ = [i for i in str(number)]
sum_ = sum(list(map(int, sum_)))
sum_total = 0
while len(str(sum_)) > 1:
for i in str(sum_):
sum_total += int(i)
sum_ = sum_total
print(sum_total)
break
else:
print(sum_)