added templates and comments

This commit is contained in:
William Ball 2020-07-17 22:36:57 -07:00
parent ed63a65924
commit 484bb8a8a0
11 changed files with 418 additions and 0 deletions

104
cpp/template.cpp Normal file
View file

@ -0,0 +1,104 @@
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using ull = unsigned long long;
auto next(ull x) -> std::vector<ull>;
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<Tree> children;
Tree(ull value, std::vector<ull> 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<ull> {
if (children.size() == 0) {
return { value };
}
std::vector<ull> 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<ull> 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<ull> {
std::vector<ull> new_xs;
return new_xs;
}
/* some helper functions */
auto is_prime(ull n) -> bool {
static std::unordered_map<ull, bool> 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 <typename T>
auto print_vec(std::vector<T> 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());
}
}

19
java/template/Main.java Normal file
View file

@ -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<Long>());
for (int i = 0; i < 20; i++) {
tree.step();
Util.printList(tree.longestPath());
}
}
}

61
java/template/Tree.java Normal file
View file

@ -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<Tree> children;
public Tree(long tree_value, ArrayList<Long> tree_children)
{
value = tree_value;
children = new ArrayList<Tree>();
for (long child : tree_children) {
children.add(new Tree(child, new ArrayList<Long>()));
}
}
public void step()
{
if (children.isEmpty()) {
ArrayList<Long> xs = next(value);
for (long val : xs) {
children.add(new Tree(val, new ArrayList<Long>()));
}
return;
}
for (Tree child : children) {
child.step();
}
}
public ArrayList<Long> longestPath()
{
ArrayList<Long> retval = new ArrayList<Long>();
if (children.isEmpty()) {
retval.add(value);
return retval;
}
int max_length = 0;
for (Tree child : children) {
ArrayList<Long> 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<Long> next(long val)
{
ArrayList<Long> new_xs = new ArrayList<Long>();
return new_xs;
}
}

61
java/template/Util.java Normal file
View file

@ -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<Long, Boolean> primes;
private static HashMap<Long, Boolean> squareFree;
public static void init()
{
primes = new HashMap<Long, Boolean>();
squareFree = new HashMap<Long, Boolean>();
}
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<Long> 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");
}
}

80
python/template.py Normal file
View file

@ -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()

10
rust/template/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "append_digit_primes"
version = "0.1.0"
authors = ["William Ball <wball1@swarthmore.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
primal = "0.3.0"

81
rust/template/src/main.rs Normal file
View file

@ -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<Tree>,
}
impl Tree {
fn new(value: u64, children: Vec<u64>) -> 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<u64> {
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<u64> {
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());
}
}

View file

@ -1,5 +1,7 @@
(load "util.scm") (load "util.scm")
; If you are messing with any of this, you already know what you are doing
(define (val tree) (car tree)) (define (val tree) (car tree))
(define (children tree) (cdr tree)) (define (children tree) (cdr tree))
(define (new-tree val children) (define (new-tree val children)