21
\$\begingroup\$

Introduction

Every number can be represented as ASCII. For example, \$0\$ comes in ASCII is \$48\$, \$1\$ is \$49\$, and so on. Using this method of translating numbers to other numbers, it is possible to infinitely expand a number, by replacing all its digits with their ASCII values and doing the same for the result. For example, if you started with \$0\$, you would expand to \$48\$, then to \$5256\$, and so on.

Challenge

  • You will be given a single digit and a number as input. You can assume the digit will be in the range \$0-9\$, or \$48-57\$ in ASCII. You can assume the digit will always be of length 1, and will be a string. The number will always be a positive integer, greater than -1. If it is 0, you do not expand at all. Other than that, there are no guarantees about its value. If, and only if your language has no method of input, you may store the input in two variables or in a list.
  • You must output the ASCII expansion of the digit if you expand it \$n\$ times, n being the number that was the input. If your language has no method of output, you may store it in a variable.

Example I/O

  • Digit = 0, N = 3

    Output = 53505354

  • Digit = 2, N = 2

    Output = 5348

  • Digit = 5, N = 0

    Output = 5

Rules

This is , so shortest answer (in bytes) wins!

\$\endgroup\$
  • 2
    \$\begingroup\$ may we take the digit as a string? \$\endgroup\$ – attinat Oct 23 at 0:30
  • 2
    \$\begingroup\$ in that case, what about the inverse - may we output or take input as numbers, rather than strings? \$\endgroup\$ – attinat Oct 23 at 0:50
  • 3
    \$\begingroup\$ If, and only if your language has no method of input, you may store the input in two variables or in a list. What does that mean? And why not just rely on our default I/O methods? \$\endgroup\$ – Arnauld Oct 23 at 1:02
  • 8
    \$\begingroup\$ BTW: Apart from that, this is a nicely written first challenge. :-) \$\endgroup\$ – Arnauld Oct 23 at 1:27
  • 1
    \$\begingroup\$ @val Functions are acceptable. \$\endgroup\$ – JL2210 Oct 23 at 16:06

37 Answers 37

10
\$\begingroup\$

Perl 6, 28 bytes

{($^a,*.ords.join...*)[$^b]}

Try it online!

Explanation:

{                          }  # Anonymous codeblock
 (               ...*)[$^b]   # Index into an infinite list
  $^a,                        # Starting from the given number
      *                       # Where each element is
       .ords.join             # The ordinal values joined
\$\endgroup\$
  • 6
    \$\begingroup\$ (As an aside, Perl 6 is now officially called Raku) \$\endgroup\$ – Uri Granta Oct 24 at 6:19
8
\$\begingroup\$

05AB1E, 3 bytes

FÇJ

Takes N as first input and the digit as second.

Try it online or verify all test cases.

Explanation:

F    # Loop the (implicit) first input (N) amount of times
 Ç   #  Convert the characters in the string at the top of the stack to its unicode values
     #  (which will take the second input implicitly in the first iteration)
  J  #  Join these unicode integers together to a single string
     # (after the loop, the result is output implicitly)
\$\endgroup\$
6
\$\begingroup\$

Python 2, 55 bytes

f=lambda s,n:f(`ord(s[0])`,n-1)+f(s[1:],n)if n*s else s

Try it online!


56 bytes

lambda s,n:eval("''.join(`ord(c)`for c in"*n+" s"+")"*n)

Try it online!

Generates and evaluates monstrosities like:

''.join(`ord(c)`for c in''.join(`ord(c)`for c in''.join(`ord(c)`for c in s)))

56 bytes

f=lambda s,n:s*0**n or''.join(`ord(c)`for c in f(s,n-1))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ and...or saves a byte TIO \$\endgroup\$ – Jonathan Allan Oct 23 at 16:42
5
\$\begingroup\$

Jelly, 7 5 bytes

ṾOVƊ¡

Try it online!

-2 bytes after a friend helped me figure out what's wrong with O

For some reason, to run all test cases with a footer, an extra byte is required for the explicit nilad: Try it online!

Ṿ        Stringify the input
 O       and convert each character to a codepoint,
  V      then concatenate them and eval the result,
   Ɗ¡    repeated a number of times equal to the right argument.
\$\endgroup\$
  • 1
    \$\begingroup\$ Nice. Don't know Jelly, but does it help at all that you can take the input as a string? I was wondering if you could somehow avoid the first step? \$\endgroup\$ – Jonah Oct 23 at 6:56
  • \$\begingroup\$ V turns it back into an integer anyhow, so the best I could do would probably just be to change the order of things, since using V to stringify and then concatenate the strings all in one byte seems like it ought to be optimal, even though it does end up with an integer. \$\endgroup\$ – Unrelated String Oct 23 at 7:46
