将;#转换成您的语言


25

相关于:生成;#解释器生成;#代码

;# -旋风指南

这是一种简单的语言,带有两个命令。它唯一的数据结构是一个累加器,它被初始化为0。

  1. ; 累加器

  2. #计算127模的累加器值,并打印相应的ASCII字符。然后,将累加器重置为0。

源代码可能包含其他字符(可打印的ASCII +空格),但是这些字符被视为注释,对程序执行没有影响。

挑战

由于大多数计算机未;#预装,因此拥有一个可以将;#代码转换为其他语言的工具将非常有用。在这个挑战中,您将编写一个程序来完成此任务。

输入项

一些;#通过参数或STDIN获取的源代码。此源代码可能包含(注释);或以外的其他字符#

输出量

代码与您提交的语言相同,该;#代码在执行时将打印/返回与原始代码相同的字符串。如果对您的语言更方便,此结果代码可能会在目标字符串之后输出尾随换行符。

笔记

需要注意的一件事是转义序列,例如打印反斜杠或打印引号的代码。另外,请;#注意可能包含与您的语言中的关键字或命令类似的内容的代码。

其他限制

所有程序都必须终止(我通常将其视为默认值,但有人对此询问,因此在此说明)。

例子

input: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#
output (python): print(";#")
input: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#
output (element): \'`

34
我的电脑附带;#预装...
programmer5000

1
另一个?为什么?
caird coinheringaahing

程序必须停止执行吗?另外,它可以在实际代码之后无限次打印无操作吗?
完全人类

2
@totallyhuman我要说所有程序最终都必须停止。
PhiNotPi

1
请让我们得到一个测试用例,其中输入包含不是;或不是的一个或多个字符#
streetster

Answers:


7

Python 2中76 69个字节

输入用引号引起来。

for y in input('print').split("#")[:-1]:print`chr(y.count(";")%127)`,

在线尝试!

说明

输出的第一部分基本上由输入使用来完成input('print')。我们将输入拆分为标签,并丢弃最后一个元素。我们打印ord(y%127)的表示形式,其中y是分号的出现次数。我们,在打印末尾附加,以确保它不会打印换行符。

这将为Hello, World!-program 提供以下Python代码:

print'H' 'e' 'l' 'l' 'o' ',' ' ' 'W' 'o' 'r' 'l' 'd' '!'

可以在线尝试


5

脑干,126字节

+++[->+++++<]>[->++++>+++<<],[+<++++++[-<++++>>------<]>[<]<<[>>>>>[--<--.++>]<+.-<.<<<<]>[->>-<<]>>[[-]<]>[>>++++[-->]<[<]],]

在线尝试!

如果;#输出超过65536个字符,则输出程序将在TIO实现中失败。我还制作了一个130字节的版本,该版本输出[+]而不是<,从而避免了此问题:

++[------>++>+<<]>+++++<,[+<++++++[-<++++>>------<]>[<]<<[>>>>>[--<.>]<+++.---<.>.<++.--<<<<]>[->>-<<]>>[[-]<]>[>>++++[-->]<[<]],]

说明

