Code Golf:Code 39 条形码

发布于 2024-09-01 02:05:53 字数 3900 浏览 3 评论 0原文

挑战

按字符数绘制最短的代码,以绘制 Code 39 条形码的 ASCII 表示形式。

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

输入

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

输出

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

开始/结束字符 * (bWbwBwBwb) 将表示如下:

                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       ^ ^ ^^ ^ ^ ^ ^^^
                       | | || | | | |||
           narrow bar -+ | || | | | |||
           wide space ---+ || | | | |||
           narrow bar -----+| | | | |||
         narrow space ------+ | | | |||
             wide bar --------+ | | |||
         narrow space ----------+ | |||
             wide bar ------------+ |||
         narrow space --------------+||
           narrow bar ---------------+|
inter-character space ----------------+
  • 开始和结束字符 * 将需要在条形码的开始和结束处输出。
  • 条形码之前或之后不需要包含任何安静空间。
  • 不需要计算校验位。
  • 不需要完整的 ASCII Code39 编码,只需标准的 43 个字符即可。
  • 无需在 ASCII 条形码表示法下方打印任何文本来识别输出内容。
  • 如果需要,可以将字符 # 替换为其他密度更高的字符。使用完整块字符 U+2588,将允许条形码在打印时实际扫描。

测试用例

Input:
ABC
Output:
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 

Input:
1/3
Output:
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 

Input:
- $     (minus space dollar)
Output:
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 

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

The challenge

The shortest code by character count to draw an ASCII representation of a Code 39 bar code.

Wikipedia article about Code 39: http://en.wikipedia.org/wiki/Code_39

Input

The input will be a string of legal characters for Code 39 bar codes. This means 43 characters are valid: 0-9 A-Z (space) and -.$/+%. The * character will not appear in the input as it is used as the start and stop characters.

Output

Each character encoded in Code 39 bar codes have nine elements, five bars and four spaces. Bars will be represented with # characters, and spaces will be represented with the space character. Three of the nine elements will be wide. The narrow elements will be one character wide, and the wide elements will be three characters wide. A inter-character space of a single space should be added between each character pattern. The pattern should be repeated so that the height of the bar code is eight characters high.

The start/stop character * (bWbwBwBwb) would be represented like this:

                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       #   # ### ### # 
                       ^ ^ ^^ ^ ^ ^ ^^^
                       | | || | | | |||
           narrow bar -+ | || | | | |||
           wide space ---+ || | | | |||
           narrow bar -----+| | | | |||
         narrow space ------+ | | | |||
             wide bar --------+ | | |||
         narrow space ----------+ | |||
             wide bar ------------+ |||
         narrow space --------------+||
           narrow bar ---------------+|
inter-character space ----------------+
  • The start and stop character * will need to be output at the start and end of the bar code.
  • No quiet space will need to be included before or after the bar code.
  • No check digit will need to be calculated.
  • Full ASCII Code39 encoding is not required, just the standard 43 characters.
  • No text needs to be printed below the ASCII bar code representation to identify the output contents.
  • The character # can be replaced with another character of higher density if wanted. Using the full block character U+2588, would allow the bar code to actually scan when printed.

Test cases

Input:
ABC
Output:
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 
#   # ### ### # ### # #   # ### # ### #   # ### ### ### #   # # #   # ### ### # 

Input:
1/3
Output:
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 
#   # ### ### # ### #   # # ### #   #   # #   # ### ###   # # # #   # ### ### # 

Input:
- $     (minus space dollar)
Output:
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 
#   # ### ### # #   # # ### ### #   ### # ### # #   #   #   # # #   # ### ### # 

Code count includes input/output (full program).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

倾城月光淡如水﹏ 2024-09-08 02:05:53

J,102 个字符

8#,:' #'{~,0,.~#:(3 u:'䝝啕啕啕䑅儑啕啕啕啕䗝䔑啕䕷煝䑑凝瑗屗眕凗瑵屵具瑝屝啕啕啕啕啕啕啕甗崗睅圗病巅呷甝崝圝畇嵇睑均痑巑嗇畱嵱坱煗䝗燕䗗煵䝵'){~32-~a.i.'*'(,,[)

解释。从下往上读:

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!

J, 102 characters

