프로그래밍
[파이썬] 연습문제. 1
금전
2018. 8. 10. 13:48
[파이썬] 연습문제. 1
# 1. 단어가 줄 단위로 저장된 words.txt 파일이 주어집니다.
# 다음 소스 코드를 완성하여 10자 이하인 단어의 개수가 출력되게 만드세요.
word.txt
anonymously
compatibility
dashboard
experience
photography
spotlight
warehouse
inFp = open("words.txt", "r")
inList = inFp.readlines()
count = 0
for i in inList:
test = str(i).rstrip()
if(len(test)<=10):
print(test, end="")
count+=1
print("10자 이하인 단어의 개수 : %d" % count)
# 2. 표준 입력으로 정수와 문자열이 각 줄에 입력됩니다.
# 다음 소스 코드를 완성하여 입력된 숫자에 해당하는 단어 단위 N-gram을 출력하세요.
# 만약 입력된 문자열의 단어 개수가 입력된 정수 미만이라면 'wrong'을 출력하세요.
#
# 실행 결과
# 7 (입력)
# Python is a programming language that lets you work quickly (입력)
# Python is a programming language that lets
# is a programming language that lets you
# a programming language that lets you work
# programming language that lets you work quickly
a = int(input("정수 입력 : "))
b = input("문자열 입력 : ")
words = b.split() # 문자열의 공백을 기준으로 리스트를 만들어주는 함수
print(words)
if(len(words)<a):
print("wrong")
else:
for i in range(len(words)-(a-1)):
for j in range(i, a+i):
print(words[j], end=' ') # 엔터없이 출력하기 위해 end = ' ' 사용
print()