'Digit of Life in Python
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.
Solution 1:[1]
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.
Solution 2:[2]
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_
Solution 3:[3]
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.
Solution 4:[4]
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))
Solution 5:[5]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | baymurat |
| Solution 2 | Egor Wexler |
| Solution 3 | Malo |
| Solution 4 | dspencer |
| Solution 5 |
