Boustrophedon transform: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
(Added Python implementation for Boustrophedon transform task) |
||
Line 840: | Line 840: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<!--</syntaxhighlight>--> |
<!--</syntaxhighlight>--> |
||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python"> |
|||
# bt_transform.py by Xing216 |
|||
from math import factorial |
|||
from itertools import islice |
|||
from sys import setrecursionlimit |
|||
setrecursionlimit(1000000) |
|||
def gen_one_followed_by_zeros(): |
|||
yield 1 |
|||
while True: |
|||
yield 0 |
|||
def gen_ones(): |
|||
while True: |
|||
yield 1 |
|||
def gen_alternating_signs(): |
|||
s = 1 |
|||
while True: |
|||
yield s |
|||
s *= -1 |
|||
def gen_primes(): |
|||
"""Code by David Eppstein, UC Irvine, 28 Feb 2002 http://code.activestate.com/recipes/117119/""" |
|||
D = {} |
|||
q = 2 |
|||
while True: |
|||
if q not in D: |
|||
yield q |
|||
D[q * q] = [q] |
|||
else: |
|||
for p in D[q]: |
|||
D.setdefault(p + q, []).append(p) |
|||
del D[q] |
|||
q += 1 |
|||
def gen_fibonacci(): |
|||
a=0 |
|||
b=1 |
|||
while True: |
|||
yield b |
|||
a,b= b,a+b |
|||
def gen_factorials(): |
|||
f = 0 |
|||
while True: |
|||
yield factorial(f) |
|||
f += 1 |
|||
def compressed(n): |
|||
strn = str(n) |
|||
return f"{strn[:20]}...{strn[-20:]} ({len(strn)} digits)" |
|||
def boustrophedon(a): |
|||
# Copied from the Wren Solution |
|||
k = len(a) |
|||
cache = [None] * k |
|||
for i in range(k): cache[i] = [0] * k |
|||
b = [0] * k |
|||
b[0] = a[0] |
|||
def T(k,n): |
|||
if n == 0: return a[k] |
|||
if cache[k][n] > 0: return cache[k][n] |
|||
cache[k][n] = T(k,n-1) + T(k-1,k-n) |
|||
return cache[k][n] |
|||
for n in range(1,k): |
|||
b[n] = T(n,n) |
|||
return b |
|||
funcs = {"One followed by an infinite series of zeros:":gen_one_followed_by_zeros, |
|||
"Infinite sequence of ones:": gen_ones, |
|||
"Alternating 1, -1, 1, -1:": gen_alternating_signs, |
|||
"Sequence of prime numbers:": gen_primes, |
|||
"Sequence of Fibonacci numbers:": gen_fibonacci, |
|||
"Sequence of factorial numbers:": gen_factorials} |
|||
for title, func in funcs.items(): |
|||
x = list(islice(func(), 1000)) |
|||
y = boustrophedon(x) |
|||
print(title) |
|||
print(y[:15]) |
|||
print(f"1000th element: {compressed(y[-1])}\n") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre style="height:15em"> |
|||
[1, 2, 4, 9, 24, 77, 294, 1309, 6664, 38177, 243034, 1701909, 13001604, 107601977, 959021574] |
|||
1000th element: 29375506567920455903...86575529609495110509 (2370 digits) |
|||
Alternating 1, -1, 1, -1: |
|||
[1, 0, 0, 1, 0, 5, 10, 61, 280, 1665, 10470, 73621, 561660, 4650425, 41441530] |
|||
1000th element: 12694307397830194676...15354198638855512941 (2369 digits) |
|||
Sequence of prime numbers: |
|||
[2, 5, 13, 35, 103, 345, 1325, 5911, 30067, 172237, 1096319, 7677155, 58648421, 485377457, 4326008691] |
|||
1000th element: 13250869953362054385...82450325540640498987 (2371 digits) |
|||
Sequence of Fibonacci numbers: |
|||
[1, 2, 5, 14, 42, 144, 563, 2526, 12877, 73778, 469616, 3288428, 25121097, 207902202, 1852961189] |
|||
1000th element: 56757474139659741321...66135597559209657242 (2370 digits) |
|||
Sequence of factorial numbers: |
|||
[1, 2, 5, 17, 73, 381, 2347, 16701, 134993, 1222873, 12279251, 135425553, 1627809401, 21183890469, 296773827547] |
|||
1000th element: 13714256926920345740...19230014799151339821 (2566 digits) |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<syntaxhighlight lang="raku" line>sub boustrophedon-transform (@seq) { map *.tail, (@seq[0], {[[\+] flat @seq[++$ ], .reverse]}…*) } |
<syntaxhighlight lang="raku" line>sub boustrophedon-transform (@seq) { map *.tail, (@seq[0], {[[\+] flat @seq[++$ ], .reverse]}…*) } |