16
\$\begingroup\$

Back in the day, telephone autodialers used punched cards with one column for each digit of the number to be dialed. Columns had seven rows. The first three rows represented the numbers (1,2,3), (4,5,6), and (7,8,9) respectively. The last three rows rotated this arrangement by 90°: (1,4,7), (2,5,8), and (3,6,9). The middle row was used for 0. Any digit 1-9 would have two holes punched - one in the first three rows, and one in the bottom three rows. Zero would only have the middle row punched. Let's visualize the punched column for the number 6 (. is unpunched, x is punched, guide on left is just to illustrate the encoding):

123 .
456 x
789 .
 0  .
147 .
258 .
369 x

We look for which row(s) contain the number we're trying to dial. For 6, this is the second row, and the ninth row. These two rows are punched, the remaining five rows are unpunched. Here are the punched patterns for all digits 0-9:

    0 1 2 3 4 5 6 7 8 9
123 . x x x . . . . . .
456 . . . . x x x . . .
789 . . . . . . . x x x
 0  x . . . . . . . . .
147 . x . . x . . x . .
258 . . x . . x . . x .
369 . . . x . . x . . x

Your goal is to (write a program or function to) punch these cards for me.

Input: A number, taken in any reasonable format (string, integer, list of integers, &c.), not to exceed 9999999999999.

Output: The grid of punched columns corresponding to the number input. You don't need the headers or extra spacing shown above, just the punched columns themselves. Leading/trailing newlines are ok, as is whitespace between rows/columns as long as it is consistent. Speaking of, as long as they are consistent, you may use any (non-whitespace) character for punched, and any other character for unpunched (while it should be obvious, please specify what characters you are using).

This is code-golf, so shortest code wins. Standard loopholes are disallowed.

Test cases (all use . for unpunched, x for punched):

In: 911
Out: .xx
     ...
     x..
     ...
     .xx
     ...
     x..

In: 8675309
Out: ....x..
     .x.x...
     x.x...x
     .....x.
     ..x....
     x..x...
     .x..x.x

In: 5553226
Out: ...xxx.
     xxx...x
     .......
     .......
     .......
     xxx.xx.
     ...x..x
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Totally unnecessary for solving the question, but there's a neat little gallery of these cards/devices here. \$\endgroup\$
    – brhfl
    Apr 16, 2018 at 17:31
  • \$\begingroup\$ you may use any character for punched, and any other character for unpunched you mean any non-whitespace characters? \$\endgroup\$ Apr 16, 2018 at 17:36
  • \$\begingroup\$ Yes, I suppose that would be preferable since whitespace is allowed elsewhere. And for the sake of being able to see. Will edit in, thanks. \$\endgroup\$
    – brhfl
    Apr 16, 2018 at 17:47
  • 1
    \$\begingroup\$ cool challenge...tempted to write a FORTRAN 77 answer, but idk how to golf it \$\endgroup\$
    – qwr
    Apr 16, 2018 at 18:44
  • \$\begingroup\$ @qwr Feel free to create a "Tips for golfing in Fortran" post. \$\endgroup\$
    – mbomb007
    Apr 16, 2018 at 20:57

12 Answers 12

4
\$\begingroup\$

Jelly, 13 bytes

9s3,Z$j0ċþDZY

Try it online!

1 = punctured, 0 = not punctured.

\$\endgroup\$
3
\$\begingroup\$

Pyth, 25 bytes

.tm?djNmX*3NkZ.Dtd3X*7N3Z

Uses 0 for punched and " for unpunched.
Try it here

Explanation

.tm?djNmX*3NkZ.Dtd3X*7N3Z
  m                      Q  For each number in the (implicit) input...
   ?d                       ... if the number is nonzero...
              .Dtd3         ... get (n - 1) divmod 3...
       mX*3NkZ              ... replace each position in `"""` with `0`...
     jN                     ... and stick them together with `"`. ...
                   X*7N3Z   ... Otherwise, `"""0"""`.
.t                          Transpose the result.
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 60 54 bytes

Takes input as an array of integers. Returns a binary matrix, with 0 = unpunched / 1 = punched.

a=>[14,112,896,1,146,292,584].map(n=>a.map(i=>n>>i&1))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 16 15 bytes

Uses 0 and 1.

ε9ÝÀ3ôD¨ø«¢O}ø»

Try it online!

Explanation

ε           }     # apply to each digit in input                
 9Ý               # push the range [0 ... 9]
   À              # rotate left
    3ô            # split into pieces of 3
      D¨          # duplicate and remove the last digit (0)
        ø         # transpose
         «        # append
          ¢O      # sum the counts of each in the current digit     
             ø    # transpose
              »   # format output
\$\endgroup\$
2
\$\begingroup\$

SOGL V0.12, 23 bytes

