G
G
God_of_Gods2021-02-20 00:05:09
Python
God_of_Gods, 2021-02-20 00:05:09

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))

it performs the following action: we indicate the number of characters in a line and it groups words,
but it does not do its job very well
with the following text: Rodents live in forests and fields that they eat these animals gnaw on grain and tree bark hares gnaw on apple trees in the garden, rodents spoil crops
Morning
The sun has just risen the sky clear everything around shines how good it is in the fresh air you hear the singing of a lark a sonorous voice is heard in a clear height
Snakes Non
-poisonous snakes live in damp places they have two yellow spots on the back of their heads snakes love water and swim well they feed on frogs and fish you have seen already do not be afraid of his
squirrel
A squirrel briskly climbs trees, how dexterous she is on the ears of a squirrel tassel, the tail is long and fluffy, why does the squirrel hide behind her tail from the cold, he serves as her rudder when jumping
In the circus
When you were in the circus, remember the bright posters and flags here the juggler catches plates on the fly magician turned the hat into a bouquet of flowers, but the clown he makes people laugh in his jacket, a rooster, how interesting it is in
the circus does this: they gnaw at apple trees spoil the sun just rose well fresh it is heard in places yellow snakes fish have seen dexterous serves flags of flowers

jacket night predatory
mice eat the bark of the garden
on you not on him to her
he to her you on him to her on the
lark frogs
sharp Found out They live
the sky water ears fly
these that everything is like Snakes
two snakes snakes his arrived
here like this
in in in y and and y and B it is
interesting trees
Owl beak owl what
is it she is she or by
and in a u in in u in

They live gnaw at
trees tassels a
magician in attics
fields of grain hares
singing clear damp
spots love A squirrel
smartly climbs which
tail why a squirrel
circus When the circus
catches posters clown hat
people rooster
bird claws lives
at home at night bird
animals rodents in the
air you hear a
voice behind your head
be afraid of long
jumps remember the
plates are round, what to change in the code so that it groups everything as much as possible
because there are lines that can be added

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question