请数我!


24

您的任务很简单。以任何语言发布一个代码段,如果该代码段重复n次,将以十进制,八进制和十六进制的顺序输出n,并以空格分隔。n是大于零的整数。没有前导零。最短答案胜出

如果片段是ABC测试用例

ABC 
1 1 1
ABCABC
2 2 2
ABCABCABCABCABCABCABCABCABC
9 11 9
ABCABCABCABCABCABCABCABCABCABCABCABC
12 14 C
ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC
18 22 12

4
我可以打印1 01 0x1吗?(包括前缀)
蓝色

如果您使用的语言具有隐式输入/输出,那么您可以使用一个1字节的解决方案,该解决方案只会增加值...
Esolanging Fruit 2016年

Answers:


11

Japt,12个字节

[°TTs8 TsG]¸

感谢@ETHproductions节省了2个字节!

与我的𝔼𝕊𝕄𝕚𝕟答案相同。


7
:O你击败了丹尼斯!
下乡

图𝔼𝕊𝕄𝕚𝕟无法做到,您已经完成了Teascript,并且我不认识Jolf,所以我使用了Japt。
Mama Fun Roll'1

[°TTs8 TsG]¸
太棒了

哦,没看到。谢谢!
Mama Fun Roll'1

14

Perl,30个字节

printf"\r%d %o %x",++$n,$n,$n;

返回到该行的开头,增加计数器并打印计数器覆盖旧的输出。


+1表示规格中的漏洞,输出擦除使这项挑战变得微不足道。
Akangka '16

1
@ChristianIrwan:实际上,这不是擦除,而是覆盖(我已纠正了我的描述)
nimi

1
两者都破坏了挑战。
Akangka '16

12

JavaScript,54 53 51 47字节

由于@ user81655,节省了4个字节

var d=-~d;d+` ${d[b='toString'](8)} `+d[b](16);

实际上,我对此工作感到有些惊讶。

说明

var d=-~d;  // `var` let's `d` not throw an error if it's not defined 
            // -~ essentially increments the variable
d+                    // decimal
` ${                  // space character
   d[b='toString'](8) // octal
} `                   // space character
+d[b](16)             // Hexadecimal

在线尝试


Iirc,您可以删除var
Conor O'Brien

@CᴏɴᴏʀO'Bʀɪᴇɴ会导致错误:ReferenceError: Can't find variable: d,即使在宽松模式D下也是如此:
Downgoat

d=d?d+1:1工作吗?
科纳·奥布莱恩

@CᴏɴᴏʀO'Bʀɪᴇɴ不,仍然引发参考错误,考虑启用宽松模式很奇怪……
Downgoat

哦,因为我们正在尝试访问d(尽管未定义)
Conor O'Brien

7

C ++,205个 179字节

int main(){};static int c=1;
#define v(x) A##x
#define u(x) v(x)
#define z u(__LINE__)
#include <cstdio>
class z{public:z(){++c;};~z(){if(c){printf("%d %o %x",--c,c,c);c=0;}}}z;//

(无尾随换行符-复制时,副本的第一行和原始行的最后一行应重合)

基本上,这是通过创建一系列静态变量来实现的,这些静态变量在构造时会增加全局变量计数器。然后,销毁时,如果计数器不为0,它将执行所有输出并将计数器设置为零。

为了定义没有名称冲突的变量序列,我们使用解释如下的宏:

#define v(x) A##x    //This concatenates the string "A" with the input x.
#define u(x) v(x)    //This slows down the preprocessor so it expands __LINE__ rather than yielding A__LINE__ as v(__LINE__) would do.
#define z u(__LINE__)//Gives a name which is unique to each line.

这在某种程度上取决于字符串处理器的怪癖。我们用z很多次来定义在复制到单独的行时不会相互冲突的类/变量。而且,必须只出现一次的定义放在第一行,在代码的副本中将其注释掉。该#define#include语句不关心他们得到重复,所以不需要特殊处理。

此代码在语句中还具有未定义的行为:

printf("%d %o %x",--c,c,c)

因为没有序列点,但是c被修改和访问。LLVM 6.0会给出警告,但会根据需要--c进行编译- 先评估c。人们可以在两个字节的费用,添加语句--c;输出之前,改变--cprintfc,这将摆脱警告。


更换std::coutprintf26分字节的感谢保存到我哥哥的建议。


6

CJam,20 19 18字节

];U):USU8bSU"%X"e%

