Phone Letters

Challenge: Phone Letters

Previously, people typed words on a phone by pressing combinations of numbers. Each number mapped to a few different possible letters, as shown below.

 1    2    3
     ABC  DEF

 4    5    6
GHI  JKL  MNO

 7    8    9
PQRS TUV  WXYZ
    
      0

In this task, you will write a generator that accepts a @ud number and returns a (list tape) containing all the different strings that it could represent.

  • Note that 1 and 0 do not map to any letters in the phonepad. Let's crash if the input @ud contains any 1 or 0.

  • Let's return the list of strings sorted in alphabetical order, all in lowercase.

Recall that @ud numbers need a dot marking every three digit places if the number is higher than 999, i.e. 234.567.892 is 234,567,892.

Example usage:

> +phone-letters 29
<<"aw" "ax" "ay" "az" "bw" "bx" "by" "bz" "cw" "cx" "cy" "cz">>

Solutions

These solutions were submitted by the Urbit community as part of a competition in ~2024.8. They are made available under the MIT License and CC0. We ask you to acknowledge authorship should you utilize these elsewhere.

Solution #1

Our style winner, a clean and well-commented solution by ~norweg-rivlex.

Solution #2

The speed winner by ~diblud-ricbet.

Unit Tests

Following a principle of test-driven development, the unit tests below allow us to check for expected behavior. To run the tests yourself, follow the instructions in the Unit Tests section.

Last updated