text stringlengths 0 897 |
|---|
``` |
But enough playing! Keep your data-scientist hat on a bit longer, but now balance your game-developer hat on top (programmers often have to wear many hats). We're going to be making a game that involves a grid of random letters. But we don't want to pick all letters with equal likelihood; that would result in a lot ... |
## Letter Frequency Analysis |
That's another data analysis question we can answer with code. This one requires a little more code than you probably want to do on the REPL command line, though. So fire up the editor, and enter the following. |
{caption: "Letter frequency analysis program."} |
```miniscript |
import "mapUtil" |
import "listUtil" |
// load the word list |
words = file.readLines("/sys/data/englishWords.txt") |
// convert all words to uppercase |
for i in words.indexes |
words[i] = words[i].upper |
end for |
// count the frequency of each letter |
print "Analyzing words..." |
counts = {} // key: letter; value: count |
for word in words |
for letter in word |
counts[letter] = counts.get(letter, 0) + 1 |
end for |
end for |
print counts |
``` |
This one takes a little longer to run, as it's iterating not only over the words, but the letters within the words. But before too long, out pops the answer: the total number of times each letter occurs in the word list. Save this program as `freqCount.ms` in case you need it later. |
Now we're going to do a trick. Select that output by clicking and dragging over it with the mouse. Now copy with Control-C (or on a Mac, you can use Command-C). Use `reset` to clear the frequency-counting program, then `edit` to start a new one. Finally, type "freq =", and then paste the output you copied before (u... |
{caption: "The start of our word game, with frequencies copied from the output of the previous program."} |
```miniscript |
freq = {"A": 40116, "H": 12329, "R": 38506, "D": 21334, |
"V": 5427, "K": 5275, "S": 49034, "B": 10303, "C": 21554, |
"I": 47047, "U": 17692, "E": 62155, "L": 27954, "O": 33150, |
"N": 37001, "G": 16930, "M": 14495, "T": 36982, "Y": 8191, |
"J": 941, "Z": 2090, "P": 15815, "F": 7635, "W": 5081, |
"Q": 990, "X": 1487} |
``` |
Save this as "wordGame.ms" and run it. It doesn't generate any output, but once run you should be able to enter `freq` and see your frequency map. |
D> You just used code to write code. Programs making programs. Scary, or cool? Maybe both! |
## Starting the Word Game |
Let's start building the rest of the game. Edit your program, and insert some lines above your frequency table, so that it looks like this. |
{i:"`/sys/lib`,`mapUtil`;`/sys/lib`,`listUtil`;`/sys/lib`,`mathUtil`;`/sys/lib`,`sounds`"} |
{caption: "Listing 1 (Start of the actual game)."} |
```miniscript |
import "mapUtil" |
import "listUtil" |
import "mathUtil" |
import "sounds" |
clear |
text.color = color.silver |
display(2).mode = displayMode.pixel |
overlay = display(2) |
overlay.clear |
kRows = 5 |
kCols = 5 |
words = file.readLines("/sys/data/englishWords.txt") |
for i in range(0, words.len-1) |
words[i] = words[i].upper |
end for |
freq = {"A": 40116, "H": 12329, "R": 38506, "D": 21334, |
"V": 5427, "K": 5275, "S": 49034, "B": 10303, "C": 21554, |
"I": 47047, "U": 17692, "E": 62155, "L": 27954, "O": 33150, |
"N": 37001, "G": 16930, "M": 14495, "T": 36982, "Y": 8191, |
"J": 941, "Z": 2090, "P": 15815, "F": 7635, "W": 5081, |
"Qu": 990, "X": 1487} |
``` |
{i:"`/sys/lib`"} |
The first four lines simply import some modules from `/sys/lib` that we will make use of elsewhere. (When writing your own programs from scratch, you can add these as you need them, or just put your favorites at the top of any program you begin. There's no harm in importing them even if you don't actually use them.) |
Lines 6-11 clear the screen and set up the displays. That includes changing the text color to silver, so don't be surprised to see that happen when you run this code. |
Lines 13 and 14 define how many rows and columns we will use in our grid of random letters. The "k" prefix is commonly used to identify a "constant" value, that is, a value that should not change throughout the life of the program. (Yes, I know that "constant" does not start with a "k" in English. Nonetheless this i... |
Then lines 16-19 load the words file, and convert all the words to upper case. That saves us from having to worry about mixed upper/lower case later on. |
Finally, after all that comes the frequency table you developed in the last section, with one little change: find the "Q" entry, and change it to "Qu". Because we determined earlier that words that use a "q" without a "u" are extremely rare, our game will never use a "q" by itself; instead we'll always pick that as th... |
## Random Letters |
Now we need some code to generate a random letter, or a whole grid of random letters. And we want to do this in a way that mirrors the normal frequency of letters in our word set, so that "E" is the most common letter picked, "J" is the least, and so on. Add the listing below to your growing program. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.