Guys I am trying t write this script that finds the age of a daughter through palindromes.
Here is the question:

Recently I had a visit with my mom and we realized that the two digits that make
up my age when reversed resulted in her age. For example, if she’s 73, I’m 37.
“When I got home I figured out that the digits of our ages have been reversible six times so far. So the question is, how old am I now?

This is what I got....the program runs, but it doesn't return anything. It doesn't even close it self. :(

``````def palindrome_ages(mom, daughter):
daughter_rev = int(str(daughter)[::-1])
palindrome_num = 0
while daughter_rev != mom:
daughter = daughter + 1
if daughter_rev == mom:
palindrome_num = palindrome_num + 1
if palindrome_num == 6:
return str(daughter)

def main():
"""
main function
"""
mom = 36
daughter = 0
print palindrome_ages(mom, daughter)

return 0

if __name__ == "__main__":
main()``````

5
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by gwmorris

Should line 5 be
"daughter_rev +=1" ?

Post deleted

Edited by woooee: n/a

No, because I am after how old the daughter is, not the reverse.
Hence the daughter's age starts at 0.

once daughter_rev == mom for the sixth time. I want the daughter number returned. and that will be her age.

The while() loop does nothing but count up to "mom". The difference between ages has to remain the same also so your approach is off. To illustrate

``````Mom  Daughter  Difference
20     2         18
21    12                  9 Too small
30     3              27
32    13         18
41    14              27
42    24         18``````

Notice that you add 11 to each date to get the next difference. You want to loop through age differences starting with some number like 18=diffrence between mother and child. So the first loop would compare 1 and 19, then "02" and 20, etc. Next use a difference of 19, etc. Assume you will not go above 99 years of age and convert all ages to a 2 digit string or list which is then easy to reverse and test (psuedo-code)
if age = age+18.reversed
An interesting problem but we can't do it for you so post back with any further questions.

Thanks wooee, I didn't think about that while loop stopping at the mom's age.
I didn't ask you to do it. I wanted to know why my program runs, but does not return anything(no Syntax Errors, nothing.)

EUREKA!:twisted:
Here is the final beast gentlemen:

``````def palindrome_ages(mom, daughter):
palindrome_num = 0
mom_int = int(mom)
while daughter <= 99:
mom_rev = int(str(mom_int)[::-1])
if daughter == mom_rev:
daughter = daughter + 11
mom_int = mom_int + 11
palindrome_num = palindrome_num + 1
if palindrome_num == 5:
return "The daughter is %s years old." % str(daughter)

def find_age():
#This is assuming the mother had her when she was 18.
mom = 20
daughter = 2
return palindrome_ages(mom, daughter)``````

Here is my palindrome_ages generator:

``````>>> import itertools
>>> for agediff,group in itertools.groupby(sorted([((b*10+a)-(a*10+b),(b*10+a),(a*10+b))  for a in range(6) for b in range(a+1,10)]), lambda x: x[0]):
print agediff,list(group)

9 [(9, 10, 1), (9, 21, 12), (9, 32, 23), (9, 43, 34), (9, 54, 45), (9, 65, 56)]
18 [(18, 20, 2), (18, 31, 13), (18, 42, 24), (18, 53, 35), (18, 64, 46), (18, 75, 57)]
27 [(27, 30, 3), (27, 41, 14), (27, 52, 25), (27, 63, 36), (27, 74, 47), (27, 85, 58)]
36 [(36, 40, 4), (36, 51, 15), (36, 62, 26), (36, 73, 37), (36, 84, 48), (36, 95, 59)]
45 [(45, 50, 5), (45, 61, 16), (45, 72, 27), (45, 83, 38), (45, 94, 49)]
54 [(54, 60, 6), (54, 71, 17), (54, 82, 28), (54, 93, 39)]
63 [(63, 70, 7), (63, 81, 18), (63, 92, 29)]
72 [(72, 80, 8), (72, 91, 19)]
81 [(81, 90, 9)]``````

So there looks like to be three alternatives:
18 [(18, 20, 2), (18, 31, 13), (18, 42, 24), (18, 53, 35), (18, 64, 46), (18, 75, 57)]
27 [(27, 30, 3), (27, 41, 14), (27, 52, 25), (27, 63, 36), (27, 74, 47), (27, 85, 58)]
36 [(36, 40, 4), (36, 51, 15), (36, 62, 26), (36, 73, 37), (36, 84, 48), (36, 95, 59)]

with 6 palindrome ages, daughter is 57, 58, or 59.

Edited by pyTony: n/a

Way to rub it in tony lol. That's a pretty clean piece of code.
I find that the harder the problems I face, the nastier my code looks.
That is my main goal as a programmer, make cleaner code.

Here is my palindrome_ages generator:

``````>>> import itertools
>>> for agediff,group in itertools.groupby(sorted([((b*10+a)-(a*10+b),(b*10+a),(a*10+b))  for a in range(6) for b in range(a+1,10)]), lambda x: x[0]):
print agediff,list(group)

9 [(9, 10, 1), (9, 21, 12), (9, 32, 23), (9, 43, 34), (9, 54, 45), (9, 65, 56)]
18 [(18, 20, 2), (18, 31, 13), (18, 42, 24), (18, 53, 35), (18, 64, 46), (18, 75, 57)]
27 [(27, 30, 3), (27, 41, 14), (27, 52, 25), (27, 63, 36), (27, 74, 47), (27, 85, 58)]
36 [(36, 40, 4), (36, 51, 15), (36, 62, 26), (36, 73, 37), (36, 84, 48), (36, 95, 59)]
45 [(45, 50, 5), (45, 61, 16), (45, 72, 27), (45, 83, 38), (45, 94, 49)]
54 [(54, 60, 6), (54, 71, 17), (54, 82, 28), (54, 93, 39)]
63 [(63, 70, 7), (63, 81, 18), (63, 92, 29)]
72 [(72, 80, 8), (72, 91, 19)]
81 [(81, 90, 9)]``````

So there looks like to be three alternatives:
18 [(18, 20, 2), (18, 31, 13), (18, 42, 24), (18, 53, 35), (18, 64, 46), (18, 75, 57)]
27 [(27, 30, 3), (27, 41, 14), (27, 52, 25), (27, 63, 36), (27, 74, 47), (27, 85, 58)]
36 [(36, 40, 4), (36, 51, 15), (36, 62, 26), (36, 73, 37), (36, 84, 48), (36, 95, 59)]

with 6 palindrome ages, daughter is 57, 58, or 59.

Aha! I had used a spreadsheet to do this right quick, and could find the 18, 27, etc., but I couldn't figure out how you were getting six of them, as I didn't use "20" and "2". I just assumed they all needed to be the same number of digits, else the ages would be "20" and "02", which we generally don't do, but at least I get it now. I was thinking of them as true palindromes, and I suppose if you write "202" that it is. My assumption was that each age was written as backwards the other one and they would all be 4 characters long.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.