Tuesday, March 29, 2022

Tom Scott Longest Word on 7 Segment Display

I was watching Tom Scott's video about how he coded the longest word that can be displayed using 7 segment display. I noticed that he was using Javascript and I suppose there's nothing wrong with that. However, I was wondering how easy it is to do using Linux shell.




And so, I simply did it, and it only took 1 line. 4 lines if you separate the pipes, and 3 if you think that sorting is optional,  and it is optional if you specify 11 letters or above. But we're not sure about it, so let's just count everything and sort it out at the end. I cheated a bit by using awk and there are ways to do it using bash, but not as easy.




First line is the dictionary file. Second is filtering out the words. Here, I use "beghilos" for calculator display. Third is filtering out the length. No less than 3. No more than 11. And print out the length of the word before the actual word. Finally, just sort it out.


Simple. Running time is about 0.2s including display. 0.14s without. Sorting, of course, takes time, but you get all the words from shortest to longest, including duplicates!


And that's it for a quick coding session. The hardest part was looking up awk documentation since I'm rusty.


Incidentally, using the letters as Tom did yields "uncharacteristically" at 20 letters, with running time of 0.3s out of 31997 possible words.


No comments:

Post a Comment