Scrupuli

blunt essays with sharp points

rad50.pl - interpret quadragesimal numbers

by Scrvpvlvs
Nov 19, 2010 6:43 AM–
  1 #! /usr/bin/perl
  2
  3 # Title:
  4 #   rad50.pl
  5 #
  6 # Author:
  7 #   MetaEd
  8 #
  9 # Version:
 10 #   November 22, 3732
 11 #
 12 # This document is a Perl 5 program which will:
 13 #
 14 #   1. take a line of text as input
 15 #
 16 #   2. interpret it as a string of three-digit quadragesimal numbers
 17 #
 18 #        NOTE: Quadragesimal notation is analogous to octal,
 19 #        decimal, and hexadecimal notation. Octal uses eight symbols
 20 #        (numerals), decimal uses ten, hexadecimal uses sixteen, and
 21 #        quadragesimal notation uses forty numerals.
 22 #
 23 #   3. convert each such number to six-digit octal numbers
 24 #
 25 #   4. type the octal numbers as a line of text
 26 #
 27 #   5. stop
 28 #
 29 # Sample input:
 30 #   "Nothing sucks like a VAX."
 31 #
 32 # Interpretation as quadragesimal numbers:
 33 #   "NOT/HIN/G S/UCK/S L/IKE/ A /VAX/.  "
 34 #
 35 #     NOTE: The interpretation is not case sensitive. If the last
 36 #     number is incomplete, it is padded on the right with space.
 37 #
 38 # Sample output:
 39 #   054754 031566 025723 101703 073314 034775 000050 104700 127400\n
 40
 41 # First, we declare the forty symbols we recognize as quadragesimal
 42 # numerals.
 43
 44 $_ =
 45 $numerals =
 46 $digital020 = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ$.%0123456789' ;
 47   # ' ' (space) = zero, 'A' = one, ..., 'V' = twenty-two, ...
 48
 49 $digital044 = ' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.$%' ;
 50   # ' ' (space) = zero, 'A' = eleven, ..., 'V' = thirty-two, ...
 51
 52 #   NOTE: Two different notation schemes are commonly used. These
 53 #   originate from Digital Equipment Corporation (DEC) sixteen bit
 54 #   systems and DEC thirty-six bit systems, respectively. The same
 55 #   symbols are used by both schemes, but the ordering is different.
 56 #   For our purpose, we adapt the sixteen bit ordering.
 57
 58 # We create a hash table which maps each numeral to its number.
 59
 60 %rad050 = map( ($_ => $i++), split // ) ;
 61
 62 #   NOTE: These schemes (both of them) are better known to systems
 63 #   programmers as Radix 50. Why Radix 50, not Radix 40? Because 50
 64 #   (octal) = 40 (decimal). Ask a silly question, get a silly
 65 #   answer.
 66
 67 # We read input from a literal string, and transform it to valid
 68 # input.
 69
 70 $_ = "Nothing sucks like a VAX." ;
 71 tr/[a-z]/[A-Z]/ ;                 # convert lower case to UPPER CASE
 72 eval "tr/$numerals//cd" ;         # delete any illegal character
 73 $_ .= '  ' ;                      # pad the last number with space
 74
 75 # We create an accumulator to hold intermediate values during
 76 # interpretation, a place counter to keep track of what place of the
 77 # quadragesimal number we're on, and a string to hold the result.
 78
 79 $accum = 0 ;
 80 $place = 0 ;
 81 $output = '' ;
 82
 83 # We consider each numeral in turn from left to right.
 84
 85 foreach( split // ) {
 86
 87   # As a failsafe, we assert that the current numeral can be
 88   # interpreted quadragesimally. If not, we construct an identifying
 89   # error message and stop.
 90
 91   index( $numerals, $_ ) >= 0
 92     or
 93   die "$0: current numeral: "
 94   . ( /^[[:graph:]]$/ ? $_ : sprintf("<%#o>",ord($_)))
 95   . "\nILLEGAL FILE NAME\n" ;
 96
 97   # We shift the accumulated value left one quadragesimal place to
 98   # make room for the value corresponding to the current numeral,
 99   # taken from the hash table.
100
101   $accum = $accum * 050 + $rad050{$_} ;
102
103   # If the place counter tells us the number is complete, we write
104   # it in octal with a space separator, and reset the accumulator
105   # and the place counter.
106
107   if ( $place++ == 2 ) {
108     $output .= sprintf "%06o ", $accum ;
109     $accum = 0 ;
110     $place = 0 ;
111   }
112
113 }
114
115 # At the end, we type the line of output.
116
117 print $output, "\n" ;
118
119 # HISTORICAL NOTE: Systems programmers used quadragesimal (Radix 50)
120 # numbers to economize on memory needed to store text: for example,
121 # file names in the disk index. Consider the file name "NONAME".
122 # Storing it as a string of six ASCII codes would cost forty-eight
123 # bits:
124 #
125 #   N        O        N        A        M        E
126 #   01001110 01001111 01001110 01000001 01001101 00100101
127 #
128 # But storing it as a string of two three-digit quadragecimal
129 # numbers would cost sixteen fewer bits:
130 #
131 #   NON               AME
132 #   01011001 11100110 00001000 01001101
133 #
134 # Economy was traded for convenience. File names were limited to the
135 # repertoire of forty quadragesimal numerals. And conversions to and
136 # from quadragesimal were arcane, despite the availability of tools
137 # in the system library. For example, here's how the PDP-11 RSTS/E
138 # systems programmer could store a file name as a quadragesimal
139 # number with this technique, saving sixteen bits of memory:
140 #
141 # 1000    T$ = "NONAME"                                     ! ASCII
142 # 1010    T$ = MID(SYS(CHR$(6%) + CHR$(-10%) + T$), 7%, 4%) ! RAD50
143 #
144 # And here's how the programmer could recover the file name as text:
145 #
146 # 2000    CHANGE T$ TO T%
147 # 2010    T$ = RAD$(      T%(0%)
148 #                 + SWAP%(T%(1%))
149 #            + RAD$(      T%(2%)
150 #                 + SWAP%(T%(3%))
151 #
152 # Or:
153 #
154 # 2000    T$ = RAD$(      ASCII(MID(T$,0%,1%))
155 #                 + SWAP%(ASCII(MID(T$,1%,1%))))
156 #            + RAD$(      ASCII(MID(T$,2%,1%))
157 #                 + SWAP%(ASCII(MID(T$,3%,1%))))
158
159 # vim:set sw=2 sts=2 tw=68 : Instructions for Vim, the text editor.

Labels: , , , , , , , , , , , , , , , , ,

Share: submit to reddit

0 Comments:

Post a Comment

<< Home

about.me

Follow

feed

E-mail: enter address

Project Euler competitor metaed

vs.

Project Euler competitor db8

profile for MetaEd on Stack Exchange, a network of free, community-driven Q&A sites

Recent Articles

Out Of Kindness, Play Dumb

Oklahoma law banning Islamic law could also ban Bi...

About Roundup: Part 1, Preface and Introduction

NPR’s firing of Juan Williams an example of an eth...

Juan Williams firing was justified

What we honestly ask Google about one another

Question, Don’t Attack, Objections

Google Translates Steve Jobs Out Of The Picture

Intelligent Transportation Systems (revisited)

Will coal, oil, and gas destroy us?

Archives

November 1999
June 2000
July 2000
September 2001
October 2001
February 2002
March 2002
June 2003
February 2004
June 2004
July 2004
August 2004
September 2004
February 2005
March 2005
November 2005
July 2007
March 2008
April 2008
May 2008
October 2008
November 2008
December 2008
January 2009
April 2009
September 2009
December 2009
February 2010
March 2010
May 2010
June 2010
September 2010
October 2010
November 2010
December 2010
January 2011
April 2011
June 2011
July 2011
August 2011
September 2011
December 2011
February 2012
April 2012
May 2012
June 2012
July 2012
August 2012
September 2012
November 2012
January 2013
February 2013
April 2013
February 2014
May 2014
October 2014