728x90
https://www.codewars.com/kata/5287e858c6b5a9678200083c/train/python
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
6kyu가 생각보다 어렵지 않았던거 같아서 다시 rank up을 풀어본다.
양수 하나를 파라미터로 받고, 해당 숫자의 각 자리수에다가 이 value의 길이만큼 제곱을 해서 더해주면 된다.
그리고 입력받은 값과, 나온 결과값이 같으면 True, 다르면 False 다.
def narcissistic(num):
result = 0
for ele in list(str(num)):
result += int(ele) ** len(list(str(num)))
if num == result:
return True
else:
return False
새롭게 배운점은, 제곱기능을 파이썬에서 ** 두개로 해결할 수 있다는 것이다.
그 외에는.. 숫자와 문자열 변환할때 str(), int() 이런식으로 가는거랑. 문자열을 배열로 만들때 list()를 사용한다는거 정도.
숏코딩은 아래와 같다. 저번에 배운걸 토대로 아래처럼 쓸 수 있었을거 같긴한데, 뭐 일단 하지는 못했다.
def narcissistic(value):
return value == sum(int(x) ** len(str(value)) for x in str(value))
코드워에서 8kyu에서 7kyu로 레벨업을 했다.
728x90
'알고리즘' 카테고리의 다른 글
[CodeWars] Simple Pig Latin- 5Kyu (1) | 2024.12.05 |
---|---|
[CodeWars] Square Every Digit - 7Kyu (0) | 2024.12.05 |
[CodeWars] Array.diff - 6Kyu (0) | 2024.12.04 |
[CodeWars] Rock Paper Scissors! - 8Kyu (0) | 2024.12.04 |
[CodeWars] Counting sheep... - 8Kyu (1) | 2024.12.04 |