5
\$\begingroup\$

Brachylog, 7 bytes

{ṫạc}ⁱ⁾

Try it online!

{   }ⁱ     Repeat
 ṫ         stringifying,
  ạ        converting to a list of codepoints,
   c       and concatenating
      ⁾    a number of times equal to the last element of the input.
\$\endgroup\$
  • \$\begingroup\$ Huh, not sure I've ever had one of my answers downvoted before. \$\endgroup\$ – Unrelated String Nov 7 at 6:20
5
\$\begingroup\$

J, 14 13 bytes

[:,":@u:~"+&3

Try it online!

How it works

The "bind" operator & is commonly used to bind a constant to a dyad, so that it can be used as a monad. However, the same form can be used as a dyad: x n&v y (where n is a noun and v is a dyadic verb) or x v&n y applies monadic n&v or v&n to y repeatedly x times. Using this feature, we can design the target function like this:

x some_constant&some_dyad y
run `some_constant some_dyad y` x times
... or ...
x some_dyad&some_constant y
run `y some_dyad some_constant` x times

In this case, there is an obvious choice for the some_constant, which is 3 for 3 u: y.

And here goes the full explanation:

[:,":@u:~"+&3
           &   Apply this function x times...
      u:~   3  Convert chars of y to ASCII values (3 u:)
   ":@   "+    Convert each number back to string
[:,            Flatten the array to get a single string
\$\endgroup\$
4
\$\begingroup\$

Ruby, 30 bytes

->s,n{n.times{s=s.bytes*''};s}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

C# (.NET Core), 174 147 bytes

class Z{static void Main(string[] a){for(int n=int.Parse(a[1]);n-->0;){var x="";foreach(char c in a[0])x+=c-0;a[0]=x;}System.Console.Write(a[0]);}}  

Big help from Jo King.

Try it online!

Ungolfed

class Z
{
   static void Main(string[] a)
    {
        int n = int.Parse(a[1]);
        for (; n-- > 0;)
        {
            var x = "";
            foreach (char c in a[0])
                x += c - 0; //c-0 gets converted to int, and then the int is 
                                   //automatically converted to a string
            a[0]=x;
        }
        System.Console.Write(a[0]);
    }
}
\$\endgroup\$
  • \$\begingroup\$ @JoKing I thought of that first and somehow thought it was longer. But after all the substring business and calc'ing p that failed. Thanks! \$\endgroup\$ – HiddenBabel Oct 23 at 2:54
  • \$\begingroup\$ foreach(char c in a[0]) -> foreach(var c in a[0]), -1 byte \$\endgroup\$ – Embodiment of Ignorance Oct 23 at 5:32
  • \$\begingroup\$ Also, get rid of extra space in Main(string[] a) \$\endgroup\$ – Embodiment of Ignorance Oct 23 at 5:33
  • \$\begingroup\$ Move the a[0]=x part into the top part of the for-loop, like this: for(int n=int.Parse(a[1]);n-->0;a[0]=x) \$\endgroup\$ – Embodiment of Ignorance Oct 23 at 5:39
  • \$\begingroup\$ With using System.Linq; the inner part of the loop can be reduced to: a[0]=string.Concat(a[0].Select(c=>c-0));, -6 bytes \$\endgroup\$ – pocki_c Oct 23 at 13:06
4
\$\begingroup\$

JavaScript (ES6),  43  40 bytes

Thanks to @tsh for reminding me that I didn't use currying this time :p (-3 bytes)

Takes input as (N)(digit).

n=>g=k=>n--?g(k.replace(/./g,c=>c^48)):k

Try it online!

How?

This is a simple recursive function. The only trick in there is c^48. Because c is a string, we need to coerce it to an integer. We could do +c+48, but that would be 1 byte longer. Using a bitwise XOR is safe here, as \$48\$ is \$110000_2\$ and c is less than \$10000_2\$.

\$\endgroup\$
  • 1
    \$\begingroup\$ I believe n=>g=k=>n--?g(k.replace(/./g,c=>c^48)):k is valid. \$\endgroup\$ – tsh Oct 23 at 2:32
3
\$\begingroup\$

Haskell, 30 35 30 bytes

(!!).iterate(>>=show.fromEnum)

Try it online!

+5 then -5 bytes from Jo King clarifying the rules and then working it into pointfree.

My first Haskell golf so I've probably done something horribly wrong. In addition to having misspelled golf, I tried to import ord without putting it in my byte count!

    .                             The composition of
     iterate                      infinitely iterating, starting with the argument,
            (>>=             )    concatenating the results of mapping
                show              finding the string representation of
                    .fromEnum     the codepoint of the argument,
(!!)                              with indexing into the resulting infinite list.
\$\endgroup\$
  • 1
    \$\begingroup\$ You do have to import Data.char if you want to use ord. You can also do this in pointfree style for 30 bytes with fromEnum \$\endgroup\$ – Jo King Oct 23 at 2:45
3
\$\begingroup\$

Japt -h, 5 bytes

VÆ=mc

Try it

VÆ=mc   V = number of times, U = digit
VÆ      V times do: (Collects each result into an array)
  =mc     Map every digit of U to it's ASCII value, and make that the new U
-h      Take last element
\$\endgroup\$
3
\$\begingroup\$

Perl 5, 22 bytes

eval's/./ord$&/ge;'x<>

Try it online!

\$\endgroup\$
3
\$\begingroup\$

J, 18 bytes

([:,3":@u:"0])@[&0

Try it online!

From the J dictionary:

The phrase x f@[&0 y is equivalent to f^:x y , apply the monad f x times to y.

That is, it's a shortcut for power of ^: applied as many times as the left arg. Which explains the

(  )@[&0

part of the code. Now for what's in the parentheses:

3 u:] converts to a unicode code point, but unfortunately has infinite rank, and we want to apply it with 0 rank, hence the added "0. The code point is a number, and we convert it back to a string with format ":. Finally, we flatten , this list of strings.

\$\endgroup\$
  • \$\begingroup\$ Can you explain your code, I don't know what's going on. Thanks! \$\endgroup\$ – Galen Ivanov Oct 23 at 6:34
  • \$\begingroup\$ @GalenIvanov Done! \$\endgroup\$ – Jonah Oct 23 at 6:42
  • 1
    \$\begingroup\$ Thank you! Apparently I can't remember this shortcut to ^: \$\endgroup\$ – Galen Ivanov Oct 23 at 6:44
3
\$\begingroup\$

PowerShell, 51 49 bytes

-2 bytes thanks to mazzy

param($s,$n),1*$n|%{$s=-join($s|% t*y|%{+$_})};$s

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 2, 63 57 bytes

f=lambda i,n:n and f(''.join(`ord(c)`for c in i),n-1)or i

Try it online!

-6 bytes due to Jonathan Allan noting that the input can be a string.

Takes input as single digit string and an integer number of repetitions.

\$\endgroup\$
  • \$\begingroup\$ Take i as a string and save six bytes, TIO \$\endgroup\$ – Jonathan Allan Oct 23 at 15:38
  • \$\begingroup\$ @Jonathan Allan: Ah! I missed that that was permitted. \$\endgroup\$ – Chas Brown Oct 23 at 21:49
2
\$\begingroup\$

Haskell, 30 bytes

(!!).iterate(show.fromEnum=<<)

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't know Haskell, but there is already almost identical solution \$\endgroup\$ – Galen Ivanov Oct 23 at 8:07
  • 1
    \$\begingroup\$ @GalenIvanov The linked one does work exactly the same way as this one. \$\endgroup\$ – Bubbler Oct 23 at 8:10
  • 1
    \$\begingroup\$ @GalenIvanov Yup, they're the same. I came up with it independently. Two same answers isn't surprising here since I think it's the best Haskell can do. \$\endgroup\$ – xnor Oct 23 at 8:15
  • 2
    \$\begingroup\$ Yes, it's not surprising to have this when the code is short :) \$\endgroup\$ – Galen Ivanov Oct 23 at 8:20
  • \$\begingroup\$ I was inspired to write mine entirely by just how perfect an application of the list monad it is, so it definitely makes sense that I'd not be alone in it \$\endgroup\$ – Unrelated String Oct 23 at 23:02
2
\$\begingroup\$

Charcoal, 9 bytes

FN≔⭆η℅κηη

Try it online! Link is to verbose version of code. Takes N as the first input and the digit as the second input. Explanation:

FN

Repeat N times...

≔⭆η℅κη

Map each character to its ordinal and concatenate.

η

Output the final result.

Sadly FN≦℅ηη doesn't work...

\$\endgroup\$
2
\$\begingroup\$

Lua, 55 bytes

a,N=...for i=1,N do a=a:gsub('.',('').byte)end
print(a)

Try it online!

Take input as arguments. Explanation is fairly obvious.

Alternative solution: Lua, 60 bytes

a,N=...for i=1,N do a=table.concat{a:byte(1,-1)}end
print(a)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Lua, 67 bytes

i=io.read d=i(1)for _=1,i()do
d=d:gsub(".",string.byte)end
print(d)

Try it online!

Let d be the digit we will transform (always of length 1, so we ask to read 1 character with io.read(1)). If N > 0, replace each character by its byte value. Return the digit.

\$\endgroup\$
2
\$\begingroup\$

Bracmat, 63 bytes

get':%?a %?n&whl'(!n+-1:~<0:?n&str$vap$((=.asc$!arg).!a):?a)&!a

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nice 1st answer. One suggestion is to add a Try It Online link in your answer so other users can easily verify your code. It's not required but highly recommended. It can also generate CG&CC posts which is nice. \$\endgroup\$ – Veskah Oct 24 at 12:23
2
\$\begingroup\$

K (oK), 13 9 bytes

-4 bytes thanks to ngn

(,/$`i$)/

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ (,/$`i$)/ works too, with swapped args \$\endgroup\$ – ngn Nov 2 at 13:22
  • \$\begingroup\$ @ngn Thank you! Apparently I didn't know how x and y are applied to this tacit function. \$\endgroup\$ – Galen Ivanov Nov 2 at 17:00
1
\$\begingroup\$

Wolfram Language (Mathematica), 41 bytes

""<>ToString/@ToCharacterCode@#&~Nest~##&

Try it online!

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

APL (Dyalog Unicode), 16 bytes

{(∊⍕¨∘⎕UCS)⍣⍵⊢⍺}

Try it online!

Left input is a one-digit string, right input is the number of iteration. Returns the string representation of the result.

How it works

{(∊⍕¨∘⎕UCS)⍣⍵⊢⍺}
{              }  Dyadic dfn, ⍺=digit string, ⍵=iteration
             ⊢⍺   Start with ⍺
 (        )⍣⍵     Repeat ⍵ times...
      ⎕UCS        Convert each char to Unicode codepoint
   ⍕¨∘            Convert each number back to string
  ∊               Enlist (flatten) all chars into one string
\$\endgroup\$
1
\$\begingroup\$

PHP, 58 bytes

for([,$a,$b]=$argv;$b--;)$a=strtr($a,range(48,57));echo$a;

Try it online!

Input digit and number are two command arguments ($argv) in same order.

Comented

for(
  [,$a,$b]=$argv;  // $a is input digit and $b is number
  $b--;            // loop $b times
)
  $a=              // set $a to
    strtr(         // strtr in array mode, replaces keys with values
      $a,          // replace in $a itself
      range(48,57) // an array with keys 0...9 and values 48...57
    );
echo$a;            // at the end, output $a
\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 55 bytes

a=>b=>{for(;b-->0;a=a.SelectMany(l=>l-0+""));return a;}

Try it online!

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

Python 3, 69 60 bytes

f=lambda n,i:i and f(''.join(str(ord(c))for c in n),i-1)or n

Try it online!

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

Red, 62 57 bytes

func[d n][loop n[parse d[any[change p: skip(0 + p/1)]]]d]

Try it online!

Explanation:

f: func [ d n ] [                  ; a function with 2 parameters
    loop n [                       ; repeat n times
        parse d [                  ; parse the string with the following rules        
            any [                  ; one ore more 
                change p: skip     ; change any string with length 1 (a single digit)
                (0 + p/1)          ; with its representation as a number 48..57 
            ]                       
        ]
    ]
    d                              ; return the altered string
]
\$\endgroup\$
1
\$\begingroup\$

Icon, 72 63 bytes

Inspired by @xnor's Python 2 solution

procedure f(d,n)
return(*d*n<1&d)|f(48+!d,n-1)||f(d[2:0],n)
end

Try it online!

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

Retina, 21 bytes

.+¶

"$+"+`.
$.(*_48*

Try it online! Takes N as the first input and the digit as the second input. Explanation:

.+¶

Delete N from the output.

"$+"+`

Repeat N times.

.
$.(*_48*

Replace each digit with its ASCII code.

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

QuadR, 8 bytes

Translation of Nahuel Fouilleul's solution. Thanks to Veskah for pointing it out.

.
⎕UCS⍵M

Try it online!

. replace any character

⎕UCS with the Universal Character Set ordinal of the
⍵M Match

This is equivalent to the Dyalog APL expression '.'⎕R{⍕⎕UCS⍵.Match}⍣⎕⊢⍞. Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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