Answer the question
In order to leave comments, you need to log in
Combining words of a given length Python?
The people are a matter of life and death,
there is such a code:
import re
Vvod = open('Vvod.txt','r',encoding='utf-8')
Vyvod = open('Vyvod.txt','w',encoding='utf-8')
Vvod = list(Vvod)
for i in range(len(Vvod)):
if i !=len(Vvod)-1 :
Vvod[i] = Vvod[i][:-1].split(' ')
elif i == len(Vvod)-1:
Vvod[i]=Vvod[i].split(' ')
Vvod_1 = []
for i in range (len(Vvod)):
for j in range (len(Vvod[i])):
Vvod_1.append(Vvod[i][j])
nabor_po = []
ne_voshlo = []
dlina = []
a = sorted(Vvod_1,key=len,reverse=True)
print(a)
for i in a:
dlina.append(len(i))
print(max(dlina),'- мінімальна кіькість символів в рядку')
Slova = []
smena = Vvod_1[0] + ' '
_copy = a.copy()
n = int(input('Кількість символів в строці'))
for j in range (1,len(a)):
if len(a[j])< (n+1)-len(smena) and len(smena)<(n+1):
smena+=a[j]+' '
else:
Slova.append(smena[:-1])
smena = ''
Slova.append(smena)
for i in range (len(Slova)):
Slova[i]=re.sub(' +', ' ', Slova[i])
AA = sorted(Slova, key=len, reverse=True)
new = []
for i in range(len(AA)):
if AA[i] != ' ':
new.append(AA[i])
for i in range(len(new)):
if new[i][-1]==' ':
new[i]=new[i][:-1]
for i in range(len(new)):
if new[i][0] == ' ':
new[i] = new[i][1:]
# print(AA)
for i in new:
Vyvod.write(i+'\n')
print(len(i))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question