Restore IP

Challenge: Restore IP Addresses

An IPv4 address consists of exactly four non-negative whole numbers, separated by single dots. Each number is between 0 and 255 (inclusive) and cannot have leading zeros, unless it is 0.

For example, the following are valid IPv4 addresses:

  • 1.1.1.1

  • 255.255.255.255

  • 192.168.0.1

  • 23.25.1.194

While the following are not:

  • 01.1.1.01

  • 256.255.255.255

  • 192.168.00.1

  • a23.b25.1.194

A database containing IPv4 addresses has gotten out of order up by addresses losing their dots. Your job is to restore it. You'll write a generator /gen/restore-ip such that it takes a @t $cord containing only numerical digits and returns a +set of all the @t $cords with dots inserted into the given digits to create a valid IPv4 address.

We also want to crash if the input given is clearly invalid. Your generator should crash in the following cases:

  • If the input contains any characters other than digits.

  • If the input length is greater than 12.

Example usage:

Solutions

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

Solution #1

By ~nodsup-halnux. Clearly written, well-commented, and very Hoonish.

Solution #2

By ~ramteb-tinmut. Another great solution, well written and commented.

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