|
| 1 | +import random |
| 2 | + |
| 3 | +def silly_string(nouns, verbs, templates): |
| 4 | + # Choose a random template. |
| 5 | + template = random.choice(templates) |
| 6 | + # We'll append strings into this list for output. |
| 7 | + output = [] |
| 8 | + # Keep track of where in the template string we are. |
| 9 | + index = 0 |
| 10 | + |
| 11 | + while index < len(template): |
| 12 | + if template[index:index+8] == '{{noun}}': |
| 13 | + # Add a random noun to the output. |
| 14 | + output.append(random.choice(nouns)) |
| 15 | + index += 8 |
| 16 | + elif template[index:index+8] == '{{verb}}': |
| 17 | + # Add a random verb to the output. |
| 18 | + output.append(random.choice(verbs)) |
| 19 | + index += 8 |
| 20 | + else: |
| 21 | + # Copy a character to the output. |
| 22 | + output.append(template[index]) |
| 23 | + index += 1 |
| 24 | + # Join the output into a single string. |
| 25 | + return ''.join(output) |
| 26 | +nouns = ['apple', 'ball', 'cat', 'dog', 'elephant', |
| 27 | + 'fish', 'goat', 'house', 'iceberg', 'jackal', |
| 28 | + 'king', 'llama', 'monkey', 'nurse', 'octopus', |
| 29 | + 'pie', 'queen', 'robot', 'snake', 'tofu', |
| 30 | + 'unicorn', 'vampire', 'wumpus', 'x-ray', 'yak', |
| 31 | + 'zebra'] |
| 32 | + |
| 33 | +verbs = ['ate', 'bit', 'caught', 'dropped', 'explained', |
| 34 | + 'fed', 'grabbed', 'hacked', 'inked', 'jumped', |
| 35 | + 'knitted', 'loved', 'made', 'nosed', 'oiled', |
| 36 | + 'puffed', 'quit', 'rushed', 'stung', 'trapped', |
| 37 | + 'uplifted', 'valued', 'wanted'] |
| 38 | + |
| 39 | +templates = ['Waiter! I found a {{noun}} in my {{noun}}!', |
| 40 | + 'The {{noun}} {{verb}} the {{noun}}.', |
| 41 | + 'If you {{verb}} the {{noun}}, ' |
| 42 | + 'the {{noun}} will get you.', |
| 43 | + "Let's go: the {{noun}} is {{verb}}.", |
| 44 | + 'Colorless green {{noun}}s {{verb}} furiously.'] |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + print(silly_string(nouns, verbs, templates)) |
| 48 | + |
0 commit comments