+++[->+++++<]>[->++++>+++<<]        initialize tape with 60 and 45
,[                                  for each input byte:
  +<++++++[-<++++>>------<]         subtract 35 (#) from byte
  >[<]<<[                           if byte was #
         >>>>>[--<--.++>]           output + (43) a number of times equal to accumulator
         <+.-<.<<<<                 output . (46) and < (60)
  ]
  >[->>-<<]>>                       subtract 24 from what's left of byte
  [[-]<]                            check difference and clear if nonzero
  >[                                if byte was originally 59 (;)
   >>++++[-->]<[<]                  add 4 to accumulator cell, then subtract 2 if nonzero. Since BF cells are mod 256, this creates an accumulator mod 127.
  ]
,]

5

空格,291个字节

NSSTTNNSTNNSTNNSTTNNSSTSNSSNSNSTNTSTTTSSSTSSSTTNTSSTSNSNTSSSSNSSSTTSSSNTSSTNTSSSTNNSNTSNNSSSSSNSNNNSTSSNNSTSTNNSTSTNNSTSNNSTTNNSTSNNSTNNSTSTNNSTTNNSTNNSTNNSNTTNNSSSSTNNSTSSNNSTSNNSTTNNSTSNNSTSSNNSNTSNNSSTNSSSTSTSNTNSSNTNNSSSSNNSTNNSTNNSSNSSSTSSSSSNTNSSNTNNSSSTNNSTSNNSTSNNSSSNSSSTSSTNTNSSNTN

用空格替换S,用制表符替换T,用换行符替换N。

在空白中生成空白并不是世界上最有效的事情。生成任何类型的动态代码都需要进行大量的位纠缠,以一种没有位操作的语言,这将导致代码大小爆炸。因此,该程序不会尝试做一些聪明的事情,而是选择仅将源程序一对一地翻译。它分解为以下内容:

early:
    call S
    call S
    call N
start:
    push  0
    dup
    ichr
    get
    push  35
    sub
    dup
    jz    hash
    push  24
    sub
    jz    semi
    jmp   start
hash:
    pop
    call SSS
    call TTT
    call TTT
    call T
    call N
    call T
    call S
    call TTT
    call N
    call S
    call S
    jmp   early
semi:
    call SSS
    call T
    call N
    call T
    call SSS
    jmp   start
N:
    push 10
    pchr
    ret
SSS:
    call S
    call S
S:
    push 32
    pchr
    ret
TTT:
    call T
    call T
T:
    push 9
    pchr
    ret

该程序生成的代码如下所示:

push  0
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  127
mod
pchr
push  0
push  1
add
...

对我不起作用。在用Haskell编写的原始空格解释器中,可能不会从数字中省略符号位,因此“ SSN”不是推零的有效方法。
aschepler '17

由于空白规范的含糊不清以及原始参考解释程序和实际规范之间的差异,因此很难判断预期的行为。据我所记得,原始站点上列出的几个示例程序实际上确实需要无意义的行为,此外,许多其他实现确实具有这种行为。在构建自己的ws JIT编译器时,我多次遇到这些问题,最后我决定坚持使用它以实现与其他实现的兼容性
CensoredUsername

4

V19 20 28字节

错误修正,如果没有#结尾,则破解

错误修正,已实施 mod 127

Í;û127}
éiA0Í#/0
ò/;
x

在线尝试!

尝试生成的代码

说明:

Í;û127}                            ' Delete runs of 127 `;`s (mod 127)
éi                                 ' Insert an `i` in front of input
  A<C-v><esc>0<esc>                ' Append <esc>0 to input
                   Í#/<C-v><C-v>0  ' Replace all `#` with `<C-v>0`
ò                                  ' Recursively
 /;                                ' Go to the next `;`
<C-a>                              ' Increment the next number come across

在V中,在插入模式下,可以使用来通过代码插入任何ASCII字符<C-v><Code>。V代码将全部替换#<C-v>0,其中零是伪累加器per #。每次#将累加器重置为0,因此每个累加器都可以工作一次。然后,代码对找到的每个分号进行递增,这只会增加找到的下一个数字,即下一个累加器。将0追加到末尾,以使指令不会在;没有跟随的情况下失败#

十六进制转储:

00000000: e969 4116 1b30 1bcd 232f 1616 300a f22f  .iA..0..#/..0../
00000010: 3b0a 7801                                ;.x.

3

05AB1E20 19 18 16字节

-1感谢Adnan
-2感谢carusocomputing
-2感谢Kevin Cruijssen

'#¡¨vy';¢ƵQ%„çJJ

在线尝试!(包括执行的05AB1E代码的输出)

'#¡              # Split on #
   ¨             # remove the last element
    vy           # For each...
      ';¢        #   count the number of ;s
         ƵQ%     #   Mod by 127
            „çJ  #   Push çJ („çJ should work, but it doesn't for some reason)
               J # Join stack and output implicitly

';¢可以gžy按128,可能以某种方式起作用,为什么不拼写完整的单词并用引号将其括起来呢?
魔术章鱼缸

1
我需要使用,';¢以防其他字符;žy<与相同127。如果其中一个字符是引号,则用引号引起来的单词的打印将会中断。
莱利

@carusocomputing我忘了给你ping……
赖利

1
@carusocomputing和Riley:ƵQ127的压缩版本。
阿德南

@Adnan为什么/如何?
Magic Octopus Urn

2

Python 2,75个字节

lambda s:"print"+`''.join(chr(x.count(';')%127)for x in s.split('#')[:-1])`

在线尝试!(包括执行已编译的Python代码的输出)

感谢ovs提供了很多很多字节!

说明

该程序将转换#;通过分割#s(s.split('#')[:-1]),对每个块模块127中的分号进行计数(x.count(';')%127for x in ...),并将其转换为相应的ASCII字符(chr(...)),从而对代码进行编码。然后''.join(...)将该列表连接(),转换为该字符串的Python表示形式(反引号),然后插入到框架Python程序中以打印字符串("print"+...)。


