Upside-Down Pyramid Addition…REVERSED!
$begingroup$
Upside-Down Pyramid Addition is the process of taking a list of numbers and consecutively adding them together until you reach one number.
When given the numbers 2, 1, 1
the following process occurs:
2 1 1
3 2
5
This ends in the number 5
.
YOUR TASK
Given the right side of an Upside-Down Pyramid (Ascending), write a program or function that will return the original list.
Extra Challenge: Try doing this in JavaScript and in less than 58 characters!
EXAMPLE
f([5, 2, 1]) => [2, 1, 1]
f([84,42,21,10,2]) => [4,7,3,8,2]
NOTE: The Upside-Down Pyramid will never be empty and will always consist of positive integers ONLY.
code-golf math algorithm
New contributor
$endgroup$
|
show 5 more comments
$begingroup$
Upside-Down Pyramid Addition is the process of taking a list of numbers and consecutively adding them together until you reach one number.
When given the numbers 2, 1, 1
the following process occurs:
2 1 1
3 2
5
This ends in the number 5
.
YOUR TASK
Given the right side of an Upside-Down Pyramid (Ascending), write a program or function that will return the original list.
Extra Challenge: Try doing this in JavaScript and in less than 58 characters!
EXAMPLE
f([5, 2, 1]) => [2, 1, 1]
f([84,42,21,10,2]) => [4,7,3,8,2]
NOTE: The Upside-Down Pyramid will never be empty and will always consist of positive integers ONLY.
code-golf math algorithm
New contributor
$endgroup$
3
$begingroup$
This sounds like a "do my homework for me" rather than a programming puzzle within the site definition.
$endgroup$
– Jeff Zeitlin
1 hour ago
$begingroup$
@JeffZeitlin This is a question which the author has also posted to CodeWars
$endgroup$
– TFeld
1 hour ago
$begingroup$
Yep, I wanted to see how short you guys could make the code!
$endgroup$
– Whimpers
1 hour ago
4
$begingroup$
@Whimpers - I've tagged it as code-golf due to your above comment and post wording.
$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
Are the numbers guaranteed to be integers $>0$, as the example suggests?
$endgroup$
– Arnauld
1 hour ago
|
show 5 more comments
$begingroup$
Upside-Down Pyramid Addition is the process of taking a list of numbers and consecutively adding them together until you reach one number.
When given the numbers 2, 1, 1
the following process occurs:
2 1 1
3 2
5
This ends in the number 5
.
YOUR TASK
Given the right side of an Upside-Down Pyramid (Ascending), write a program or function that will return the original list.
Extra Challenge: Try doing this in JavaScript and in less than 58 characters!
EXAMPLE
f([5, 2, 1]) => [2, 1, 1]
f([84,42,21,10,2]) => [4,7,3,8,2]
NOTE: The Upside-Down Pyramid will never be empty and will always consist of positive integers ONLY.
code-golf math algorithm
New contributor
$endgroup$
Upside-Down Pyramid Addition is the process of taking a list of numbers and consecutively adding them together until you reach one number.
When given the numbers 2, 1, 1
the following process occurs:
2 1 1
3 2
5
This ends in the number 5
.
YOUR TASK
Given the right side of an Upside-Down Pyramid (Ascending), write a program or function that will return the original list.
Extra Challenge: Try doing this in JavaScript and in less than 58 characters!
EXAMPLE
f([5, 2, 1]) => [2, 1, 1]
f([84,42,21,10,2]) => [4,7,3,8,2]
NOTE: The Upside-Down Pyramid will never be empty and will always consist of positive integers ONLY.
code-golf math algorithm
code-golf math algorithm
New contributor
New contributor
edited 1 hour ago
Whimpers
New contributor
asked 1 hour ago
WhimpersWhimpers
394
394
New contributor
New contributor
3
$begingroup$
This sounds like a "do my homework for me" rather than a programming puzzle within the site definition.
$endgroup$
– Jeff Zeitlin
1 hour ago
$begingroup$
@JeffZeitlin This is a question which the author has also posted to CodeWars
$endgroup$
– TFeld
1 hour ago
$begingroup$
Yep, I wanted to see how short you guys could make the code!
$endgroup$
– Whimpers
1 hour ago
4
$begingroup$
@Whimpers - I've tagged it as code-golf due to your above comment and post wording.
$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
Are the numbers guaranteed to be integers $>0$, as the example suggests?
$endgroup$
– Arnauld
1 hour ago
|
show 5 more comments
3
$begingroup$
This sounds like a "do my homework for me" rather than a programming puzzle within the site definition.
$endgroup$
– Jeff Zeitlin
1 hour ago
$begingroup$
@JeffZeitlin This is a question which the author has also posted to CodeWars
$endgroup$
– TFeld
1 hour ago
$begingroup$
Yep, I wanted to see how short you guys could make the code!
$endgroup$
– Whimpers
1 hour ago
4
$begingroup$
@Whimpers - I've tagged it as code-golf due to your above comment and post wording.
$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
Are the numbers guaranteed to be integers $>0$, as the example suggests?
$endgroup$
– Arnauld
1 hour ago
3
3
$begingroup$
This sounds like a "do my homework for me" rather than a programming puzzle within the site definition.
$endgroup$
– Jeff Zeitlin
1 hour ago
$begingroup$
This sounds like a "do my homework for me" rather than a programming puzzle within the site definition.
$endgroup$
– Jeff Zeitlin
1 hour ago
$begingroup$
@JeffZeitlin This is a question which the author has also posted to CodeWars
$endgroup$
– TFeld
1 hour ago
$begingroup$
@JeffZeitlin This is a question which the author has also posted to CodeWars
$endgroup$
– TFeld
1 hour ago
$begingroup$
Yep, I wanted to see how short you guys could make the code!
$endgroup$
– Whimpers
1 hour ago
$begingroup$
Yep, I wanted to see how short you guys could make the code!
$endgroup$
– Whimpers
1 hour ago
4
4
$begingroup$
@Whimpers - I've tagged it as code-golf due to your above comment and post wording.
$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
@Whimpers - I've tagged it as code-golf due to your above comment and post wording.
$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
Are the numbers guaranteed to be integers $>0$, as the example suggests?
$endgroup$
– Arnauld
1 hour ago
$begingroup$
Are the numbers guaranteed to be integers $>0$, as the example suggests?
$endgroup$
– Arnauld
1 hour ago
|
show 5 more comments
7 Answers
7
active
oldest
votes
$begingroup$
JavaScript (ES6), 62 58 49 bytes
f=a=>+a?a:[...f(a.map(n=>a-(a=n),a=a.shift())),a]
Try it online!
Commented
f = a => // f = recursive function taking the input list a
+a ? // if a consists of a single positive integer:
a // stop recursion and return a
: // else:
[ // build an array consisting of:
...f( // 1) all entries generated by a recursive call to f:
a.map(n => // for each value n in a:
a - (a = n), // yield the difference between the previous value and n
// and update a to n
a = a.shift() // start by removing the first element and saving it in a
) // end of map()
), // end of recursive call
a // 2) followed by the last entry from a
] // end of array
$endgroup$
$begingroup$
Please name your function reverse, as stated in the YOUR TASK section
$endgroup$
– Whimpers
1 hour ago
6
$begingroup$
@Whimpers In code-golf challenges you tend to use as short as possible variable names and functions, usually single characters. The Python lambda also isn't named 'reverse'. Also, what about programming languages which can't give names to the functions? Or programming languages which don't have functions at all, only full programs?
$endgroup$
– Kevin Cruijssen
1 hour ago
$begingroup$
I see, I edited the description a bit to make this broader.
$endgroup$
– Whimpers
51 mins ago
add a comment |
$begingroup$
Jelly, 6 bytes
ṚIƬZḢṚ
A monadic Link accepting a list of integers which yields a list of integers.
Try it online!
How?
Builds the whole triangle then extracts the required elements.
ṚIƬZḢṚ - Link: list of integers e.g. [84,42,21,10,2]
Ṛ - reverse [2,10,21,42,84]
Ƭ - collect & apply until a fixed point:
I - incremental differences [[2,10,21,42,84],[8,11,21,42],[3,10,21],[7,11],[4],]
Z - transpose [[2,8,3,7,4],[10,11,10,11],[21,21,21],[42,42],[84]]
Ḣ - head [2,8,3,7,4]
Ṛ - reverse [4,7,3,8,2]
$endgroup$
$begingroup$
Had an almost identical solution but withU
s instead ofṚ
!
$endgroup$
– Nick Kennedy
20 mins ago
$begingroup$
IƬUZḢA
would work with the given question too; I wonder if there is a byte save somewhere...
$endgroup$
– Jonathan Allan
16 mins ago
add a comment |
$begingroup$
MathGolf, 14 11 bytes
xÆ‼├│?;∟;]x
Try it online!
Explanation
x reverse int/array/string
Æ ∟ do while true without popping using 5 operators
‼ apply next 2 operators to TOS
├ pop from left of list
│ get differences of list
? rot3
; discard TOS (removes rest from ├ command)
loop ends here
; discard TOS (removes empty array from stack)
] wrap stack in array
x reverse array
$endgroup$
add a comment |
$begingroup$
Python 2, 78 bytes
lambda n,*a:R(lambda r,v:R(lambda x,w:x+[w-x[-1]],r,[v]),a,[n])[::-1]
R=reduce
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 55 bytes
y=x=scan();for(i in sum(1|x):2-1)y[i]=(x=-diff(x))[i];y
Try it online!
$endgroup$
add a comment |
$begingroup$
Javascript (ES6), 127 bytes
f=a=>{for(e=[a],a=[a[l=a.length-1]],i=0;i<l;i++){for(e.push(g=),j=-1;j<l;)g.push(e[i][j]-e[i][++j]);r.unshift(g[j])}return r}
Original code
function f(a){
var e=[a];
var r=[a[a.length-1]];
for (var i=1;i<a.length;i++){
var g=;
for (var j=0;j<a.length;j++){
g.push(e[i-1][j-1]-e[i-1][j]);
}
e.push(g);
r.unshift(g[j-1]);
}
return r;
}
Oh, I lost like... a lot... to the previous answer...
New contributor
$endgroup$
add a comment |
$begingroup$
TI-BASIC, 54 bytes
Ans→L₁:dim(L₁→dim(L₂:While 1-Ans:L₁(Ans→L₂(Ans:-ΔList(L₁→L₁:dim(Ans:End:L₁(Ans→L₂(Ans:L₂
Input is the list of the right side of the triangle in Ans
, as is described in the challenge.
Output is the top row of said triangle.
Examples:
{5,2,1
{5 2 1}
prgmCDGF19
{2 1 1}
{84,42,21,10,2
{84 42 21 10 2}
prgmCDGF19
{4 7 3 8 2}
Explanation:
This solution abuses the fact that the triangle formed using the right side of the triangle as the start ends up being the change in each element.
In other words,
2 1 1
3 2
5
becomes:
5 2 1
3 1
2
Thus, the resulting list is the right side of this new triangle, which can be formed by setting the last element to the index of its parent list's length in the resulting list.
Ans→L₁ ;store the input list in L₁
dim(L₁→dim(L₂ ;set the length of L₂ to the length of L₁
While 1-Ans ;while the L₁'s length is not 1
L₁(Ans→L₂(Ans ;set the last element of L₁ to the corresponding index in L₂
-ΔList(L₁→L₁ ;get the change in each element, then negate
; (elements are in descending order so the change in each
; element will be negative)
; and store the resulting list in L₁
dim(Ans ;leave the length of L₁ in "Ans"
End
L₁(Ans→L₂(Ans ;set the element again
; (needed for final step)
L₂ ;leave L₂ in "Ans"
;implicit print of "Ans"
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
$endgroup$
add a comment |
Your Answer
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
});
}
});
Whimpers is a new contributor. Be nice, and check out our Code of Conduct.
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%2f184951%2fupside-down-pyramid-addition-reversed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
JavaScript (ES6), 62 58 49 bytes
f=a=>+a?a:[...f(a.map(n=>a-(a=n),a=a.shift())),a]
Try it online!
Commented
f = a => // f = recursive function taking the input list a
+a ? // if a consists of a single positive integer:
a // stop recursion and return a
: // else:
[ // build an array consisting of:
...f( // 1) all entries generated by a recursive call to f:
a.map(n => // for each value n in a:
a - (a = n), // yield the difference between the previous value and n
// and update a to n
a = a.shift() // start by removing the first element and saving it in a
) // end of map()
), // end of recursive call
a // 2) followed by the last entry from a
] // end of array
$endgroup$
$begingroup$
Please name your function reverse, as stated in the YOUR TASK section
$endgroup$
– Whimpers
1 hour ago
6
$begingroup$
@Whimpers In code-golf challenges you tend to use as short as possible variable names and functions, usually single characters. The Python lambda also isn't named 'reverse'. Also, what about programming languages which can't give names to the functions? Or programming languages which don't have functions at all, only full programs?
$endgroup$
– Kevin Cruijssen
1 hour ago
$begingroup$
I see, I edited the description a bit to make this broader.
$endgroup$
– Whimpers
51 mins ago
add a comment |
$begingroup$
JavaScript (ES6), 62 58 49 bytes
f=a=>+a?a:[...f(a.map(n=>a-(a=n),a=a.shift())),a]
Try it online!
Commented
f = a => // f = recursive function taking the input list a
+a ? // if a consists of a single positive integer:
a // stop recursion and return a
: // else:
[ // build an array consisting of:
...f( // 1) all entries generated by a recursive call to f:
a.map(n => // for each value n in a:
a - (a = n), // yield the difference between the previous value and n
// and update a to n
a = a.shift() // start by removing the first element and saving it in a
) // end of map()
), // end of recursive call
a // 2) followed by the last entry from a
] // end of array
$endgroup$
$begingroup$
Please name your function reverse, as stated in the YOUR TASK section
$endgroup$
– Whimpers
1 hour ago
6
$begingroup$
@Whimpers In code-golf challenges you tend to use as short as possible variable names and functions, usually single characters. The Python lambda also isn't named 'reverse'. Also, what about programming languages which can't give names to the functions? Or programming languages which don't have functions at all, only full programs?
$endgroup$
– Kevin Cruijssen
1 hour ago
$begingroup$
I see, I edited the description a bit to make this broader.
$endgroup$
– Whimpers
51 mins ago
add a comment |
$begingroup$
JavaScript (ES6), 62 58 49 bytes
f=a=>+a?a:[...f(a.map(n=>a-(a=n),a=a.shift())),a]
Try it online!
Commented
f = a => // f = recursive function taking the input list a
+a ? // if a consists of a single positive integer:
a // stop recursion and return a
: // else:
[ // build an array consisting of:
...f( // 1) all entries generated by a recursive call to f:
a.map(n => // for each value n in a:
a - (a = n), // yield the difference between the previous value and n
// and update a to n
a = a.shift() // start by removing the first element and saving it in a
) // end of map()
), // end of recursive call
a // 2) followed by the last entry from a
] // end of array
$endgroup$
JavaScript (ES6), 62 58 49 bytes
f=a=>+a?a:[...f(a.map(n=>a-(a=n),a=a.shift())),a]
Try it online!
Commented
f = a => // f = recursive function taking the input list a
+a ? // if a consists of a single positive integer:
a // stop recursion and return a
: // else:
[ // build an array consisting of:
...f( // 1) all entries generated by a recursive call to f:
a.map(n => // for each value n in a:
a - (a = n), // yield the difference between the previous value and n
// and update a to n
a = a.shift() // start by removing the first element and saving it in a
) // end of map()
), // end of recursive call
a // 2) followed by the last entry from a
] // end of array
edited 38 mins ago
answered 1 hour ago
ArnauldArnauld
82.5k798339
82.5k798339
$begingroup$
Please name your function reverse, as stated in the YOUR TASK section
$endgroup$
– Whimpers
1 hour ago
6
$begingroup$
@Whimpers In code-golf challenges you tend to use as short as possible variable names and functions, usually single characters. The Python lambda also isn't named 'reverse'. Also, what about programming languages which can't give names to the functions? Or programming languages which don't have functions at all, only full programs?
$endgroup$
– Kevin Cruijssen
1 hour ago
$begingroup$
I see, I edited the description a bit to make this broader.
$endgroup$
– Whimpers
51 mins ago
add a comment |
$begingroup$
Please name your function reverse, as stated in the YOUR TASK section
$endgroup$
– Whimpers
1 hour ago
6
$begingroup$
@Whimpers In code-golf challenges you tend to use as short as possible variable names and functions, usually single characters. The Python lambda also isn't named 'reverse'. Also, what about programming languages which can't give names to the functions? Or programming languages which don't have functions at all, only full programs?
$endgroup$
– Kevin Cruijssen
1 hour ago
$begingroup$
I see, I edited the description a bit to make this broader.
$endgroup$
– Whimpers
51 mins ago
$begingroup$
Please name your function reverse, as stated in the YOUR TASK section
$endgroup$
– Whimpers
1 hour ago
$begingroup$
Please name your function reverse, as stated in the YOUR TASK section
$endgroup$
– Whimpers
1 hour ago
6
6
$begingroup$
@Whimpers In code-golf challenges you tend to use as short as possible variable names and functions, usually single characters. The Python lambda also isn't named 'reverse'. Also, what about programming languages which can't give names to the functions? Or programming languages which don't have functions at all, only full programs?
$endgroup$
– Kevin Cruijssen
1 hour ago
$begingroup$
@Whimpers In code-golf challenges you tend to use as short as possible variable names and functions, usually single characters. The Python lambda also isn't named 'reverse'. Also, what about programming languages which can't give names to the functions? Or programming languages which don't have functions at all, only full programs?
$endgroup$
– Kevin Cruijssen
1 hour ago
$begingroup$
I see, I edited the description a bit to make this broader.
$endgroup$
– Whimpers
51 mins ago
$begingroup$
I see, I edited the description a bit to make this broader.
$endgroup$
– Whimpers
51 mins ago
add a comment |
$begingroup$
Jelly, 6 bytes
ṚIƬZḢṚ
A monadic Link accepting a list of integers which yields a list of integers.
Try it online!
How?
Builds the whole triangle then extracts the required elements.
ṚIƬZḢṚ - Link: list of integers e.g. [84,42,21,10,2]
Ṛ - reverse [2,10,21,42,84]
Ƭ - collect & apply until a fixed point:
I - incremental differences [[2,10,21,42,84],[8,11,21,42],[3,10,21],[7,11],[4],]
Z - transpose [[2,8,3,7,4],[10,11,10,11],[21,21,21],[42,42],[84]]
Ḣ - head [2,8,3,7,4]
Ṛ - reverse [4,7,3,8,2]
$endgroup$
$begingroup$
Had an almost identical solution but withU
s instead ofṚ
!
$endgroup$
– Nick Kennedy
20 mins ago
$begingroup$
IƬUZḢA
would work with the given question too; I wonder if there is a byte save somewhere...
$endgroup$
– Jonathan Allan
16 mins ago
add a comment |
$begingroup$
Jelly, 6 bytes
ṚIƬZḢṚ
A monadic Link accepting a list of integers which yields a list of integers.
Try it online!
How?
Builds the whole triangle then extracts the required elements.
ṚIƬZḢṚ - Link: list of integers e.g. [84,42,21,10,2]
Ṛ - reverse [2,10,21,42,84]
Ƭ - collect & apply until a fixed point:
I - incremental differences [[2,10,21,42,84],[8,11,21,42],[3,10,21],[7,11],[4],]
Z - transpose [[2,8,3,7,4],[10,11,10,11],[21,21,21],[42,42],[84]]
Ḣ - head [2,8,3,7,4]
Ṛ - reverse [4,7,3,8,2]
$endgroup$
$begingroup$
Had an almost identical solution but withU
s instead ofṚ
!
$endgroup$
– Nick Kennedy
20 mins ago
$begingroup$
IƬUZḢA
would work with the given question too; I wonder if there is a byte save somewhere...
$endgroup$
– Jonathan Allan
16 mins ago
add a comment |
$begingroup$
Jelly, 6 bytes
ṚIƬZḢṚ
A monadic Link accepting a list of integers which yields a list of integers.
Try it online!
How?
Builds the whole triangle then extracts the required elements.
ṚIƬZḢṚ - Link: list of integers e.g. [84,42,21,10,2]
Ṛ - reverse [2,10,21,42,84]
Ƭ - collect & apply until a fixed point:
I - incremental differences [[2,10,21,42,84],[8,11,21,42],[3,10,21],[7,11],[4],]
Z - transpose [[2,8,3,7,4],[10,11,10,11],[21,21,21],[42,42],[84]]
Ḣ - head [2,8,3,7,4]
Ṛ - reverse [4,7,3,8,2]
$endgroup$
Jelly, 6 bytes
ṚIƬZḢṚ
A monadic Link accepting a list of integers which yields a list of integers.
Try it online!
How?
Builds the whole triangle then extracts the required elements.
ṚIƬZḢṚ - Link: list of integers e.g. [84,42,21,10,2]
Ṛ - reverse [2,10,21,42,84]
Ƭ - collect & apply until a fixed point:
I - incremental differences [[2,10,21,42,84],[8,11,21,42],[3,10,21],[7,11],[4],]
Z - transpose [[2,8,3,7,4],[10,11,10,11],[21,21,21],[42,42],[84]]
Ḣ - head [2,8,3,7,4]
Ṛ - reverse [4,7,3,8,2]
edited 24 mins ago
answered 30 mins ago
Jonathan AllanJonathan Allan
55.1k537176
55.1k537176
$begingroup$
Had an almost identical solution but withU
s instead ofṚ
!
$endgroup$
– Nick Kennedy
20 mins ago
$begingroup$
IƬUZḢA
would work with the given question too; I wonder if there is a byte save somewhere...
$endgroup$
– Jonathan Allan
16 mins ago
add a comment |
$begingroup$
Had an almost identical solution but withU
s instead ofṚ
!
$endgroup$
– Nick Kennedy
20 mins ago
$begingroup$
IƬUZḢA
would work with the given question too; I wonder if there is a byte save somewhere...
$endgroup$
– Jonathan Allan
16 mins ago
$begingroup$
Had an almost identical solution but with
U
s instead of Ṛ
!$endgroup$
– Nick Kennedy
20 mins ago
$begingroup$
Had an almost identical solution but with
U
s instead of Ṛ
!$endgroup$
– Nick Kennedy
20 mins ago
$begingroup$
IƬUZḢA
would work with the given question too; I wonder if there is a byte save somewhere...$endgroup$
– Jonathan Allan
16 mins ago
$begingroup$
IƬUZḢA
would work with the given question too; I wonder if there is a byte save somewhere...$endgroup$
– Jonathan Allan
16 mins ago
add a comment |
$begingroup$
MathGolf, 14 11 bytes
xÆ‼├│?;∟;]x
Try it online!
Explanation
x reverse int/array/string
Æ ∟ do while true without popping using 5 operators
‼ apply next 2 operators to TOS
├ pop from left of list
│ get differences of list
? rot3
; discard TOS (removes rest from ├ command)
loop ends here
; discard TOS (removes empty array from stack)
] wrap stack in array
x reverse array
$endgroup$
add a comment |
$begingroup$
MathGolf, 14 11 bytes
xÆ‼├│?;∟;]x
Try it online!
Explanation
x reverse int/array/string
Æ ∟ do while true without popping using 5 operators
‼ apply next 2 operators to TOS
├ pop from left of list
│ get differences of list
? rot3
; discard TOS (removes rest from ├ command)
loop ends here
; discard TOS (removes empty array from stack)
] wrap stack in array
x reverse array
$endgroup$
add a comment |
$begingroup$
MathGolf, 14 11 bytes
xÆ‼├│?;∟;]x
Try it online!
Explanation
x reverse int/array/string
Æ ∟ do while true without popping using 5 operators
‼ apply next 2 operators to TOS
├ pop from left of list
│ get differences of list
? rot3
; discard TOS (removes rest from ├ command)
loop ends here
; discard TOS (removes empty array from stack)
] wrap stack in array
x reverse array
$endgroup$
MathGolf, 14 11 bytes
xÆ‼├│?;∟;]x
Try it online!
Explanation
x reverse int/array/string
Æ ∟ do while true without popping using 5 operators
‼ apply next 2 operators to TOS
├ pop from left of list
│ get differences of list
? rot3
; discard TOS (removes rest from ├ command)
loop ends here
; discard TOS (removes empty array from stack)
] wrap stack in array
x reverse array
edited 23 mins ago
answered 33 mins ago
maxbmaxb
3,35311332
3,35311332
add a comment |
add a comment |
$begingroup$
Python 2, 78 bytes
lambda n,*a:R(lambda r,v:R(lambda x,w:x+[w-x[-1]],r,[v]),a,[n])[::-1]
R=reduce
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 78 bytes
lambda n,*a:R(lambda r,v:R(lambda x,w:x+[w-x[-1]],r,[v]),a,[n])[::-1]
R=reduce
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 78 bytes
lambda n,*a:R(lambda r,v:R(lambda x,w:x+[w-x[-1]],r,[v]),a,[n])[::-1]
R=reduce
Try it online!
$endgroup$
Python 2, 78 bytes
lambda n,*a:R(lambda r,v:R(lambda x,w:x+[w-x[-1]],r,[v]),a,[n])[::-1]
R=reduce
Try it online!
answered 1 hour ago
TFeldTFeld
16.7k21451
16.7k21451
add a comment |
add a comment |
$begingroup$
R, 55 bytes
y=x=scan();for(i in sum(1|x):2-1)y[i]=(x=-diff(x))[i];y
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 55 bytes
y=x=scan();for(i in sum(1|x):2-1)y[i]=(x=-diff(x))[i];y
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 55 bytes
y=x=scan();for(i in sum(1|x):2-1)y[i]=(x=-diff(x))[i];y
Try it online!
$endgroup$
R, 55 bytes
y=x=scan();for(i in sum(1|x):2-1)y[i]=(x=-diff(x))[i];y
Try it online!
answered 1 hour ago
Kirill L.Kirill L.
6,3831529
6,3831529
add a comment |
add a comment |
$begingroup$
Javascript (ES6), 127 bytes
f=a=>{for(e=[a],a=[a[l=a.length-1]],i=0;i<l;i++){for(e.push(g=),j=-1;j<l;)g.push(e[i][j]-e[i][++j]);r.unshift(g[j])}return r}
Original code
function f(a){
var e=[a];
var r=[a[a.length-1]];
for (var i=1;i<a.length;i++){
var g=;
for (var j=0;j<a.length;j++){
g.push(e[i-1][j-1]-e[i-1][j]);
}
e.push(g);
r.unshift(g[j-1]);
}
return r;
}
Oh, I lost like... a lot... to the previous answer...
New contributor
$endgroup$
add a comment |
$begingroup$
Javascript (ES6), 127 bytes
f=a=>{for(e=[a],a=[a[l=a.length-1]],i=0;i<l;i++){for(e.push(g=),j=-1;j<l;)g.push(e[i][j]-e[i][++j]);r.unshift(g[j])}return r}
Original code
function f(a){
var e=[a];
var r=[a[a.length-1]];
for (var i=1;i<a.length;i++){
var g=;
for (var j=0;j<a.length;j++){
g.push(e[i-1][j-1]-e[i-1][j]);
}
e.push(g);
r.unshift(g[j-1]);
}
return r;
}
Oh, I lost like... a lot... to the previous answer...
New contributor
$endgroup$
add a comment |
$begingroup$
Javascript (ES6), 127 bytes
f=a=>{for(e=[a],a=[a[l=a.length-1]],i=0;i<l;i++){for(e.push(g=),j=-1;j<l;)g.push(e[i][j]-e[i][++j]);r.unshift(g[j])}return r}
Original code
function f(a){
var e=[a];
var r=[a[a.length-1]];
for (var i=1;i<a.length;i++){
var g=;
for (var j=0;j<a.length;j++){
g.push(e[i-1][j-1]-e[i-1][j]);
}
e.push(g);
r.unshift(g[j-1]);
}
return r;
}
Oh, I lost like... a lot... to the previous answer...
New contributor
$endgroup$
Javascript (ES6), 127 bytes
f=a=>{for(e=[a],a=[a[l=a.length-1]],i=0;i<l;i++){for(e.push(g=),j=-1;j<l;)g.push(e[i][j]-e[i][++j]);r.unshift(g[j])}return r}
Original code
function f(a){
var e=[a];
var r=[a[a.length-1]];
for (var i=1;i<a.length;i++){
var g=;
for (var j=0;j<a.length;j++){
g.push(e[i-1][j-1]-e[i-1][j]);
}
e.push(g);
r.unshift(g[j-1]);
}
return r;
}
Oh, I lost like... a lot... to the previous answer...
New contributor
New contributor
answered 28 mins ago
NaruyokoNaruyoko
715
715
New contributor
New contributor
add a comment |
add a comment |
$begingroup$
TI-BASIC, 54 bytes
Ans→L₁:dim(L₁→dim(L₂:While 1-Ans:L₁(Ans→L₂(Ans:-ΔList(L₁→L₁:dim(Ans:End:L₁(Ans→L₂(Ans:L₂
Input is the list of the right side of the triangle in Ans
, as is described in the challenge.
Output is the top row of said triangle.
Examples:
{5,2,1
{5 2 1}
prgmCDGF19
{2 1 1}
{84,42,21,10,2
{84 42 21 10 2}
prgmCDGF19
{4 7 3 8 2}
Explanation:
This solution abuses the fact that the triangle formed using the right side of the triangle as the start ends up being the change in each element.
In other words,
2 1 1
3 2
5
becomes:
5 2 1
3 1
2
Thus, the resulting list is the right side of this new triangle, which can be formed by setting the last element to the index of its parent list's length in the resulting list.
Ans→L₁ ;store the input list in L₁
dim(L₁→dim(L₂ ;set the length of L₂ to the length of L₁
While 1-Ans ;while the L₁'s length is not 1
L₁(Ans→L₂(Ans ;set the last element of L₁ to the corresponding index in L₂
-ΔList(L₁→L₁ ;get the change in each element, then negate
; (elements are in descending order so the change in each
; element will be negative)
; and store the resulting list in L₁
dim(Ans ;leave the length of L₁ in "Ans"
End
L₁(Ans→L₂(Ans ;set the element again
; (needed for final step)
L₂ ;leave L₂ in "Ans"
;implicit print of "Ans"
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
$endgroup$
add a comment |
$begingroup$
TI-BASIC, 54 bytes
Ans→L₁:dim(L₁→dim(L₂:While 1-Ans:L₁(Ans→L₂(Ans:-ΔList(L₁→L₁:dim(Ans:End:L₁(Ans→L₂(Ans:L₂
Input is the list of the right side of the triangle in Ans
, as is described in the challenge.
Output is the top row of said triangle.
Examples:
{5,2,1
{5 2 1}
prgmCDGF19
{2 1 1}
{84,42,21,10,2
{84 42 21 10 2}
prgmCDGF19
{4 7 3 8 2}
Explanation:
This solution abuses the fact that the triangle formed using the right side of the triangle as the start ends up being the change in each element.
In other words,
2 1 1
3 2
5
becomes:
5 2 1
3 1
2
Thus, the resulting list is the right side of this new triangle, which can be formed by setting the last element to the index of its parent list's length in the resulting list.
Ans→L₁ ;store the input list in L₁
dim(L₁→dim(L₂ ;set the length of L₂ to the length of L₁
While 1-Ans ;while the L₁'s length is not 1
L₁(Ans→L₂(Ans ;set the last element of L₁ to the corresponding index in L₂
-ΔList(L₁→L₁ ;get the change in each element, then negate
; (elements are in descending order so the change in each
; element will be negative)
; and store the resulting list in L₁
dim(Ans ;leave the length of L₁ in "Ans"
End
L₁(Ans→L₂(Ans ;set the element again
; (needed for final step)
L₂ ;leave L₂ in "Ans"
;implicit print of "Ans"
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
$endgroup$
add a comment |
$begingroup$
TI-BASIC, 54 bytes
Ans→L₁:dim(L₁→dim(L₂:While 1-Ans:L₁(Ans→L₂(Ans:-ΔList(L₁→L₁:dim(Ans:End:L₁(Ans→L₂(Ans:L₂
Input is the list of the right side of the triangle in Ans
, as is described in the challenge.
Output is the top row of said triangle.
Examples:
{5,2,1
{5 2 1}
prgmCDGF19
{2 1 1}
{84,42,21,10,2
{84 42 21 10 2}
prgmCDGF19
{4 7 3 8 2}
Explanation:
This solution abuses the fact that the triangle formed using the right side of the triangle as the start ends up being the change in each element.
In other words,
2 1 1
3 2
5
becomes:
5 2 1
3 1
2
Thus, the resulting list is the right side of this new triangle, which can be formed by setting the last element to the index of its parent list's length in the resulting list.
Ans→L₁ ;store the input list in L₁
dim(L₁→dim(L₂ ;set the length of L₂ to the length of L₁
While 1-Ans ;while the L₁'s length is not 1
L₁(Ans→L₂(Ans ;set the last element of L₁ to the corresponding index in L₂
-ΔList(L₁→L₁ ;get the change in each element, then negate
; (elements are in descending order so the change in each
; element will be negative)
; and store the resulting list in L₁
dim(Ans ;leave the length of L₁ in "Ans"
End
L₁(Ans→L₂(Ans ;set the element again
; (needed for final step)
L₂ ;leave L₂ in "Ans"
;implicit print of "Ans"
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
$endgroup$
TI-BASIC, 54 bytes
Ans→L₁:dim(L₁→dim(L₂:While 1-Ans:L₁(Ans→L₂(Ans:-ΔList(L₁→L₁:dim(Ans:End:L₁(Ans→L₂(Ans:L₂
Input is the list of the right side of the triangle in Ans
, as is described in the challenge.
Output is the top row of said triangle.
Examples:
{5,2,1
{5 2 1}
prgmCDGF19
{2 1 1}
{84,42,21,10,2
{84 42 21 10 2}
prgmCDGF19
{4 7 3 8 2}
Explanation:
This solution abuses the fact that the triangle formed using the right side of the triangle as the start ends up being the change in each element.
In other words,
2 1 1
3 2
5
becomes:
5 2 1
3 1
2
Thus, the resulting list is the right side of this new triangle, which can be formed by setting the last element to the index of its parent list's length in the resulting list.
Ans→L₁ ;store the input list in L₁
dim(L₁→dim(L₂ ;set the length of L₂ to the length of L₁
While 1-Ans ;while the L₁'s length is not 1
L₁(Ans→L₂(Ans ;set the last element of L₁ to the corresponding index in L₂
-ΔList(L₁→L₁ ;get the change in each element, then negate
; (elements are in descending order so the change in each
; element will be negative)
; and store the resulting list in L₁
dim(Ans ;leave the length of L₁ in "Ans"
End
L₁(Ans→L₂(Ans ;set the element again
; (needed for final step)
L₂ ;leave L₂ in "Ans"
;implicit print of "Ans"
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
edited 14 mins ago
answered 47 mins ago
TauTau
1,148519
1,148519
add a comment |
add a comment |
Whimpers is a new contributor. Be nice, and check out our Code of Conduct.
Whimpers is a new contributor. Be nice, and check out our Code of Conduct.
Whimpers is a new contributor. Be nice, and check out our Code of Conduct.
Whimpers is a new contributor. Be nice, and check out our Code of Conduct.
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).
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%2f184951%2fupside-down-pyramid-addition-reversed%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
3
$begingroup$
This sounds like a "do my homework for me" rather than a programming puzzle within the site definition.
$endgroup$
– Jeff Zeitlin
1 hour ago
$begingroup$
@JeffZeitlin This is a question which the author has also posted to CodeWars
$endgroup$
– TFeld
1 hour ago
$begingroup$
Yep, I wanted to see how short you guys could make the code!
$endgroup$
– Whimpers
1 hour ago
4
$begingroup$
@Whimpers - I've tagged it as code-golf due to your above comment and post wording.
$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
Are the numbers guaranteed to be integers $>0$, as the example suggests?
$endgroup$
– Arnauld
1 hour ago