Code Golf:Code 39条形码

gwe*_*ell 26 language-agnostic code-golf barcode code39 rosetta-stone

挑战

最短的代码字符数用于绘制Code 39条形码的ASCII表示.

关于Code 39的维基百科文章:http: //en.wikipedia.org/wiki/Code_39

输入

输入将是Code 39条形码的一系列合法字符.这意味着43个字符有效:0- 9 A- Z (space) and -.$/+%.该*字符不会出现在输入中,因为它用作开始和结束字符.

产量

Code 39条形码中编码的每个字符都有九个元素,五个条形和四个空格.条形将用#字符表示,空格用空格字符表示.九个元素中的三个将是广泛的.窄元素将是一个字符宽,宽元素将是三个字符宽.应在每个字符图案之间添加单个空格的字符间空格.应重复该模式,以使条形码的高度为八个字符高.

开始/停止字符*(bWbwBwBwb)将表示如下:

                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       ^ ^ ^^ ^ ^ ^ ^^^
                       | | || | | | |||
           narrow bar -+ | || | | | |||
           wide space ---+ || | | | |||
           narrow bar -----+| | | | |||
         narrow space ------+ | | | |||
             wide bar --------+ | | |||
         narrow space ----------+ | |||
             wide bar ------------+ |||
         narrow space --------------+||
           narrow bar ---------------+|
inter-character space ----------------+
Run Code Online (Sandbox Code Playgroud)
  • *需要在条形码的开头和结尾输出开始和结束字符.
  • 条形码之前或之后不需要包含安静的空间.
  • 不需要计算校验位.
  • 不需要完整的ASCII Code39编码,只需标准的43个字符.
  • 不需要在ASCII条形码表示下方打印文本以识别输出内容.
  • #如果需要,可以用另一个更高密度的字符替换该字符.使用完整的块字符U + 2588,将允许条形码在打印时实际扫描.

测试用例

Input:
ABC
Output:
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
Run Code Online (Sandbox Code Playgroud)
Input:
1/3
Output:
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
Run Code Online (Sandbox Code Playgroud)
Input:
- $     (minus space dollar)
Output:
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
Run Code Online (Sandbox Code Playgroud)

代码计数包括输入/​​输出(完整程序).

Dav*_*vid 25

J,102个字符

8#,:' #'{~,0,.~#:(3 u:'???????????????????????????????????????????????????????????'){~32-~a.i.'*'(,,[)
Run Code Online (Sandbox Code Playgroud)

说明.从下往上阅读:

