Rafraîchissoir

By Shahed Nooshmand

Perl Weekly Challenge: week 69

Task #1

A strobogrammatic number is a number that looks the same when looked at upside down.

You are given two positive numbers $A and $B such that 1 <= $A <= $B <= 10^15.

Write a script to print all strobogrammatic numbers between the given two numbers.

Here’s one solution:

($A ^..^ $B).grep: { /^ <[01689]>+ $/ and $_ eq .flip.trans(“69” => “96”) }

Task #2

A 0/1 string is a string in which every character is either 0 or 1.

Write a script to perform switch and reverse to generate S30 as described below:

switch:

Every 0 becomes 1 and every 1 becomes 0. For example, “101” becomes “010”.

reverse:

The string is reversed. For example, “001” becomes “100”.

  S0 = “”
  S1 = “0”
  S2 = “001”
  S3 = “0010011”
  …
  SN = SN-1 + “0” + switch(reverse(SN-1))

Here you go:

“”, { $_ ~ 0 ~ .flip.trans(“01” => “10”) } … *

Now just subscript that with 30 if you’re really patient. 1000 if you’re immortal.