8#,:' #'{~,0,.~#:(3 u:'䝝啕啕啕䑅儑啕啕啕啕䗝䔑啕䕷煝䑑凝瑗屗眕凗瑵屵具瑝屝啕啕啕啕啕啕啕甗崗睅圗病巅呷甝崝圝畇嵇睑均痑巑嗇畱嵱坱煗䝗燕䗗煵䝵'){~32-~a.i.'*'(,,[)

Explanation. Read from the bottom up.:

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!
乜一 2024-09-08 02:05:53

红宝石 (1.9) - 121 132 141 166 170 289 295

向 David 致敬

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'

仅存储所需的 44 个字符,并使用 Ruby 的音译函数将这些字符映射

<space>
<-> to <9>
<*>
<
gt;
<+>
<%>
<A> to <Z>

到编码值。

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

Hats-off to David

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'

Only stores the 44 characters required and uses Ruby's transliteration function to map these

<space>
<-> to <9>
<*>
<
gt;
<+>
<%>
<A> to <Z>

to the encoded values.

不喜欢何必死缠烂打 2024-09-08 02:05:53

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)

Python, 304 characters

No fancy Unicode compression. Only trick is to reorder the characters to maximize overlap. My first Python program.

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)
夜雨飘雪 2024-09-08 02:05:53

汇编器

汇编为 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:

这里没有太多发挥巧妙技巧的余地。

Assembler

Assembles to 220 bytes.

    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:

Not much scope for doing clever tricks here.

蓝色星空 2024-09-08 02:05:53

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)

) :

代码 39 序列被编码为基数 4 的数字(最左边 = 最不重要):

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

然后对序列进行排序,例如,

20333   Q
21233   N
21323   L
...

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

此代码还利用了 * 不会出现在输入中的优势,因此我们可以将其替换为任何无效字符,包括 '。在 CPython 中 repr("ABC") == "'ABC'",所以我们可以去掉 2 个字符。

Python 3.1, without Unicode (213 215 223 240 248 249 chars)

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)

Explanation:

The code 39 sequence is encoded into a base-4 number (left-most = least significant) with:

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

The sequence is then sorted, e.g.

20333   Q
21233   N
21323   L
...

The difference of adjacent entries are taken, giving a list like [48, 12, 3, …]. Then 35 is added to this list to ensure the numbers fall in the ASCII range. This gives the "))&&&,…" string.

This code also has taken advantaged of the * will not appear in the input, so we may replace it by any invalid character, including '. In CPython repr("ABC") == "'ABC'", so we could get rid of 2 characters.

笙痞 2024-09-08 02:05:53

Python 3.1,带有 Unicode(154 158 字符),

基于 J 解决方案利用“如果需要,可以将字符#替换为另一个密度更高的字符”规则,将密度定义为深色部分的面积除以字形的最小边界矩形。 :)

print((''.join(" #"[int(c)]for d in"%r"%input()for c in bin(2*ord("䝝啕啕啕䑅儑啕䗝啕啕啕䔑啕䕷煝䑑凝瑗屗眕凗瑵屵具瑝屝啕啕啕啕啕啕啕甗崗睅圗病巅呷甝崝圝畇嵇睑均痑巑嗇畱嵱坱煗䝗燕䗗煵䝵"[ord(d)-32]))[2:])+"\n")*8)

Python 3.1, with Unicode (154 158 chars)

Based on the J solution, taking advantage of the "The character # can be replaced with another character of higher density if wanted" rule, by defining density as area of dark parts divided by the smallest bounding rectangle of the glyph. :)

print((''.join(" #"[int(c)]for d in"%r"%input()for c in bin(2*ord("䝝啕啕啕䑅儑啕䗝啕啕啕䔑啕䕷煝䑑凝瑗屗眕凗瑵屵具瑝屝啕啕啕啕啕啕啕甗崗睅圗病巅呷甝崝圝畇嵇睑均痑巑嗇畱嵱坱煗䝗燕䗗煵䝵"[ord(d)-32]))[2:])+"\n")*8)
小梨窩很甜 2024-09-08 02:05:53

Python (2.6) -- 430 312 302 个字符

第三次解决问题,仍有改进的空间。按 wc -m 计算字符数。

