Let’s assume we only have one line to work with. We have string “fo0”, and we want to print each character a random amount of times, and redo that entire statement another random amount of times; Resulting in something like the following:
ffooooo000 ffffffoo00 fffo00000000
How does one go about doing this, using for loops or something similar?
As far as I’ve gotten:
import random; x = "fo0"; print "".join(x for i in range(0,random.randrange(0,40)))
All this results in is something like “fo0fo0fo0fo0fo0fo0“.
You can use something like the following. Below,
sis the input string,char_rand_maxis the maximum number of times a single character is repeated in a word, andstr_rand_maxis the maximum number of times to produce a word. Each character appears at least once, and there is at least one word.Examples:
One-line solution
You can, of course, do this all in one line, if you want. Rather than squashing the function from above into a single line, you can do the following:
Note that I added a line break to make it more readable in the answer. Also, unlike the solution above, the maximum numbers of character repeats and words is hard-coded at five.