24
\$\begingroup\$

Your task is simple. Post a snippet in any language that if the snippet is repeated n times, will output n in decimal, octal, and hexadecimal, in that order, separated in spaces. n is an integer larger than zero. There is no leading zeroes. Shortest answer wins

Example

If the snippet is ABC then the test case is

ABC 
1 1 1
ABCABC
2 2 2
ABCABCABCABCABCABCABCABCABC
9 11 9
ABCABCABCABCABCABCABCABCABCABCABCABC
12 14 C
ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC
18 22 12
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Is it ok if I print 1 01 0x1? (Includes prefixes) \$\endgroup\$
    – Blue
    Commented Jan 2, 2016 at 18:28
  • \$\begingroup\$ If you have a language with implicit input/output, then you could have a 1 byte solution that just incremented the value... \$\endgroup\$ Commented Dec 1, 2016 at 5:00

19 Answers 19

15
\$\begingroup\$

Perl, 30 bytes

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

Go back to the beginning of the line, increment counter and print counter overwriting the old output.

\$\endgroup\$
3
  • \$\begingroup\$ +1 for spotting a hole in specification, Output erasure make this challenge trivial. \$\endgroup\$
    – Xwtek
    Commented Jan 2, 2016 at 5:07
  • 1
    \$\begingroup\$ @ChristianIrwan: actually it's not erasing, but overwriting (I've corrected my description) \$\endgroup\$
    – nimi
    Commented Jan 2, 2016 at 7:29
  • 1
    \$\begingroup\$ That both ruins the challenge. \$\endgroup\$
    – Xwtek
    Commented Jan 2, 2016 at 8:02
12
\$\begingroup\$

JavaScript, 54 53 51 47 bytes

Saved 4 bytes thanks to @user81655

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

I'm actually kinda surprised this works.

Explanation

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

Try it online

\$\endgroup\$
6
  • \$\begingroup\$ Iirc you can remove the var \$\endgroup\$ Commented Jan 2, 2016 at 5:47
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ that causes an error: ReferenceError: Can't find variable: d, even on loose mode D: \$\endgroup\$
    – Downgoat
    Commented Jan 2, 2016 at 5:50
  • \$\begingroup\$ Does d=d?d+1:1 work? \$\endgroup\$ Commented Jan 2, 2016 at 5:52
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ nope, still throws a reference error, weird considering loose mode is enabled... \$\endgroup\$
    – Downgoat
    Commented Jan 2, 2016 at 5:53
  • \$\begingroup\$ Ohhhh because we're trying to access d though it is undefined \$\endgroup\$ Commented Jan 2, 2016 at 5:54
11
\$\begingroup\$

Japt, 12 bytes

[°TTs8 TsG]¸

Thanks to @ETHproductions for saving 2 bytes!

Same as my 𝔼𝕊𝕄𝕚𝕟 answer.

\$\endgroup\$
4
  • 7
    \$\begingroup\$ :O you beat Dennis! \$\endgroup\$
    – Downgoat
    Commented Jan 2, 2016 at 5:00
  • \$\begingroup\$ Figured 𝔼𝕊𝕄𝕚𝕟 couldn't do it, and you had already done Teascript, and I didn't know Jolf, so I used Japt. \$\endgroup\$ Commented Jan 2, 2016 at 5:01
  • \$\begingroup\$ Awesome :) Here's 2 bytes saved: [°TTs8 TsG]¸ \$\endgroup\$ Commented Jan 3, 2016 at 3:47
  • \$\begingroup\$ Oh, didn't see that. Thanks! \$\endgroup\$ Commented Jan 3, 2016 at 4:16
7
\$\begingroup\$

C++, 205 179 bytes

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;//

(No trailing newline - when copied, the first line of the copy and last line of the original should coincide)

Basically, this works by making a sequence of static variables which, on construction, increment a global variable counter. Then, on destruction, if the counter is not 0, it does all its output and sets the counter to zero.

In order to define a sequence of variables with no name conflicts, we use the macro explained as follows:

#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.

which somewhat relies on the quirks of the string processor. We use z many times to define classes/variables that will not conflict with each other when copied onto separate lines. Moreover, the definitions which must occur only once are placed on the first line, which is commented out in copies of the code. The #define and #include statements don't care that they get repeated, so need no special handling.

This code also features undefined behavior in the statement:

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