#coding:UTF8
k=""
for c in"*%s*"%raw_input():
 i=" $*.02468BDFHJLNPRTVXZ%+-/13579ACEGIKMOQSUWY".find(c)*2
 for j in"%05d%s"%tuple(map(ord,u"ಊҺ௖ூ௄Һ姢ҺЈҺӎϴЈϴӐϲ刦ҺҺ௄ϴ௄Ҽூ划ಊϴಊҺЈϴЈҼІ划ӎϴӎಊϴ௄ϴಌϲІ௖ூ௖ூҼ௖І刦ϴ勮ϲ刨ϲІҼӎҺ划௄勚ூ刔ூϲಌҺಊ划Ј勚І刔ІϲӐҺӎ姢ϴ媪ϲ姤ϲ"[i:i+2])):k+=["#"," ","###","   "][int(j)]
 k+=" "
exec"print k;"*8

Python (2.6) -- 430 312 302 characters

Third go at the problem, still room for improvement. Character count by wc -m.

#coding:UTF8
k=""
for c in"*%s*"%raw_input():
 i=" $*.02468BDFHJLNPRTVXZ%+-/13579ACEGIKMOQSUWY".find(c)*2
 for j in"%05d%s"%tuple(map(ord,u"ಊҺ௖ூ௄Һ姢ҺЈҺӎϴЈϴӐϲ刦ҺҺ௄ϴ௄Ҽூ划ಊϴಊҺЈϴЈҼІ划ӎϴӎಊϴ௄ϴಌϲІ௖ூ௖ூҼ௖І刦ϴ勮ϲ刨ϲІҼӎҺ划௄勚ூ刔ூϲಌҺಊ划Ј勚І刔ІϲӐҺӎ姢ϴ媪ϲ姤ϲ"[i:i+2])):k+=["#"," ","###","   "][int(j)]
 k+=" "
exec"print k;"*8
双马尾 2024-09-08 02:05:53

Lua,318 个字符

z={"1358ACEHKMORUWY.","UVWXYZ-. $/+*","2369BCFILMPSVWZ ","0123456789$/%","0456DEFJNOPTXYZ*","ABCDEFGHIJ$+%","0789GHIJQRST-. *","KLMNOPQRST/+%","1247ABDGKLNQUVX-",""}i="*"..(...).."*"o=""for c in i:gfind(".")do for j=1,10 do o=o..((j%2==0 and" "or"#"):rep(z[j]:find(c,1,true)and 3 or 1))end end for j=1,8 do print(o)end

我不希望用 Lua 赢得任何代码高尔夫问题,所以在这里回答我自己的挑战并不感到难过。它还使用了一种不同的编码,我认为其他人可能会感兴趣。

其他观察

结果仔细查看编码后,似乎有一种方法可以省去查表,直接计算条形码的编码。然而,我发现我尝试构建计算所花费的代码比表更多。 (在其他语言中可能不是这种情况。)

将字符分为指示宽条和空格所在位置的组后,我看到了一些有趣的模式。似乎只有一个宽空格可容纳 40 个字符,$/+% 是例外(它们每个字符都有 3 个空格)。这 40 个字符被分成每个槽 10 个字符。并且,同样的 40 个字符有两个宽条。宽条似乎具有二进制编码,最后一个条是奇偶校验位。该条的位模式为 1、2、3、4、5、6、8、9、10、12,其中避免设置多于 2 位的数字。

1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ-._*
1 3 5  8  A C E  H  K M O  R  U W Y  .   1010100100
 23  6  9  BC  F  I  LM  P  S  VW  Z  _  0110010010
   456   0   DEF   J   NOP   T   XYZ   * 0001110001
      7890      GHIJ      QRST      -._* 0000001111
12 4  7   AB D  G   KL N  Q   UV X  -    1101001000 --> Parity

我想,看看是否可以在没有查找表的情况下对条形码进行编码,这将是另一天的挑战。

Lua, 318 Characters

z={"1358ACEHKMORUWY.","UVWXYZ-. $/+*","2369BCFILMPSVWZ ","0123456789$/%","0456DEFJNOPTXYZ*","ABCDEFGHIJ$+%","0789GHIJQRST-. *","KLMNOPQRST/+%","1247ABDGKLNQUVX-",""}i="*"..(...).."*"o=""for c in i:gfind(".")do for j=1,10 do o=o..((j%2==0 and" "or"#"):rep(z[j]:find(c,1,true)and 3 or 1))end end for j=1,8 do print(o)end