感谢@MartinBüttner打高尔夫球1个字节!

在线尝试!

怎么运行的

]                  e# Wrap the entire stack in an array.
 ;                 e# Discard the array.
  U                e# Push U (initially 0).
   ):U             e# Increment and save in U.
      S            e# Push a space.
       U8b         e# Convert U to base 8 (array of integers).
          S        e# Push a space.
           U"%X"e% e# Convert U to hexadecimal (string).

4

𝔼𝕊𝕄𝕚𝕟,14个字符/ 28个字节

[⧺Ḁ,Ḁß8,Ḁⓧ]ø⬭;

Try it here (Firefox only).

第一个答案!尽管可能有更好的方法可以解决此问题。

说明

[⧺Ḁ,Ḁß8,Ḁⓧ]ø⬭; // implicit: Ḁ = 0
[⧺Ḁ,             // increment Ḁ by 1
    Ḁß8,         // octal representation of Ḁ
        Ḁⓧ]     // hex representation of Ḁ
            ø⬭; // join above array with spaces
                 // repeat as desired until implicit output

7
这是什么语言?
科尔·约翰逊


3

MATL,26字节

使用当前版本(6.0.0)。在八度上工作。

0$N1+ttYUb8YAb16YA3$XhZc1$

例子

一旦:

>> matl 0$N1+ttYUb8YAb16YA3$XhZc1$
1 1 1

两次:

>> matl 0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$
2 2 2

16次:

>> matl 0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$0$N1+ttYUb8YAb16YA3$XhZc1$
16 20 10

说明

堆栈中的元素数量用于指示我们运行了代码段的次数

0$         % specify zero inputs for next function, in case this is not the first
           % occurence of the snippet.
N          % number of elements in stack
1+         % add one
tt         % duplicate twice. We now have three copies of the number
YU         % convert to string (decimal)
b8YA       % bubble up number and convert to octal string
b16YA      % bubble up number and convert to hex string
3$XhZc     % join top three elements (strings) with a space
1$         % specify one input for next function. If the program ends here, that next
           % function will be implicit display, so it will print the top of the stack.
           % Else the stack will be left with one element more than at the beginning of
           % the current snippet

2

OCaml,198个字节

;;open Char
;;(if Sys.argv.(0).[0]='~'then Sys.argv.(0).[0]<-'\000'else Sys.argv.(0).[0]<-chr(1+int_of_char Sys.argv.(0).[0]));let n=1+int_of_char Sys.argv.(0).[0]in Printf.printf"\r%d %o %x"n n n

包含尾随换行符,并要求文件名以波浪号开头(我曾经用过~.ml;可以使用来运行它ocaml \~.ml),因为它是价值最高的标准可打印ASCII字符。滥用以下事实:字符串中的所有字符都是可变的,并且Sys.argv.(0).[0]是文件名中的第一个字符。

它仅适用于n = 1到126,因为的ASCII码~是126,我在输出中加了一个。如果我们只希望n = 1到125,可以将其缩短两个字节。重复126次后,它将循环回到n = 1。

这是我第一次参加高尔夫球比赛,因此任何意见或改进都将不胜感激。

非高尔夫版本:

;; open Char
;; if Sys.argv.(0).[0] = '~' 
   then Sys.argv.(0).[0] <- '\000'
   else Sys.argv.(0).[0] <- chr (1 + int_of_char Sys.argv.(0).[0])
;; let n = 1 + int_of_char Sys.argv.(0).[0] in
   Printf.printf "\r%d %o %x" n n n

+1我的问题中有很多漏洞使我选择不赞成自己的问题。(尽管我不能这样做。)
Akangka '16

我对重复表示怀疑Sys.argv.(0).[0]。我对OCaml不太了解。
Akangka '16

2

TeaScript21 20字节

[┼d,dT8),dT16)]j(p);

我应该将其自动关闭 ;

在线尝试

说明

变成 ++

    // Implicit: d = 0
[   // Start array
 ++d,  // Increment d, decimal value
dT8),  // d to base 8
dT16)  // d to base 16
]j(p); // Join by spaces
    // Implicit: Output *last* expression

下注?这个答案有问题吗?它是否与ASCII字符混杂有关,因为它在几分钟之内也被
投票否定了

1

Perl,40个字节

