# Draw a descendant pyramid (addition)

Challenge

Given a sequence of non-negative integers, create a function that output a descendant pyramid where the children are the sum of both parent numbers.

The result is outputted as an string using tab \t.

Note: trailing tabs on each line are not required

1\t\t2\t\t3\n\t3\t\t5\t\n\t\t8\t\t results in
1       2       3
3       5
8


Optionally result can be outputted as an array using an empty position as space.

[[1,,2,,3],
[,3,,5,],
[,,8,,]]


Example:

Given 4,5,7,3,4,2 Output:

4       5       7       3       4       2
9       12      10      7       6
21      22      17      13
43      39      30
82      69
151


Rules

• Standard rules apply
• Input can be a string using any separator or an array
• Leading/trailing newlines and tabs are permitted (but not required as long as the output is an inverted pyramid)

Test Cases

Input: 1,3,5

1       3       5
4       8
12

Input: 9,8,7,6,5

9       8       7       6       5
17      15      13      11
32      28      24
60      52
112

Input: 5,5,2,5

[[5,,5,,2,,5],
[,10,,7,,7,],
[,,17,,14,,],
[,,,31,,,]]

Input: 3,3,3

[[3,,3,,3],
[,6,,6,],
[,,12,,]]


Winning criteria is

Note: This challenge is very close to Generate Pascal's triangle. The difference is the custom starting sequence (pascal goes from 1 and in ascendant mode, this from an input and in descendant mode).

Sandbox post

• – AdmBorkBork Jul 3 '18 at 17:47
• non-negative integers do we have to handle 0? There are some golfing possibilities if empty spaces are filled with 0s. – JungHwan Min Jul 3 '18 at 18:00
• May we return an array with tabs or empty strings/arrays in the empty positions? – Adám Jul 3 '18 at 18:07
• "Leading/trailing newlines and tabs are permitted" - May we include empty result(s) at the bottom in the array output too? – Jonathan Allan Jul 3 '18 at 18:26
• @JonathanAllan trailing newlines and tabs are permitted – Adám Jul 3 '18 at 18:28

# APL (Dyalog Unicode), 31 28 bytesSBCS

Tacit prefix function. Uses a single space as empty slot marker. Returns list of lists of numbers/spaces.

⍳∘≢{∊(⍺⍴''),,∘' '¨2+/⍣⍺⊢⍵}¨⊂


Try it online!

⍳∘≢{}¨⊂ apply the below function for each of
⍳ the ɩndices ∘ of ≢ the tally with
⊂ the entire argument:

⊢⍵ yield the right argument (the original input)

2+/⍣⍺ apply pairwise sum as many times as indicated by the left argument

,∘' '¨ append a space to each number

(), append that to the following:

⍺⍴'' left-argument elements of the empty string (padded with spaces)

∊ϵnlist (flatten)

# 05AB1E, 15 bytes

gFD„		ý'	N×ì,ü+


Try it online!

• That's a lot of spaces... tabs? – JungHwan Min Jul 3 '18 at 18:10
• ...or zero spaces and a few tabs :p – Jonathan Allan Jul 3 '18 at 18:10
• Indeed, those are tabs :) – Emigna Jul 3 '18 at 18:11

# Wolfram Language (Mathematica), 74 bytes

