Skip to main content
added 389 characters in body
Source Link
pajonk
  • 18.3k
  • 3
  • 25
  • 74

R, 96 65 7272 69 bytes

Or R>=4.1, 6562 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.
-3 bytes thanks to @Dominic van Essen - converting back to one (reusable) function.

i=0;s=scan`/`=function(s,t=0);f=function"if"((a=s[i<<-i+1]t*i+1])"if"(a<58<58,a,c(40,f()s/1,a,f()s/1,41));f();i=9

Try it online!Try it online!

Port of @loopy walt's answer.

Explanation

Initiates global counterA function takes string is toand 0t and takes input withdefaulting to scan0 (for resetting global variable).
The recursive function (named f/ overriding division) takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function (with t=1 to keep i as is between calls).
The code at the end initiates global counter i to a digit (9 chosen for demonstration purposes).


Solution shorter for R>=4.1:

R, 74 bytes

Or R>=4.1, 60 bytes by replacing two function occurrences with \s.

function(s,i=0,f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41)))f()

Try it online!

Same as above, but here i is "local" for the main function, but "global" for the recursive one.

R, 96 65 72 bytes

Or R>=4.1, 65 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.

i=0;s=scan();f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41));f()

Try it online!

Port of @loopy walt's answer.

Explanation

Initiates global counter i to 0 and takes input with scan.
The recursive function f takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function.


Solution shorter for R>=4.1:

R, 74 bytes

Or R>=4.1, 60 bytes by replacing two function occurrences with \s.

function(s,i=0,f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41)))f()

Try it online!

Same as above, but here i is "local" for the main function, but "global" for the recursive one.

R, 96 65 72 69 bytes

Or R>=4.1, 62 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.
-3 bytes thanks to @Dominic van Essen - converting back to one (reusable) function.

`/`=function(s,t=0)"if"((a=s[i<<-t*i+1])<58,a,c(40,s/1,a,s/1,41));i=9

Try it online!

Port of @loopy walt's answer.

Explanation

A function takes string s and t defaulting to 0 (for resetting global variable).
The recursive function (named / overriding division) takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function (with t=1 to keep i as is between calls).
The code at the end initiates global counter i to a digit (9 chosen for demonstration purposes).


Solution shorter for R>=4.1:

R, 74 bytes

Or R>=4.1, 60 bytes by replacing two function occurrences with \s.

function(s,i=0,f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41)))f()

Try it online!

Same as above, but here i is "local" for the main function, but "global" for the recursive one.

added 764 characters in body
Source Link
pajonk
  • 18.3k
  • 3
  • 25
  • 74

R, 96 65 72 bytes

Or R>=4.1, 65 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.

i=0;s=scan();f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41));f()

Try it online!

Port of @loopy walt's answer.

Explanation

Initiates global counter i to 0 and takes input with scan.
The recursive function f takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function.


Solution shorter for R>=4.1:

R, 74 bytes

Or R>=4.1, 60 bytes by replacing two function occurrences with \s.

function(s,i=0,f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41)))f()

Try it online!

Same as above, but here i is "local" for the main function, but "global" for the recursive one.

R, 96 65 72 bytes

Or R>=4.1, 65 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.

i=0;s=scan();f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41));f()

Try it online!

Port of @loopy walt's answer.

Explanation

Initiates global counter i to 0 and takes input with scan.
The recursive function f takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function.

R, 96 65 72 bytes

Or R>=4.1, 65 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.

i=0;s=scan();f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41));f()

Try it online!

Port of @loopy walt's answer.

Explanation

Initiates global counter i to 0 and takes input with scan.
The recursive function f takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function.


Solution shorter for R>=4.1:

R, 74 bytes

Or R>=4.1, 60 bytes by replacing two function occurrences with \s.

function(s,i=0,f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41)))f()

Try it online!

Same as above, but here i is "local" for the main function, but "global" for the recursive one.

added 3 characters in body
Source Link
pajonk
  • 18.3k
  • 3
  • 25
  • 74

R, 96 6565 72 bytes

Or R>=4.1, 5865 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.

f={i=0;functioni=0;s=scan(s,);f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(s),a,f(s),41))};f()

Try it online!Try it online!

Port of @loopy walt's answer.

Explanation

Creates local environment with {} and initiatesInitiates global counter i to 0.
The environment returns a function which is assigned to and takes input with fscan.
The recursive function insidef takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function.

R, 96 65 bytes

Or R>=4.1, 58 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.

f={i=0;function(s,a=s[i<<-i+1])"if"(a<58,a,c(40,f(s),a,f(s),41))}

Try it online!

Port of @loopy walt's answer.

Explanation

Creates local environment with {} and initiates counter i to 0.
The environment returns a function which is assigned to f.
The recursive function inside takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function.

R, 96 65 72 bytes

Or R>=4.1, 65 bytes by replacing the word function with \.

-many bytes by changing I/O to vector of char codes.
-another some bytes thanks to @Dominic van Essen.
+7 bytes costed converting from not-so-self-contained function to a program.

i=0;s=scan();f=function(a=s[i<<-i+1])"if"(a<58,a,c(40,f(),a,f(),41));f()

Try it online!

Port of @loopy walt's answer.

Explanation

Initiates global counter i to 0 and takes input with scan.
The recursive function f takes one character at a time (a) incrementing the global counter each time. If the character char code is less then 58, then it's a digit and needs to be returned as is. Otherwise, we surround the character with braces (char codes 40,41) and recursive calls to our function.

added 633 characters in body
Source Link
pajonk
  • 18.3k
  • 3
  • 25
  • 74
Loading
Source Link
pajonk
  • 18.3k
  • 3
  • 25
  • 74
Loading