백준_1152_단어의 개수

임정민·2022년 11월 27일
2

알고리즘 문제풀이

목록 보기
6/173
post-thumbnail

코딩테스트 연습 스터디 진행중 입니다. ✍✍✍
Notion : https://www.notion.so/1c911ca6572e4513bd8ed091aa508d67

문제

https://www.acmicpc.net/problem/1152

풀이

[나의 풀이]

sentence = input()
sentence = list(sentence)

cnt = 1

if sentence[0] == ' ' :
    sentence[0] = ''

if sentence[len(sentence)- 1] == ' ':
    sentence[len(sentence)-1] = ''

if sentence == ['']:
    cnt = 0

for i in sentence:
    if i == ' ':
        cnt +=1

print(cnt)

[팀원의 풀이1]

s = input()
s = s.strip().split()
print(len(list(s)))

[팀원의 풀이2]

# coding = utf-8

if __name__ == '__main__' :
    import sys
    input = sys.stdin.readline
    from collections import deque
    from itertools import *
    def getdata() :
        n=int(input())
        board=list(list(map(int,input().split())) for _ in range(n))
        return

메모

  1. 백준 문제를 처음 풀었다.
    프로그래머스 처럼 def solution() 형태로 제출 했다가 계속 안되서 혼쭐났다.

  2. strip() , split() 에 대해 알게 되었다.
    https://codechacha.com/ko/python-string-strip/

# strip() : 문자제거

# 공백제거
text = ' Water boils at 100 degrees '
print('[' + text.rstrip() + ']')
print('[' + text.lstrip() + ']')
print('[' + text.strip() + ']')
[ Water boils at 100 degrees]
[Water boils at 100 degrees ]
[Water boils at 100 degrees]

# 동일한 문자 제거
text = '0000000Water boils at 100 degrees 000'
print(text.lstrip('0'))
print(text.rstrip('0'))
print(text.strip('0'))
Water boils at 100 degrees 000
0000000Water boils at 100 degrees
Water boils at 100 degrees

# 여러 문자 제거
text = ",,,,,123.....water....pp"
print(text.lstrip(',123.p'))
print(text.rstrip(',123.p'))
print(text.strip(',123.p'))
water....pp
,,,,,123.....water
water

https://codechacha.com/ko/python-string-split/

# split() : 문자열 쪼개기

text = 'Hello world, python'
strings = text.split()
print(strings)
['Hello', 'world,', 'python'] # 리스트 형태로 반환

# 특정 문자를 기준으로
text = 'Hello world, python'
strings = text.split(',')
print(strings)
['Hello world', ' python']

# 나눌 횟수 지정하기
text = 'Hello, world, python'
strings = text.split(',', 0)
print(strings)
['Hello, world, python'] # 0개로 나누어짐

text = 'Hello, world, python'
strings = text.split(',', 1)
print(strings)
['Hello', ' world, python'] # 1개로 나누어짐

text = 'Hello, world, python'
strings = text.split(',', -1)
print(strings)
['Hello', ' world', ' python'] # -1 이면 횟수에 제한없이 나누어짐

감사합니다.🐼🐼🐼

profile
https://github.com/min731

0개의 댓글