Padovan n-step number sequences: Difference between revisions

→‎Python: Functional: Updated formatting – foregrounded parallels with with N-Step Fibonacci task.
(Added Perl)
(→‎Python: Functional: Updated formatting – foregrounded parallels with with N-Step Fibonacci task.)
Line 1,062:
Patterns of functional composition are constrained more by mathematical necessity than by arbitrary convention, but this still leaves room for alternative idioms of functional coding in Python. It is to be hoped that others will contribute divergent examples, enriching the opportunities for contrastive insight which Rosetta code aims to provide.
 
<lang python>'''Padovan Nn-step seriesnumber sequences'''
 
from itertools import chain, islice, repeat
 
 
# padovansnStepPadovan :: Int -> [Int]
def padovansnStepPadovan(n):
'''Non-finite series of N-step PadovanFibonacci numbers,
defined by a recurrence relation.
'''
def recurrencerelation(nsxs):
return nsxs[0], nsxs[1:] + [sum(take(n)(nsxs))]
 
return unfoldr(recurrence)(
relation
)(
take(1 + n)(
repeat(1) if 3 > n else padovansnStepPadovan(n - 1)
)
) if 0 <= n else []
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 15 terms each n-step PadovanFibonacci(n) series
where n is drawn from [2..8]
'''
defxs sample= range(n2, 1 + 8):
table = list(
return take(15)(padovans(n))
map(
def columnWidth( lambda k, n): list(
def go chain(xs):
return [k + ' -> '.join([],
str(x).rjust(n, ' ') for x in xs(
] str(x) for x
in take(15)(nStepPadovan(n))
return go
)
)
),
(str(x) for x in xs),
xs
)
)
print('Padovan n-step series:\n')
print(
spacedTable(table)
fTable('Padovan n-step series:')(repr)(
columnWidth(4)
)(sample)(range(2, 1 + 8))
)
 
 
# ----------------------- GENERIC ------------------------
 
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
def go(xs):
return (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
return go
 
 
# unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
def unfoldr(f):
Line 1,122 ⟶ 1,144:
valueResidue = f(valueResidue[1])
return go
 
 
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
def go(xs):
return (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
return go
# ---------------------- FORMATTING ----------------------
 
# fTablespacedTable :: [[String -> (a]] -> String) ->
def spacedTable(rows):
# (b -> String) -> (a -> b) -> [a] -> String
columnWidths = list(map(
def fTable(s):
lambda col: max([len(x) for x in col]),
'''Heading -> x display function ->
zip(*rows)
fx display function -> f -> xs -> tabular string.
'''))
defreturn gox'\n'.join(xShow):[
def' gofx'.join(fxShow):
def gofmap(f):
deflambda goxsx, w: x.rjust(xsw, ' '):,
row, ys = [xShow(x) for x in xs]columnWidths
w = max(map(len, ys))
)
for row in def arrowed(x, y):rows
])
return y.rjust(w, ' ') + ' ->' + (
 
fxShow(f(x))
 
)
return s + '\n' + '\n'.join(
map(arrowed, xs, ys)
)
return goxs
return gof
return gofx
return gox
# MAIN ---
if __name__ == '__main__':
Line 1,172 ⟶ 1,170:
{{Out}}
<pre>Padovan n-step series:
 
2 -> 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
32 -> 1 1 1 2 2 3 4 5 6 7 9 12 1316 1921 28 41 60 88 12937
43 -> 1 1 1 2 3 4 6 5 7 11 17 9 2613 19 4028 41 61 60 94 14488 221129
54 -> 1 1 1 2 3 5 7 11 8 12 19 3017 26 4740 61 74 11694 182144 286221
65 -> 1 1 1 2 3 5 8 12 13 20 32 19 5130 47 8174 129116 205182 326286
76 -> 1 1 1 2 3 5 8 13 20 21 33 5332 51 8581 136129 218205 349326
87 -> 1 1 1 2 3 5 8 13 21 33 3453 85 54136 218 87 140 225 362</pre>349
8 -> 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362</pre>
 
=={{header|Raku}}==
9,655

edits