Python: Simple Fun #23: Square Digits Sequence [closed]












-2














I am solving this Kata - https://www.codewars.com/kata/simple-fun-number-23-square-digits-sequence/python



Consider a sequence of numbers a0, a1, ..., an, in which an element is equal to the sum of squared digits of the previous element. The sequence ends once an element that has already been in the sequence appears again.



Given the first element a0, find the length of the sequence.



Example
For a0 = 16, the output should be 9



Here's how elements of the sequence are constructed:



a0 = 16



a1 = 12 + 62 = 37



a2 = 32 + 72 = 58



a3 = 52 + 82 = 89



a4 = 82 + 92 = 145



a5 = 12 + 42 + 52 = 42



a6 = 42 + 22 = 20



a7 = 22 + 02 = 4



a8 = 42 = 16, which has already occurred before (a0)



Thus, there are 9 elements in the sequence.



For a0 = 103, the output should be 4



The sequence goes as follows: 103 -> 10 -> 1 -> 1, 4 elements altogether.



I took a wrong approach to solve the code (that's what I already know from other solutions) but still do not understand why is it not working. Any help?



def mysquare(digits, previous = , loop = 2):
sum = 0
for digit in str(digits):
sum+=int(digit)**2
if sum in previous or digits == sum:
return loop
else:
previous.append(sum)
return mysquare(sum, previous, loop+1)

def square_digits_sequence(n):
return mysquare(n)









share|improve this question







New contributor




Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











closed as off-topic by πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe Dec 20 at 22:26


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed."
    – Sᴀᴍ Onᴇᴌᴀ
    Dec 20 at 20:06
















-2














I am solving this Kata - https://www.codewars.com/kata/simple-fun-number-23-square-digits-sequence/python



Consider a sequence of numbers a0, a1, ..., an, in which an element is equal to the sum of squared digits of the previous element. The sequence ends once an element that has already been in the sequence appears again.



Given the first element a0, find the length of the sequence.



Example
For a0 = 16, the output should be 9



Here's how elements of the sequence are constructed:



a0 = 16



a1 = 12 + 62 = 37



a2 = 32 + 72 = 58



a3 = 52 + 82 = 89



a4 = 82 + 92 = 145



a5 = 12 + 42 + 52 = 42



a6 = 42 + 22 = 20



a7 = 22 + 02 = 4



a8 = 42 = 16, which has already occurred before (a0)



Thus, there are 9 elements in the sequence.



For a0 = 103, the output should be 4



The sequence goes as follows: 103 -> 10 -> 1 -> 1, 4 elements altogether.



I took a wrong approach to solve the code (that's what I already know from other solutions) but still do not understand why is it not working. Any help?



def mysquare(digits, previous = , loop = 2):
sum = 0
for digit in str(digits):
sum+=int(digit)**2
if sum in previous or digits == sum:
return loop
else:
previous.append(sum)
return mysquare(sum, previous, loop+1)

def square_digits_sequence(n):
return mysquare(n)









share|improve this question







New contributor




Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











closed as off-topic by πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe Dec 20 at 22:26


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed."
    – Sᴀᴍ Onᴇᴌᴀ
    Dec 20 at 20:06














-2












-2








-2







I am solving this Kata - https://www.codewars.com/kata/simple-fun-number-23-square-digits-sequence/python



Consider a sequence of numbers a0, a1, ..., an, in which an element is equal to the sum of squared digits of the previous element. The sequence ends once an element that has already been in the sequence appears again.



Given the first element a0, find the length of the sequence.



Example
For a0 = 16, the output should be 9



Here's how elements of the sequence are constructed:



a0 = 16



a1 = 12 + 62 = 37



a2 = 32 + 72 = 58



a3 = 52 + 82 = 89



a4 = 82 + 92 = 145



a5 = 12 + 42 + 52 = 42



a6 = 42 + 22 = 20



a7 = 22 + 02 = 4



a8 = 42 = 16, which has already occurred before (a0)



Thus, there are 9 elements in the sequence.



For a0 = 103, the output should be 4



The sequence goes as follows: 103 -> 10 -> 1 -> 1, 4 elements altogether.



I took a wrong approach to solve the code (that's what I already know from other solutions) but still do not understand why is it not working. Any help?



def mysquare(digits, previous = , loop = 2):
sum = 0
for digit in str(digits):
sum+=int(digit)**2
if sum in previous or digits == sum:
return loop
else:
previous.append(sum)
return mysquare(sum, previous, loop+1)

def square_digits_sequence(n):
return mysquare(n)









share|improve this question







New contributor




Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am solving this Kata - https://www.codewars.com/kata/simple-fun-number-23-square-digits-sequence/python



Consider a sequence of numbers a0, a1, ..., an, in which an element is equal to the sum of squared digits of the previous element. The sequence ends once an element that has already been in the sequence appears again.



Given the first element a0, find the length of the sequence.



Example
For a0 = 16, the output should be 9



Here's how elements of the sequence are constructed:



a0 = 16



a1 = 12 + 62 = 37



a2 = 32 + 72 = 58



a3 = 52 + 82 = 89



a4 = 82 + 92 = 145



a5 = 12 + 42 + 52 = 42



a6 = 42 + 22 = 20



a7 = 22 + 02 = 4



a8 = 42 = 16, which has already occurred before (a0)



Thus, there are 9 elements in the sequence.



For a0 = 103, the output should be 4



The sequence goes as follows: 103 -> 10 -> 1 -> 1, 4 elements altogether.



I took a wrong approach to solve the code (that's what I already know from other solutions) but still do not understand why is it not working. Any help?



def mysquare(digits, previous = , loop = 2):
sum = 0
for digit in str(digits):
sum+=int(digit)**2
if sum in previous or digits == sum:
return loop
else:
previous.append(sum)
return mysquare(sum, previous, loop+1)

def square_digits_sequence(n):
return mysquare(n)






python






share|improve this question







New contributor




Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 20 at 20:04









Koss645

1




1




New contributor




Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Koss645 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




closed as off-topic by πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe Dec 20 at 22:26


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe Dec 20 at 22:26


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, Sᴀᴍ Onᴇᴌᴀ, Gerrit0, alecxe

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed."
    – Sᴀᴍ Onᴇᴌᴀ
    Dec 20 at 20:06














  • 3




    Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed."
    – Sᴀᴍ Onᴇᴌᴀ
    Dec 20 at 20:06








3




3




Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed."
– Sᴀᴍ Onᴇᴌᴀ
Dec 20 at 20:06




Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed."
– Sᴀᴍ Onᴇᴌᴀ
Dec 20 at 20:06















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Список кардиналов, возведённых папой римским Каликстом III

Deduzione

Mysql.sock missing - “Can't connect to local MySQL server through socket”