2

果冻 25 24  16 字节

ṣ”#Ṗċ€”;%127;€”Ọ

完整的程序会打印等效的Jelly代码(作为单子链接,它将返回混合类型列表的列表)。

第一个示例位于“ 在线试用”!这产生了这个程序

怎么样?

;s之间#的每次运行中对s进行计数,并在每个127之后取模127,并在每个指令之后附加强制转换指令,即单原子。

当果冻通过类似程序(即将72Ọ101Ọ108Ọ108Ọ111Ọ44Ọ32Ọ119Ọ111Ọ114Ọ108Ọ100Ọ33Ọ要打印)运行时,果冻将每个值隐式地传递给STDOUT Hello, world!

ṣ”#Ṗċ€”;%127;€”Ọ - Main link: list of characters
 ”#              - literal '#'
ṣ                - split the result at #s
   Ṗ             - pop (remove the last result, list of trailing non-# characters)
      ”;         - literal ';'
    ċ€           - count for €ach
        %127     - modulo 127 (vectorises)
              ”Ọ - literal 'Ọ' (Jelly's cast to ordinal monadic atom)
            ;€   - concatenate for €ach - making a list of lists like [[72,'Ọ],[101,'Ọ'],...]
                 - implicit print (this smashes, printing something like: 72Ọ101Ọ...)

关于输入的注释:Jelly采用Python格式的字符串输入。空计划可输入作为"",唯一的哈希方案为"#""##",可能需要包含输入反斜线和报价等其他操作。


2

立方体138个 137字节

+1/1+54@6:5+1/1+5@6/1+52@6:4/1+5@6:1/1+54@6:4/1+5@6:5+2/1+4@6(~-61/1=7&6+5/1+51=7?6{+3/1+5@6}:5+3/1+3=7?6{+52@6:5+1@6-1@6+1@6:5+2/1+4@6})

在线尝试!

注意:您可能需要更换&6?6&为它的TIO工作。&6是在语言规范中。

怎么运行的

+1/1+54@6        Output 'R'
:5+1/1+5@6       Output '3'
/1+52@6          Output 'D'
:4/1+5@6         Output '1'
:1/1+54@6        Output 'R'
:4/1+5@6         Output '1'
:5+2/1+4@6       Output '+'
(                Loop indefinitely
  ~                Get next character
  -61/1=7&6        If input is -1 (EOF), exit program
  +5/1+51=7?6{     If input is `;`
    +3/1+5@6         Output '0'
  }                End if
  :5+3/1+3=7?6{   If input is '#'
    +52@6            Output '@'
    :5+1@6           Output '6'
    -1@6             Output '-'
    +1@6             Output '6'
    :5+2/1+4@6       Output '+'
  }                End if
)                End loop

输出程序:

R3D1R1           Set top face to 1
+00...           Add top face to notepad, aka increment notepad
@6               Output notepad as character
-6               Set notepad to 0
...

节省了大量从取出的参数字节@6%6-6。以前在隐式调用时没有执行任何操作的命令现在使用记事本。所以@是一样的@6%是一样的%6,等等
MD XF

1

JavaScript(ES6),101个字节

s=>`_=>'${s.replace(/[^#;]/g,``)}'.replace(/;*(#|$)/g,(l,c)=>c&&String.fromCharCode(~-l.length%127))`

给定输入字符串,删除所有不必要的字符,然后返回以下函数的源:

_=>'...'.replace(/;*(#|$)/g,(l,c)=>c&&String.fromCharCode(~-l.length%127))

此处...代表已清理的;#源。


1

C,98 96 99 98 97字节

+3个字节,因为我忘记了C不会被解释:(

c,a;f(){printf("f(){puts(\"");for(;~c;c=getchar())c==59?a++:c==35?a=!printf(&a):a;puts("\");}");}

运行:

echo ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#" | ./a.out

将打印:

f(){puts("Hello, World!");}

2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#产生f(){puts(""");},这是无效的。挑战特别指出“需要注意的一件事是转义序列,例如打印反斜杠或打印引号的代码。”
hvd

@hvd Fixing ....
MD XF

1

果冻,26个字节

”‘x
f”;L%127Ç;“Ọø”
ṣ”#Ç€ṙ-

在线尝试!

并在此处尝试该果冻代码!

ṣ”#Ç€ṙ-      Main link, input is a string of ";;;;;;#lala;;;;;;;;;#"
ṣ”#          Split input on char #
   ǀ        Call helper link 1 for each segment
     ṙ-      Rotate returns 1 to the right (SPLIT introduces an empty element which is moved up front)

f”;L%127Ç;“Ọø”  Helper link 1, determines the number of increments
f”;             Throw out all but semicolons
   L%127        Take the count mod 127
        Ç       Call helper 2
         ;“Ọø”  Add a Jelly link that prints characters and splits print statements

”‘x             Helper 2, receives the count of ;'s
”‘              Return the character ‘ (Jelly's increment command
  x             Once for each ; in the input

Jelly输出变为类似于的代码Ọø‘‘‘‘‘‘‘‘‘‘‘‘‘Ọø‘‘‘‘‘‘‘‘‘‘Ọø,该代码显示chr(13)+ chr(10)


使用的奇怪示例(仅打印空白)使我感到困惑。
乔纳森·艾伦

1
@JonathanAllan添加了带有TIO链接的示例。
steenbergh

1

PHP,72字节

for(;~$c=$argn[$i++];)echo[Z=>'$n++;',B=>'echo~$n=~chr($n%127);'][a^$c];

1

> <>,106 81 77字节

这是我第一次>>>(鱼)打高尔夫球!我不得不说一种非常有趣的语言。多好玩!

0n01.
>i:1+?!v:";"=?v"#"=?v
^ ;o";"<   .37<
^oo"1+"
^oooooooo'"~"1+%o0' <

欢迎来到池塘!您可以缩短i:1+?!i:0(?,如果将结果构造在堆栈上并等待输入的末尾将其输出,则我还可以节省一些字节。我的意思是,这是很多o;)
亚伦(Aaron

1

C#169字节

打高尔夫球:

class P{static int Main(string[] args){var r="Console.Write(";foreach(var y in args[0].Split('#')){r+=(char)(-1+y.Split(';').Length%127);}System.Console.Write(r+");");}}

可读版本:

class P
{
    static int Main(string[] args)
    {
        var r="Console.Write(\"";
        foreach (var y in args[0].Split('#'))
        {
            r +=(char)(-1+y.Split(';').Length% 127);
        }
        System.Console.Write(r+"\");");
    }
}

1

哈斯克尔106102字节

';'!(x:a)=x+1:a;'#'!a=0:a;_!a=a;h s="main=putStr"++(show$""++(toEnum.(`mod`127)<$>init(foldr(!)[0]s)))

在线尝试!

不打高尔夫球

step ';' (x:acc) = x+1:acc
step '#' acc = 0:acc
step _ acc = acc;

semicolonHash s = toEnum . (`mod` 127) <$> init (foldr step [0] s)

toHaskell s = "main = putStr " ++ (show $ "" ++ semicolonHash s)

1

Brachylog,33个字节

{";"∧"+₁"|"#"∧"%₁₂₇g~ạw0"}ˢ;"0"↔c

在线尝试!

如果有人看到了这一点,并想知道它是如何工作的,现在感到有点累而无法解释,请发表评论以提醒我。


1

MathGolf,17个字节

⌡(¶{gÉ'8=£♣(%$''\

在线尝试!

说明

⌡                      decrement twice
 (                     decrement
                       this transforms ";;;#;;#" -> "888 88 "
  ¶                    split string on spaces
   {                   foreach...
    gÉ                 filter using next 3 characters
      '8               push single character "8"
        =              pop(a, b), push(a==b)
                       this filters out all non-";"
         £             length of array/string with pop
          ♣            push 128
           (           decrement
            %          modulo
             $         pop(a), push ord(a) or char(a) (gets character with code)
              ''       push single character "'"
                \      swap top elements

由于可以使用来将任何字符放入堆栈(以及输出)'<char>,因此将输出一系列这样的代码块。


忘记了如何创建聊天。。无论如何,``(丢弃堆栈顶部以外的所有内容)当前都已被窃听。它给出了一个Python FileNotFoundError。
凯文·克鲁伊森

@KevinCruijssen检查自述文件!我在代码页中切换了该字符,以避免使用两个空格字符。新字符是Þ
maxb

是的,@ JoKing确实表示已将其更改为Þ。(不过,我使用的文档仍然声明了旧值。
Kevin Cruijssen

1
@KevinCruijssen哦,该文档需要更新,谢谢您的提醒!我将尝试编写脚本以使它们都保持更新。我建议您使用它,直到我将其安装到位。
maxb

1

MATL32 28字节

35lF4$Yb"@g59=z]xv127\!&D99h

在线尝试!

基于完全不同的方法,strsplit而不是基于自动机类型的程序。

      Yb % strsplit
    4$   %  with 4 inputs, namely:
35       %   Split on '#'
  lF     %   Do not (F) collapse delimiters (l).
         %   And of course the implicit program input.

"@g    ] % Loop over strings, convert each to a normal char array
   59=z  % How many equal to 59 ';'?

xv!127\  % Delete last element, concatenate and flip to horizontal, mod 127
&D       % Convert to MATL representation
  99h    % Append a 'c' to output ASCII.

0

其实 25个位元组

'#@s⌠';@c7╙D@%⌡MdX$"♂cΣ"o

在线尝试!(包括执行已编译的“实际”代码的输出)

说明:

'#@s⌠';@c7╙D@%⌡MdX$"♂cΣ"o
'#@s                       split on "#"
    ⌠';@c7╙D@%⌡M           for each chunk:
     ';@c                    count ";"s
         7╙D@%               mod by 127 (2**7-1)
                dX         discard last value
                  $        stringify
                   "♂cΣ"o  append "♂cΣ":
                    ♂c       convert each value to an ASCII character
                      Σ      concatenate


0

Fourier,32个字节

$(I~S{59}{`^`}S{35}{`%127a0`}&i)

在FourIDE上尝试!

这是一个非常容易的挑战,因为傅立叶基本上是;#的超集。

;# command > Fourier equivalent
; > ^ (Increment the accumulator)
# > %127a0 (Modulo accumulator by 127, output corresponding code point and set accumulator to zero)

0

CJam,14个字节

q'#/);';fe=:c`

说明:

q               e# Read input
 '#/            e# Split on '#'
    );          e# Delete last element
      ';fe=     e# Count occurrences of ';' in each
           :c   e# Convert each to character (code point)
             `  e# Escape

0

APL,31个字节

{⍕'⎕UCS',⌽127|+/¨';'=⍵⊂⍨'#'=⍵}⌽

输出:

      ({⍕'⎕UCS',⌽127|+/¨';'=⍵⊂⍨'#'=⍵}⌽) ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#'
⎕UCS 59 35
      ⍝ evaluate the output
      ⍎({⍕'⎕UCS',⌽127|+/¨';'=⍵⊂⍨'#'=⍵}⌽) ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#'
;#

说明:

  • :反转输入
  • {... }:将其传递给此函数:
    • ⍵⊂⍨'#'=⍵#对字符串中的每个字符进行分区(从一开始,这就是为什么必须首先将其反转)的原因
    • +/¨';'=:计算;每个分区中的
    • 127|:模127
    • :再次反转
    • '⎕UCS',:在字符串前面加上⎕UCSUnicode函数。
    • :字符串表示

您可以删除,因为与STDOUT的实际输出相同。
亚当

0

红宝石,47 + 1 = 48字节

$_=gsub(/.*?#/){"$><<#{$&.count(?;)%127}.chr;"}

的+1个字节-p

在线尝试!

-30个字节,感谢@manatwork


不幸的是,这与“该源代码可能包含(;或)(或)以外的(注释)字符#”部分要求不完美。
manatwork

@manatwork固定,稍后将成为高尔夫球手。
帕维尔

足以更改正则表达式/;+#//.*?#/和代码块s.length-1s.count(?;)。顺便说一句,您的数学也是错误的,因为%它的优先级高于-,所以应该是(s.length-1)%127。而在.gsub的代码块你可以访问捕获组,$&$1,...这样的|s|代码块参数通常是不可行的。字符串插值将字符串化为:{"$><<#{$&.count(?;)%127}.chr;"}在线尝试!
manatwork '17

@manatwork非常感谢!我认为您的评论使我的红宝石知识增加了一倍。
帕维尔

已经很晚了,但是代码挑战本身最近因修改后的答案而受到冲击,无论如何。直接gsub修改$_,这意味着您无需重新分配它。但是,你有问题,如果您在您上次有注释字符#... 看到这里
价值油墨

0

Pyth,25 23 24字节

j\\+"jk["mC%/d\;127Pcw\#

+1个字节,感谢@FryAmTheEggman

试试吧!

处理仅需使用1个字符的字符串即可转义的字符。

样本输出:

jk[\"

jk[\H\e\l\l\o\,\ \W\o\r\l\d\!

使用我的;#解释器


如果#输入中没有内容,则此方法无效,因为它将打印出来0。您可以使用jk代替来解决此问题s
FryAmTheEggman's

0

C,150字节

#include<stdio.h>
c;n=0;main(){puts("#include<stdio.h>\nmain(){");while(~(c=getchar()))c-35?c-59?:n++:(printf("putchar(%d);",n%127)&(n=0));puts("}");}

展开:

#include<stdio.h>
c;
n=0;
main(){
        puts("#include<stdio.h>\nmain(){");
        while( ~(
            c=getchar()
        ))
                c-35?
                        c-59?
                                :
                                n++
                        :
                        ( printf("putchar(%d);",n%127)
                        &(n=0))
                ;
        puts("}");
}

这是一个完整的程序,(应该)终止,忽略注释并产生始终正确的输出代码。我假设EOF = -1

在SystemResque-Cd 4.9.6上测试,使用gcc 4.9.4编译


0

脑力激荡,40字节

,[35-z["[127-s[127+.>]]<0*".]24-z[43.],]

得说,这简直令人惊讶。

,[                                    ,]     Read a byte until end of input
  35-                                          Subtract 35.
     z[                     ]                  If the result is zero,
       "[127-s[127+.>]]<0*".                     print the code for `#`.
                             24-               Subtract 24.
                                z[   ]         If the result is zero,
                                  43.            print the code for `;`

为生成的代码;+,它会增加当前单元格的数量。

生成的代码#包含一个笨拙的手动模运算

[127-s[127+.>]]<0*
[             ]       While current cell is not zero
 127-                   Subtract 127
     s[      ]          If result is negative
       127+.              Add 127 again and print current cell
            >             Step to next cell to break the loop
               <0*      Step back to first cell and clear it

0

Braingolf,55个字节

V#0VR&,l1-MvMR[R.#;e"+1"MM|.##e"@%+1~#"MMMMMM|$_vv]Rv&@

在线尝试!

基本上取代了;1+##~1+%@和预暂时搁置整个事情有0,因为一元+运营商现在被打破。

1+ 将1添加到堆栈中的最后一项。

#~1+%@ 推的char值 ~(126),将1加到127,与堆栈上的其他项目模量,然后弹出并打印为char。

说明

V#0VR&,l1-MvMR                         Implicit input of each char to stack
[R.#;e"+1"MM|.##e"@%+1~#"MMMMMM|$_vv]
Rv&@

V#0                                    Create stack2, push char value of 0
   VR                                  Create stack3, return to stack1
     &,                                Reverse stack
       l1-                             Push length of stack - 1
          MvMR                         Move length to stack2, switch to stack 2
                                       Move length to stack3, switch back to stack1
[R................................vv]  Do-While loop, will run l times
                                       where l is length of input
  .#;                                  Duplicate last item and push char value of semicolon
     e                                 Pop last 2 items, if they are equal..
      "+1"                             ..Push char values of +1 in that order
          MM                           ..Move last 2 items to stack2
            |                          endif
             .##                       Duplicate last item and push char value of hash
                e                      Pop last 2 items, if they are equal..
                 "@%+1~#"              ..Push char values of @%+1~# in that order
                         MMMMMM        ..Move last 6 chars to stack2
                               |       endif
                                $_     Silently pop last item
Rv                                     Switch to stack2
  &@                                   Greedy char print, print every item in stack as char

0

q / kdb +,42个字节

解:

{-1_"c"$mod[;127](#:)@/:"#"vs x inter"#;"}

例子:

注意,,用来表示一个列表(相对于原子),就像"\""一个元素的列表一样,而不是一个原子。

q){-1_"c"$mod[;127](#:)@/:"#"vs x inter"#;"}";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#"
";#"
q){-1_"c"$mod[;127](#:)@/:"#"vs x inter"#;"}";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#"
,"\""

说明:

取输入字符串,除去不是a #或a的所有内容;,将其拆分为list,对#每个列表中的元素数进行计数,对结果执行mod 127并转换为字符串:

{-1_"c"$mod[;127]count each"#"vs x inter"#;"} / ungolfed
{                                           } / lambda function
                                 x inter"#;"  / intersection of input and ';#' to clear out any 'comments'
                           "#"vs              / split string on hash
                 count each                   / length of each sub-list
        mod[;127]                             / 127 modulo result (vectorised)
    "c"$                                      / cast back to string
 -1_                                          / drop last character (assuming terminated string)

笔记:

  • 假设输入以a终止#,否则最后一个块将被错误地丢弃-1_
  • 如果保证输入只包含,则可以短10个字节;#

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.