DEV Community

Cover image for Counting 5-letter words
Sebastian Nozzi
Sebastian Nozzi

Posted on

Counting 5-letter words

Over at the MiniScript community a "daily MiniScript challenge" has been announced.

It consists of small exercises to try on MiniScript / Mini Micro.

For a while I have been wanting to write blog posts about Mini Micro - so I take this as an opportunity not only to solve the challenges (the easy part) but to write a blog post about it!

So, let's begin ...

The first challenge is called:

How many 5-letter words are there?

The first challenge is easy: from a given list of words, count how many 5-letter words there are.

The list of words can be found on Mini Micro under:

/sys/data/englishWords.txt
Enter fullscreen mode Exit fullscreen mode

How do we solve this?

We need to:

1) Open the file
2) Iterate over each line
3) Determine the line's length
4) Increase a counter if the length is 5
5) Print the result

A file is opened with file.open. It returns a FileHandle object.

In turn, we can ask the FileHandle to read a line (with readLine) and also if we are at the end of the file (with atEnd).

Let's try that:

] wordsFile = file.open("/sys/data/englishWords.txt")
] wordsFile.atEnd
0
Enter fullscreen mode Exit fullscreen mode

Let's read a some lines:

] wordsFile.readLine
a
] wordsFile.readLine
aah
] wordsFile.readLine
aardvark
] wordsFile.readLine
aardvarks
] wordsFile.readLine
abaci
] wordsFile.readLine
aback
Enter fullscreen mode Exit fullscreen mode

We see it works. With this we have the necessary elements iterate over all lines.

Over at the editor we could type something like this:

wordsFile = file.open("/sys/data/englishWords.txt")
while not wordsFile.atEnd
    word = wordsFile.readLine
    print word
end while
Enter fullscreen mode Exit fullscreen mode

Note how we are iterating in a loop while the file is not at the end. Don't forget the not operator.

This will print all words contained in the file ... but they are a lot! After some seconds I interrupted the program with CTRL-C

We could instead count the amount of lines. Let's do that instead:

count = 0

wordsFile = file.open("/sys/data/englishWords.txt")

while not wordsFile.atEnd
    word = wordsFile.readLine
    count = count + 1
end while

print "The file contains " + count + " words"
Enter fullscreen mode Exit fullscreen mode

Now, this was much faster. My result was:

The file contains 64664 words
Enter fullscreen mode Exit fullscreen mode

Yours? Should be the same. Unless the list got updated.

This program is now very close to what we wanted to achieve: count only the words that are 5 letters long.

In order to measure a string's length we use the intrinsic function len, which works on lists, maps and strings.

The program then needs only a small change. Instead of just always counting each line:

count = count + 1
Enter fullscreen mode Exit fullscreen mode

... we will do so conditionally. Change the line above for:

if len(word) == 5 then count = count + 1
Enter fullscreen mode Exit fullscreen mode

This can be written in one line as above or alternatively as:

if len(word) == 5 then 
    count = count + 1
end if
Enter fullscreen mode Exit fullscreen mode

It would be also appropriate to change the result-message. Something like this would do:

print "There are " + count + " 5-letter words"
Enter fullscreen mode Exit fullscreen mode

Try the program now. What happens when you run it?

I get:

program-result showing amount of 5-letter words

Did you also get that?

This is the end version of my program:

end-version of the program

Hope this mini-challenge was fun and you learnt some MiniScript / MiniMicro.

See you in the next one!

Top comments (2)

Collapse
 
joestrout profile image
JoeStrout

Great post! And don't forget, an alternative to using file.open, f.atEnd, and f.readLine is to use file.readLines, which loads the whole file into a list you can easily iterate over.

Collapse
 
sebnozzi profile image
Sebastian Nozzi • Edited

Thanks! Did not know that :-)

For completeness' sake, here is the program with the for-loop / readLines variant:

Program with readLines variant

Note how a file-handle is not returned. Instead a list of all lines in the files in returned directly.

It can then be directly iterated with for [variable] in [collection], where collection in this case is a list (but could have been a string or a map).