Skip to content

Instantly share code, notes, and snippets.

@tomnomnom
Last active December 4, 2024 19:20
Show Gist options
  • Save tomnomnom/cb82114a3f41eb0ea9f4246ea601a5dd to your computer and use it in GitHub Desktop.
Save tomnomnom/cb82114a3f41eb0ea9f4246ea601a5dd to your computer and use it in GitHub Desktop.
Morse Numbers vs. Letters
<?php
// run as: php gen.php > morse.dat
// then run gnuplot morse.gnuplot
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
for ($i = 0; $i < 5000000; $i++){
$letters = strlen(morse($f->format($i)));
$numbers = strlen((string) $i) * 5;
echo "{$i} {$numbers} {$letters}\n";
}
function morse($in){
$lookup = [
"a" => ".-",
"b" => "-...",
"c" => "-.-.",
"d" => "-..",
"e" => ".",
"f" => "..-.",
"g" => "--.",
"h" => "....",
"i" => "..",
"j" => ".---",
"k" => "-.-",
"l" => ".-..",
"m" => "--",
"n" => "-.",
"o" => "---",
"p" => ".--.",
"q" => "--.-",
"r" => ".-.",
"s" => "...",
"t" => "-",
"u" => "..-",
"v" => "...-",
"w" => ".--",
"x" => "-..-",
"y" => "-.--",
"z" => "--.."
];
$out = "";
foreach(str_split($in) as $letter){
$out .= $lookup[$letter]?? "";
}
return $out;
}
set datafile separator " "
set terminal pngcairo size 1200,800
set output "morse.png"
set title "Morse Numbers vs. Letters"
set xlabel "Number"
set ylabel "Chars"
set grid
set style data line
set key off
plot 'morse.dat' using 1:2, 'morse.dat' using 1:3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment