| Bytes | Lang | Time | Link |
|---|---|---|---|
| 068 | AWK | 250521T164941Z | xrs |
| 046 | Arturo | 230730T060838Z | chunes |
| 033 | Factor | 230730T054729Z | chunes |
| 003 | Thunno 2 | 230729T175613Z | The Thon |
| 110 | Racket | 230717T204610Z | Ed The & |
| 020 | V vim | 230717T202840Z | nmjcman1 |
| 094 | Setanta | 230717T185420Z | bb94 |
| 020 | Juby | 230717T162000Z | Jordan |
| 005 | Vyxal | 220105T220202Z | emanresu |
| 009 | K oK | 191023T063920Z | Galen Iv |
| 054 | Wren | 191028T055801Z | user8505 |
| 063 | Bracmat | 191024T102547Z | Bart Jon |
| 075 | Ceylon | 191026T010123Z | Paŭlo Eb |
| 009 | Husk | 191025T071508Z | Unrelate |
| 064 | dc | 191025T012057Z | brhfl |
| 055 | Zsh | 191024T152019Z | GammaFun |
| 088 | C gcc | 191023T215729Z | forevers |
| 057 | Python 2 | 191023T003827Z | Chas Bro |
| 049 | PowerShell | 191023T122359Z | Veskah |
| 057 | Gema | 191023T162912Z | manatwor |
| 067 | Lua | 191023T152723Z | anderium |
| 008 | QuadR | 191023T130510Z | Adá |
| 021 | Retina | 191023T122932Z | Neil |
| 055 | Lua | 191023T111929Z | val - di |
| 009 | Charcoal | 191023T093713Z | Neil |
| 063 | Icon | 191023T065835Z | Galen Iv |
| 040 | JavaScript ES6 | 191023T010913Z | Arnauld |
| 057 | Red | 191023T070919Z | Galen Iv |
| 030 | Haskell | 191023T080112Z | xnor |
| 013 | J | 191023T074555Z | Bubbler |
| 055 | Python 2 | 191023T073325Z | xnor |
| 060 | Python 3 | 191023T060131Z | Paul-B98 |
| 018 | J | 191023T062521Z | Jonah |
| 003 | 05AB1E | 191023T064159Z | Kevin Cr |
| 022 | Perl 5 | 191023T061453Z | Nahuel F |
| 005 | Japt h | 191023T052256Z | Gymhgy |
| 055 | C# Visual C# Interactive Compiler | 191023T051448Z | Gymhgy |
| 007 | Brachylog | 191023T002715Z | Unrelate |
| 030 | Haskell | 191023T012529Z | Unrelate |
| 058 | PHP | 191023T025828Z | Night2 |
| 005 | Jelly | 191023T003445Z | Unrelate |
| 147 | C# .NET Core | 191023T020320Z | HiddenBa |
| 016 | APL Dyalog Unicode | 191023T015544Z | Bubbler |
| 041 | Wolfram Language Mathematica | 191023T005048Z | att |
| 030 | Ruby | 191023T004736Z | Value In |
| 028 | Perl 6 | 191023T002440Z | Jo King |
AWK, 68 bytes
{for(s=$1;$2--;){k=split(s,a,X);for(s=j=X;j++<k;)s=s a[j]+48}}1,$0=s
Factor, 33 bytes
[ [ [ >dec ] map-concat ] times ]
[ ... ] timescall[ ... ]the number of times denoted by the number on top of the stack[ >dec ] map-concatmap over each character in a string, convert it to a string, and flatten
Thunno 2, 3 bytes
{CJ
Explanation
{CJ # Implicit input
{ # Repeat N times:
C # Get ASCII codes
J # Join into a string
# Implicit output
Racket, 110 bytes
(define(a x n)(if(< n 1)x(a(string->number(string-join(for/list([c(~a x)])(~a(char->integer c)))""))(- n 1))))
Also, as a bonus, it works with bigger numbers as well! For example: (a 11 3) and (a 123456789 10) (this gives a number with 9216 digits) are both valid inputs.
Explanation
We receive two inputs
x(the number to expand) andnthe number of expansions.If
nis less than 1, we returnxas is.We iterate through all the digits of
xby convertingxto a string. During each iteration, we obtain the string representation of each digit's ASCII value and append it to the list.for/listhandle all list appending for us, so all we need to do is return the string at the end of the iteration.
Once the list is obtained, we join it to a string without any spaces between all the values.
We convert the final string back into a number, pass it to step 1 as the new
xand passn - 1as thenargument.- Repeat steps 1-5 until step 2 is executed.
(define (expand-to-ascii x n)
(if (< n 1)
x
(a (string->number
(string-join
(for/list ([c (~a x)])
(~a (char->integer c)))
""))
(- n 1))))
Have an awesome week ahead!
V (vim), 20 bytes
ÀñÓ./½submatch(0)+48
Input is given as buffer input and an argument. It runs :s/./\=submatch(0)+47/g the arg number of times
Setanta, 106 94 bytes
gniomh(d,n){le i idir(0,n){i=""le j idir(0,fad(d)){i+=go_teacs(48+go_uimh(d[j]))}d=i}toradh d}
Requires d to be passed in as a string.
J-uby, 20 bytes
Takes curried arguments with \$n\$ first and \$digit\$ second, i.e. F[n][digit].
:**&(S|:bytes|:join)
Explanation
The function S|:bytes|:join converts to a string, gets the character codes, then joins them. The :** operator applies that function \$n\$ times.
Vyxal, 5 bytes
(SCfṅ
( # Input times...
S # Stringify
C # Charcodes
fṅ # Join together (Should just be ṅ, but bugs)
Wren, 54 bytes
One iteration is simply x.bytes.join()....
Fn.new{|x,y|
for(i in 1..y)x=x.bytes.join()
return x
}
Ceylon, 75
String?x(String d,Integer n)=>loop(d)((s)=>"".join(s*.hash)).skip(n).first;
Here is the long version, which might be more understandable:
"""convert a string of digits (or other characters) into a string of the digits'
ASCII values, represented themselves as decimal ASCII digits (without any
separator).
"""
String asciify(String input) {
// maps each character of the string into its ASCII value (actually unicode,
// but that's the same for the relevant range), and this to decimal digits.
{String*} asciiDecimals = input.map((Character c) => c.integer.string);
// concatenate all those strings
return String.sum(asciiDecimals);
}
"""expand a digit (or a string) into its ASCII representation several times."""
String asciiExpand(String digit, Integer times) {
// infinite (lazy) list of all the expansions
{String*} allExpansions = loop(digit)(asciify);
// take the entry `times` steps from the first.
return allExpansions.getFromFirst(times)
// If that is null (which can't happen, as we have an infinite list),
// throw an exception instead.
else nothing;
}
Tricks used to make it shorter:
- one-letter function and variable names
- no comments/doc-strings
- inline the intermediate
{String*}variables - use the fat-arrow syntax (
=>) instead of blocks with just onereturnfor the functions - inline the
asciifyfunction → then we can use type interference, and don't need to writeStringagain for parameter and return type - use
"".join(...)instead ofString.sum(...). Possibly a bit less efficient, but a slightly shorter (+ next point). jointakes an iterable of any objects, not just strings, and will apply.stringon each element, so we don't need to write down.stringourselves.- use
.hashinstead of.integerto get the ASCII/Unicode values – Character's hash code happens to be implemented just the right way. - replace
s.map((Character c)=>c.hash)withs*.hash(a spread attribute) – the first one is lazy (returns an iterable), the second one is non-lazy (returns a sequence – it's the equivalent fors.collect(...)), but this difference doesn't matter here (as we join the strings shortly after anyways), and the second one is a lot shorter. - replace
.getFromFirst(n)with the slightly shorter.skip(n).first. (This might get a different result for negativen, but those are outside of the specified input range anyways.) - instead of adding
else nothingfor the return (to convince the compiler that this is not nullable), we can just declare the nullable return typeString?. It won't happen anyways for valid inputs. - remove non-essential whitespace
I also thought I found a shorter way of writing this (which works for finite iterables), but it actually doesn't work here, because loop returns an infinite iterable, and trying to convert it into a sequence (with [* ... ])will run forever until it runs out of memory:
String?y(String d, Integer n)=>[*loop(d)((s)=>"".join(s*.hash))][n];
Husk, 9 bytes
~!ȯ¡ṁosc→
Mostly a translation of the Haskell answer, with some combinator stuff thrown in to deal with 1-indexing.
! Index into
ȯ¡ the infinite list of iterations of
ṁ concat-mapping
s show
o .
c ord
~ starting with the first argument, and with the index being the second argument
→ incremented.
dc, 64 bytes
dsi[A~48+lrZAr^*lr+srdZ1<P]sP[0srA*lPxlPxlrI3^/li1-dsi0<M]sM0<Mp
I wouldn't be surprised if I could golf this down a bit. It's more verbose than anticipated due to handling a lot of edge cases, etc. Put the 'digits' to transform on the stack first, then the number of iterations.
dsi stores the number of iterations in i. Macro P does one iteration of the transformation. A~ to divide w/ remainder by 10, 48+ to give us ASCII. Variable r holds our result. lrZAr^* loads this variable for the sake of finding how many digits it currently has. It shifts our ASCII value left (decimal) this many places (48 becomes 4800), then lr+sr adds our old value r and stores the result back into r. Here's tricky situation number one: dZ1<P says to keep running P as long as our source material is at least one digit. But, we need to do a final run when we hit 1 digit as well.
Macro M is our main. 0sr initializes r to 0, and then... the rest of this I'm going to explain a bit out of order. For each iteration, we need to run P twice, because of the aforementioned single-digit thing. So, lPxlPx. However, this screws us up when our original value is only a single digit. Plus, we often get left with an extra 0 at the right side. A* multiplies the original by ten, and I3^/ at the end divides by a thousand. This is pretty goofy, but it ensures all values work ok. li1-dsi0<M keeps running M until i, our number of iterations, is complete. We can't do dsMx as per usual to save & run a macro, because this won't work if our number of iterations is 0. So we test for that, leaving us with the longer sM0<M. Finally, we print.
Zsh, 55 bytes
(($2*#1))&&<<<`$0 $[#1] $[$2-1]``$0 "${1:1}" $2`||<<<$1
A port of @xnor's Python 2 solution, it came in just two bytes below my first solution.
We require quotes around ${1:1}, otherwise it is removed when empty, turning $2 into $1 on the recursive call.
If the repeated expansion never grows past 2^63, 53 bytes with a recursive math function:
(){s=
for c (${(s::)1})s+=$[#c]
(($2?$0(s,$2-1):$1))}
C (gcc), 88 bytes
Thanks ceilingcat and peter cordez for their contributions.
z(v,n){for(sprintf(o,"%d",v);n--;)for(strcpy(t,o),v=0;t[v];)sprintf(o+v++*2,"%d",t[v]);}
Python 2, 63 57 bytes
f=lambda i,n:n and f(''.join(`ord(c)`for c in i),n-1)or i
-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.
Gema, 57 characters
* *=@set{d;*}@repeat{*;@set{d;@x{$d}}}$d
x:?=@char-int{?}'
Sample run:
bash-5.0$ echo -n '0 3' | gema '* *=@set{d;*}@repeat{*;@set{d;@x{$d}}}$d;x:?=@char-int{?}'
53505354
Lua, 67 bytes
i=io.read d=i(1)for _=1,i()do
d=d:gsub(".",string.byte)end
print(d)
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.
QuadR, 8 bytes
Translation of Nahuel Fouilleul's solution. Thanks to Veskah for pointing it out.
.
⎕UCS⍵M
. 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!
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.
Lua, 55 bytes
a,N=...for i=1,N do a=a:gsub('.',('').byte)end
print(a)
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)
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...
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
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
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\$.
Red, 62 57 bytes
func[d n][loop n[parse d[any[change p: skip(0 + p/1)]]]d]
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
]
J, 14 13 bytes
[:,":@u:~"+&3
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
Python 2, 55 bytes
f=lambda s,n:f(`ord(s[0])`,n-1)+f(s[1:],n)if n*s else s
56 bytes
lambda s,n:eval("''.join(`ord(c)`for c in"*n+" s"+")"*n)
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))
J, 18 bytes
([:,3":@u:"0])@[&0
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.
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)
Japt -h, 5 bytes
VÆ=mc
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
C# (Visual C# Interactive Compiler), 55 bytes
a=>b=>{for(;b-->0;a=a.SelectMany(l=>l-0+""));return a;}
Brachylog, 7 bytes
{ṫạc}ⁱ⁾
{ }ⁱ Repeat
ṫ stringifying,
ạ converting to a list of codepoints,
c and concatenating
⁾ a number of times equal to the last element of the input.
Haskell, 30 35 30 bytes
(!!).iterate(>>=show.fromEnum)
+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.
PHP, 58 bytes
for([,$a,$b]=$argv;$b--;)$a=strtr($a,range(48,57));echo$a;
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
Jelly, 7 5 bytes
ṾOVƊ¡
-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.
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.
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]);
}
}
APL (Dyalog Unicode), 16 bytes
{(∊⍕¨∘⎕UCS)⍣⍵⊢⍺}
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
Perl 6, 28 bytes
{($^a,*.ords.join...*)[$^b]}
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