3 ms·
I like to use a simple name generator, used to generate names for game characters. It's virtues are that it's simple enough to write in less than an hour, compl
by mikelevins 10y ago
I like to use a simple name generator, used to generate names for game characters. It's virtues are that it's simple enough to write in less than an hour, complex enough to give you a sense of how the language and its standard library work, it uses standard I/O streams and reads files, it uses random numbers, and it involves some manipulation of strings and sequences.
The basic idea is:
1. read a file containing a list of sample names, one name per line
2 convert each name into a sequence of triplets; as an example, 'Fred' becomes the list ['Fre', 'red']
3 choose a random starting triplet
4. choose a random non-starting triplet that matches the start. A triplet matches the previous choice when the last two letters of the previous one are the same as the first two of the next. Repeat with the new triplet as the start.
5. When you reach and end triplet or the length exceeds some limit, stop and merge the triplet. Merging the sequence means merging them two at a time left-to-right. Merging two triplets means dropping the first two letters of the one on the right and tacking the remaining letter onto the one on the left.
Besides being easy and kind of useful for learning languages, it's also kind of fun. With the right input lists the names generated can be pretty funny.
'Corichadwinardo'
'Tadforgio'
- rashkov 10y agoThat's really neat. I like that it's solving a language problem and coming up with something intelligible and fun. It is also right in the sweet spot for complexity. A nice "hello programming language" exercise indeed