16일차 문제

양진혁·2021년 11월 16일
0

문제풀이

첫번째 문제는 6kyu 난이도 문제로
n | score
---+-------
1 | 50
2 | 150
3 | 300
4 | 500
5 | 750

규칙을 찾아서 score를 리턴 하는 문제이다.

def get_score(n):
  for i in range(n):
    i = (i+1) * (50+(i*25))
  return i

규칙이 되게 간단해서 쉽게 풀 수 있는 문제였다. score가 25씩 늘어나고 n을 곱해주면 정답이 나온다.

두번째 문제는 6kyu 난이도 문제로

약속시간에 10분 먼저 도착해서 n,s,e,w 방향으로 산책을 다녀오려고 한다. 한번 걷는데 1분이 소요되며 딱 시간을 맞춰서 오려고 한다.

test.expect(is_valid_walk(['n','s','n','s','n','s','n','s','n','s']), 'should return True');
test.expect(not is_valid_walk(['w','e','w','e','w','e','w','e','w','e','w','e']), 'should return False');
test.expect(not is_valid_walk(['w']), 'should return False');
test.expect(not is_valid_walk(['n','n','n','s','n','s','n','s','n','s']), 'should return False');

def is_valid_walk(walk):
  if walk.count('n') == walk.count('s') and walk.count('e') == walk.count('w') and len(walk) == 10:
        return True
  return False

count를 사용해서 n,s의 개수와 e,w의 개수가 같으며 길이가 10 즉 10분만에 올 수 있을 때 True를 아니면 False를 사용하는 방식으로 코드를 작성했다.

세번째 문제는 6kyu 난이도로
In this kata you are required to, given a string, replace every letter with its position in the alphabet.

If anything in the text isn't a letter, ignore it and don't return it.

"a" = 1, "b" = 2, etc.

a부터 z까지를 1~26번까지 매긴 후 리턴하는 것이다.

def alphabet_position(text):
  empty= []
  lowertext = text.lower()
  for i in lowertext:
    if i.isalpha():
      empty.append(str((ord(i))-96))
  return ' '.join(empty)

빈 리스트를 추가해주고 영어를 소문자로 다 바꿔준 후 만약 i가 알파벳일때만 숫자로 바꿔주었다. 숫자는 아스키 코드에서 소문자 a인 97에서 96을 빼주었다.

0개의 댓글