Last active
February 17, 2020 11:13
-
-
Save Bundi-py/1c8d5edf31c710c164be425e979baf06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ovo je program koji nalazi najdužu reč/reči u tekstu. Prvo uklanja | |
# svu interpunkciju, a na kraju ispisuje odgovarajuću poruku, | |
# koja uključuje broj slova najduže reči, zajedno sa svim rečima | |
# te dužine koje se nalaze u tekstu. | |
# | |
import re | |
import string | |
with open('novi.txt', 'r') as fajl: | |
text = fajl.read() | |
samo_reci = re.sub('['+string.punctuation+']', '', text) | |
reci = samo_reci.split() # string konvertuje u listu | |
maxrec = 0 | |
lista = [] | |
for word in reci: | |
if len(word) > maxrec: | |
maxrec = len(word) | |
lista = [word] | |
elif len(word) == maxrec: | |
lista.append(word) | |
if len(lista) == 1: | |
for item in lista: | |
print('Najduža reč je: {}, ima {} slova'.format(item, len(item))) | |
else: | |
print('Najduže reči su: {}, imaju po {} slova.'.format(set(lista), len(lista[0]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment