Skip to content

Instantly share code, notes, and snippets.

@Bundi-py
Last active February 17, 2020 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bundi-py/1c8d5edf31c710c164be425e979baf06 to your computer and use it in GitHub Desktop.
Save Bundi-py/1c8d5edf31c710c164be425e979baf06 to your computer and use it in GitHub Desktop.
# 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