8일차 문제

양진혁·2021년 11월 8일
0

문제풀이

오늘은 총 3문제를 풀었다.

첫번째 문제는 5kyu 문제로
foo -> foo1

foobar23 -> foobar24

foo0042 -> foo0043

foo9 -> foo10

foo099 -> foo100

즉 숫자가 없으면 1을 넣어주고 숫자가 존재하면 1을 더해주는 것이다.

def increment_string(strng):
    strn = strng.rstrip('0123456789')
    num = strng[len(strn):]
    if num == "": 
      return strng+"1"
    return strn + str(int(num) + 1).zfill(len(num))

rstrip을 사용해서 문자열만 선택한 후 num을 통해서 뒤에 숫자가 없으면 문자열 1을 생성해주고 그게 아니라면 1을 더하고 num 길이에 맞게 0을 채워 넣었다.

두번째 문제는 4kyu로

Given an input string of:

apples, pears # and bananas
grapes
bananas !apples

The output expected would be:

apples, pears
grapes
bananas

def solution(string,markers):
    lst = string.split('\n')
    ans = []
    for line in lst:
        for m in markers:
            if m in line:
                line = line[:line.find(m)].strip()
        ans.append(line) 
    return '\n'.join(ans)

string을 줄바꿈을 기준으로 나눈 후 markers가 line 안에 존재할 시 find()를 활용하여 markers 전까지 출력했다.

세번째 문제는 5kyu 난이도로
number2words(0) ==> "zero"
number2words(1) ==> "one"
number2words(9) ==> "nine"
number2words(10) ==> "ten"
number2words(17) ==> "seventeen"
number2words(20) ==> "twenty"
number2words(21) ==> "twenty-one"
number2words(45) ==> "forty-five"
number2words(80) ==> "eighty"
number2words(99) ==> "ninety-nine"
number2words(100) ==> "one hundred"
number2words(301) ==> "three hundred one"
number2words(799) ==> "seven hundred ninety-nine"
number2words(800) ==> "eight hundred"
number2words(950) ==> "nine hundred fifty"
number2words(1000) ==> "one thousand"
number2words(1002) ==> "one thousand two"
number2words(3051) ==> "three thousand fifty-one"
number2words(7200) ==> "seven thousand two hundred"
number2words(7219) ==> "seven thousand two hundred nineteen"
number2words(8330) ==> "eight thousand three hundred thirty"
number2words(99999) ==> "ninety-nine thousand nine hundred ninety-nine"
number2words(888888) ==> "eight hundred eighty-eight thousand eight hundred eighty-eight"

숫자를 영어로 적어내는 것이다.

def int_to_en(num):
    n = { 0 : 'zero', 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five',
          6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten',
          11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen',
          15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen',
          19 : 'nineteen', 20 : 'twenty',
          30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty',
          70 : 'seventy', 80 : 'eighty', 90 : 'ninety' }
    th =1000
    tt = 10 * th
    ht = 10 * tt
    m = 10 * ht
    if num < 20:
        return n[num]
    if num < 100:
        if num % 10 == 0 : 
            return n[num]
        else:
            return n[num//10*10] + "-" + n[num%10]
    if num < th:
        if num % 100 == 0:
            return n[num//100] + " " +"hundred"
        else:
            return n[num//100] + " " + 'hundred' +" "+ int_to_en(num%100)
    if num < tt:
        if num % th == 0:
            return n[num// th] + " " + "thousand"
        else:
            return n[num // th] + " " + "thousand"+ " " + int_to_en(num%th)
    if num < ht:
        if num % tt == 0:
            return n[num//th] + " "+ "thousand"
        else:
            return int_to_en(num//th) + " " + "thousand" +  " " + int_to_en(num%th)
    if num < m:
        if num % tt == 0:
          return int_to_en(num//th) + " " + "thousand"
        else:
          return int_to_en(num//th) + " " + "thousand" + " " + int_to_en(num%th)

먼저 dict를 만든 다음 기본 숫자와 10의 자리 숫자까지 입력해준다.
그 다음 if문을 돌려서 딱 떨어지면 그 숫자를 딱 떨어지지 않으면 그 수의
몫과 나머지를 통해서 표현하는 방법을 사용했다.

0개의 댓글