I don't expect to win any code-golf questions with Lua, so I don't feel bad answering my own challenge here. It was also using a different encoding I thought might be interesting to others.

Other observations

After looking closely at the encoding, it looks as if there may be a way to dispense with table look-up, and compute the bar code's encoding directly. However, I found that my attempts to build the computations took more code than the table. (This may not be the case in other languages.)

Having divided the characters into groups that indicated where the wide bars and spaces were, I saw some interesting patterns. It seems that there is only one wide space for 40 of the characters, with $/+% being exceptions (they each have three spaces.) The 40 characters being split with 10 in each slot. And, there are two wide bars for the same 40 characters. The wide bars seem to have a binary encoding with the last bar being a parity bit. The bar's bit patterns being 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 where the numbers having more then 2 bits set being avoided.

1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ-._*
1 3 5  8  A C E  H  K M O  R  U W Y  .   1010100100
 23  6  9  BC  F  I  LM  P  S  VW  Z  _  0110010010
   456   0   DEF   J   NOP   T   XYZ   * 0001110001
      7890      GHIJ      QRST      -._* 0000001111
12 4  7   AB D  G   KL N  Q   UV X  -    1101001000 --> Parity

I guess it is a challenge for another day to see if the bar codes could be encoded without look-up tables.

半衬遮猫 2024-09-08 02:05:53

我已经编写了在嵌入式系统上使用代码 39 条形码的代码。有两种类型的代码,我认为是 39 个“常规”代码和 4 个“奇怪”代码。常规代码具有十种条宽模式之一(二宽三窄)和四种空白宽度模式之一(一宽三窄)。奇怪的东西有五个窄条和四种条宽模式之一(三宽一窄)。要使用紧凑代码呈现条形码,请将字符转换为数字 0-39(对于普通代码),或者对于“oddballs”,将字符转换为 40、50、60 或 70。然后divmod 10。高位数字将选择八种空间宽度模式之一;如果上位数字为 3 或更少,则下位数字将选择十种条形宽度模式之一。如果上面的数字是 4-7,则所有五个条形都应该是窄的。

该代码使用一些小的查找表非常紧凑地工作。因为查找表可以在源代码中紧凑地表示为字符串,所以具有较大查找表的方法可能具有较短的源代码(尽管我认为对于 Code Golf 源代码大小应该使用最有利的编码以字节为单位计算;使用字符串的方法在 UTF-8 中需要三个字节存储,在 UTF-16 中需要两个字节存储的奇怪字符应该为每个奇怪字符“充电”3 个字节,或者为所有字符每个“充电”两个字节,以产生较小的总数为准)。使用完全适合某些特定单字节代码页的一系列奇怪字符的方法应按每个字符一个字节收费。

I've done code to work with code 39 barcodes on embedded systems. There are two styles of codes, which I think of as the 39 "regular" codes and the four "oddballs". The regular codes have one of ten patterns of bar widths (two wide three narrow), and one of four patterns of space widths (one wide three narrow). The oddballs have five narrow bars and one of four patterns of bar widths (three wide one narrow). To render a barcode with compact code, convert the character into a number 0-39 (for normal codes), or 40, 50, 60, or 70 for "oddballs". Then divmod 10. The upper digit will select one of eight patterns of space widths; if the upper digit is three or less the lower digit will select one of ten patterns of bar widths. If the upper digit is 4-7, all five bars should be narrow.

The code works out very compactly using a few small lookup tables. Because lookup tables may be compactly represented as strings in source code, approaches with larger lookup tables may have shorter source code (though I think for Code Golf source code size should be counted in bytes using the most favorable coding; an approach with a string of oddball characters that would take three bytes to store in UTF-8 and two bytes for UTF-16 should either be 'charged' 3 bytes each for oddball characters, or two bytes each for all characters, whichever yields a smaller total). An approach which uses a selection of oddball characters that fit entirely within some particular single-byte code page should be charged one byte per character.

回首观望 2024-09-08 02:05:53

垃圾邮件 垃圾邮件 垃圾邮件 垃圾邮件 可爱的垃圾邮件

spam spam spam spam lovely spam

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文