Py310: recursion with match-case
kirby urner <[email protected]> Tue, 16 Nov 2021 17:58:46 -0800
| Newsgroups | gmane.comp.python.education |
|---|---|
| Message-ID | <CAPJgG3RJ1DhZd7pq3+cCKXPe2-bZNQTcS+rfP8FiEzpi0363pA@mail.gmail.com> |
So are we encouraged to use match-case in recursion instead of if-else?
It's more readable this way maybe:
cubocta310.py:
def cubocta(n):
"""
https://oeis.org/A005902
"""
match n:
case 0: return 1
case _: return (10*n*n + 2) + cubocta(n - 1)
print([cubocta(i) for i in range(10)])
(py310) Kirbys-MacBook-Pro:School_of_Tomorrow mac$ python cubocta310.py
[1, 13, 55, 147, 309, 561, 923, 1415, 2057, 2869]
https://flic.kr/p/2mKh2nd
(image version)
Kirby
_______________________________________________
Edu-sig mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/edu-sig.python.org/
Member address: [email protected]