since there are no sequence points, but c is modified and accessed. LLVM 6.0 gives a warning, but compiles it as desired - that --c evaluates before c. One could, at the expense of two bytes, add the statement --c; before the outputs and change --c in printf to c, which would get rid of the warning.


Replaced std::cout with printf saving 26 bytes thanks to a suggestion of my brother.

\$\endgroup\$
0
6
\$\begingroup\$

CJam, 20 19 18 bytes

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

Thanks to @MartinBüttner for golfing off 1 byte!

Try it online!

How it works

]                  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).
\$\endgroup\$
0
4
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 14 chars / 28 bytes

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

Try it here (Firefox only).

First answer! Although there are probably better ways to handle this.

Explanation

[⧺Ḁ,Ḁß8,Ḁⓧ]ø⬭; // implicit: Ḁ = 0
[⧺Ḁ,             // increment Ḁ by 1
    Ḁß8,         // octal representation of Ḁ
        Ḁⓧ]     // hex representation of Ḁ
            ø⬭; // join above array with spaces
                 // repeat as desired until implicit output
\$\endgroup\$
2
  • 7
    \$\begingroup\$ What even is this language? \$\endgroup\$
    – Cole Tobin
    Commented Jan 2, 2016 at 6:04
  • \$\begingroup\$ A fun one. \$\endgroup\$ Commented Jan 2, 2016 at 16:23
3
\$\begingroup\$

MATL, 26 bytes

Uses current release (6.0.0). Works on Octave.

0$N1+ttYUb8YAb16YA3$XhZc1$

Examples

Once:

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

Twice:

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

16 times:

>> 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

Explanation

The number of elements in the stack is used to indicate how many times we've run the snippet

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
\$\endgroup\$
2
\$\begingroup\$

OCaml, 198 bytes

;;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

Includes a trailing newline and requires that the filename starts with a tilde (I used ~.ml; you can run it with ocaml \~.ml) because it's the highest-valued standard printable ASCII character. Abuses the fact that all characters in a string are mutable and Sys.argv.(0).[0] is the first character in the filename.

It should only work for n = 1 to 126, because the ASCII code for ~ is 126 and I'm adding one to the output. It could be made two bytes shorter if we only want n = 1 to 125. After it's repeated 126 times, it'll cycle back to n = 1.

This is my first ever golf so any comments or improvements would be much appreciated.

Ungolfed version:

;; 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
\$\endgroup\$
2
  • \$\begingroup\$ +1 The ,many holes in my question make me choose to downvote my own question. (I can't do that, though.) \$\endgroup\$
    – Xwtek
    Commented Jan 3, 2016 at 5:05
  • \$\begingroup\$ I'm suspicious about repeated Sys.argv.(0).[0]. I don't know much about OCaml, though. \$\endgroup\$
    – Xwtek
    Commented Jan 3, 2016 at 5:06
2
\$\begingroup\$

TeaScript, 21 20 bytes

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

I should make it auto-close on ;

Try it online

Explanation

becomes ++

    // 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
\$\endgroup\$
1
  • \$\begingroup\$ Downvote? Is there something wrong with this answer? Does it have to do with ASCII Character Jumble as that also got downvoted within minutes of this if not less \$\endgroup\$
    – Downgoat
    Commented Jan 2, 2016 at 20:30
2
\$\begingroup\$

Vyxal, 12 bytes

_&›¥D8τṅ$HWṄ

Try it Online!

Try it Online 10 times!

\$\endgroup\$
1
\$\begingroup\$

Perl, 40 bytes

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

There's a final newline behind the colon.

Treats everything after the first line as a here document and counts the z in it. For every further copy of the code one z is added. We have to add 1 to the count, because there's none for the first snippet (the one that is executed).

If additional output to stderr is allowed, we can omit the 2 single quotes '' and can get down to 38 bytes. Without the '' perl emits a warning about a deprecated feature.

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 76 bytes

Note that n should have no definitions before.

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

Here, the behaviour of ; is used. The snippet above is one single CompoundExpression, however, when a couple of snippets are put together, there is still one CompoundExpression as is shown below. (Some unnecessary rearrangements are made.)

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 *)

So one cannot make such snippet works if writting explicit CompoundExpression. Also, almost everything you like can be put before the first ; such as E, Pi or MandelbrotSetPlot[],.

\$\endgroup\$
1
\$\begingroup\$

bash, 49 bytes

File count.bash:

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

...no trailing newline.

Run:

$ 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
\$\endgroup\$
0
1
\$\begingroup\$

