From 484bb8a8a0c5c5d5210ea98a2eb1dfd6a2b6360c Mon Sep 17 00:00:00 2001 From: William Ball Date: Fri, 17 Jul 2020 22:36:57 -0700 Subject: [PATCH] added templates and comments --- cpp/template.cpp | 104 +++++++++++++++++++++++++++++ java/{ => append_primes}/Main.java | 0 java/{ => append_primes}/Tree.java | 0 java/{ => append_primes}/Util.java | 0 java/template/Main.java | 19 ++++++ java/template/Tree.java | 61 +++++++++++++++++ java/template/Util.java | 61 +++++++++++++++++ python/template.py | 80 ++++++++++++++++++++++ rust/template/Cargo.toml | 10 +++ rust/template/src/main.rs | 81 ++++++++++++++++++++++ scheme/tree.scm | 2 + 11 files changed, 418 insertions(+) create mode 100644 cpp/template.cpp rename java/{ => append_primes}/Main.java (100%) rename java/{ => append_primes}/Tree.java (100%) rename java/{ => append_primes}/Util.java (100%) create mode 100644 java/template/Main.java create mode 100644 java/template/Tree.java create mode 100644 java/template/Util.java create mode 100644 python/template.py create mode 100644 rust/template/Cargo.toml create mode 100644 rust/template/src/main.rs diff --git a/cpp/template.cpp b/cpp/template.cpp new file mode 100644 index 0000000..9793c62 --- /dev/null +++ b/cpp/template.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include + +using ull = unsigned long long; + +auto next(ull x) -> std::vector; +auto is_prime(ull n) -> bool; + +/* all tree stuff, don't really need to change it unless you have some ideas for + * optimizations + */ +class Tree +{ +public: + ull value; + std::vector children; + + Tree(ull value, std::vector tree_children) : value(value) { + for (ull child : tree_children) { + children.push_back(Tree(child, {})); + } + } + + void step() { + if (children.size() == 0) { + auto xs = next(value); + for (auto val : xs) { + children.push_back(Tree(val, {})); + } + } else { + for (auto &child : children) { + child.step(); + } + } + } + + auto longest_path() -> std::vector { + if (children.size() == 0) { + return { value }; + } + std::vector max_path; + int max_length = 0; + for (auto &child : children) { + auto temp = child.longest_path(); + if (temp.size() > max_length) { + max_length = temp.size(); + max_path = temp; + } + } + std::vector retval = { value }; + retval.insert(retval.end(), max_path.begin(), max_path.end()); + return retval; + } +}; + +/* TODO Tweak this to change functionality according to explanation on the + * Overleaf + */ +auto next(ull x) -> std::vector { + std::vector new_xs; + return new_xs; +} + +/* some helper functions */ +auto is_prime(ull n) -> bool { + static std::unordered_map primes; + if (n < 2) { + primes[n] = false; + return false; + } + if (primes.count(n) > 0) { + return primes[n]; + } + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + primes[n] = false; + return false; + } + } + primes[n] = true; + return true; +} + +template +auto print_vec(std::vector vec) -> void { + std::cout << '['; + for (T &val : vec) { + std::cout << val << ", "; + } + std::cout << "]\n"; +} + +auto main() -> int { + /* starts off with a tree with value 0 and no children and searches 20 + * iterations, feel free to tweak to whatever you want + */ + Tree tree(0, {}); + for (int i = 0; i < 20; i++) { + tree.step(); + print_vec(tree.longest_path()); + } +} diff --git a/java/Main.java b/java/append_primes/Main.java similarity index 100% rename from java/Main.java rename to java/append_primes/Main.java diff --git a/java/Tree.java b/java/append_primes/Tree.java similarity index 100% rename from java/Tree.java rename to java/append_primes/Tree.java diff --git a/java/Util.java b/java/append_primes/Util.java similarity index 100% rename from java/Util.java rename to java/append_primes/Util.java diff --git a/java/template/Main.java b/java/template/Main.java new file mode 100644 index 0000000..d17bd03 --- /dev/null +++ b/java/template/Main.java @@ -0,0 +1,19 @@ +import java.util.ArrayList; + +/* Here is where you can tweak the starting conditions */ +public class Main +{ + public static void main(String[] args) + { + Util.init(); + + /* here we create a new list with value 0 and no children and check 20 + * iterations, feel free to tweak this to your heart's content + */ + Tree tree = new Tree(0, new ArrayList()); + for (int i = 0; i < 20; i++) { + tree.step(); + Util.printList(tree.longestPath()); + } + } +} diff --git a/java/template/Tree.java b/java/template/Tree.java new file mode 100644 index 0000000..4252ff2 --- /dev/null +++ b/java/template/Tree.java @@ -0,0 +1,61 @@ +import java.util.ArrayList; + +/* Basic tree stuff. I wouldn't recommend tweaking this unless you have some + * ideas for optimizations. Or if my code is bad; it's been a long while since I + * wrote Java. However, please modify `next` below to change functionality as + * described in the Overleaf + */ +class Tree +{ + private long value; + private ArrayList children; + + public Tree(long tree_value, ArrayList tree_children) + { + value = tree_value; + children = new ArrayList(); + for (long child : tree_children) { + children.add(new Tree(child, new ArrayList())); + } + } + + public void step() + { + if (children.isEmpty()) { + ArrayList xs = next(value); + for (long val : xs) { + children.add(new Tree(val, new ArrayList())); + } + return; + } + for (Tree child : children) { + child.step(); + } + } + + public ArrayList longestPath() + { + ArrayList retval = new ArrayList(); + if (children.isEmpty()) { + retval.add(value); + return retval; + } + int max_length = 0; + for (Tree child : children) { + ArrayList temp = child.longestPath(); + if (temp.size() > max_length) { + max_length = temp.size(); + retval = temp; + } + } + retval.add(0, value); + return retval; + } + + /* TODO please tweak this function according to what you want the code to do */ + public ArrayList next(long val) + { + ArrayList new_xs = new ArrayList(); + return new_xs; + } +} diff --git a/java/template/Util.java b/java/template/Util.java new file mode 100644 index 0000000..5e80476 --- /dev/null +++ b/java/template/Util.java @@ -0,0 +1,61 @@ +import java.util.HashMap; +import java.util.ArrayList; + +/* Just some utility functions like isPrime, isSquareFree, and printList, all of + * which do exactly what they sound like. I wouldn't recommend tweaking this + * unless you have some really good ideas for optimization + */ +class Util +{ + private static HashMap primes; + private static HashMap squareFree; + + public static void init() + { + primes = new HashMap(); + squareFree = new HashMap(); + } + + public static boolean isPrime(long val) + { + if (primes.containsKey(val)) { + return primes.get(val); + } + for (int i = 2; i * i <= val; i++) { + if (val % i == 0) { + primes.put(val, false); + return false; + } + } + primes.put(val, true); + return true; + } + + public static boolean isSquareFree(long val) + { + if (squareFree.containsKey(val)) { + return squareFree.get(val); + } + for (int i = 2; i * i <= val; i++) { + if (isPrime(i)) { + if (val % (i * i) == 0) { + squareFree.put(val, false); + return false; + } + } + } + squareFree.put(val, true); + return true; + } + + public static void printList(ArrayList ls) + { + System.out.print("["); + for (int i = 0; i < ls.size() - 1; i++) { + System.out.print(ls.get(i)); + System.out.print(", "); + } + System.out.print(ls.get(ls.size() - 1)); + System.out.print("]\n"); + } +} diff --git a/python/template.py b/python/template.py new file mode 100644 index 0000000..91a54e5 --- /dev/null +++ b/python/template.py @@ -0,0 +1,80 @@ +import math + +# tree stuff, I wouldn't recommend tweaking unless you have serious ideas for +# optimizations +class Tree: + def __init__(self, value, children): + self.value = value + self.children = [] + for child in children: + self.children.append(Tree(child, [])) + + def step(self): + if len(self.children) == 0: + for val in next(self.value): + self.children.append(Tree(val, [])) + return + for child in self.children: + child.step() + + def __str__(self): + retval = '[' + str(self.value) + ': ' + for child in self.children: + retval += str(child) + retval += ']' + return retval + + def __repr__(self): + return str(self) + + def longest_path(self): + if len(self.children) == 0: + return [self.value] + max_path = [] + max_length = 0 + for child in self.children: + temp = child.longest_path() + if len(temp) > max_length: + max_path = temp + max_length = len(temp) + return [self.value] + max_path + +# some utility functions, same as above +def is_square(x): + return int(math.sqrt(x)) == math.sqrt(x) + +primes = {} + +def is_prime(x): + try: + return primes[x] + except KeyError: + if x < 2: + primes[x] = False + return False + i = 2 + while i * i <= x: + if x % i == 0: + primes[x] = False + return False + i += 1 + primes[x] = True + return True + +# TODO: tweak this to do whatever you want as I described in the Overleaf +def next(x): + return [] + + +# here we create a tree with value 0 and no children and check it for 20 +# iterations, but feel free to change that up +def main(): + tree = Tree(0, []) + for i in range(20): + print(tree.longest_path()) + tree.step() + print(tree.longest_path()) + + +if __name__ == '__main__': + main() diff --git a/rust/template/Cargo.toml b/rust/template/Cargo.toml new file mode 100644 index 0000000..91c2789 --- /dev/null +++ b/rust/template/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "append_digit_primes" +version = "0.1.0" +authors = ["William Ball "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +primal = "0.3.0" diff --git a/rust/template/src/main.rs b/rust/template/src/main.rs new file mode 100644 index 0000000..759d9be --- /dev/null +++ b/rust/template/src/main.rs @@ -0,0 +1,81 @@ +/* tree stuff, I wouldn't recommend tweaking any of this unless you have serious ideas for + * optimization + */ +#[derive(Debug)] +struct Tree { + value: u64, + children: Vec, +} + +impl Tree { + fn new(value: u64, children: Vec) -> Tree { + let mut tree_children = Vec::with_capacity(children.len()); + for child in children { + tree_children.push(Tree::new(child, Vec::new())); + } + Tree { + value, + children: tree_children, + } + } + + fn to_string(&self) -> String { + let mut retval = format!("[{}: ", self.value); + for child in &self.children { + retval.push_str(&child.to_string()[..]); + } + retval.push(']'); + retval + } + + fn step(&mut self) { + if self.children.is_empty() { + let xs = next(self.value); + for val in xs { + self.children.push(Tree::new(val, Vec::new())); + } + return; + } + for child in &mut self.children { + child.step(); + } + } + + fn longest_path(&self) -> Vec { + if self.children.is_empty() { + return vec![self.value]; + } + let mut max_path = vec![]; + let mut max_length = 0; + for child in &self.children { + let temp = child.longest_path(); + if temp.len() > max_length { + max_length = temp.len(); + max_path = temp; + } + } + let mut retval = vec![self.value]; + retval.append(&mut max_path); + return retval; + } +} + +/* TODO: tweak this function like I mentioned in the Overleaf to get this to do what you want; also + * NOTE: use `Primal::is_prime` to test if a number is prime; it's a library that is way faster + * than anything I could write + */ +fn next(x: u64) -> Vec { + let mut new_xs = Vec::new(); + return new_xs; +} + +/* here are the initial conditions. By default it creates a tree with value 0 and no children, but + * feel free to tweak this to your heart's content + */ +fn main() { + let mut tree = Tree::new(0, Vec::new()); + for _ in 0..20 { + tree.step(); + println!("{:?}", tree.longest_path()); + } +} diff --git a/scheme/tree.scm b/scheme/tree.scm index aa4ea44..f5793d5 100644 --- a/scheme/tree.scm +++ b/scheme/tree.scm @@ -1,5 +1,7 @@ (load "util.scm") +; If you are messing with any of this, you already know what you are doing + (define (val tree) (car tree)) (define (children tree) (cdr tree)) (define (new-tree val children)