$_=<<'';printf"%d %o %x",(1+y/z//)x3;
:

冒号后面有最后一条换行符。

将第一行之后的所有内容均视为此处文档,并将其中的内容计数z。对于代码的每个其他副本,z都会添加一个。我们必须增加1计数,因为第一个代码段(已执行的代码)没有任何内容。

如果允许向stderr附加输出,我们可以省略2个单引号,''并且可以减少到38个字节。如果没有''perl,则会发出有关不推荐使用的功能的警告。


1

Mathematica,76个字节

请注意,n之前不应有任何定义。

0;If[ValueQ@n,++n,n=1];StringJoin@Riffle[IntegerString[n,#]&/@{10,8,16}," "]

在这里,使用的行为;。上面的代码段是单个代码段CompoundExpression,但是,当将两个代码段放在一起时,仍然存在CompoundExpression如下所示的代码段。(进行了一些不必要的重排。)

0;
If[ValueQ@n,++n,n=1]; StringJoin@Riffle[IntegerString[n,#]&/@{10,8,16}," "] 0;
If[ValueQ@n,++n,n=1]; StringJoin@Riffle[IntegerString[n,#]&/@{10,8,16}," "] 0;
If[ValueQ@n,++n,n=1]; StringJoin@Riffle[IntegerString[n,#]&/@{10,8,16}," "]

(* 3 3 3 *)

因此,如果写明确的话,就不能使这样的代码片段起作用CompoundExpression。此外,几乎您喜欢的所有内容都可以放在第一个字符之前,;例如EPiMandelbrotSetPlot[]


1

bash,49个字节

档案count.bash

((++n));trap 'printf "%d %o %x\n" $n $n $n' exit;

...没有尾随换行符。

跑:

$ bash count.bash
1 1 1
$ cat count.bash count.bash count.bash | bash
3 3 3
$ for i in $(seq 10) ; do cat count.bash ; done | bash
10 12 a

1

Python 2,54个字节

n=len(open(__file__).read())/54;print n,oct(n),hex(n)#

没有尾随换行符。输出形式为1 01 0x1

如果还不行,则为56个字节

n=len(open(__file__).read())/56;print"%d %o %x"%(n,n,n)#

相互粘贴时,每次粘贴时文件长度将增加1行。基本情况以2行开始,因此您必须从行长中减去1。注释抑制了计算。


"%d %o %x"%(n,n,n),这很酷。我不知道你能做到这一点。如果事实证明留下前缀不行,我将不得不借用该前缀。
rp.beltran '16

1

Python 2.x 140字节

这并不是要竞争过度的解决方案,而是我发现一种有趣的方法,就是尝试进行多线程代码高尔夫。

import thread;n=eval("n+1")if"n"in globals()else 1;
def t(c):99**99;print("%s "*3)%(n,oct(n),hex(n))*(c==n)
thread.start_new_thread(t,(n,));

保留一个计数器,为每个计数生成一个线程,并且如果在完成昂贵的数学问题(而不是保存字节的计时器)后关闭计数器计时器时计数器没有变化,则将打印格式化的字符串。

一些示例配置及其输出:

import thread;n=eval("n+1")if"n"in globals()else 1;
def t(c):99**99;print("%s "*3)%(n,oct(n),hex(n))*(c==n)
thread.start_new_thread(t,(n,));

Outputs 1 01 0x1 

和十五个复制粘贴:

import thread;n=eval("n+1")if"n"in globals()else 1;
def t(c):99**99;print("%s "*3)%(n,oct(n),hex(n))*(c==n)
thread.start_new_thread(t,(n,));import thread;n=eval("n+1")if"n"in globals()else 1;
def t(c):99**99;print("%s "*3)%(n,oct(n),hex(n))*(c==n)
thread.start_new_thread(t,(n,));import thread;n=eval("n+1")if"n"in globals()else 1;

...


Outputs 15 017 0xf 

thread.start_new_threadpython是否可以想到代码打高尔夫球的较差方法名称?
rp.beltran '16

我很想看看它是否可以在python 3.x中工作,我看不到任何我不知道的东西,但是我从未在python 3中完成过线程化
。– rp.beltran,


0

Ruby,35个字节

1;$.+=1;$><<"#$. %1$o %1$x"%$.*-~-0

每个代码段递增$.(如果未读取文件,则从0开始),但只有最后一个输出。*-~-0计算结果为*1,表示一次打印字符串,但将其串联后变成*-~-01一个八进制表达式,计算结果为0。由于$><<不包含尾随换行符,因此打印空字符串意味着不打印任何内容。

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.