Python: Simple Fun #23: Square Digits Sequence [closed]
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
New contributor
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.
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
python
New contributor
New contributor
New contributor
asked Dec 20 at 20:04
Koss645
1
1
New contributor
New contributor
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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