45일차 문제

양진혁·2021년 12월 16일
0

문제풀이

As the name may already reveal, it works basically like a Fibonacci, but summing the last 3 (instead of 2) numbers of the sequence to generate the next. And, worse part of it, regrettably I won't get to hear non-native Italian speakers trying to pronounce it :(

So, if we are to start our Tribonacci sequence with [1, 1, 1] as a starting input (AKA signature), we have this sequence:

def tribonacci(signature, n):
  if n == 0:
    return []
  if n > 3:
    for i in range(n-3):
      num = sum(signature[-3:])
      signature.append(num)
    return signature
  else:
    return signature[:n]

이 문제는 기본적으로 피보나치처럼 작용하지만, 다음 수를 생성하기 위해 수열의 마지막 세 수를 합한다.
만약 n이 0이라면 []을 리턴하고 n이 3보다 크다면 반복문의 길이를 n-3으로 지정한 후 앞에 3개의 합을 signature에 계속 추가해준다. 만약 3보다 작다면 signature의 n번째 숫자까지 리턴한다.

두번째 문제는
"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"

def to_camel_case(text):
  a = text.replace("-"," ").replace("_", " ")
  a = a.split()
  if len(text) == 0:
    return text
  return a[0] + "".join(i.capitalize() for i in a[1:])

"-"와 "_"를 " "로 대체한 후 a를 나눈다. 만약 길이가 0이라면 text를 그대로 리턴하고 그게 아니라면 capitalize를 사용해서 문자의 앞글자는 대문자로 바꾸어 주고 첫번째 인덱스는 그대로 출력하고 나머지는 더해서 출력해준다.

0개의 댓글