-
Notifications
You must be signed in to change notification settings - Fork 1
/
lost.py
executable file
·66 lines (57 loc) · 1.75 KB
/
lost.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
#
# lost.py [nth] - return the nth number in the number sequence
# from the TV Show 'Lost'.
#
# See also https://oeis.org/A104101 and
# https://oeis.org/A122115
#
# Copyright (C) 2023 Michael Davies <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
import sys
import os
def lost(n):
return lost_num(n+2)
def lost_num(n):
if n <= 0:
return 0
elif n == 1:
return -3
elif n == 2:
return -1
elif n == 3:
return 4
elif n == 4:
return 8
elif n == 5:
return 15
else:
return lost_num(n-1) + lost_num(n-3) + lost_num(n-5)
def exit_with_usage():
sys.exit(f"Usage: {os.path.basename(sys.argv[0])} [nth]"
f" - return the nth lost number.")
if __name__ == '__main__':
if len(sys.argv) == 1:
for i in range(1, 7):
print(lost(i), end=' ')
elif len(sys.argv) == 2:
try:
print(lost(int(sys.argv[1])))
except ValueError:
exit_with_usage()
else:
exit_with_usage()