8#,:         NB. Copy 8 times
' #'{~       NB. Turn binary 0 and 1 into space and #
,            NB. Link the array into a list
0,.~         NB. Append a 0 to the end of each row of the array.
#:           NB. Turn the list of numbers into a binary array where each row is the base-2 representation of the corresponding number
(3 u:'???????????????????????????????????????????????????????????') NB. Turn this wchar string into a list of ints in range 0-65535.
{~           NB. Select numbers from the string-list whose indices are...
32-~         NB. ... 32 less than ...
a.i.         NB. ... the ascii values of ...
'*'(,,[)     NB. ... the input string with a '*' on either side!
Run Code Online (Sandbox Code Playgroud)


Anu*_*rag 8

Ruby(1.9) - 121 132 141 166 170 289 295

向大卫致敬

puts"*#{$_}*
".tr(" --9*$+%A-Z","????????????????????????????????????????????").gsub(/./){|c|c.ord.to_s(2).tr"01"," #"}*8


echo "ABC" | ruby -ne 'puts"*#{$_}*
".tr(" --9*$+%A-Z","????????????????????????????????????????????").gsub(/./){|c|c.ord.to_s(2).tr"01"," #"}*8'
Run Code Online (Sandbox Code Playgroud)

仅存储所需的44个字符,并使用Ruby的音译功能来映射这些字符

<space>
<-> to <9>
<*>
<$>
<+>
<%>
<A> to <Z>
Run Code Online (Sandbox Code Playgroud)

到编码值.


xan*_*xan 7

Python,304个字符

没有花哨的Unicode压缩.唯一的技巧是重新排序字符以最大化重叠.我的第一个Python程序.

b="BWbwbwBwbWBwbwbwBWbwBwbwbWBwbwBwbWbwBwbwBWbwbwBWBwbwbwbWBwBwbwbWbwBwBwbWbwbwBwBWbwbwbwBWBwbWbWbWbwbWbWbWb"
s=t=""
for x in"*"+raw_input()+"*":
 i=".NI5VRD9YLH4 OB8XSE2?J6WKG0ZMA7*PC1-TF3UQ????$/+%".find(x)*2
 s+=b[i:i+9]+"w"
for x in s:t+=["#"," ","###","   "]["bwBW".find(x)]
for k in b[-8:]:print(t)
Run Code Online (Sandbox Code Playgroud)

  • 你的第一个Python程序是代码高尔夫.你在开玩笑吧 (12认同)

Ski*_*izz 6

汇编

组装为220个字节.

    mov di,ds
    mov al,42
    call a3
    mov dh,[80h]
    mov si,82h
 a1:lodsb
    call a3
    dec dh
    jnz a1
    mov al,42
    call a3
    mov ax,2573
    stosw
    mov al,36
    stosb
    mov cl,8
 a2:mov dx,ds
    mov ah,9
    int 21h
    loop a2
 a3:sub al,97
    cmp al,26
    ja a4
    sub al,32
 a4:mov bx,a6-3
 a8:add bx,3
    cmp bx,a7
    jae ret
    cmp al,[bx]
    jne a8
    mov bp,[bx+1]
 a5:rcr bp,1
    mov al,36
    sbb al,0
    and al,35
    stosb
    or bp,bp
    jnz a5
    mov al,32
    stosb
    ret
 a6:dd 0D05DC5CFh,01DD17517h,05477D275h,0D475C5D3h,01DD55717h,07745D657h,0D85D17D7h,057E05D1Dh
    dd 0745DE174h,0E35177E2h,0D7E47475h,051DDE551h,0E77715E6h,05DE85C57h,05C75E95Ch,0EB7157EAh
    dd 077EC715Dh,07175ED45h,0EF45D7EEh,0D5F045DDh,04757F171h,0F3475DF2h,047F44775h,07571F575h
    dd 0F755C7F6h,047F875D1h,05771F957h,0CD7751CCh,071BF5D47h,05111C35Dh,0CA4511CEh,045C44451h
    dd 05DD1C944h
 a7:
Run Code Online (Sandbox Code Playgroud)

在这里做巧妙的技巧的范围不大.


ken*_*ytm 5

Python 3.1,没有Unicode(213 215 223 240 248 249 字符)

o=""
for c in"%r"%input():
 u="W3YZ56C$EF. 89'0HIJM/OP+%RSTUV12X4ABD-7GKLNQ".find(c);n=sum(b"))&&&,(*&2&&&)),&/8(*&1)<&/V&&&)),&/5);D&/S"[u:])-930+35*u
 while n:o+="###"[n%2*2:]+"   "[n&2:];n>>=2
print((o+"\n")*8)
Run Code Online (Sandbox Code Playgroud)

说明:

代码39序列被编码为base-4数字(最左边=最不重要),其中:

  • bw →3
  • Bw →2
  • bW →1
  • BW →0

然后对序列进行分类,例如

20333   Q
21233   N
21323   L
...
Run Code Online (Sandbox Code Playgroud)

获取相邻条目的差异,给出类似的列表[48, 12, 3, …].然后35添加到此列表中以确保数字落在ASCII范围内.这给了"))&&&,…"字符串.

此代码也利用*了输入中不会出现的内容,因此我们可能会将其替换为任何无效字符,包括'.在CPython中repr("ABC") == "'ABC'",我们可以摆脱2个字符.