-
문제 09. TypoglycemiaNLP/NLP100 2021. 1. 4. 18:20
공백으로 구분된 단어의 나열 문자열을 이용, 각 단어의 첫 글자와 마지막 글자는 남기고 그 이외의 문자의 순서를 랜덤으로 섞는 프로그램을 작성하시오. 단, 길이가 4 이하인 단어에 대해서는 섞는 과정을 생략하시오. 적당한 영어 문장을(e.g. "I couldn't believe that i could actually understand what i was reading: the phenomenal power of the human mind.) 이용하여 실행 결과를 확인하시오.
import random s = "I couldn’t believe that I could actually understand what I was reading : the phenomenal power of the human mind" s = s.split(" ") def rand(text): if 4 >= len(text): return text head = text[0] body = text[1:-1] tail = text[-1] return "".join((head, "".join(random.sample(body, len(body))),tail)) print(" ".join([rand(text) for text in s]))
[출력 결과]
I cold’nut beivlee that I cluod acuatlly uesdatnnrd what I was renadig : the poehnaemnl pwoer of the huamn mind
'NLP > NLP100' 카테고리의 다른 글
문제 11. 탭을 공백으로 변환 (0) 2021.01.04 문제 10. 몇 줄짜리 파일 (0) 2021.01.04 문제 08. 암호문 (0) 2021.01.04 문제 07. 템플릿을 이용한 문자열 생성 (0) 2021.01.04 문제 06. 집합 (0) 2021.01.04