본문 바로가기
CodeSignal/Arcade

CodeSignal [41/60] digitDegree

by 벤진[Benzene] 2020. 5. 26.

Problem:

Let's define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.

Given an integer, find its digit degree.

 

Example

  • For n = 5, the output should be
    digitDegree(n) = 0;
  • For n = 100, the output should be
    digitDegree(n) = 1.
    1 + 0 + 0 = 1.
  • For n = 91, the output should be
    digitDegree(n) = 2.
    9 + 1 = 10 -> 1 + 0 = 1.

-Summary-

1. str(n) 의 len 가 1이 되기전까지는 while loop을 계속 돈다. 

  - while loop 하나 돌때마다 count를 +1 해준다.

  - n 안에 있는 각 digit을 더한값을 n 에 넣어준다.

  - 계속해서 반복.

2. while loop을 빠져나온후에 count 값을 리턴.

 

사람들이 문제 푼것을 보니 아이디어는 비슷했으나, list comprehension 과 recursive 를 이용하면 더 깔끔하게 풀수 있었던 문제였다. 오늘도 다른 사람들의 코드를 보며 배운다.

if n < 10:
        return 0
    
    sumOfDigits = sum([int(i) for i in str(n)])
    
    return digitDegree(sumOfDigits) + 1

 

모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]

https://codesignal.com/

'CodeSignal > Arcade' 카테고리의 다른 글

CodeSignal [43/60] isBeautifulString  (0) 2020.05.26
CodeSignal [42/60] bishopAndPawn  (0) 2020.05.26
CodeSignal [40/60] longestDigitsPrefix  (0) 2020.05.26
CodeSignal [39/60] knapsackLight  (0) 2020.05.26
CodeSignal [38/60] growingPlant  (0) 2020.05.25

댓글