Data house

[어이털리는 알고리즘] python/string - raw string, join 본문

Computer Knowledge/코테 대비 오답노트

[어이털리는 알고리즘] python/string - raw string, join

l._.been 2023. 5. 6. 17:15
728x90

1. Escape문자가 있는 특수문자 출력

url: https://school.programmers.co.kr/learn/courses/30/lessons/181948

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제) 출력 예시

!@#$%^&*(\'"<>?:;

 

풀이

 

필요한 능력) r-string (즉, raw string)

 

출력할 때, f-string (formatted string)은 자주 봤지만 raw string은 생소했다.

그래서 조사를 해보니,

" Python에서 string 앞에 r을 표기해 주는 것은 해당 string literal을 raw string으로 만들어 주기 위함이다.

따라서 출력 결과가 모든 Escape 문자를 그대로 출력해 준다." 라고 한다.

 

string 앞에 prefix로 사용되는 f와 r의 의미를 잘 알아두면 편리할 것 같다.

 

 

 

2. 문자열 하나씩 출력

url: https://school.programmers.co.kr/learn/courses/30/lessons/181945

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

인상 깊었던 문풀

str = input()
print('\n'.join(str))

나처럼 for문 사용하지 말고 멋있게 join함수 사용하자 

 

 

 

3. 홀짝수 구분해서 출력하기

url: https://school.programmers.co.kr/learn/courses/30/lessons/181944

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

인상 깊었던 문풀

n=int(input())
print(f"{n} is {'eovdedn'[n&1::2]}")

'eovdedn'이 사실 짝수 인덱스만 합치면 'even'이 되고 홀수 인덱스만 합치면 'odd'가 된다. 

이거 관련 자세한 설명은 아래 텐서플로우에 있음 

이거 보면서 세상엔 천재가 많구나 싶었다ㅋㅋㅋㅋ 재밌어

https://stackoverflow.com/questions/72093934/what-is-eovdedn-doing-in-this-function

 

What is 'eovdedn' doing in this function?

I ran across this code while doing some coding challenges and don't understand how it is working. I don't understand what the 'eovdedn' part is doing. The challenge was to return odd or even if the

stackoverflow.com

 

'Computer Knowledge > 코테 대비 오답노트' 카테고리의 다른 글

[python] str.replace()  (0) 2023.05.08
python 부분 문자열  (0) 2023.05.08
[어이털리는 알고리즘] python 대소문자 변경  (0) 2023.05.06
알고리즘과 시간복잡도  (0) 2023.04.26
자료구조  (2) 2023.04.26