Triangle Roll-Up
September 23, 2014
It’s best to solve problems like this by composing small, general-purpose functions which can often be reused for other, similar problems. Although our Standard Prelude doesn’t have it, but-last
is a common function on lists, returning all elements of a list except the last:
(define (but-last xs) (reverse (cdr (reverse xs))))
Another general-purpose function, pair-wise
, applies an operator f on each pair of elements of a list, returning a new list with one less element than the input:
(define (pair-wise f xs) (map f (but-last xs) (cdr xs)))
Then, our roll-up
procedure is recursive, with output produced after the recursive call so the last recursive call produces the first output:
(define (roll-up xs)
(when (pair? xs)
(roll-up (pair-wise + xs))
(display xs) (newline)))
It always seems to work that way: with a suitable set of primitives, you build the language to meet the current task, so that the task itself becomes trivial. All the other solutions at http://www.careercup.com/question?id=5668983114039296, where this exercise came from, use some complicated-looking set of nested loops; by contrast, our solution is a simple recursion with nary a loop in sight, easy to read and trivial to write. Here’s what it looks like in action:
> (roll-up '(4 7 3 6 7))
(81)
(40 41)
(21 19 22)
(11 10 9 13)
(4 7 3 6 7)
You can run the program at http://programmingpraxis.codepad.org/uNQ5qsvJ. We’ll add but-last
to the Standard Prelude the next time we revise it.
A bit messy…!
In python:
Java
We can do this without using any extra memory by constructing each row in the right hand part of the original array:
The final array is the left hand column of the triangle. We can then reverse the process and print out each row on the way, so they come out in the correct order. I can’t see a very neat way of doing this with lists (since we need to iterate in both directions).
A solution in Haskell, split up into two function. One to generate the triangle and another to print it.
Prints the numbers using a fixed field width so the triangle looks nice.
Example:
Here is a php program for this solution
<?php
$inputArray = array_slice($argv, 1);
//$inputArray = array(4,7,3,6,7);
$size = count($inputArray);
$output = array();
$newsize=$size;
for($k = 0; $k 0)
{
if($k == 0)
$sum = $inputArray[$i-1];
else
$sum = $inputArray[$i-1]+$inputArray[$i];
if($sum 0; $i–)
echo “\n”.$output[$i];
?>
$inputArray = array_slice($argv, 1);
//$inputArray = array(4,7,3,6,7);
$size = count($inputArray);
$output = array();
$newsize=$size;
for($k = 0; $k 0)
{
if($k == 0)
$sum = $inputArray[$i-1];
else
$sum = $inputArray[$i-1]+$inputArray[$i];
if($sum 0; $i--)
echo "\n".$output[$i];
I had attached the link for my code:
It will take max 5 input number, you can change inside the code by changing the value of MAX_ARRAY_SIZE.
A bit lazy for printing…
P!
#include
using namespace std;
void TriangleRollup(int a[],int n)
{
int k = n-1;
for(int i = 1; i<=n;i++)
{
for( int j = 0; j<k; j++)
{
a[j]+=a[j+1];
cout<<a[j]<<" ";
}
k–;
cout<<endl;
}
for( int i=0; i<n;i++)
{
for( int j = 0; j<=i;j++)
{
cout<<a[j]<<" ";
}
cout<=0;j–)
{
a[j] -= a[j+1];
}
}
}
int main(int argc, char ** argv)
{
int N;
cout<>N;
//int a[N] = {4,7,3,6,7};
int array[N];
int i = 0;
while( i >array[i++];
}
TriangleRollup(array,N);
return 0;
}
int main(int argc, char ** argv)
{
int N;
cout<>N;
//int a[N] = {4,7,3,6,7};
int array[N];
int i = 0;
while( i >array[i++];
}
TriangleRollup(array,N);
return 0;
}
Why is some of the code missing when I pasted my code here ?
@Claire: See: https://programmingpraxis.com/contents/howto-posting-source-code/
Easiest thing is to wrap in ‘
‘ lines.
Gaa, that was screwed by formatter as well, I meant, easiest thing is to use the ‘sourcecode’ directive as described in that page.
$input = array(6,10,11,12);
$output = $input;
$triangleList = array();
array_push($triangleList, implode(“,”, $input));
while(count($output) >= 1){
$newList = array();
for($i = 1; $i = 0; $i–){
echo $triangleList[$i].””;
}
In Erlang
Perl newbie here.
print”No. of elements?”;
$n=;
print”enter the elements”;
for($i=0;$i<$n;$i++){
$arr[$i]=;
chomp $arr[$i];
}
print “@arr\n”;
$j=$n;
$l=$n-1;
$i=0;
for($k=0;$k<$l;$k++){
for( ;$i+1<$n;$j++,$i++){
$arr[$j]=$arr[$i]+$arr[$i+1];
print "$arr[$j] ";
}
$n=$j;
$i++;
print "\n";
}
$m=1;
for($i=1;$i<=$l+1;$i++){
for($j=$j-$m,$k=0;$k<$i;$j++,$k++){
print "$arr[$j] ";
}
print "\n";
$m+=2;
}