D
D
Dmustache Usakov2021-05-26 22:41:05
Python
Dmustache Usakov, 2021-05-26 22:41:05

How to mix the words of a phrase?

How to mix phrase segments with honey?
A phrase is given:
If a plane passes through a given line parallel to another plane and intersects this plane, then the line of intersection of the planes is parallel to the given
line.
My code:

def chunk(in_string,num_chunks):
    in_string = letter.split(' ')
    chunk_size = len(in_string) // num_chunks
    if len(in_string) % num_chunks:
        chunk_size += 1
        iterator = iter(in_string)
        for _ in range(num_chunks):
            accumulator = list()
            for _ in range(chunk_size):
                try:
                    accumulator.append(next(iterator))
                except StopIteration:
                    break
            yield ' '.join(accumulator)
letter = 'Если плоскость проходит через данную прямую, параллельную другой плоскости, и пересекает эту плоскость, то прямая пересечения плоскостей параллельна данной прямой'

letter = list(chunk(letter, 3))

phrase = letter
while letter == phrase:
    letter = random.sample(letter, len(letter))
print(letter)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MinTnt, 2021-05-26
@Dmustache

letter = 'Если плоскость проходит через данную прямую, параллельную другой плоскости, и пересекает эту плоскость, то прямая пересечения плоскостей параллельна данной прямой'

def chunk(st, lns):
  import random
  mv = st.split()
  mv = [' '.join(mv[x:x+lns]) for x in range(0, len(mv), lns)]
  return ' '.join(random.sample(mv, len(mv)))
  
print(chunk(letter, 3))

S
soremix, 2021-05-26
@SoreMix

import random

sentence = 'Если плоскость проходит через данную прямую, параллельную другой плоскости, и пересекает эту плоскость, то прямая пересечения плоскостей параллельна данной прямой'

chunk_size = 3
words = sentence.split()

chunked_words = []

for i in range(0, len(words), chunk_size):
    chunked_words.append(words[i:i + chunk_size])

random.shuffle(chunked_words)

new_sentence = ''

for chunk in chunked_words:
    new_sentence += ' '.join(chunk) + ' '

print(new_sentence)

A
aRegius, 2021-05-27
@aRegius

from itertools import chain
from random import shuffle
from more_itertools import chunked


sub_sentences = list(chunked(sentence.split(), 3))
shuffle(sub_sentences)
new_sentence = ' '.join(item for item in chain.from_iterable(sub_sentences))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question