KISS 🇺🇦

Stop the war!

Stop the war in Ukraine! Fuck putin!

More information is at: https://war.ukraine.ua/.

There is a fund to support the Ukrainian Army: https://savelife.in.ua/en/donate/, and there is a special bank account that accepts funds in multiple currencies: https://bank.gov.ua/en/about/support-the-armed-forces. I donated to them. Please donate if you can!

Killer putin

Killer putin. Source: politico.eu.

Arrested putin

"It hasn't happened yet, but it will happen sooner or later. Beautiful photo, isn't it?" Source: twitter.

A postal code encoding question

| comments

USSR used a fixed stencil to write postal codes on mail envelopes. It’s similar to a seven-segment display, but had 9 segments with a standard way to write all 10 digits:

I stumbled upon an article (in Russian) claiming, and confirming, that only 4 of the 9 segments are enough to distinguish all 10 digits. The article had a small challenge to write a program to find out the number of different ways to pick 4 segments that allow to distinguish all the digits. It’s a small, yet interesting problem to solve; my program is in Haskell and is available at https://github.com/eunikolsky/postalcode.

I’ve had a few versions of the script, but didn’t save them as separate commits. It took me about an hour to arrive at the final version:

  • First, I hardcoded the digits by manually entering the empty/fill state of every segment (this code is commented on lines 33–46). But it wasn’t obvious at all what the lists meant and how they were constructed and whether I entered them correctly.

  • This comment (also a script in Haskell) gave me an idea of parsing the segments out of the visual representation. I wrote down each digit as its own 2D list and wrote a parser to read the segments. That worked, but the source file got quite a bit longer because each digit used 7 lines. Each digit had only 9 segments like this:

1
2
3
4
5
6
[ " - "
, "  |"
, "   "
, " / "
, " - "
]
  • Then I compacted all the digits into one list of strings like in that comment above. Thus, I needed to cut out each digit out of every string, but it was easy by transposing the list (so that the strings had the digits rotated 90° clockwise), taking 3 lines for each digit and transposing each of those lists back (rotating 90° counter-clockwise).

  • Finally, that 2 above doesn’t look pretty because there is a lot of empty space. I updated my parser to expect and ignore certain characters on joints, allowing me to parse this:

1
2
3
4
5
6
[ "--+"
, "  |"
, "  +"
, " / "
, "+--"
]

The final digits string is on lines 26–31.

It was easy to write the script in Haskell because the type system helped me on the way and I liked that the text parser Text.Parsec is available out of the box.

By the way, the answer is four.

Haskell

Note: The comments in the blog are provided by disqus.com; if you don't see the comment form under the post, probably your browser or its extension (such as uBlock Origin or NoScript) blocks their scripts.

« Details about a Haskell puzzle

Comments