ArrayPad[Riffle[#,],k++,]&/@NestList[k=0;BlockMap[Tr,#,2,1]&,#,Tr[1^#]-1]&


Try it online!

# Jelly,  14  13 bytes

14 -> 13 assumes trailing spaces-only entry is allowed in the list format output; if not I shall revert to the 14 (asked)

+ƝƬK€z⁶Zṙ"JC$ A monadic link. Output is a list of lists using space characters as the spaces (includes a single trailing spaces-only entry). Try it online! (includes a footer to show the list structure since running the monadic link as a full program will print smashed output implicitly.) # Husk, 22 bytes zoΣ~+R" "oJ" "msNU¡Ẋ+  Try it online! ### Explanation z(Σ~+R" "(J" "ms))NU¡Ẋ+ -- example input: [1,2,3] ¡ -- infinitely do and collect in list Ẋ+ -- | sum adjacent -- : [[1,2,3],[3,5],[8],[],[],... U -- keep up until first repetition: [[1,2,3],[3,5],[8],[]] z( )N -- zip with natural numbers under (example on 1 [1,2,3]) ~+ ( ) -- | join arguments modified by R" " -- | | replicate tab: " " ms -- | | show each: ["1","2","3"] J" " -- | | join with 2 tabs: ["1"," ","2"," ","3"] -- | : [" ","1"," ","2"," ","3"] Σ -- | concatenate: " 1 2 3" -- : [" 1 2 3"," 3 5"," 8"]  # JavaScript (ES6), 73 bytes Weird source code ... Anyway, that's ! Includes leading and trailing whitespace. f=(a,p= )=>p+a.join +(1/(y=a.shift())?f(a.map(x=>y+(y=x)),p+ ):'')  Try it online! # Perl 5-a, 62 bytes $,="\t\t";do{say"\t"x$i++,@F;map$_+=$F[++$q[@F]],@F}while$#F--  Try it online! # Python 3, 10193 80 bytes Saved 7 bytes (map(str,x) instead of for in and init y as '') and got rid of the trailing newlines (x and[ instead of print(), thanks to Jo King. Saved 13 bytes ([*map(sum,zip(x,x[1:]))] instead of [x[j]+x[j+1]for j in range(len(x)-1)]) thanks to ovs. f=lambda x,y='':x and[y+' '.join(map(str,x))]+f([*map(sum,zip(x,x[1:]))],y+' ')  Try it online! • @ovs: Ah, cool. I should have realized I could map the sum. Thanks! – Emigna Jul 4 '18 at 9:45 # Retina, 35 bytes +¶<\d+ *(?=(\d+)?)$#1*$.(*_$1*)


Try it online! Explanation:

+


Repeat until the buffer stops changing,

¶<


Print the contents of the buffer before the substitution, with a trailing newline, and

\d+ *(?=(\d+)?)
$#1*$.(*_$1*)  Substitute a single number with a pair of tabs, but a number followed by another number with a tab, the sum, and another tab. # C (clang), 112 bytes j,k;f(*i,c){for(j=c+1;--j;puts("")){for(k=c-j;k--;)printf("\t");for(;++k<j;i[k]+=i[k+1])printf("%d\t\t",i[k]);}}  Try it online! • Suggest putchar(9) instead of printf("\t") – ceilingcat Aug 4 '18 at 2:41 # Red, 151 bytes func[b][m: length? b repeat n m[loop n - 1[prin"^-"]print replace/all form b" ""^-^-"c: copy[]repeat i -1 + length? b[append c b/(i) + b/(i + 1)]b: c]]  Try it online! ## More readable: f: func [ b ] [ m: length? b repeat n m [ loop n - 1 [ prin tab ] print replace/all form b " " rejoin [ tab tab ] c: copy [] repeat i (length? b) - 1 [ append c b/(i) + b/(i + 1) ] b: c ] ]  # Perl 6, 50 48 bytes {$_,{say "	"x$++~.join(" ");$_[1..*]Z+@\$_}...0}


Try it online!

Anonymous code block that takes a list of integers and prints the inverted pyramid.

# C# (Visual C# Compiler), 163 bytes

b=>{var s="";var c=b.Length;for(int i=0;i<c;i++){for(int z=0;z<i;z++)s+="\t";for(int x=0;x<c-i;x++){s+=b[x]+"\t\t";b[x]=x==c-i-1?0:b[x]+b[x+1];}s+="\n";}return s;}


Try it online!

b =>    {
var s = "";
var c = b.Length;
for (int i = 0; i < c; i++)
{
for (int z = 0; z < i; z++)
s += "\t";
for (int x = 0; x < c - i; x++)
{
s += b[x] + "\t\t";
b[x] = x == c - i - 1 ? 0 : b[x] + b[x + 1];
}
s += "\n";
}
return s;
}


# Japt-Rx, 20 18 bytes

The second line includes a trailing tab.

Nc¡=ä+
Ëq' ² iEç'


Try it

## Explanation

                  :Implicit input of array U
N                 :The array of inputs (NOT the input array)
c                :Concatenate
¡               :  Map U
ä+            :    Consecutive sums of U
=              :    Reassign to U for next iteration
\n                :Reassign the resulting 2D-array to U
Ë                 :Map each array at index E
q                :  Join with
'\t             :   Tab
²            :   Repeated twice
i          :  Prepend
'\t     :   Tab
ç        :   Repeated
E         :   E times
:Implicitly join with newlines, trim (not strictly necessary) and output.