Algorithm/Baekjoon

#백준 1157 단어 공부, 파이썬 대문자/소문자 변경 함수, set, count 함수

say! 2022. 6. 29. 16:20
728x90
#백준 1157 : 단어 공부

word = input()
alph = [0 for i in range(26)]

if len(word)>1000000:
    print("문자길이를 초과했습니다.")
    exit()

word = word.lower()
for i in range(len(word)):
    for j in range(len(alph)):
        if ord(word[i])-97==j: #아스키코드 이용해서 구분
            alph[j]+=1

most = alph[0]
index = 0
for i in range(len(alph)):
    if most < alph[i]:
        most = alph[i]
        index = i

n=0
for i in range(len(alph)):
    if most == alph[i]:
        n+=1

if(n < 2):
    print(chr(index+65))
else:
     print("?")

코드가 정말정말 더럽다... 그냥 퍼즐 짜맞추듯이 꾸역꾸역 푼 느낌... 파이썬에 아직 익숙하지 못해서 그런지 파이썬 함수를 잘 이용하지 못하는 느낌이다.

# 소문자로 변경

s = s.lower()

# 대문자로 변경

s = s.upper()

# 모든 문자가 대문자/소문자인지 확인

s.islower() s.isupper()

*파이썬에는 증감연산자가 없다!!

 

# 파이썬 종료하는 법

 

https://www.delftstack.com/ko/howto/python/python-exit-program/

 

Python에서 프로그램을 종료하는 방법

Python에서는 quit(), exit(), sys.exit() 및 os.exit()와 같은 많은 내장 함수가 Python 프로그램을 종료하는 데 사용됩니다.

www.delftstack.com

 

-참고한 코드

https://ooyoung.tistory.com/70

 

백준 1157번 [파이썬 알고리즘] 단어 공부

[Python] 백준 알고리즘 온라인 저지 1157번 : 단어 공부 Python3 코드 words = input().upper() unique_words = list(set(words)) # 입력받은 문자열에서 중복값을 제거 cnt_list = [] for x in unique_words..

ooyoung.tistory.com

 

# set 함수

set함수는 중복을 제거해준다. 새롭게 알게된 사실!

list = set([1,1,1,2,2,2])
print(s) # {1,2}

 

# count 함수

count 함수는 문자열에서 특정 문자나 문자열이 몇 개 포함되어있는지 반환해준다. (대소문자 구분함)