Python 2, 54 bytes

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

No trailing newline. Outputs in the form 1 01 0x1.

If that's not ok, 56 bytes

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

When pasted in front of each other, the length of the file gets longer by 1 line for each time pasted. The base case starts with 2 lines so you have to subtract 1 from the line length. Computation is suppressed by the comment.

\$\endgroup\$
1
  • \$\begingroup\$ "%d %o %x"%(n,n,n), that is pretty cool. I had no idea you could do that. If it turns out that leaving prefixes is not ok I'm going to have to borrow that. \$\endgroup\$
    – rp.beltran
    Commented Jan 3, 2016 at 7:20
1
\$\begingroup\$

Python 2.x 140 bytes

This was not meant to be an overly competitive solution, but a method that I found amusing, being for one thing, an attempt at a multithreaded code golf.

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,));

Keeps a counter, spawns a thread for each count and if the counter has not changed when the counters timer goes off after a completing an expensive math problem (instead of a timer to save bytes), the formatted string is printed.

Some example configurations and their outputs:

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 

and fifteen copy pastes:

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 
\$\endgroup\$
2
  • \$\begingroup\$ thread.start_new_thread Could python have thought of a worse method name for code golfing? \$\endgroup\$
    – rp.beltran
    Commented Jan 3, 2016 at 7:15
  • \$\begingroup\$ I would be interested to see if this works in python 3.x, I don't see anything that I'm aware would not, but I have never done threading in python 3. \$\endgroup\$
    – rp.beltran
    Commented Jan 3, 2016 at 7:28
1
\$\begingroup\$

Perl 5, 31 bytes

exit+printf"%d %o %x",(++$-)x3,

Uses the same approach as my answer for 'I double the source, you double the output!'.

Try it online!

\$\endgroup\$
1
+100
\$\begingroup\$

APL (Dyalog APL), 30 bytes

Requires ⎕IO to be 0.

(+,8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢)⊃1

Attempt This Online!

The main idea is to use an ambivalent function, where the monadic variant would return the result, and the dyadic variant would keep track of the count.

(+,8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢)⊃1
‎⁡
(+,8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢)    # ‎⁢The ambivalent function:
 +                              # ‎⁣  monadic: identity (*); dyadic: left + right
  ,                             # ‎⁤  concatenated with
   8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢     # ‎⁢⁡‎⁣  octal and hexadecimal versions:
       {(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}       # ‎⁢⁢    dyadic function to convert int to base-n string:
        (⎕D,⎕A)                 # ‎⁢⁣      digits concatenated to alphabet
               [       ]        # ‎⁢⁤      indexed by
                      ⍵         # ‎⁣⁡        right argument
                 ⊥⍣¯1           # ‎⁣⁢        in base  (returns digit vector, e.g. 42 in base-16 is [2, 10])
                ⍺               # ‎⁣⁣        left argument
                         ¨      # ‎⁣⁤    applied to:
   8                      ⊢     # ‎⁤⁡      8 and right argument,
     16                   ⊢     # ‎⁤⁢      16 and right argument,
‎⁤⁣
# ‎⁤⁤‎⁤⁢To Summarize:
# ‎⁢⁡⁡‎⁤⁣  monadic version returns: [arg, oct(arg), hex(arg)]
# ‎⁢⁡⁢  dyadic version returns: [left + right, oct(right), hex(right)]
#     since left argument is always 1 when snippet is repeated, we can simplify it to
#     [1 + right, oct(right), hex(right)]
‎⁢⁡⁣
⊃   # ‎⁢⁡⁤pick first (extracts the current count)
    # when snippet is repeated this would be in front of all the dyadic calls
    # but not in front of the final monadic call

 1  # ‎⁢⁢⁡The initial count
💎

Created with the help of Luminespire.

*monadic + is the conjugate, but can be treated as identity for non complex inputs

\$\endgroup\$
1
\$\begingroup\$

Awk, 33 bytes

{printf v" %o %x",v,v}BEGIN{v++}0

Try it online!

This solution abuses the BEGIN statements running before anything happens and the 0 at the end to prevent any other patterns from running.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 35 bytes

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

Each snippet increments $. (which starts as 0 if no files have been read), but only the last outputs anything. *-~-0 evaluates to *1, meaning print the string once, but with concatenation it becomes *-~-01, an octal expression evaluating to 0. Since $><< doesn't include a trailing newline, printing the empty string means printing nothing.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.