From 18307eebbed6be993c70d29e4a9351d039b8712d Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 22 Nov 2024 19:01:00 -0500 Subject: [PATCH] Add see-it sequence generator --- math-problems/see-it-generator/.gitignore | 1 + math-problems/see-it-generator/README.md | 18 +++++++ math-problems/see-it-generator/main.py | 61 +++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 math-problems/see-it-generator/.gitignore create mode 100644 math-problems/see-it-generator/README.md create mode 100644 math-problems/see-it-generator/main.py diff --git a/math-problems/see-it-generator/.gitignore b/math-problems/see-it-generator/.gitignore new file mode 100644 index 0000000..9347aa6 --- /dev/null +++ b/math-problems/see-it-generator/.gitignore @@ -0,0 +1 @@ +output.csv diff --git a/math-problems/see-it-generator/README.md b/math-problems/see-it-generator/README.md new file mode 100644 index 0000000..4204178 --- /dev/null +++ b/math-problems/see-it-generator/README.md @@ -0,0 +1,18 @@ +# See-it Generator + +The See it sequence is exactly as the name implies, what you see is the next entry in the sequence. For example, + +$$ +1 +11 +21 +1211 +111221 +312211 +13112221 +1113213211 +$$ + +I learned about this sequence from [Genius at Play](https://www.amazon.com/Genius-At-Play-Curious-Horton/dp/1620405938). + +This code just generates this sequence. diff --git a/math-problems/see-it-generator/main.py b/math-problems/see-it-generator/main.py new file mode 100644 index 0000000..0305ed5 --- /dev/null +++ b/math-problems/see-it-generator/main.py @@ -0,0 +1,61 @@ +import pandas as pd +import time + +def main(): + """ + Print out a sequence following the see-it sequence + """ + + upper_bound = 100 + seed = "1" + file_location = "output.csv" + + df = pd.DataFrame(columns=["Iteration", "Length", "Value"]) + + val = seed + + for i in range(0,upper_bound): + #print(f"Iteration : {i}") + #print(f"Length : {len(val)}") + #print(val) + df.loc[len(df)] = [i, len(val), val] + start_time = time.time() + val = next_step(val) + elasped_time = time.time() - start_time + print(f"Iteration : {i} of {upper_bound} (took {elasped_time:.4f} seconds)") + df.to_csv(file_location, index=False) + print("Progress saved to file") + print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") + + print("Result logged to {file_location.csv}") + + +def next_step(sequence: str) -> str: + last_char = None + last_char_count = 0 + + ret = "" + + for char in sequence: + if last_char == None: + last_char = char + last_char_count = 1 + elif char == last_char: + last_char_count += 1 + else: + ret += str(last_char_count) + ret += str(last_char) + last_char_count = 1 + last_char = char + + if ret != None: + ret += str(last_char_count) + ret += str(last_char) + + + return ret + + + +if __name__ == "__main__": + main()