Skip to main content
added 365 characters in body
Source Link
Dom Hastings
  • 24.4k
  • 4
  • 56
  • 93

Perl 5, 195 bytes

194 bytes code + 1 for -p.

%h=map{$_,(A..Z,0..9)[$i++]}unpack"S26I2S7I","\xd5]u]\xddUw\xd7uww\xdd\xd7]WWwWwuwwwwwWwWUU\xd5uw\xdd\xdd";s/0{7}/ /g;s/(\d+?)(000|\b)/$h{oct"0b$1"}/ge

TIO seems to think this only has 141 bytes, but that isn't right, not sure if that's a bug or what! I couldn't get this working with just a standard packed binary string, I had to escape the higher-byte chars otherwise I'd be on 171, if anyone knows what I've missed, or why it's breaking that'd be great!

Try it online!

Explanation

The binary string is a packed list of the numbers that relate to the morse characters (101011101 - 349 for F etc) and this is zipped with the ranges A..Z,0..9 and used as a lookup. The s/// expressions replace all runs of seven 0s with space and then all runs of digits, separated with three 0s or word boundaries \b, with their corresponding key from the %h hash.

Perl 5, 195 bytes

194 bytes code + 1 for -p.

%h=map{$_,(A..Z,0..9)[$i++]}unpack"S26I2S7I","\xd5]u]\xddUw\xd7uww\xdd\xd7]WWwWwuwwwwwWwWUU\xd5uw\xdd\xdd";s/0{7}/ /g;s/(\d+?)(000|\b)/$h{oct"0b$1"}/ge

TIO seems to think this only has 141 bytes, but that isn't right, not sure if that's a bug or what! I couldn't get this working with just a standard packed binary string, I had to escape the higher-byte chars otherwise I'd be on 171, if anyone knows what I've missed, or why it's breaking that'd be great!

Try it online!

Perl 5, 195 bytes

194 bytes code + 1 for -p.

%h=map{$_,(A..Z,0..9)[$i++]}unpack"S26I2S7I","\xd5]u]\xddUw\xd7uww\xdd\xd7]WWwWwuwwwwwWwWUU\xd5uw\xdd\xdd";s/0{7}/ /g;s/(\d+?)(000|\b)/$h{oct"0b$1"}/ge

I couldn't get this working with just a standard packed binary string, I had to escape the higher-byte chars otherwise I'd be on 171, if anyone knows what I've missed, or why it's breaking that'd be great!

Try it online!

Explanation

The binary string is a packed list of the numbers that relate to the morse characters (101011101 - 349 for F etc) and this is zipped with the ranges A..Z,0..9 and used as a lookup. The s/// expressions replace all runs of seven 0s with space and then all runs of digits, separated with three 0s or word boundaries \b, with their corresponding key from the %h hash.

Source Link
Dom Hastings
  • 24.4k
  • 4
  • 56
  • 93

Perl 5, 195 bytes

194 bytes code + 1 for -p.

%h=map{$_,(A..Z,0..9)[$i++]}unpack"S26I2S7I","\xd5]u]\xddUw\xd7uww\xdd\xd7]WWwWwuwwwwwWwWUU\xd5uw\xdd\xdd";s/0{7}/ /g;s/(\d+?)(000|\b)/$h{oct"0b$1"}/ge

TIO seems to think this only has 141 bytes, but that isn't right, not sure if that's a bug or what! I couldn't get this working with just a standard packed binary string, I had to escape the higher-byte chars otherwise I'd be on 171, if anyone knows what I've missed, or why it's breaking that'd be great!

Try it online!