,{r"ΧL→▓lφ℮o¤κ²‘7nwι}⁰H

Try it Here!

Compression.

\$\endgroup\$
2
\$\begingroup\$

Python 2, 84 bytes

lambda a:[''.join(`(ord('(1Aa2Bb4Dd'[int(n)])-32)>>k&1`for n in a)for k in range(7)]

Try it online!

0/1 is used for unpunched/punched.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 84 80 bytes

def f(s):[print(*[int(i in[~-n//3,6--n%3-3*(n<1)])for n in s])for i in range(7)]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Something seems funky with your fifth and sixth rows (they seem to be flipped). [1,2,3] should form a diagonal line down, for instance. \$\endgroup\$
    – brhfl
    Apr 16, 2018 at 18:22
  • \$\begingroup\$ @brhfl thanks for telling me, fixed it \$\endgroup\$
    – ovs
    Apr 16, 2018 at 18:26
2
\$\begingroup\$

C (clang), 108 107 bytes

c,i;f(*q){char*r;for(i=~0;i++<6;puts(""))for(r=q;c=*r++;c-=48,putchar(".X"[(c--?16<<c%3|1<<c/3:8)>>i&1]));}

Try it online!

Takes input number as string. Prints output in . and X as in examples.

Credits

-1 byte thanks @ASCII-only

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 107, and removed header otherwise you'd need to include the header in the bytecount \$\endgroup\$
    – ASCII-only
    Apr 21, 2018 at 2:57
  • \$\begingroup\$ Can you please point me to general consensus that header inclusions needs to be counted for byte count towards function solutions (not full programs) \$\endgroup\$
    – GPS
    Apr 21, 2018 at 3:38
  • \$\begingroup\$ 1, 2, 3 \$\endgroup\$
    – ASCII-only
    Apr 21, 2018 at 8:02
  • \$\begingroup\$ Suggest putchar(".X"[(c--?16<<c%3|1<<c/3:8)>>i&1]))c-=48 instead of c-=48,putchar(".X"[(c--?16<<c%3|1<<c/3:8)>>i&1])) \$\endgroup\$
    – ceilingcat
    Nov 23, 2018 at 15:19
2
\$\begingroup\$

J, 31 20 bytes

-11 bytes thanks to FrownyFrog!

(e."1],0,|:)1+i.@3 3

Try it online!

J, 31 bytes

1*@|:@:#.(a,0,|:a=.1+i.3 3)=/~]

Try it online!

Takes the input as a list of digits

0 - unpunched, 1 - punched

Explanation:

   a=.1+i.3 3 - generates the matrix and stores it into a
1 2 3
4 5 6
7 8 9

   (a,0,|:a=.1+i.3 3) - generates the entire comparison table 
1 2 3
4 5 6
7 8 9
0 0 0
1 4 7
2 5 8
3 6 9

   ]=/ - creates an equality table between the input and the comparison table

 ((a,0,|:a=.1+i.3 3)=/~]) 9 1 1
0 0 0
0 0 0
0 0 1
0 0 0
0 0 0
0 0 0
0 0 1

1 0 0
0 0 0
0 0 0
0 0 0
1 0 0
0 0 0
0 0 0

1 0 0
0 0 0
0 0 0
0 0 0
1 0 0
0 0 0
0 0 0

  1*@|:@:#. - adds the tables, transposes the resulting table and finds the magnitude

  (1*@|:@:#.(a,0,|:a=.1+i.3 3)=/~])  9 1 1
0 1 1
0 0 0
1 0 0
0 0 0
0 1 1
0 0 0
1 0 0
\$\endgroup\$
2
  • \$\begingroup\$ @FrownyFrog Thank you! You are brilliant as always! \$\endgroup\$ Apr 21, 2018 at 6:23
  • 1
    \$\begingroup\$ 20 \$\endgroup\$
    – FrownyFrog
    Apr 21, 2018 at 15:32
1
\$\begingroup\$

Canvas, 27 25 bytes

{9+├3÷u4% ×#+#¹╷3%5+1╋]↶↕

Try it here!

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 28 bytes

E⁴⭆θI⁼ι÷﹪⊖λχ³E³⭆θI∧Iλ¬﹪⁻⊖λι³

Try it online! Link is to verbose version of code. Uses 0/1, but can support arbitrary characters at a cost of 1 byte: Try it online!. Explanation:

E⁴              Loop from 0 to 3
  ⭆θ            Loop over input string and join
           λ     Current character
         ⊖      Cast to integer and decrement
        ﹪  χ    Modulo predefined variable 10 (changes -1 to 9)
       ÷    ³   Integer divide by literal 3
     ⁼ι         Compare to outer loop variable
    I           Cast to string
                Implicitly print each outer result on a separate line

E³              Loop from 0 to 2
  ⭆θ            Loop over input string and join
            λ   Current character
           ⊖    Cast to integer and decrement
          ⁻  ι  Subtract outer loop variable
         ﹪    ³ Modulo by literal 3
        ¬       Logical not
       λ        Inner loop character
      I         Cast to integer
     ∧          Logical and
    I           Cast to string
                Implicitly print each outer result on a separate line
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -F, 52 bytes

for$i(123,456,789,0,147,258,369){say map$i=~$_|0,@F}

Try it online!

Uses 1 for punched and 0 for unpunched.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.