Skip to content

Instantly share code, notes, and snippets.

@laudai
Last active April 2, 2021 16:06
Show Gist options
  • Save laudai/696a335c3b912e30593987ffbeb77e48 to your computer and use it in GitHub Desktop.
Save laudai/696a335c3b912e30593987ffbeb77e48 to your computer and use it in GitHub Desktop.
Questen : feed a sequence, it will be an any sequence. Please answer the following 10 numbers.
#!/usr/bin/env python
# encoding: utf8
# Author : laudai
# codestyle : black
from typing import List, Tuple
import sys
"""
Questen : feed a sequence, it will be an any sequence.
Please answer the following 10 numbers.
Usage :
python3 guess_sequence.py 1 2 4
python3 guess_sequence.py 1 5 9
python3 guess_sequence.py 2 4 6
python3 guess_sequence.py 2 4 8
python3 guess_sequence.py 10 20 30
python3 guess_sequence.py 14 24 34 44 54
python3 guess_sequence.py 74 154 314 634
python3 guess_sequence.py 0 0 0
python3 guess_sequence.py 0 1 2
"""
def getdata() -> List[int]:
data_list = [*sys.argv[1:]]
return data_list
def checkargvtype(datas_list: list) -> int:
"""
check all data is integer, return int bool (0 or 1)
"""
try:
changed_list = [int(data) for data in datas_list]
except ValueError:
return False
return True
def checkSequenceandMagnification(checked_list: list) -> Tuple[str, int, int]:
"""
return the alpha and beta value that is the magnification
"""
alpha, beta = 0, 0
alpha_list, beta_list = [], []
for index, _ in enumerate(checked_list):
if index + 1 == len(checked_list):
break
try:
alpha = checked_list[index + 1] // checked_list[index]
beta = checked_list[index + 1] % checked_list[index]
except ZeroDivisionError:
alpha_list.append(0)
beta_list.append(0)
else:
alpha_list.append(alpha)
beta_list.append(beta)
# get true when arithmetic or fibonacci sequence
if beta_list[0] ^ beta_list[1] > 0 or alpha_list[0] != alpha_list[1]:
alpha_list = [0] * (len(checked_list) - 1)
beta_list = getdelta(checked_list)
alphaallthesame_bool = all(element == alpha_list[0] for element in alpha_list)
betaallthesame_bool = all(element == beta_list[0] for element in beta_list)
# check whehter fibonacci sequence
if alphaallthesame_bool and betaallthesame_bool:
return "arithmetic or geometric sequence", alpha_list[0], beta_list[0]
else:
return "fibonacci sequence", checked_list[-2], checked_list[-1]
return "arithmetic or geometric sequence", alpha_list[0], beta_list[0]
def getdelta(checked_list: list):
delta = 0
delta_list = []
for index, _ in enumerate(checked_list):
if index + 1 == len(checked_list):
break
delta = checked_list[index + 1] - checked_list[index]
delta_list.append(delta)
return delta_list
def fibonacci(num1, num2):
count, a, b = 0, num1, num2
while True:
if count == 11:
break
a, b = b, a + b
yield a
count += 1
def main():
datas_list = getdata()
if checkargvtype(datas_list):
strdata2int_list = [int(data) for data in datas_list]
print("type check : correct. All arguments are number.")
else:
print("type check : error. Please check all arguments are number, quit program.")
sys.exit()
sequence, alpha, beta = checkSequenceandMagnification(strdata2int_list)
print(sequence)
if sequence == "arithmetic or geometric sequence":
if alpha != 0:
initvalue = strdata2int_list[-1]
value_list = [alpha * initvalue + beta]
for index in range(9):
value_list.append(alpha * value_list[index] + beta)
print("Answer:", value_list)
else:
initvalue = strdata2int_list[-1]
value_list = [initvalue + beta]
for index in range(9):
value_list.append(value_list[index] + beta)
print("Answer:", value_list)
elif sequence == "fibonacci sequence":
fib = fibonacci(alpha, beta)
fib_list = [i for i in fib]
print("Answer:", fib_list[-10:])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment