17일차 문제

양진혁·2021년 11월 17일
0

문제풀이

첫번째 문제는 7kyu 난이도 문제로
"Dermatoglyphics" --> true
"aba" --> false
"moOse" --> false (ignore letter casing)

같은 알파벳이 있으면 false 아니면 True를 해주는 것이다.

def is_isogram(string):
  if len(string) == len(set(string.lower())):
    return True
  else: 
    return False

비교적 간단한 문제로 집합 함수인 set를 활용해서 겹치는걸 없애준 후 길이비교를 통해서 True와 False를 출력했다.

두번째 문제는 6kyu 난이도 문제로
"is2 Thi1s T4est 3a" --> "Thi1s is2 3a T4est"
"4of Fo1r pe6ople g3ood th5e the2" --> "Fo1r the2 g3ood 4of th5e pe6ople"
"" --> ""

즉 문자열 안에 있는 숫자에 따라서 배치하는 것이다.

 def order(sentence):
  if not sentence:
    return ""
  emptylist = []
  senten = sentence.split()
  for i in range(1,10):
    for j in senten:
      if str(i) in j:
        emptylist.append(j)
  return " ".join(emptylist)

split을 통해서 문자열을 나눠주었고 그 다음 1~9까지의 숫자를 문자열로 바꾼 후 그게 들어있으면 빈 리스트에 추가하는 방식으로 문제를 해결했다.

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

Each smiley face must contain a valid pair of eyes. Eyes can be marked as : or ;
A smiley face can have a nose but it does not have to. Valid characters for a nose are - or ~
Every smiling face must have a smiling mouth that should be marked with either ) or D
No additional characters are allowed except for those mentioned.

Valid smiley face examples: :) :D ;-D :~)
Invalid smiley faces: ;( :> :} :]

즉 웃는 얼굴을 만들어야 한다. 코는 꼭 들어갈 필요는 없지만 들어간다면 "-", "~" 이어야 하고 눈과 입은 무조건 들어가야 하는 문제이다.

def count_smileys(arr):
    count = 0
    for i in arr:
        if len(i) == 3:
            if i[0] in [':',';'] and i[1] in ['-','~'] and i[-1] in ['D',')']:
                count +=1
        else:
            if i[0] in [':',';'] and i[-1] in ['D',')']:
                count +=1
    return count

i의 길이가 3일때의 경우의 수와 아닐때의 경우의 수를 통해서 웃는 얼굴의 총 갯수를 더해줬다.

네번째 문제는 백준에서 풀어본 문제로

첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
라고 하는 문제이다.

a = input().upper()
emptylist = []
count = {}
for i in a:
  try: count[i] +=1
  except: count[i] = 1
for k,v in count.items():
  if max(count.values()) == v:
    emptylist.append(k)
if len(emptylist) == 1:
  print( "".join(emptylist))
else:
  print("?")

먼저 모든 단어를 대문자로 바꾼 후 딕셔너리 형태를 통해서 몇번 반복되는지 찾고 max를 이용해 값이 가장 큰 밸류에 대한 키 값을 빈 리스트에 넣어주고 그것이 두개 이상이면 ? 아니면 그 키 값을 출력하도록 했다.

0개의 댓글