Collapsing numbers
Let's define the a function on natural numbers $n$, written as base 10 digits $d_k; d_{k-1}; dotsc; d_1; d_0$, as follows:
As long as there are equal adjacent digits $d_i;d_{i-1}$, replace them by their sum $d_i+d_{i-1}$ from left to right. If there were any such digits, repeat the same procedure.
In other words, in each iteration we greedily take all pairs of equal adjacent digits and replace them by their sum at the same time (using the left-most pair if they overlap).
Example
Let's take $texttt{9988}$ for example:
- The first adjacent digits which are equal are the two $texttt{9}$
- So we replace them by $texttt{9 + 9} = texttt{18}$ which gives us $texttt{1888}$
- Since we're still in the first left-right traversal and there were still two $texttt{8}$s we need to first replace these
- So we get $texttt{1816}$
- Something changed, so we need to do another iteration
- But there are no such digits, so we stop
Therefore the $9988^text{th}$ number in that sequence is $1816$.
Challenge
The first 200 terms are:
0,1,2,3,4,5,6,7,8,9,10,2,12,13,14,15,16,17,18,19,20,21,4,23,24,25,26,27,28,29,30,31,32,6,34,35,36,37,38,39,40,41,42,43,8,45,46,47,48,49,50,51,52,53,54,10,56,57,58,59,60,61,62,63,64,65,12,67,68,69,70,71,72,73,74,75,76,14,78,79,80,81,82,83,84,85,86,87,16,89,90,91,92,93,94,95,96,97,98,18,10,101,102,103,104,105,106,107,108,109,20,21,4,23,24,25,26,27,28,29,120,121,14,123,124,125,126,127,128,129,130,131,132,16,134,135,136,137,138,139,140,141,142,143,18,145,146,147,148,149,150,151,152,153,154,20,156,157,158,159,160,161,162,163,164,165,4,167,168,169,170,171,172,173,174,175,176,24,178,179,180,181,182,183,184,185,186,187,26,189,190,191,192,193,194,195,196,197,198,28
Your task is to generate that sequence, either
- given $n$, return the $n^text{th}$ number in that sequence,
- given $n$, return the first $n$ numbers in that sequence
- or generate the sequence indefinitely.
You may choose your submission to use either $0$- or $1$-indexing, but please specify which.
Test cases
You may use the above given terms, however here are some larger ones:
222 -> 42
1633 -> 4
4488 -> 816
15519 -> 2019
19988 -> 2816
99999 -> 18189
119988 -> 21816
100001 -> 101
999999 -> 181818
code-golf sequence integer
add a comment |
Let's define the a function on natural numbers $n$, written as base 10 digits $d_k; d_{k-1}; dotsc; d_1; d_0$, as follows:
As long as there are equal adjacent digits $d_i;d_{i-1}$, replace them by their sum $d_i+d_{i-1}$ from left to right. If there were any such digits, repeat the same procedure.
In other words, in each iteration we greedily take all pairs of equal adjacent digits and replace them by their sum at the same time (using the left-most pair if they overlap).
Example
Let's take $texttt{9988}$ for example:
- The first adjacent digits which are equal are the two $texttt{9}$
- So we replace them by $texttt{9 + 9} = texttt{18}$ which gives us $texttt{1888}$
- Since we're still in the first left-right traversal and there were still two $texttt{8}$s we need to first replace these
- So we get $texttt{1816}$
- Something changed, so we need to do another iteration
- But there are no such digits, so we stop
Therefore the $9988^text{th}$ number in that sequence is $1816$.
Challenge
The first 200 terms are:
0,1,2,3,4,5,6,7,8,9,10,2,12,13,14,15,16,17,18,19,20,21,4,23,24,25,26,27,28,29,30,31,32,6,34,35,36,37,38,39,40,41,42,43,8,45,46,47,48,49,50,51,52,53,54,10,56,57,58,59,60,61,62,63,64,65,12,67,68,69,70,71,72,73,74,75,76,14,78,79,80,81,82,83,84,85,86,87,16,89,90,91,92,93,94,95,96,97,98,18,10,101,102,103,104,105,106,107,108,109,20,21,4,23,24,25,26,27,28,29,120,121,14,123,124,125,126,127,128,129,130,131,132,16,134,135,136,137,138,139,140,141,142,143,18,145,146,147,148,149,150,151,152,153,154,20,156,157,158,159,160,161,162,163,164,165,4,167,168,169,170,171,172,173,174,175,176,24,178,179,180,181,182,183,184,185,186,187,26,189,190,191,192,193,194,195,196,197,198,28
Your task is to generate that sequence, either
- given $n$, return the $n^text{th}$ number in that sequence,
- given $n$, return the first $n$ numbers in that sequence
- or generate the sequence indefinitely.
You may choose your submission to use either $0$- or $1$-indexing, but please specify which.
Test cases
You may use the above given terms, however here are some larger ones:
222 -> 42
1633 -> 4
4488 -> 816
15519 -> 2019
19988 -> 2816
99999 -> 18189
119988 -> 21816
100001 -> 101
999999 -> 181818
code-golf sequence integer
add a comment |
Let's define the a function on natural numbers $n$, written as base 10 digits $d_k; d_{k-1}; dotsc; d_1; d_0$, as follows:
As long as there are equal adjacent digits $d_i;d_{i-1}$, replace them by their sum $d_i+d_{i-1}$ from left to right. If there were any such digits, repeat the same procedure.
In other words, in each iteration we greedily take all pairs of equal adjacent digits and replace them by their sum at the same time (using the left-most pair if they overlap).
Example
Let's take $texttt{9988}$ for example:
- The first adjacent digits which are equal are the two $texttt{9}$
- So we replace them by $texttt{9 + 9} = texttt{18}$ which gives us $texttt{1888}$
- Since we're still in the first left-right traversal and there were still two $texttt{8}$s we need to first replace these
- So we get $texttt{1816}$
- Something changed, so we need to do another iteration
- But there are no such digits, so we stop
Therefore the $9988^text{th}$ number in that sequence is $1816$.
Challenge
The first 200 terms are:
0,1,2,3,4,5,6,7,8,9,10,2,12,13,14,15,16,17,18,19,20,21,4,23,24,25,26,27,28,29,30,31,32,6,34,35,36,37,38,39,40,41,42,43,8,45,46,47,48,49,50,51,52,53,54,10,56,57,58,59,60,61,62,63,64,65,12,67,68,69,70,71,72,73,74,75,76,14,78,79,80,81,82,83,84,85,86,87,16,89,90,91,92,93,94,95,96,97,98,18,10,101,102,103,104,105,106,107,108,109,20,21,4,23,24,25,26,27,28,29,120,121,14,123,124,125,126,127,128,129,130,131,132,16,134,135,136,137,138,139,140,141,142,143,18,145,146,147,148,149,150,151,152,153,154,20,156,157,158,159,160,161,162,163,164,165,4,167,168,169,170,171,172,173,174,175,176,24,178,179,180,181,182,183,184,185,186,187,26,189,190,191,192,193,194,195,196,197,198,28
Your task is to generate that sequence, either
- given $n$, return the $n^text{th}$ number in that sequence,
- given $n$, return the first $n$ numbers in that sequence
- or generate the sequence indefinitely.
You may choose your submission to use either $0$- or $1$-indexing, but please specify which.
Test cases
You may use the above given terms, however here are some larger ones:
222 -> 42
1633 -> 4
4488 -> 816
15519 -> 2019
19988 -> 2816
99999 -> 18189
119988 -> 21816
100001 -> 101
999999 -> 181818
code-golf sequence integer
Let's define the a function on natural numbers $n$, written as base 10 digits $d_k; d_{k-1}; dotsc; d_1; d_0$, as follows:
As long as there are equal adjacent digits $d_i;d_{i-1}$, replace them by their sum $d_i+d_{i-1}$ from left to right. If there were any such digits, repeat the same procedure.
In other words, in each iteration we greedily take all pairs of equal adjacent digits and replace them by their sum at the same time (using the left-most pair if they overlap).
Example
Let's take $texttt{9988}$ for example:
- The first adjacent digits which are equal are the two $texttt{9}$
- So we replace them by $texttt{9 + 9} = texttt{18}$ which gives us $texttt{1888}$
- Since we're still in the first left-right traversal and there were still two $texttt{8}$s we need to first replace these
- So we get $texttt{1816}$
- Something changed, so we need to do another iteration
- But there are no such digits, so we stop
Therefore the $9988^text{th}$ number in that sequence is $1816$.
Challenge
The first 200 terms are:
0,1,2,3,4,5,6,7,8,9,10,2,12,13,14,15,16,17,18,19,20,21,4,23,24,25,26,27,28,29,30,31,32,6,34,35,36,37,38,39,40,41,42,43,8,45,46,47,48,49,50,51,52,53,54,10,56,57,58,59,60,61,62,63,64,65,12,67,68,69,70,71,72,73,74,75,76,14,78,79,80,81,82,83,84,85,86,87,16,89,90,91,92,93,94,95,96,97,98,18,10,101,102,103,104,105,106,107,108,109,20,21,4,23,24,25,26,27,28,29,120,121,14,123,124,125,126,127,128,129,130,131,132,16,134,135,136,137,138,139,140,141,142,143,18,145,146,147,148,149,150,151,152,153,154,20,156,157,158,159,160,161,162,163,164,165,4,167,168,169,170,171,172,173,174,175,176,24,178,179,180,181,182,183,184,185,186,187,26,189,190,191,192,193,194,195,196,197,198,28
Your task is to generate that sequence, either
- given $n$, return the $n^text{th}$ number in that sequence,
- given $n$, return the first $n$ numbers in that sequence
- or generate the sequence indefinitely.
You may choose your submission to use either $0$- or $1$-indexing, but please specify which.
Test cases
You may use the above given terms, however here are some larger ones:
222 -> 42
1633 -> 4
4488 -> 816
15519 -> 2019
19988 -> 2816
99999 -> 18189
119988 -> 21816
100001 -> 101
999999 -> 181818
code-golf sequence integer
code-golf sequence integer
edited yesterday
asked yesterday
BMO
11.5k22187
11.5k22187
add a comment |
add a comment |
17 Answers
17
active
oldest
votes
Python 3, 128 bytes
def t(z):j=z and(z[0]==z[1:2])+1;return[str(int(z[0])*j),*t(z[j:])]if j else''
def c(n):r="".join(t(n));return r!=n and c(r)or r
Try it online!
New contributor
5
Welcome to PPCG! Nice first post!
– Riker
22 hours ago
108 bytes
– Jo King
14 hours ago
add a comment |
Python 2, 97 96 93 bytes
def f(n):r=re.sub(r'(.)1',lambda m:`int(m.group(1))*2`,n);return r!=n and f(r)or r
import re
Try it online!
Non regex version:
Python 2, 133 130 122 112 98 bytes
def f(n):
r='';s=n
while s:a=1+(s[0]==s[1:2]);r+=`int(s[0])*a`;s=s[a:]
return r!=n and f(r)or r
Try it online!
add a comment |
Jelly, 11 bytes
DŒg+2/€FVµ¡
This is an unnecessarily slow, full program.
Try it online!
Alternate version, 12 bytes
DŒg+2/€FVµƬṪ
One byte longer, but much faster. Works as a program or a function.
Try it online!
How it works
DŒg+2/€FVµƬṪ Main link. Argument: n (integer)
µ Combine the previous links into a chain. Begin a new one.
D Decimal; yield n's digit array in base 10.
Œg Group adjacent, identical digits into subarrays.
+2/€ Map non-overlapping, pairwise sum over the subarrays.
If there is an odd number of digits in a subarray, the
last digit will remain untouched.
F Flatten; dump all sums and digits into a single array.
V Eval; turn the result into an integer.
Ƭ Execute the chain 'til the results are no longer unique.
Return all unique results.
Ṫ Tail; extract the last result.
The 11-byte version does the same, except it calls the link n times for input n, instead of calling it until a fixed point is reached.
It's not unnecessary if it saves 1 byte :-)
– Luis Mendo
15 hours ago
add a comment |
Haskell, 70 bytes
until((==)=<<f)f
f(a:b:c)|a==b=show(2*read[a])++f c|1<2=a:f(b:c)
f a=a
Input is taken as a string.
Try it online!
It doesn't save you anything so far, but with the same length you can replace the second clause with|x<-b:c=a:f x
or evenf(a:c)=a:f c
, in case one or the other could actually lead to an improvement:)
– flawr
19 hours ago
add a comment |
JavaScript, 48 47 bytes
Input and output as strings. Returns the nth
term of the sequence.
f=s=>s-(s=s.replace(/(.)1/g,x=>x[0]*2))?f(s):s
Try it online
1 byte saved thanks to Arnauld
x[0]*2
->x/5.5
– tsh
9 hours ago
add a comment |
Perl 6, 37 bytes
{($_,{S:g[(d)$0]=2*$0}...*==*)[*-1]}
Try it online!
This is a function which generates the nth term of the sequence, given n as its argument.
($_, { ... } ... * == *)
is the sequence of successive changes to the input number, generated by the bracketed expression (a simple regex substitution) and stopping when * == *
, that is, when the last two numbers in the sequence are equal. Then the [*-1]
takes just the final element of that sequence as the return value.
You can save bytes by removing the==*
and replacing the*-1
with$_
, since there are always less thann
replacements for a numbern
. 33 bytes
– Jo King
15 hours ago
add a comment |
C# (.NET Core), 231, 203, 200, 196 bytes
EDIT: Function is now at 185 bytes plus 18 for using System.Linq;
Thanks to BMO (for 1>0 being equal to true plus newline removal) and Mr. XCoder (for f=!f statements)!
EDIT2: Down to 182 bytes plus 18 for using System.Linq
thanks to dana for sharing a few golf tips!
EDIT3: Thanks to Embodiment of Ignorance for the int -> var, removal of short circuit && -> &, and changing up ToArray -> ToList! (178 bytes + 18 using)
p=>{var f=1>0;while(f){var t=p.Select(n=>n-48).ToList();p="";f=!f;var h=t.Count;for(var j=0;j<h;j++){if(j<h-1&t[j]==t[1+j]){p+=t[j]+t[1+j++];f=!f;continue;}p+=t[j];}};return p;};
Try it online!
1
I think you can save some more bytes by replacing int with var, and ToArray() to ToList()
– Embodiment of Ignorance
20 hours ago
1
tio.run/##TZAxa8MwEIV3/…
– Embodiment of Ignorance
20 hours ago
add a comment |
Retina, 16 bytes
+`(.)1
$.(2*$1*
Try it online! Link includes test cases. Explanation:
+`
Repeat until the input stops changing.
(.)1
Replace pairs of adjacent digits...
$.(2*$1*
... with twice the digit. ($1*
generates a string of $1
_
s, 2*
duplicates that, and $.(
takes the length. Actually, the Retina engine is cleverer than that and just doubles $1
.)
add a comment |
Perl 5 -p
, 21 bytes
s/(.)1/$1*2/ge&&redo
Try it online!
add a comment |
C# (Visual C# Interactive Compiler), 112+33 bytes
/u:System.Text.RegularExpressions
string x(string p)=>Regex.IsMatch(p,@"(d)1")?x(Regex.Replace(p,@"(d)1",m=>((m.Groups[0]+"")[0]-48)*2+"")):p;
Try it online!
Regex/Recursion-based solution. Thanks to @dana for -11 bytes
using System.Text.RegularExpressions;
//Recursive statement taking a string as parameter
string x(string p)=>
//Does the number have any pairs?
Regex.IsMatch(p,@"(d)1")?
//If yes, then call this method again with this new string
x(
//Find every pair in the string
Regex.Replace(p,@"(d)1",
//And replace them with the pair's sum
m=>((m.Groups[0]+"")[0]-48)*2+""))
//Else just return the string as is
:p;
95+34 - 33 + 1 for the extra space you need in the commandline args, iirc
– ASCII-only
10 hours ago
Recursive anonymous functions have to be defined first, and the definition is in included in the byte count.
– Embodiment of Ignorance
10 hours ago
Oh, it's recursive
– ASCII-only
9 hours ago
add a comment |
Groovy, 63 bytes
{s->r=/(d)1/
while(s=~r)s=s.replaceAll(r){(it[1]as int)*2}
s}
Try it online!
add a comment |
05AB1E, 11 bytes
Δγε2ôSO}˜J
Try it online or verify all test cases.
Explanation:
Δ # Continue until the (implicit) input no longer changes:
γ # Split the integer in chunks of the same adjacent digits
# i.e. 199999889 → [1,99999,88,9]
ε } # Map each to:
2ô # Split it into parts of size 2
# i.e. 99999 → [99,99,9]
€S # Split each part into digits
# i.e. [99,99,9] → [[9,9],[9,9],[9]]
O # And take the sum of each part
# i.e. [[9,9],[9,9],[9]] → [18,18,9]
˜ # Flatten the list
# i.e. [[1],[18,18,9],[16],[9]] → [1,18,18,9,16,9]
J # Join everything together
# i.e. [1,18,18,9,16,9] → 118189169
# (And output the result implicitly at the end)
# i.e. output = 28189169
add a comment |
Japt v2.0a0 -h
, 15 14 bytes
Returns the nth
term of the sequence.
Æ=s_r/(.)1/ÏÑ
Try it
This should work for 10 bytes but there seems to be a bug in Japt's recursive replacement method.
e/(.)1/ÏÑ
add a comment |
Clean, 118 bytes
import StdEnv,Data.List
$[a,b:t]|a==b=[1,(a*2)rem 10]%(1-a/5,1)++ $t=[a: $[b:t]]
$l=l
limit o iterate$o map digitToInt
Try it online!
Takes the first repeated value (limit
) from the infinite list of applications (iterate
) of a lambda performing a single step of the collapsing process. Input taken as a [Char]
.
add a comment |
Red, 84 83 80 bytes
func[n][if parse s: form n[to some change[copy d skip d](2 * do d)to end][f s]s]
Try it online!
Returns the nth
term of the sequence.
Explanation:
Red
f: func [ n ] [
if parse s: form n [ ; parse the input converted to a string
to some change [ ; find and change one or more
copy d skip ; digit (in fact any character, no predefined character classes)
d ; followed by itself
] (2 * do d) ; with its doubled numeric value
to end ; go to the end of the string
] [ f s ] ; call the function with the altered string if parse returned true
s ; finally return the string
]
add a comment |
Wolfram Language 108 bytes
ToExpression[""<>ToString/@Total/@Flatten[Partition[#,UpTo@2]&/@Split@IntegerDigits@#,1]]&~FixedPoint~#&
Explanation
IntegerDigits
transforms the input number into a list of its digits.
Split
groups consecutive repeated digits.
Partition[#, UpTo@2]&/@
breaks runs of like digits into lists of, at most, lengths of 2.
Flatten[...,1]
eliminates occasional overly-nested braces--e.g., {{2,2}} becomes {2,2}
Total/@
sums totals of paired digits. Isolated digits need not be summed.
ToString
converts the totals (and isolated digits) to strings.
""<>
joins all the strings in the list.
ToExpression
converts the outcome to an integer.
...~FixedPoint~#&
applies the function until the result ceases to change.
add a comment |
C# (Visual C# Interactive Compiler), 113 bytes
s=>{var t=s;do{s=t;t="";for(int i=0;i<s.Length;)t+=(s[i]-48)*(s[i++]!=(s+0)[i]?1:2*++i/i);}while(t!=s);return t;}
Try it online!
HUGE credit to @ASCIIOnly for golfing ~30 ;) At first we were both posting updates simultaneously, but at some point he clearly went to town!
Less golfed code...
// s is the input as a string
s=>{
// t is another string used
// to hold intermediate results
var t=s;
// the algorithm repeatedly
// processes s and saves the
// result to t
do{
// copy the last result to s
// and blank out t
s=t;
t="";
// iterate over s
for(int i=0;i<s.Length;)
// append either 1 or 2 times
// the current digit to t
t+=(s[i]-48)*
// compare the current digit
// to the next digit. to prevent
// an out-of-bounds exception,
// append a 0 to s which either
// gets ignored or collapses
// to 0
(s[i++]!=(s+0)[i]
// if they are different, then
// the multiplier is 1
?1
// if they are the same, then
// the multiplier is 2, and we
// have to increment i
:2*++i/i);
}
// continue this until the input
// and output are the same
while(t!=s);
return t;
}
139
– ASCII-only
10 hours ago
134
– ASCII-only
10 hours ago
@ASCIIOnly - Good move :)(s[i++]-48)*2
=>s[i++]*2-96
– dana
10 hours ago
131
– ASCII-only
9 hours ago
1
114
– ASCII-only
9 hours ago
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178256%2fcollapsing-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
Python 3, 128 bytes
def t(z):j=z and(z[0]==z[1:2])+1;return[str(int(z[0])*j),*t(z[j:])]if j else''
def c(n):r="".join(t(n));return r!=n and c(r)or r
Try it online!
New contributor
5
Welcome to PPCG! Nice first post!
– Riker
22 hours ago
108 bytes
– Jo King
14 hours ago
add a comment |
Python 3, 128 bytes
def t(z):j=z and(z[0]==z[1:2])+1;return[str(int(z[0])*j),*t(z[j:])]if j else''
def c(n):r="".join(t(n));return r!=n and c(r)or r
Try it online!
New contributor
5
Welcome to PPCG! Nice first post!
– Riker
22 hours ago
108 bytes
– Jo King
14 hours ago
add a comment |
Python 3, 128 bytes
def t(z):j=z and(z[0]==z[1:2])+1;return[str(int(z[0])*j),*t(z[j:])]if j else''
def c(n):r="".join(t(n));return r!=n and c(r)or r
Try it online!
New contributor
Python 3, 128 bytes
def t(z):j=z and(z[0]==z[1:2])+1;return[str(int(z[0])*j),*t(z[j:])]if j else''
def c(n):r="".join(t(n));return r!=n and c(r)or r
Try it online!
New contributor
New contributor
answered 22 hours ago
anon3128
611
611
New contributor
New contributor
5
Welcome to PPCG! Nice first post!
– Riker
22 hours ago
108 bytes
– Jo King
14 hours ago
add a comment |
5
Welcome to PPCG! Nice first post!
– Riker
22 hours ago
108 bytes
– Jo King
14 hours ago
5
5
Welcome to PPCG! Nice first post!
– Riker
22 hours ago
Welcome to PPCG! Nice first post!
– Riker
22 hours ago
108 bytes
– Jo King
14 hours ago
108 bytes
– Jo King
14 hours ago
add a comment |
Python 2, 97 96 93 bytes
def f(n):r=re.sub(r'(.)1',lambda m:`int(m.group(1))*2`,n);return r!=n and f(r)or r
import re
Try it online!
Non regex version:
Python 2, 133 130 122 112 98 bytes
def f(n):
r='';s=n
while s:a=1+(s[0]==s[1:2]);r+=`int(s[0])*a`;s=s[a:]
return r!=n and f(r)or r
Try it online!
add a comment |
Python 2, 97 96 93 bytes
def f(n):r=re.sub(r'(.)1',lambda m:`int(m.group(1))*2`,n);return r!=n and f(r)or r
import re
Try it online!
Non regex version:
Python 2, 133 130 122 112 98 bytes
def f(n):
r='';s=n
while s:a=1+(s[0]==s[1:2]);r+=`int(s[0])*a`;s=s[a:]
return r!=n and f(r)or r
Try it online!
add a comment |
Python 2, 97 96 93 bytes
def f(n):r=re.sub(r'(.)1',lambda m:`int(m.group(1))*2`,n);return r!=n and f(r)or r
import re
Try it online!
Non regex version:
Python 2, 133 130 122 112 98 bytes
def f(n):
r='';s=n
while s:a=1+(s[0]==s[1:2]);r+=`int(s[0])*a`;s=s[a:]
return r!=n and f(r)or r
Try it online!
Python 2, 97 96 93 bytes
def f(n):r=re.sub(r'(.)1',lambda m:`int(m.group(1))*2`,n);return r!=n and f(r)or r
import re
Try it online!
Non regex version:
Python 2, 133 130 122 112 98 bytes
def f(n):
r='';s=n
while s:a=1+(s[0]==s[1:2]);r+=`int(s[0])*a`;s=s[a:]
return r!=n and f(r)or r
Try it online!
edited yesterday
answered yesterday
TFeld
14.2k21240
14.2k21240
add a comment |
add a comment |
Jelly, 11 bytes
DŒg+2/€FVµ¡
This is an unnecessarily slow, full program.
Try it online!
Alternate version, 12 bytes
DŒg+2/€FVµƬṪ
One byte longer, but much faster. Works as a program or a function.
Try it online!
How it works
DŒg+2/€FVµƬṪ Main link. Argument: n (integer)
µ Combine the previous links into a chain. Begin a new one.
D Decimal; yield n's digit array in base 10.
Œg Group adjacent, identical digits into subarrays.
+2/€ Map non-overlapping, pairwise sum over the subarrays.
If there is an odd number of digits in a subarray, the
last digit will remain untouched.
F Flatten; dump all sums and digits into a single array.
V Eval; turn the result into an integer.
Ƭ Execute the chain 'til the results are no longer unique.
Return all unique results.
Ṫ Tail; extract the last result.
The 11-byte version does the same, except it calls the link n times for input n, instead of calling it until a fixed point is reached.
It's not unnecessary if it saves 1 byte :-)
– Luis Mendo
15 hours ago
add a comment |
Jelly, 11 bytes
DŒg+2/€FVµ¡
This is an unnecessarily slow, full program.
Try it online!
Alternate version, 12 bytes
DŒg+2/€FVµƬṪ
One byte longer, but much faster. Works as a program or a function.
Try it online!
How it works
DŒg+2/€FVµƬṪ Main link. Argument: n (integer)
µ Combine the previous links into a chain. Begin a new one.
D Decimal; yield n's digit array in base 10.
Œg Group adjacent, identical digits into subarrays.
+2/€ Map non-overlapping, pairwise sum over the subarrays.
If there is an odd number of digits in a subarray, the
last digit will remain untouched.
F Flatten; dump all sums and digits into a single array.
V Eval; turn the result into an integer.
Ƭ Execute the chain 'til the results are no longer unique.
Return all unique results.
Ṫ Tail; extract the last result.
The 11-byte version does the same, except it calls the link n times for input n, instead of calling it until a fixed point is reached.
It's not unnecessary if it saves 1 byte :-)
– Luis Mendo
15 hours ago
add a comment |
Jelly, 11 bytes
DŒg+2/€FVµ¡
This is an unnecessarily slow, full program.
Try it online!
Alternate version, 12 bytes
DŒg+2/€FVµƬṪ
One byte longer, but much faster. Works as a program or a function.
Try it online!
How it works
DŒg+2/€FVµƬṪ Main link. Argument: n (integer)
µ Combine the previous links into a chain. Begin a new one.
D Decimal; yield n's digit array in base 10.
Œg Group adjacent, identical digits into subarrays.
+2/€ Map non-overlapping, pairwise sum over the subarrays.
If there is an odd number of digits in a subarray, the
last digit will remain untouched.
F Flatten; dump all sums and digits into a single array.
V Eval; turn the result into an integer.
Ƭ Execute the chain 'til the results are no longer unique.
Return all unique results.
Ṫ Tail; extract the last result.
The 11-byte version does the same, except it calls the link n times for input n, instead of calling it until a fixed point is reached.
Jelly, 11 bytes
DŒg+2/€FVµ¡
This is an unnecessarily slow, full program.
Try it online!
Alternate version, 12 bytes
DŒg+2/€FVµƬṪ
One byte longer, but much faster. Works as a program or a function.
Try it online!
How it works
DŒg+2/€FVµƬṪ Main link. Argument: n (integer)
µ Combine the previous links into a chain. Begin a new one.
D Decimal; yield n's digit array in base 10.
Œg Group adjacent, identical digits into subarrays.
+2/€ Map non-overlapping, pairwise sum over the subarrays.
If there is an odd number of digits in a subarray, the
last digit will remain untouched.
F Flatten; dump all sums and digits into a single array.
V Eval; turn the result into an integer.
Ƭ Execute the chain 'til the results are no longer unique.
Return all unique results.
Ṫ Tail; extract the last result.
The 11-byte version does the same, except it calls the link n times for input n, instead of calling it until a fixed point is reached.
edited yesterday
answered yesterday
Dennis♦
186k32296735
186k32296735
It's not unnecessary if it saves 1 byte :-)
– Luis Mendo
15 hours ago
add a comment |
It's not unnecessary if it saves 1 byte :-)
– Luis Mendo
15 hours ago
It's not unnecessary if it saves 1 byte :-)
– Luis Mendo
15 hours ago
It's not unnecessary if it saves 1 byte :-)
– Luis Mendo
15 hours ago
add a comment |
Haskell, 70 bytes
until((==)=<<f)f
f(a:b:c)|a==b=show(2*read[a])++f c|1<2=a:f(b:c)
f a=a
Input is taken as a string.
Try it online!
It doesn't save you anything so far, but with the same length you can replace the second clause with|x<-b:c=a:f x
or evenf(a:c)=a:f c
, in case one or the other could actually lead to an improvement:)
– flawr
19 hours ago
add a comment |
Haskell, 70 bytes
until((==)=<<f)f
f(a:b:c)|a==b=show(2*read[a])++f c|1<2=a:f(b:c)
f a=a
Input is taken as a string.
Try it online!
It doesn't save you anything so far, but with the same length you can replace the second clause with|x<-b:c=a:f x
or evenf(a:c)=a:f c
, in case one or the other could actually lead to an improvement:)
– flawr
19 hours ago
add a comment |
Haskell, 70 bytes
until((==)=<<f)f
f(a:b:c)|a==b=show(2*read[a])++f c|1<2=a:f(b:c)
f a=a
Input is taken as a string.
Try it online!
Haskell, 70 bytes
until((==)=<<f)f
f(a:b:c)|a==b=show(2*read[a])++f c|1<2=a:f(b:c)
f a=a
Input is taken as a string.
Try it online!
answered 23 hours ago
nimi
31.4k32185
31.4k32185
It doesn't save you anything so far, but with the same length you can replace the second clause with|x<-b:c=a:f x
or evenf(a:c)=a:f c
, in case one or the other could actually lead to an improvement:)
– flawr
19 hours ago
add a comment |
It doesn't save you anything so far, but with the same length you can replace the second clause with|x<-b:c=a:f x
or evenf(a:c)=a:f c
, in case one or the other could actually lead to an improvement:)
– flawr
19 hours ago
It doesn't save you anything so far, but with the same length you can replace the second clause with
|x<-b:c=a:f x
or even f(a:c)=a:f c
, in case one or the other could actually lead to an improvement:)– flawr
19 hours ago
It doesn't save you anything so far, but with the same length you can replace the second clause with
|x<-b:c=a:f x
or even f(a:c)=a:f c
, in case one or the other could actually lead to an improvement:)– flawr
19 hours ago
add a comment |
JavaScript, 48 47 bytes
Input and output as strings. Returns the nth
term of the sequence.
f=s=>s-(s=s.replace(/(.)1/g,x=>x[0]*2))?f(s):s
Try it online
1 byte saved thanks to Arnauld
x[0]*2
->x/5.5
– tsh
9 hours ago
add a comment |
JavaScript, 48 47 bytes
Input and output as strings. Returns the nth
term of the sequence.
f=s=>s-(s=s.replace(/(.)1/g,x=>x[0]*2))?f(s):s
Try it online
1 byte saved thanks to Arnauld
x[0]*2
->x/5.5
– tsh
9 hours ago
add a comment |
JavaScript, 48 47 bytes
Input and output as strings. Returns the nth
term of the sequence.
f=s=>s-(s=s.replace(/(.)1/g,x=>x[0]*2))?f(s):s
Try it online
1 byte saved thanks to Arnauld
JavaScript, 48 47 bytes
Input and output as strings. Returns the nth
term of the sequence.
f=s=>s-(s=s.replace(/(.)1/g,x=>x[0]*2))?f(s):s
Try it online
1 byte saved thanks to Arnauld
edited 23 hours ago
answered yesterday
Shaggy
19k21666
19k21666
x[0]*2
->x/5.5
– tsh
9 hours ago
add a comment |
x[0]*2
->x/5.5
– tsh
9 hours ago
x[0]*2
-> x/5.5
– tsh
9 hours ago
x[0]*2
-> x/5.5
– tsh
9 hours ago
add a comment |
Perl 6, 37 bytes
{($_,{S:g[(d)$0]=2*$0}...*==*)[*-1]}
Try it online!
This is a function which generates the nth term of the sequence, given n as its argument.
($_, { ... } ... * == *)
is the sequence of successive changes to the input number, generated by the bracketed expression (a simple regex substitution) and stopping when * == *
, that is, when the last two numbers in the sequence are equal. Then the [*-1]
takes just the final element of that sequence as the return value.
You can save bytes by removing the==*
and replacing the*-1
with$_
, since there are always less thann
replacements for a numbern
. 33 bytes
– Jo King
15 hours ago
add a comment |
Perl 6, 37 bytes
{($_,{S:g[(d)$0]=2*$0}...*==*)[*-1]}
Try it online!
This is a function which generates the nth term of the sequence, given n as its argument.
($_, { ... } ... * == *)
is the sequence of successive changes to the input number, generated by the bracketed expression (a simple regex substitution) and stopping when * == *
, that is, when the last two numbers in the sequence are equal. Then the [*-1]
takes just the final element of that sequence as the return value.
You can save bytes by removing the==*
and replacing the*-1
with$_
, since there are always less thann
replacements for a numbern
. 33 bytes
– Jo King
15 hours ago
add a comment |
Perl 6, 37 bytes
{($_,{S:g[(d)$0]=2*$0}...*==*)[*-1]}
Try it online!
This is a function which generates the nth term of the sequence, given n as its argument.
($_, { ... } ... * == *)
is the sequence of successive changes to the input number, generated by the bracketed expression (a simple regex substitution) and stopping when * == *
, that is, when the last two numbers in the sequence are equal. Then the [*-1]
takes just the final element of that sequence as the return value.
Perl 6, 37 bytes
{($_,{S:g[(d)$0]=2*$0}...*==*)[*-1]}
Try it online!
This is a function which generates the nth term of the sequence, given n as its argument.
($_, { ... } ... * == *)
is the sequence of successive changes to the input number, generated by the bracketed expression (a simple regex substitution) and stopping when * == *
, that is, when the last two numbers in the sequence are equal. Then the [*-1]
takes just the final element of that sequence as the return value.
answered 21 hours ago
Sean
3,45637
3,45637
You can save bytes by removing the==*
and replacing the*-1
with$_
, since there are always less thann
replacements for a numbern
. 33 bytes
– Jo King
15 hours ago
add a comment |
You can save bytes by removing the==*
and replacing the*-1
with$_
, since there are always less thann
replacements for a numbern
. 33 bytes
– Jo King
15 hours ago
You can save bytes by removing the
==*
and replacing the *-1
with $_
, since there are always less than n
replacements for a number n
. 33 bytes– Jo King
15 hours ago
You can save bytes by removing the
==*
and replacing the *-1
with $_
, since there are always less than n
replacements for a number n
. 33 bytes– Jo King
15 hours ago
add a comment |
C# (.NET Core), 231, 203, 200, 196 bytes
EDIT: Function is now at 185 bytes plus 18 for using System.Linq;
Thanks to BMO (for 1>0 being equal to true plus newline removal) and Mr. XCoder (for f=!f statements)!
EDIT2: Down to 182 bytes plus 18 for using System.Linq
thanks to dana for sharing a few golf tips!
EDIT3: Thanks to Embodiment of Ignorance for the int -> var, removal of short circuit && -> &, and changing up ToArray -> ToList! (178 bytes + 18 using)
p=>{var f=1>0;while(f){var t=p.Select(n=>n-48).ToList();p="";f=!f;var h=t.Count;for(var j=0;j<h;j++){if(j<h-1&t[j]==t[1+j]){p+=t[j]+t[1+j++];f=!f;continue;}p+=t[j];}};return p;};
Try it online!
1
I think you can save some more bytes by replacing int with var, and ToArray() to ToList()
– Embodiment of Ignorance
20 hours ago
1
tio.run/##TZAxa8MwEIV3/…
– Embodiment of Ignorance
20 hours ago
add a comment |
C# (.NET Core), 231, 203, 200, 196 bytes
EDIT: Function is now at 185 bytes plus 18 for using System.Linq;
Thanks to BMO (for 1>0 being equal to true plus newline removal) and Mr. XCoder (for f=!f statements)!
EDIT2: Down to 182 bytes plus 18 for using System.Linq
thanks to dana for sharing a few golf tips!
EDIT3: Thanks to Embodiment of Ignorance for the int -> var, removal of short circuit && -> &, and changing up ToArray -> ToList! (178 bytes + 18 using)
p=>{var f=1>0;while(f){var t=p.Select(n=>n-48).ToList();p="";f=!f;var h=t.Count;for(var j=0;j<h;j++){if(j<h-1&t[j]==t[1+j]){p+=t[j]+t[1+j++];f=!f;continue;}p+=t[j];}};return p;};
Try it online!
1
I think you can save some more bytes by replacing int with var, and ToArray() to ToList()
– Embodiment of Ignorance
20 hours ago
1
tio.run/##TZAxa8MwEIV3/…
– Embodiment of Ignorance
20 hours ago
add a comment |
C# (.NET Core), 231, 203, 200, 196 bytes
EDIT: Function is now at 185 bytes plus 18 for using System.Linq;
Thanks to BMO (for 1>0 being equal to true plus newline removal) and Mr. XCoder (for f=!f statements)!
EDIT2: Down to 182 bytes plus 18 for using System.Linq
thanks to dana for sharing a few golf tips!
EDIT3: Thanks to Embodiment of Ignorance for the int -> var, removal of short circuit && -> &, and changing up ToArray -> ToList! (178 bytes + 18 using)
p=>{var f=1>0;while(f){var t=p.Select(n=>n-48).ToList();p="";f=!f;var h=t.Count;for(var j=0;j<h;j++){if(j<h-1&t[j]==t[1+j]){p+=t[j]+t[1+j++];f=!f;continue;}p+=t[j];}};return p;};
Try it online!
C# (.NET Core), 231, 203, 200, 196 bytes
EDIT: Function is now at 185 bytes plus 18 for using System.Linq;
Thanks to BMO (for 1>0 being equal to true plus newline removal) and Mr. XCoder (for f=!f statements)!
EDIT2: Down to 182 bytes plus 18 for using System.Linq
thanks to dana for sharing a few golf tips!
EDIT3: Thanks to Embodiment of Ignorance for the int -> var, removal of short circuit && -> &, and changing up ToArray -> ToList! (178 bytes + 18 using)
p=>{var f=1>0;while(f){var t=p.Select(n=>n-48).ToList();p="";f=!f;var h=t.Count;for(var j=0;j<h;j++){if(j<h-1&t[j]==t[1+j]){p+=t[j]+t[1+j++];f=!f;continue;}p+=t[j];}};return p;};
Try it online!
edited 20 hours ago
answered yesterday
Destroigo
613
613
1
I think you can save some more bytes by replacing int with var, and ToArray() to ToList()
– Embodiment of Ignorance
20 hours ago
1
tio.run/##TZAxa8MwEIV3/…
– Embodiment of Ignorance
20 hours ago
add a comment |
1
I think you can save some more bytes by replacing int with var, and ToArray() to ToList()
– Embodiment of Ignorance
20 hours ago
1
tio.run/##TZAxa8MwEIV3/…
– Embodiment of Ignorance
20 hours ago
1
1
I think you can save some more bytes by replacing int with var, and ToArray() to ToList()
– Embodiment of Ignorance
20 hours ago
I think you can save some more bytes by replacing int with var, and ToArray() to ToList()
– Embodiment of Ignorance
20 hours ago
1
1
tio.run/##TZAxa8MwEIV3/…
– Embodiment of Ignorance
20 hours ago
tio.run/##TZAxa8MwEIV3/…
– Embodiment of Ignorance
20 hours ago
add a comment |
Retina, 16 bytes
+`(.)1
$.(2*$1*
Try it online! Link includes test cases. Explanation:
+`
Repeat until the input stops changing.
(.)1
Replace pairs of adjacent digits...
$.(2*$1*
... with twice the digit. ($1*
generates a string of $1
_
s, 2*
duplicates that, and $.(
takes the length. Actually, the Retina engine is cleverer than that and just doubles $1
.)
add a comment |
Retina, 16 bytes
+`(.)1
$.(2*$1*
Try it online! Link includes test cases. Explanation:
+`
Repeat until the input stops changing.
(.)1
Replace pairs of adjacent digits...
$.(2*$1*
... with twice the digit. ($1*
generates a string of $1
_
s, 2*
duplicates that, and $.(
takes the length. Actually, the Retina engine is cleverer than that and just doubles $1
.)
add a comment |
Retina, 16 bytes
+`(.)1
$.(2*$1*
Try it online! Link includes test cases. Explanation:
+`
Repeat until the input stops changing.
(.)1
Replace pairs of adjacent digits...
$.(2*$1*
... with twice the digit. ($1*
generates a string of $1
_
s, 2*
duplicates that, and $.(
takes the length. Actually, the Retina engine is cleverer than that and just doubles $1
.)
Retina, 16 bytes
+`(.)1
$.(2*$1*
Try it online! Link includes test cases. Explanation:
+`
Repeat until the input stops changing.
(.)1
Replace pairs of adjacent digits...
$.(2*$1*
... with twice the digit. ($1*
generates a string of $1
_
s, 2*
duplicates that, and $.(
takes the length. Actually, the Retina engine is cleverer than that and just doubles $1
.)
answered 19 hours ago
Neil
79.4k744177
79.4k744177
add a comment |
add a comment |
Perl 5 -p
, 21 bytes
s/(.)1/$1*2/ge&&redo
Try it online!
add a comment |
Perl 5 -p
, 21 bytes
s/(.)1/$1*2/ge&&redo
Try it online!
add a comment |
Perl 5 -p
, 21 bytes
s/(.)1/$1*2/ge&&redo
Try it online!
Perl 5 -p
, 21 bytes
s/(.)1/$1*2/ge&&redo
Try it online!
answered 21 hours ago
Xcali
5,188520
5,188520
add a comment |
add a comment |
C# (Visual C# Interactive Compiler), 112+33 bytes
/u:System.Text.RegularExpressions
string x(string p)=>Regex.IsMatch(p,@"(d)1")?x(Regex.Replace(p,@"(d)1",m=>((m.Groups[0]+"")[0]-48)*2+"")):p;
Try it online!
Regex/Recursion-based solution. Thanks to @dana for -11 bytes
using System.Text.RegularExpressions;
//Recursive statement taking a string as parameter
string x(string p)=>
//Does the number have any pairs?
Regex.IsMatch(p,@"(d)1")?
//If yes, then call this method again with this new string
x(
//Find every pair in the string
Regex.Replace(p,@"(d)1",
//And replace them with the pair's sum
m=>((m.Groups[0]+"")[0]-48)*2+""))
//Else just return the string as is
:p;
95+34 - 33 + 1 for the extra space you need in the commandline args, iirc
– ASCII-only
10 hours ago
Recursive anonymous functions have to be defined first, and the definition is in included in the byte count.
– Embodiment of Ignorance
10 hours ago
Oh, it's recursive
– ASCII-only
9 hours ago
add a comment |
C# (Visual C# Interactive Compiler), 112+33 bytes
/u:System.Text.RegularExpressions
string x(string p)=>Regex.IsMatch(p,@"(d)1")?x(Regex.Replace(p,@"(d)1",m=>((m.Groups[0]+"")[0]-48)*2+"")):p;
Try it online!
Regex/Recursion-based solution. Thanks to @dana for -11 bytes
using System.Text.RegularExpressions;
//Recursive statement taking a string as parameter
string x(string p)=>
//Does the number have any pairs?
Regex.IsMatch(p,@"(d)1")?
//If yes, then call this method again with this new string
x(
//Find every pair in the string
Regex.Replace(p,@"(d)1",
//And replace them with the pair's sum
m=>((m.Groups[0]+"")[0]-48)*2+""))
//Else just return the string as is
:p;
95+34 - 33 + 1 for the extra space you need in the commandline args, iirc
– ASCII-only
10 hours ago
Recursive anonymous functions have to be defined first, and the definition is in included in the byte count.
– Embodiment of Ignorance
10 hours ago
Oh, it's recursive
– ASCII-only
9 hours ago
add a comment |
C# (Visual C# Interactive Compiler), 112+33 bytes
/u:System.Text.RegularExpressions
string x(string p)=>Regex.IsMatch(p,@"(d)1")?x(Regex.Replace(p,@"(d)1",m=>((m.Groups[0]+"")[0]-48)*2+"")):p;
Try it online!
Regex/Recursion-based solution. Thanks to @dana for -11 bytes
using System.Text.RegularExpressions;
//Recursive statement taking a string as parameter
string x(string p)=>
//Does the number have any pairs?
Regex.IsMatch(p,@"(d)1")?
//If yes, then call this method again with this new string
x(
//Find every pair in the string
Regex.Replace(p,@"(d)1",
//And replace them with the pair's sum
m=>((m.Groups[0]+"")[0]-48)*2+""))
//Else just return the string as is
:p;
C# (Visual C# Interactive Compiler), 112+33 bytes
/u:System.Text.RegularExpressions
string x(string p)=>Regex.IsMatch(p,@"(d)1")?x(Regex.Replace(p,@"(d)1",m=>((m.Groups[0]+"")[0]-48)*2+"")):p;
Try it online!
Regex/Recursion-based solution. Thanks to @dana for -11 bytes
using System.Text.RegularExpressions;
//Recursive statement taking a string as parameter
string x(string p)=>
//Does the number have any pairs?
Regex.IsMatch(p,@"(d)1")?
//If yes, then call this method again with this new string
x(
//Find every pair in the string
Regex.Replace(p,@"(d)1",
//And replace them with the pair's sum
m=>((m.Groups[0]+"")[0]-48)*2+""))
//Else just return the string as is
:p;
edited 10 hours ago
answered 21 hours ago
Embodiment of Ignorance
46014
46014
95+34 - 33 + 1 for the extra space you need in the commandline args, iirc
– ASCII-only
10 hours ago
Recursive anonymous functions have to be defined first, and the definition is in included in the byte count.
– Embodiment of Ignorance
10 hours ago
Oh, it's recursive
– ASCII-only
9 hours ago
add a comment |
95+34 - 33 + 1 for the extra space you need in the commandline args, iirc
– ASCII-only
10 hours ago
Recursive anonymous functions have to be defined first, and the definition is in included in the byte count.
– Embodiment of Ignorance
10 hours ago
Oh, it's recursive
– ASCII-only
9 hours ago
95+34 - 33 + 1 for the extra space you need in the commandline args, iirc
– ASCII-only
10 hours ago
95+34 - 33 + 1 for the extra space you need in the commandline args, iirc
– ASCII-only
10 hours ago
Recursive anonymous functions have to be defined first, and the definition is in included in the byte count.
– Embodiment of Ignorance
10 hours ago
Recursive anonymous functions have to be defined first, and the definition is in included in the byte count.
– Embodiment of Ignorance
10 hours ago
Oh, it's recursive
– ASCII-only
9 hours ago
Oh, it's recursive
– ASCII-only
9 hours ago
add a comment |
Groovy, 63 bytes
{s->r=/(d)1/
while(s=~r)s=s.replaceAll(r){(it[1]as int)*2}
s}
Try it online!
add a comment |
Groovy, 63 bytes
{s->r=/(d)1/
while(s=~r)s=s.replaceAll(r){(it[1]as int)*2}
s}
Try it online!
add a comment |
Groovy, 63 bytes
{s->r=/(d)1/
while(s=~r)s=s.replaceAll(r){(it[1]as int)*2}
s}
Try it online!
Groovy, 63 bytes
{s->r=/(d)1/
while(s=~r)s=s.replaceAll(r){(it[1]as int)*2}
s}
Try it online!
answered 8 hours ago
ASCII-only
3,2221136
3,2221136
add a comment |
add a comment |
05AB1E, 11 bytes
Δγε2ôSO}˜J
Try it online or verify all test cases.
Explanation:
Δ # Continue until the (implicit) input no longer changes:
γ # Split the integer in chunks of the same adjacent digits
# i.e. 199999889 → [1,99999,88,9]
ε } # Map each to:
2ô # Split it into parts of size 2
# i.e. 99999 → [99,99,9]
€S # Split each part into digits
# i.e. [99,99,9] → [[9,9],[9,9],[9]]
O # And take the sum of each part
# i.e. [[9,9],[9,9],[9]] → [18,18,9]
˜ # Flatten the list
# i.e. [[1],[18,18,9],[16],[9]] → [1,18,18,9,16,9]
J # Join everything together
# i.e. [1,18,18,9,16,9] → 118189169
# (And output the result implicitly at the end)
# i.e. output = 28189169
add a comment |
05AB1E, 11 bytes
Δγε2ôSO}˜J
Try it online or verify all test cases.
Explanation:
Δ # Continue until the (implicit) input no longer changes:
γ # Split the integer in chunks of the same adjacent digits
# i.e. 199999889 → [1,99999,88,9]
ε } # Map each to:
2ô # Split it into parts of size 2
# i.e. 99999 → [99,99,9]
€S # Split each part into digits
# i.e. [99,99,9] → [[9,9],[9,9],[9]]
O # And take the sum of each part
# i.e. [[9,9],[9,9],[9]] → [18,18,9]
˜ # Flatten the list
# i.e. [[1],[18,18,9],[16],[9]] → [1,18,18,9,16,9]
J # Join everything together
# i.e. [1,18,18,9,16,9] → 118189169
# (And output the result implicitly at the end)
# i.e. output = 28189169
add a comment |
05AB1E, 11 bytes
Δγε2ôSO}˜J
Try it online or verify all test cases.
Explanation:
Δ # Continue until the (implicit) input no longer changes:
γ # Split the integer in chunks of the same adjacent digits
# i.e. 199999889 → [1,99999,88,9]
ε } # Map each to:
2ô # Split it into parts of size 2
# i.e. 99999 → [99,99,9]
€S # Split each part into digits
# i.e. [99,99,9] → [[9,9],[9,9],[9]]
O # And take the sum of each part
# i.e. [[9,9],[9,9],[9]] → [18,18,9]
˜ # Flatten the list
# i.e. [[1],[18,18,9],[16],[9]] → [1,18,18,9,16,9]
J # Join everything together
# i.e. [1,18,18,9,16,9] → 118189169
# (And output the result implicitly at the end)
# i.e. output = 28189169
05AB1E, 11 bytes
Δγε2ôSO}˜J
Try it online or verify all test cases.
Explanation:
Δ # Continue until the (implicit) input no longer changes:
γ # Split the integer in chunks of the same adjacent digits
# i.e. 199999889 → [1,99999,88,9]
ε } # Map each to:
2ô # Split it into parts of size 2
# i.e. 99999 → [99,99,9]
€S # Split each part into digits
# i.e. [99,99,9] → [[9,9],[9,9],[9]]
O # And take the sum of each part
# i.e. [[9,9],[9,9],[9]] → [18,18,9]
˜ # Flatten the list
# i.e. [[1],[18,18,9],[16],[9]] → [1,18,18,9,16,9]
J # Join everything together
# i.e. [1,18,18,9,16,9] → 118189169
# (And output the result implicitly at the end)
# i.e. output = 28189169
answered 5 hours ago
Kevin Cruijssen
35.7k554186
35.7k554186
add a comment |
add a comment |
Japt v2.0a0 -h
, 15 14 bytes
Returns the nth
term of the sequence.
Æ=s_r/(.)1/ÏÑ
Try it
This should work for 10 bytes but there seems to be a bug in Japt's recursive replacement method.
e/(.)1/ÏÑ
add a comment |
Japt v2.0a0 -h
, 15 14 bytes
Returns the nth
term of the sequence.
Æ=s_r/(.)1/ÏÑ
Try it
This should work for 10 bytes but there seems to be a bug in Japt's recursive replacement method.
e/(.)1/ÏÑ
add a comment |
Japt v2.0a0 -h
, 15 14 bytes
Returns the nth
term of the sequence.
Æ=s_r/(.)1/ÏÑ
Try it
This should work for 10 bytes but there seems to be a bug in Japt's recursive replacement method.
e/(.)1/ÏÑ
Japt v2.0a0 -h
, 15 14 bytes
Returns the nth
term of the sequence.
Æ=s_r/(.)1/ÏÑ
Try it
This should work for 10 bytes but there seems to be a bug in Japt's recursive replacement method.
e/(.)1/ÏÑ
edited 21 hours ago
answered yesterday
Shaggy
19k21666
19k21666
add a comment |
add a comment |
Clean, 118 bytes
import StdEnv,Data.List
$[a,b:t]|a==b=[1,(a*2)rem 10]%(1-a/5,1)++ $t=[a: $[b:t]]
$l=l
limit o iterate$o map digitToInt
Try it online!
Takes the first repeated value (limit
) from the infinite list of applications (iterate
) of a lambda performing a single step of the collapsing process. Input taken as a [Char]
.
add a comment |
Clean, 118 bytes
import StdEnv,Data.List
$[a,b:t]|a==b=[1,(a*2)rem 10]%(1-a/5,1)++ $t=[a: $[b:t]]
$l=l
limit o iterate$o map digitToInt
Try it online!
Takes the first repeated value (limit
) from the infinite list of applications (iterate
) of a lambda performing a single step of the collapsing process. Input taken as a [Char]
.
add a comment |
Clean, 118 bytes
import StdEnv,Data.List
$[a,b:t]|a==b=[1,(a*2)rem 10]%(1-a/5,1)++ $t=[a: $[b:t]]
$l=l
limit o iterate$o map digitToInt
Try it online!
Takes the first repeated value (limit
) from the infinite list of applications (iterate
) of a lambda performing a single step of the collapsing process. Input taken as a [Char]
.
Clean, 118 bytes
import StdEnv,Data.List
$[a,b:t]|a==b=[1,(a*2)rem 10]%(1-a/5,1)++ $t=[a: $[b:t]]
$l=l
limit o iterate$o map digitToInt
Try it online!
Takes the first repeated value (limit
) from the infinite list of applications (iterate
) of a lambda performing a single step of the collapsing process. Input taken as a [Char]
.
edited 17 hours ago
answered 19 hours ago
Οurous
6,46211033
6,46211033
add a comment |
add a comment |
Red, 84 83 80 bytes
func[n][if parse s: form n[to some change[copy d skip d](2 * do d)to end][f s]s]
Try it online!
Returns the nth
term of the sequence.
Explanation:
Red
f: func [ n ] [
if parse s: form n [ ; parse the input converted to a string
to some change [ ; find and change one or more
copy d skip ; digit (in fact any character, no predefined character classes)
d ; followed by itself
] (2 * do d) ; with its doubled numeric value
to end ; go to the end of the string
] [ f s ] ; call the function with the altered string if parse returned true
s ; finally return the string
]
add a comment |
Red, 84 83 80 bytes
func[n][if parse s: form n[to some change[copy d skip d](2 * do d)to end][f s]s]
Try it online!
Returns the nth
term of the sequence.
Explanation:
Red
f: func [ n ] [
if parse s: form n [ ; parse the input converted to a string
to some change [ ; find and change one or more
copy d skip ; digit (in fact any character, no predefined character classes)
d ; followed by itself
] (2 * do d) ; with its doubled numeric value
to end ; go to the end of the string
] [ f s ] ; call the function with the altered string if parse returned true
s ; finally return the string
]
add a comment |
Red, 84 83 80 bytes
func[n][if parse s: form n[to some change[copy d skip d](2 * do d)to end][f s]s]
Try it online!
Returns the nth
term of the sequence.
Explanation:
Red
f: func [ n ] [
if parse s: form n [ ; parse the input converted to a string
to some change [ ; find and change one or more
copy d skip ; digit (in fact any character, no predefined character classes)
d ; followed by itself
] (2 * do d) ; with its doubled numeric value
to end ; go to the end of the string
] [ f s ] ; call the function with the altered string if parse returned true
s ; finally return the string
]
Red, 84 83 80 bytes
func[n][if parse s: form n[to some change[copy d skip d](2 * do d)to end][f s]s]
Try it online!
Returns the nth
term of the sequence.
Explanation:
Red
f: func [ n ] [
if parse s: form n [ ; parse the input converted to a string
to some change [ ; find and change one or more
copy d skip ; digit (in fact any character, no predefined character classes)
d ; followed by itself
] (2 * do d) ; with its doubled numeric value
to end ; go to the end of the string
] [ f s ] ; call the function with the altered string if parse returned true
s ; finally return the string
]
edited 5 hours ago
answered 7 hours ago
Galen Ivanov
6,34711032
6,34711032
add a comment |
add a comment |
Wolfram Language 108 bytes
ToExpression[""<>ToString/@Total/@Flatten[Partition[#,UpTo@2]&/@Split@IntegerDigits@#,1]]&~FixedPoint~#&
Explanation
IntegerDigits
transforms the input number into a list of its digits.
Split
groups consecutive repeated digits.
Partition[#, UpTo@2]&/@
breaks runs of like digits into lists of, at most, lengths of 2.
Flatten[...,1]
eliminates occasional overly-nested braces--e.g., {{2,2}} becomes {2,2}
Total/@
sums totals of paired digits. Isolated digits need not be summed.
ToString
converts the totals (and isolated digits) to strings.
""<>
joins all the strings in the list.
ToExpression
converts the outcome to an integer.
...~FixedPoint~#&
applies the function until the result ceases to change.
add a comment |
Wolfram Language 108 bytes
ToExpression[""<>ToString/@Total/@Flatten[Partition[#,UpTo@2]&/@Split@IntegerDigits@#,1]]&~FixedPoint~#&
Explanation
IntegerDigits
transforms the input number into a list of its digits.
Split
groups consecutive repeated digits.
Partition[#, UpTo@2]&/@
breaks runs of like digits into lists of, at most, lengths of 2.
Flatten[...,1]
eliminates occasional overly-nested braces--e.g., {{2,2}} becomes {2,2}
Total/@
sums totals of paired digits. Isolated digits need not be summed.
ToString
converts the totals (and isolated digits) to strings.
""<>
joins all the strings in the list.
ToExpression
converts the outcome to an integer.
...~FixedPoint~#&
applies the function until the result ceases to change.
add a comment |
Wolfram Language 108 bytes
ToExpression[""<>ToString/@Total/@Flatten[Partition[#,UpTo@2]&/@Split@IntegerDigits@#,1]]&~FixedPoint~#&
Explanation
IntegerDigits
transforms the input number into a list of its digits.
Split
groups consecutive repeated digits.
Partition[#, UpTo@2]&/@
breaks runs of like digits into lists of, at most, lengths of 2.
Flatten[...,1]
eliminates occasional overly-nested braces--e.g., {{2,2}} becomes {2,2}
Total/@
sums totals of paired digits. Isolated digits need not be summed.
ToString
converts the totals (and isolated digits) to strings.
""<>
joins all the strings in the list.
ToExpression
converts the outcome to an integer.
...~FixedPoint~#&
applies the function until the result ceases to change.
Wolfram Language 108 bytes
ToExpression[""<>ToString/@Total/@Flatten[Partition[#,UpTo@2]&/@Split@IntegerDigits@#,1]]&~FixedPoint~#&
Explanation
IntegerDigits
transforms the input number into a list of its digits.
Split
groups consecutive repeated digits.
Partition[#, UpTo@2]&/@
breaks runs of like digits into lists of, at most, lengths of 2.
Flatten[...,1]
eliminates occasional overly-nested braces--e.g., {{2,2}} becomes {2,2}
Total/@
sums totals of paired digits. Isolated digits need not be summed.
ToString
converts the totals (and isolated digits) to strings.
""<>
joins all the strings in the list.
ToExpression
converts the outcome to an integer.
...~FixedPoint~#&
applies the function until the result ceases to change.
edited 2 hours ago
answered yesterday
DavidC
23.9k243102
23.9k243102
add a comment |
add a comment |
C# (Visual C# Interactive Compiler), 113 bytes
s=>{var t=s;do{s=t;t="";for(int i=0;i<s.Length;)t+=(s[i]-48)*(s[i++]!=(s+0)[i]?1:2*++i/i);}while(t!=s);return t;}
Try it online!
HUGE credit to @ASCIIOnly for golfing ~30 ;) At first we were both posting updates simultaneously, but at some point he clearly went to town!
Less golfed code...
// s is the input as a string
s=>{
// t is another string used
// to hold intermediate results
var t=s;
// the algorithm repeatedly
// processes s and saves the
// result to t
do{
// copy the last result to s
// and blank out t
s=t;
t="";
// iterate over s
for(int i=0;i<s.Length;)
// append either 1 or 2 times
// the current digit to t
t+=(s[i]-48)*
// compare the current digit
// to the next digit. to prevent
// an out-of-bounds exception,
// append a 0 to s which either
// gets ignored or collapses
// to 0
(s[i++]!=(s+0)[i]
// if they are different, then
// the multiplier is 1
?1
// if they are the same, then
// the multiplier is 2, and we
// have to increment i
:2*++i/i);
}
// continue this until the input
// and output are the same
while(t!=s);
return t;
}
139
– ASCII-only
10 hours ago
134
– ASCII-only
10 hours ago
@ASCIIOnly - Good move :)(s[i++]-48)*2
=>s[i++]*2-96
– dana
10 hours ago
131
– ASCII-only
9 hours ago
1
114
– ASCII-only
9 hours ago
|
show 6 more comments
C# (Visual C# Interactive Compiler), 113 bytes
s=>{var t=s;do{s=t;t="";for(int i=0;i<s.Length;)t+=(s[i]-48)*(s[i++]!=(s+0)[i]?1:2*++i/i);}while(t!=s);return t;}
Try it online!
HUGE credit to @ASCIIOnly for golfing ~30 ;) At first we were both posting updates simultaneously, but at some point he clearly went to town!
Less golfed code...
// s is the input as a string
s=>{
// t is another string used
// to hold intermediate results
var t=s;
// the algorithm repeatedly
// processes s and saves the
// result to t
do{
// copy the last result to s
// and blank out t
s=t;
t="";
// iterate over s
for(int i=0;i<s.Length;)
// append either 1 or 2 times
// the current digit to t
t+=(s[i]-48)*
// compare the current digit
// to the next digit. to prevent
// an out-of-bounds exception,
// append a 0 to s which either
// gets ignored or collapses
// to 0
(s[i++]!=(s+0)[i]
// if they are different, then
// the multiplier is 1
?1
// if they are the same, then
// the multiplier is 2, and we
// have to increment i
:2*++i/i);
}
// continue this until the input
// and output are the same
while(t!=s);
return t;
}
139
– ASCII-only
10 hours ago
134
– ASCII-only
10 hours ago
@ASCIIOnly - Good move :)(s[i++]-48)*2
=>s[i++]*2-96
– dana
10 hours ago
131
– ASCII-only
9 hours ago
1
114
– ASCII-only
9 hours ago
|
show 6 more comments
C# (Visual C# Interactive Compiler), 113 bytes
s=>{var t=s;do{s=t;t="";for(int i=0;i<s.Length;)t+=(s[i]-48)*(s[i++]!=(s+0)[i]?1:2*++i/i);}while(t!=s);return t;}
Try it online!
HUGE credit to @ASCIIOnly for golfing ~30 ;) At first we were both posting updates simultaneously, but at some point he clearly went to town!
Less golfed code...
// s is the input as a string
s=>{
// t is another string used
// to hold intermediate results
var t=s;
// the algorithm repeatedly
// processes s and saves the
// result to t
do{
// copy the last result to s
// and blank out t
s=t;
t="";
// iterate over s
for(int i=0;i<s.Length;)
// append either 1 or 2 times
// the current digit to t
t+=(s[i]-48)*
// compare the current digit
// to the next digit. to prevent
// an out-of-bounds exception,
// append a 0 to s which either
// gets ignored or collapses
// to 0
(s[i++]!=(s+0)[i]
// if they are different, then
// the multiplier is 1
?1
// if they are the same, then
// the multiplier is 2, and we
// have to increment i
:2*++i/i);
}
// continue this until the input
// and output are the same
while(t!=s);
return t;
}
C# (Visual C# Interactive Compiler), 113 bytes
s=>{var t=s;do{s=t;t="";for(int i=0;i<s.Length;)t+=(s[i]-48)*(s[i++]!=(s+0)[i]?1:2*++i/i);}while(t!=s);return t;}
Try it online!
HUGE credit to @ASCIIOnly for golfing ~30 ;) At first we were both posting updates simultaneously, but at some point he clearly went to town!
Less golfed code...
// s is the input as a string
s=>{
// t is another string used
// to hold intermediate results
var t=s;
// the algorithm repeatedly
// processes s and saves the
// result to t
do{
// copy the last result to s
// and blank out t
s=t;
t="";
// iterate over s
for(int i=0;i<s.Length;)
// append either 1 or 2 times
// the current digit to t
t+=(s[i]-48)*
// compare the current digit
// to the next digit. to prevent
// an out-of-bounds exception,
// append a 0 to s which either
// gets ignored or collapses
// to 0
(s[i++]!=(s+0)[i]
// if they are different, then
// the multiplier is 1
?1
// if they are the same, then
// the multiplier is 2, and we
// have to increment i
:2*++i/i);
}
// continue this until the input
// and output are the same
while(t!=s);
return t;
}
edited 2 hours ago
answered 10 hours ago
dana
46135
46135
139
– ASCII-only
10 hours ago
134
– ASCII-only
10 hours ago
@ASCIIOnly - Good move :)(s[i++]-48)*2
=>s[i++]*2-96
– dana
10 hours ago
131
– ASCII-only
9 hours ago
1
114
– ASCII-only
9 hours ago
|
show 6 more comments
139
– ASCII-only
10 hours ago
134
– ASCII-only
10 hours ago
@ASCIIOnly - Good move :)(s[i++]-48)*2
=>s[i++]*2-96
– dana
10 hours ago
131
– ASCII-only
9 hours ago
1
114
– ASCII-only
9 hours ago
139
– ASCII-only
10 hours ago
139
– ASCII-only
10 hours ago
134
– ASCII-only
10 hours ago
134
– ASCII-only
10 hours ago
@ASCIIOnly - Good move :)
(s[i++]-48)*2
=> s[i++]*2-96
– dana
10 hours ago
@ASCIIOnly - Good move :)
(s[i++]-48)*2
=> s[i++]*2-96
– dana
10 hours ago
131
– ASCII-only
9 hours ago
131
– ASCII-only
9 hours ago
1
1
114
– ASCII-only
9 hours ago
114
– ASCII-only
9 hours ago
|
show 6 more comments
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178256%2fcollapsing-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown