🐍 Python Problem 0ne
Problem: Square every digit of a number and concatenate them.
Example:
-
-
---
My Solution:
AI's Solution (via ChatGPT):
---
💡 Key Takeaway:
The AI uses
🔗 Sources: Codewars, ChatGPT
👉 Try it yourself!
How would YOU approach this problem?
#Python #Codewars #Coding #LearnPython
Problem: Square every digit of a number and concatenate them.
Example:
-
9119
→ 811181
(since 9²=81, 1²=1, 1²=1, 9²=81 → "81"+"1"+"1"+"81") -
765
→ 493625
(49-36-25) ---
My Solution:
def square_every_digit(num):
num_str = str(num)
result = ""
for digit in num_str:
squared = int(digit) ** 2
result += str(squared)
return int(result)
AI's Solution (via ChatGPT):
def square_every_digit(num):
squared_digits = ''.join(str(int(digit) ** 2) for digit in str(num))
return int(squared_digits)
---
💡 Key Takeaway:
The AI uses
join()
with a generator expression to simplify the loop, making the code shorter and more "Pythonic". Both methods work, but join()
is cleaner for string concatenation!🔗 Sources: Codewars, ChatGPT
👉 Try it yourself!
How would YOU approach this problem?
#Python #Codewars #Coding #LearnPython