September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
19
Task/Pascals-triangle/ALGOL-W/pascals-triangle.alg
Normal file
19
Task/Pascals-triangle/ALGOL-W/pascals-triangle.alg
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
begin
|
||||
% prints the first n lines of Pascal's triangle lines %
|
||||
% if n is <= 0, no output is produced %
|
||||
procedure printPascalTriangle( integer value n ) ;
|
||||
if n > 0 then begin
|
||||
integer array pascalLine ( 1 :: n );
|
||||
pascalLine( 1 ) := 1;
|
||||
for line := 1 until n do begin
|
||||
for i := line - 1 step - 1 until 2 do pascalLine( i ) := pascalLine( i - 1 ) + pascalLine( i );
|
||||
pascalLine( line ) := 1;
|
||||
write( s_w := 0, " " );
|
||||
for i := line until n do writeon( s_w := 0, " " );
|
||||
for i := 1 until line do writeon( i_w := 6, s_w := 0, pascalLine( i ) )
|
||||
end for_line ;
|
||||
end printPascalTriangle ;
|
||||
|
||||
printPascalTriangle( 8 )
|
||||
|
||||
end.
|
||||
|
|
@ -1,57 +1,73 @@
|
|||
-- PASCAL ---------------------------------------------------------------------
|
||||
|
||||
-- pascal :: Int -> [[Int]]
|
||||
on pascal(intRows)
|
||||
|
||||
script addRow
|
||||
on nextRow(row)
|
||||
script add
|
||||
on lambda(a, b)
|
||||
on |λ|(a, b)
|
||||
a + b
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
zipWith(add, [0] & row, row & [0])
|
||||
end nextRow
|
||||
|
||||
on lambda(xs)
|
||||
on |λ|(xs)
|
||||
xs & {nextRow(item -1 of xs)}
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldr(addRow, {{1}}, range(1, intRows - 1))
|
||||
foldr(addRow, {{1}}, enumFromTo(1, intRows - 1))
|
||||
end pascal
|
||||
|
||||
|
||||
-- TEST
|
||||
|
||||
-- TEST -----------------------------------------------------------------------
|
||||
on run
|
||||
set lstTriangle to pascal(7)
|
||||
|
||||
script spaced
|
||||
on lambda(xs)
|
||||
on |λ|(xs)
|
||||
script rightAlign
|
||||
on lambda(x)
|
||||
on |λ|(x)
|
||||
text -4 thru -1 of (" " & x)
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
intercalate("", map(rightAlign, xs))
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
script indented
|
||||
on lambda(a, x)
|
||||
on |λ|(a, x)
|
||||
set strIndent to leftSpace of a
|
||||
|
||||
{rows:strIndent & x & linefeed & rows of a, leftSpace:leftSpace of a & " "}
|
||||
end lambda
|
||||
{rows:¬
|
||||
strIndent & x & linefeed & rows of a, leftSpace:¬
|
||||
leftSpace of a & " "} ¬
|
||||
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
rows of foldr(indented, {rows:"", leftSpace:""}, map(spaced, lstTriangle))
|
||||
rows of foldr(indented, ¬
|
||||
{rows:"", leftSpace:""}, map(spaced, lstTriangle))
|
||||
end run
|
||||
|
||||
-- GENERIC FUNCTIONS ----------------------------------------------------------
|
||||
|
||||
|
||||
-- GENERIC LIBRARY FUNCTIONS
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end enumFromTo
|
||||
|
||||
-- foldr :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldr(f, startValue, xs)
|
||||
|
|
@ -59,51 +75,12 @@ on foldr(f, startValue, xs)
|
|||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from lng to 1 by -1
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldr
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set nx to length of xs
|
||||
set ny to length of ys
|
||||
if nx < 1 or ny < 1 then
|
||||
{}
|
||||
else
|
||||
set lng to cond(nx < ny, nx, ny)
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end if
|
||||
end zipWith
|
||||
|
||||
-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)
|
||||
on cond(bool, f, g)
|
||||
if bool then
|
||||
f
|
||||
else
|
||||
g
|
||||
end if
|
||||
end cond
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
|
|
@ -112,16 +89,26 @@ on intercalate(strText, lstText)
|
|||
return strJoined
|
||||
end intercalate
|
||||
|
||||
-- range :: Int -> Int -> [Int]
|
||||
on range(m, n)
|
||||
set lng to (n - m) + 1
|
||||
set base to m - 1
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to i + base
|
||||
end repeat
|
||||
return lst
|
||||
end range
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- min :: Ord a => a -> a -> a
|
||||
on min(x, y)
|
||||
if y < x then
|
||||
y
|
||||
else
|
||||
x
|
||||
end if
|
||||
end min
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
|
|
@ -130,7 +117,19 @@ on mReturn(f)
|
|||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set lng to min(length of xs, length of ys)
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end zipWith
|
||||
|
|
|
|||
11
Task/Pascals-triangle/Common-Lisp/pascals-triangle-3.lisp
Normal file
11
Task/Pascals-triangle/Common-Lisp/pascals-triangle-3.lisp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(defun next-pascal (l)
|
||||
`(1 ,@(loop for i from 0 to (- (length l) 2)
|
||||
collect (+ (nth i l) (nth (1+ i) l)))
|
||||
1))
|
||||
|
||||
(defun pascal-print (r)
|
||||
(let* ((pasc (loop with p = (list (list 1))
|
||||
repeat r do (nconc p (list (apply #'next-pascal (last p))))
|
||||
finally (return p)))
|
||||
(len (length (format nil "~A" (car (last pasc))))))
|
||||
(format t (format nil "~~{~~~D:@<~~{~~A ~~}~~>~~%~~}" len) pasc)))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(pascal-print 4)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
1 4 6 4 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
(pascal-print 8)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
1 4 6 4 1
|
||||
1 5 10 10 5 1
|
||||
1 6 15 20 15 6 1
|
||||
1 7 21 35 35 21 7 1
|
||||
1 8 28 56 70 56 28 8 1
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
MODULE PascalTriangle;
|
||||
IMPORT StdLog, DevCommanders, TextMappers;
|
||||
|
||||
TYPE
|
||||
Expansion* = POINTER TO ARRAY OF LONGINT;
|
||||
|
||||
PROCEDURE Show*(e: Expansion);
|
||||
VAR
|
||||
i: INTEGER;
|
||||
BEGIN
|
||||
i := 0;
|
||||
WHILE (i < LEN(e)) & (e[i] # 0) DO
|
||||
StdLog.Int(e[i]);
|
||||
INC(i)
|
||||
END;
|
||||
StdLog.Ln
|
||||
END Show;
|
||||
|
||||
PROCEDURE GenFor*(p: LONGINT): Expansion;
|
||||
VAR
|
||||
expA,expB: Expansion;
|
||||
i,j: LONGINT;
|
||||
|
||||
PROCEDURE Swap(VAR x,y: Expansion);
|
||||
VAR
|
||||
swap: Expansion;
|
||||
BEGIN
|
||||
swap := x; x := y; y := swap
|
||||
END Swap;
|
||||
|
||||
BEGIN
|
||||
ASSERT(p >= 0);
|
||||
NEW(expA,p + 2);NEW(expB,p + 2);
|
||||
FOR i := 0 TO p DO
|
||||
IF i = 0 THEN expA[0] := 1
|
||||
ELSE
|
||||
FOR j := 0 TO i DO
|
||||
IF j = 0 THEN
|
||||
expB[j] := expA[j]
|
||||
ELSE
|
||||
expB[j] := expA[j - 1] + expA[j]
|
||||
END
|
||||
END;
|
||||
Swap(expA,expB)
|
||||
END;
|
||||
END;
|
||||
expB := NIL; (* for the GC *)
|
||||
RETURN expA
|
||||
END GenFor;
|
||||
|
||||
|
||||
PROCEDURE Do*;
|
||||
VAR
|
||||
s: TextMappers.Scanner;
|
||||
exp: Expansion;
|
||||
BEGIN
|
||||
s.ConnectTo(DevCommanders.par.text);
|
||||
s.SetPos(DevCommanders.par.beg);
|
||||
s.Scan;
|
||||
WHILE (~s.rider.eot) DO
|
||||
IF (s.type = TextMappers.char) & (s.char = '~') THEN
|
||||
RETURN
|
||||
ELSIF (s.type = TextMappers.int) THEN
|
||||
exp := GenFor(s.int);
|
||||
Show(exp)
|
||||
END;
|
||||
s.Scan
|
||||
END
|
||||
END Do;
|
||||
|
||||
END PascalTriangle.
|
||||
39
Task/Pascals-triangle/Dart/pascals-triangle.dart
Normal file
39
Task/Pascals-triangle/Dart/pascals-triangle.dart
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import 'dart:io';
|
||||
|
||||
pascal(n) {
|
||||
if(n<=0) print("Not defined");
|
||||
|
||||
else if(n==1) print(1);
|
||||
|
||||
else {
|
||||
List<List<int>> matrix = new List<List<int>>();
|
||||
matrix.add(new List<int>());
|
||||
matrix.add(new List<int>());
|
||||
matrix[0].add(1);
|
||||
matrix[1].add(1);
|
||||
matrix[1].add(1);
|
||||
for (var i = 2; i < n; i++) {
|
||||
List<int> list = new List<int>();
|
||||
list.add(1);
|
||||
for (var j = 1; j<i; j++) {
|
||||
list.add(matrix[i-1][j-1]+matrix[i-1][j]);
|
||||
}
|
||||
list.add(1);
|
||||
matrix.add(list);
|
||||
}
|
||||
for(var i=0; i<n; i++) {
|
||||
for(var j=0; j<=i; j++) {
|
||||
stdout.write(matrix[i][j]);
|
||||
stdout.write(' ');
|
||||
}
|
||||
stdout.write('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
pascal(0);
|
||||
pascal(1);
|
||||
pascal(3);
|
||||
pascal(6);
|
||||
}
|
||||
|
|
@ -1,63 +1,79 @@
|
|||
(function (n) {
|
||||
'use strict';
|
||||
|
||||
// A Pascal triangle of n rows
|
||||
// PASCAL TRIANGLE --------------------------------------------------------
|
||||
|
||||
// pascal :: Int -> [[Int]]
|
||||
function pascal(n) {
|
||||
return range(1, n - 1)
|
||||
.reduce(function (a) {
|
||||
var lstPreviousRow = a.slice(-1)[0];
|
||||
|
||||
return a
|
||||
.concat(
|
||||
[zipWith(
|
||||
function (a, b) {
|
||||
return a + b
|
||||
},
|
||||
[0].concat(lstPreviousRow),
|
||||
lstPreviousRow.concat(0)
|
||||
)]
|
||||
);
|
||||
}, [[1]]);
|
||||
}
|
||||
return foldl(function (a) {
|
||||
var xs = a.slice(-1)[0]; // Previous row
|
||||
return append(a, [zipWith(
|
||||
function (a, b) {
|
||||
return a + b;
|
||||
},
|
||||
append([0], xs),
|
||||
append(xs, [0])
|
||||
)]);
|
||||
}, [
|
||||
[1] // Initial seed row
|
||||
], enumFromTo(1, n - 1));
|
||||
};
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
// (++) :: [a] -> [a] -> [a]
|
||||
function append(xs, ys) {
|
||||
return xs.concat(ys);
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
function enumFromTo(m, n) {
|
||||
return Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, function (_, i) {
|
||||
return m + i;
|
||||
});
|
||||
};
|
||||
|
||||
// foldl :: (b -> a -> b) -> b -> [a] -> b
|
||||
function foldl(f, a, xs) {
|
||||
return xs.reduce(f, a);
|
||||
};
|
||||
|
||||
// foldr (a -> b -> b) -> b -> [a] -> b
|
||||
function foldr(f, a, xs) {
|
||||
return xs.reduceRight(f, a);
|
||||
};
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
function map(f, xs) {
|
||||
return xs.map(f);
|
||||
};
|
||||
|
||||
// min :: Ord a => a -> a -> a
|
||||
function min(a, b) {
|
||||
return b < a ? b : a;
|
||||
};
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
function zipWith(f, xs, ys) {
|
||||
return xs.length === ys.length ? (
|
||||
xs.map(function (x, i) {
|
||||
return f(x, ys[i]);
|
||||
})
|
||||
) : undefined;
|
||||
}
|
||||
return Array.from({
|
||||
length: min(xs.length, ys.length)
|
||||
}, function (_, i) {
|
||||
return f(xs[i], ys[i]);
|
||||
});
|
||||
};
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1))
|
||||
.map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
|
||||
// TEST
|
||||
// TEST and FORMAT --------------------------------------------------------
|
||||
var lstTriangle = pascal(n);
|
||||
|
||||
|
||||
// FORMAT OUTPUT AS WIKI TABLE
|
||||
|
||||
// [[a]] -> bool -> s -> s
|
||||
function wikiTable(lstRows, blnHeaderRow, strStyle) {
|
||||
return '{| class="wikitable" ' + (
|
||||
strStyle ? 'style="' + strStyle + '"' : ''
|
||||
) + lstRows.map(function (lstRow, iRow) {
|
||||
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
|
||||
|
||||
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (
|
||||
v) {
|
||||
return '{| class="wikitable" ' + (strStyle ? 'style="' + strStyle +
|
||||
'"' : '') + lstRows.map(function (lstRow, iRow) {
|
||||
var strDelim = blnHeaderRow && !iRow ? '!' : '|';
|
||||
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
|
||||
return typeof v === 'undefined' ? ' ' : v;
|
||||
})
|
||||
.join(' ' + strDelim + strDelim + ' ');
|
||||
|
|
@ -66,29 +82,21 @@
|
|||
}
|
||||
|
||||
var lstLastLine = lstTriangle.slice(-1)[0],
|
||||
lngBase = (lstLastLine.length * 2) - 1,
|
||||
lngBase = lstLastLine.length * 2 - 1,
|
||||
nWidth = lstLastLine.reduce(function (a, x) {
|
||||
var d = x.toString()
|
||||
.length;
|
||||
return d > a ? d : a;
|
||||
}, 1) * lngBase;
|
||||
|
||||
return [
|
||||
wikiTable(
|
||||
lstTriangle.map(function (lst) {
|
||||
return lst.join(';;')
|
||||
.split(';');
|
||||
})
|
||||
.map(function (line, i) {
|
||||
var lstPad = Array((lngBase - line.length) / 2);
|
||||
return lstPad.concat(line)
|
||||
.concat(lstPad);
|
||||
}),
|
||||
false,
|
||||
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
|
||||
'em;table-layout:fixed;'
|
||||
),
|
||||
|
||||
JSON.stringify(lstTriangle)
|
||||
].join('\n\n');
|
||||
return [wikiTable(lstTriangle.map(function (lst) {
|
||||
return lst.join(';;')
|
||||
.split(';');
|
||||
})
|
||||
.map(function (line, i) {
|
||||
var lstPad = Array((lngBase - line.length) / 2);
|
||||
return lstPad.concat(line)
|
||||
.concat(lstPad);
|
||||
}), false, 'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
|
||||
'em;table-layout:fixed;'), JSON.stringify(lstTriangle)].join('\n\n');
|
||||
})(7);
|
||||
|
|
|
|||
|
|
@ -1,50 +1,68 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// PASCAL'S TRIANGLE ------------------------------------------------------
|
||||
|
||||
// pascal :: Int -> [[Int]]
|
||||
let pascal = n =>
|
||||
range(1, n - 1)
|
||||
.reduce(a => {
|
||||
let lstPreviousRow = a.slice(-1)[0];
|
||||
|
||||
return a
|
||||
.concat([zipWith((a, b) => a + b,
|
||||
[0].concat(lstPreviousRow),
|
||||
lstPreviousRow.concat(0)
|
||||
)]);
|
||||
}, [
|
||||
[1]
|
||||
const pascal = n =>
|
||||
foldl(a => {
|
||||
const xs = a.slice(-1)[0]; // Previous row
|
||||
return append(a, [
|
||||
zipWith(
|
||||
(a, b) => a + b,
|
||||
append([0], xs),
|
||||
append(xs, [0])
|
||||
)
|
||||
]);
|
||||
}, [
|
||||
[1] // Initial seed row
|
||||
], enumFromTo(1, n - 1));
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
let range = (m, n, step) => {
|
||||
let d = (step || 1) * (n >= m ? 1 : -1);
|
||||
return Array.from({
|
||||
length: Math.floor((n - m) / d) + 1
|
||||
}, (_, i) => m + (i * d));
|
||||
},
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
zipWith = (f, xs, ys) =>
|
||||
xs.length === ys.length ? (
|
||||
xs.map((x, i) => f(x, ys[i]))
|
||||
) : undefined;
|
||||
// (++) :: [a] -> [a] -> [a]
|
||||
const append = (xs, ys) => xs.concat(ys);
|
||||
|
||||
// TEST
|
||||
return pascal(7)
|
||||
.reduceRight((a, x) => {
|
||||
let strIndent = a.indent;
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
const flip = f => (a, b) => f(b, a);
|
||||
|
||||
// foldl :: (b -> a -> b) -> b -> [a] -> b
|
||||
const foldl = (f, a, xs) => xs.reduce(f, a);
|
||||
|
||||
// foldr (a -> b -> b) -> b -> [a] -> b
|
||||
const foldr = (f, a, xs) => xs.reduceRight(flip(f), a);
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// min :: Ord a => a -> a -> a
|
||||
const min = (a, b) => b < a ? b : a;
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = (f, xs, ys) =>
|
||||
Array.from({
|
||||
length: min(xs.length, ys.length)
|
||||
}, (_, i) => f(xs[i], ys[i]));
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
return foldr((x, a) => {
|
||||
const strIndent = a.indent;
|
||||
return {
|
||||
rows: strIndent + x
|
||||
.map(n => (' ' + n).slice(-4))
|
||||
rows: strIndent + map(n => (' ' + n)
|
||||
.slice(-4), x)
|
||||
.join('') + '\n' + a.rows,
|
||||
indent: strIndent + ' '
|
||||
};
|
||||
}, {
|
||||
rows: '',
|
||||
indent: ''
|
||||
}).rows;
|
||||
}, pascal(7))
|
||||
.rows;
|
||||
})();
|
||||
|
|
|
|||
8
Task/Pascals-triangle/Julia/pascals-triangle.julia
Normal file
8
Task/Pascals-triangle/Julia/pascals-triangle.julia
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
iround(x) = round(Int64, x)
|
||||
|
||||
triangle(n) = iround.(expm(diagm(1:n, -1)))
|
||||
|
||||
function pascal(n)
|
||||
println.(join.([filter(!iszero, triangle(n)[i,:]) for i in 1:(n+1)], " "))
|
||||
return
|
||||
end
|
||||
7
Task/Pascals-triangle/Q/pascals-triangle.q
Normal file
7
Task/Pascals-triangle/Q/pascals-triangle.q
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
pascal:{(x-1){0+':x,0}\1}
|
||||
pascal 5
|
||||
1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
1 4 6 4 1
|
||||
|
|
@ -4,11 +4,7 @@
|
|||
(define (next-row current-row)
|
||||
(map + (cons 0 current-row)
|
||||
(append current-row '(0))))
|
||||
(let-values
|
||||
([(previous-rows final-row)
|
||||
(for/fold ([triangle null]
|
||||
[row '(1)])
|
||||
([row-number (in-range 1 n)])
|
||||
(values (cons row triangle)
|
||||
(next-row row)))])
|
||||
(reverse (cons final-row previous-rows))))
|
||||
(reverse
|
||||
(for/fold ([triangle '((1))])
|
||||
([row (in-range 1 n)])
|
||||
(cons (next-row (first triangle)) triangle))))
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
func pascal(rows) {
|
||||
var row = [1];
|
||||
var row = [1]
|
||||
{ | n|
|
||||
say row.join(' ');
|
||||
row = [1, 0..(n-2).map {|i| row[i] + row[i+1] }..., 1];
|
||||
} * rows;
|
||||
say row.join(' ')
|
||||
row = [1, {|i| row[i] + row[i+1] }.map(0 .. n-2)..., 1]
|
||||
} << 1..rows
|
||||
}
|
||||
|
||||
pascal(10);
|
||||
|
||||
pascal(10)
|
||||
|
|
|
|||
22
Task/Pascals-triangle/Swift/pascals-triangle.swift
Normal file
22
Task/Pascals-triangle/Swift/pascals-triangle.swift
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
func pascal(n:Int)->[Int]{
|
||||
if n==1{
|
||||
let a=[1]
|
||||
print(a)
|
||||
return a
|
||||
}
|
||||
else{
|
||||
var a=pascal(n:n-1)
|
||||
var temp=a
|
||||
for i in 0..<a.count{
|
||||
if i+1==a.count{
|
||||
temp.append(1)
|
||||
break
|
||||
}
|
||||
temp[i+1] = a[i]+a[i+1]
|
||||
}
|
||||
a=temp
|
||||
print(a)
|
||||
return a
|
||||
}
|
||||
}
|
||||
let waste = pascal(n:10)
|
||||
13
Task/Pascals-triangle/Zkl/pascals-triangle.zkl
Normal file
13
Task/Pascals-triangle/Zkl/pascals-triangle.zkl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
fcn pascalTriangle(n){ // n<=0-->""
|
||||
foreach i in (n){
|
||||
c := 1;
|
||||
print(" "*(2*(n-1-i)));
|
||||
foreach k in (i+1){
|
||||
print("%3d ".fmt(c));
|
||||
c = c * (i-k)/(k+1);
|
||||
}
|
||||
println();
|
||||
}
|
||||
}
|
||||
|
||||
pascalTriangle(8);
|
||||
Loading…
Add table
Add a link
Reference in a new issue