From db882dd5dcf7f4b63a4104bd78e499184c1ed008 Mon Sep 17 00:00:00 2001 From: nkoch001 Date: Fri, 26 Oct 2018 18:05:57 +0200 Subject: [PATCH] reordered files to support more languagues than just rust, also added a few python challenges --- python/dice.rolls | 7 + python/dice_roller.py | 51 +++++++ python/easy_word_funnel.py | 31 ++++ rust/rabbits/Cargo.toml | 6 + rust/rabbits/src/main.rs | 103 +++++++++++++ rust/tally/.idea/misc.xml | 13 ++ rust/tally/.idea/modules.xml | 8 + rust/tally/.idea/tally.iml | 9 ++ rust/tally/.idea/workspace.xml | 267 +++++++++++++++++++++++++++++++++ rust/tally/Cargo.lock | 4 + rust/tally/Cargo.toml | 6 + rust/tally/src/main.rs | 42 ++++++ 12 files changed, 547 insertions(+) create mode 100644 python/dice.rolls create mode 100644 python/dice_roller.py create mode 100644 python/easy_word_funnel.py create mode 100644 rust/rabbits/Cargo.toml create mode 100644 rust/rabbits/src/main.rs create mode 100644 rust/tally/.idea/misc.xml create mode 100644 rust/tally/.idea/modules.xml create mode 100644 rust/tally/.idea/tally.iml create mode 100644 rust/tally/.idea/workspace.xml create mode 100644 rust/tally/Cargo.lock create mode 100644 rust/tally/Cargo.toml create mode 100644 rust/tally/src/main.rs diff --git a/python/dice.rolls b/python/dice.rolls new file mode 100644 index 0000000..cf4f78f --- /dev/null +++ b/python/dice.rolls @@ -0,0 +1,7 @@ +5d12 +6d4 +1d2 +1d8 +3d6 +4d20 +100d100 diff --git a/python/dice_roller.py b/python/dice_roller.py new file mode 100644 index 0000000..9a3347b --- /dev/null +++ b/python/dice_roller.py @@ -0,0 +1,51 @@ +from random import randint + +def roll(number, sides): + """ + roll a number of virtual dice with a given number of sides + """ + res = 0 + throws = [] + for die in range(number): + throws.append(randint(1, sides)) + res += throws[-1] + print(res, ":", *throws) + return res + +def parselist(path): + """ + parse a list containing dice rolls (one per line) a la + 5d12 + 6d4 + 1d2 + 1d8 + 3d6 + 4d20 + 100d100 + """ + roll_call = [] + with open(path, 'r') as rolls: + for line in rolls: + line = line.strip() + roll_call.append(line.split('d')) + + return roll_call + + +if __name__ == '__main__': + pali = parselist('dice.rolls') + print(pali) + for to_roll in pali: + n, s = to_roll + print("Rolling", n + "d" + s) + print("Result:", roll(int(n), int(s)), '\n') + + # interactive mode + x = 0 + while x != 'x': + x = input("please input what you want to throw as 'NdM', or 'x' to quit\n") + if len(x.split('d')) == 2: + n, m = x.split('d') + roll(int(n), int(m)) + + diff --git a/python/easy_word_funnel.py b/python/easy_word_funnel.py new file mode 100644 index 0000000..3b3460b --- /dev/null +++ b/python/easy_word_funnel.py @@ -0,0 +1,31 @@ +def funnel_naive(string1, string2): + """ + detect if the second string can be made from the first by removing one letter + """ + if len(string1) - len(string2) != 1: + return False + if string2 in string1: + return True + miss = 0 + for i, letter2 in enumerate(string2): + if letter2 != string1[i]: + if miss > 1: + return False + miss += 1 + if letter2 == string1[i + 1]: + i += 1 + else: + return False + + return True + +funnel = lambda word, other: other in [w[:i] + w[i+1:] for i, w in enumerate([word] * len(word))] + +print(funnel_naive("leave", "leav")) +print(funnel("leave", "eave")) +print(funnel("reset", "rest")) +print(funnel("dragoon", "dragon")) +print(funnel("eave", "leave")) +print(funnel("sleet", "lets")) +print(funnel("skiff", "ski")) + diff --git a/rust/rabbits/Cargo.toml b/rust/rabbits/Cargo.toml new file mode 100644 index 0000000..cf38f98 --- /dev/null +++ b/rust/rabbits/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rabbits" +version = "0.1.0" +authors = ["Cpt. Captain "] + +[dependencies] diff --git a/rust/rabbits/src/main.rs b/rust/rabbits/src/main.rs new file mode 100644 index 0000000..2dc51f4 --- /dev/null +++ b/rust/rabbits/src/main.rs @@ -0,0 +1,103 @@ +// https://www.reddit.com/r/dailyprogrammer/comments/7s888w/20180122_challenge_348_easy_the_rabbit_problem/ + +use std::{thread, time, io}; + +fn init(male: i128, female: i128, needed_alive: i128) -> ([i128; 96], [i128; 96], i128){ + + let mut m: [i128; 96] = [0; 96]; + let mut f: [i128; 96] = [0; 96]; + m[2] = male; + f[2] = female; + return (m, f, needed_alive); +} + + +// reproduce, by letting all females 4 months and over have 9 female and 5 male descendants +// fn reproduce(m_pop: [i128; 96], f_pop: [i128; 96]) -> ([i128; 96], [i128;96], i128, i128) { +fn reproduce(m_pop: [i128; 96], f_pop: [i128; 96]) -> (i128, i128) { + let mut res_m = 0; + let mut res_f = 0; + let f_iter = f_pop.iter().enumerate(); + for (i, count) in f_iter { + if i < 95 && i > 3{ + // res_f[0] = res_f[0] + count * 9; + // res_m[0] = res_m[0] + count * 5; + res_m = res_m + count * 5; + res_f = res_f + count * 9; + } + } + return (res_m, res_f); +} + +// age the bunny population by shifting the array one to the right +fn age(m_pop: [i128; 96], f_pop: [i128; 96], new_males: i128, new_females: i128) -> ([i128; 96], [i128; 96]){ + let mut res_m: [i128; 96] = [0; 96]; + let mut res_f: [i128; 96] = [0; 96]; + + res_m[0] = new_males; + res_f[0] = new_females; + + let f_iter = f_pop.iter().enumerate(); + let m_iter = m_pop.iter().enumerate(); + for (i, count) in f_iter { + if i < 95{ + res_f[i+1] = *count; + } + } + for (i, count) in m_iter { + if i < 95{ + res_m[i+1] = *count; + } + } + return (res_m, res_f); +} + +fn main() { + + // this whole part is just for reading from stdin... + + let mut input_m = String::new(); + let mut input_f = String::new(); + let mut input_n = String::new(); + + println!("Enter the number of male rabbits to start with:"); + io::stdin().read_line(&mut input_m).expect("failed to read input"); + println!("Enter the number of female rabbits to start with:"); + io::stdin().read_line(&mut input_f).expect("failed to read input"); + println!("Enter the number of rabbits you want to reach:"); + io::stdin().read_line(&mut input_n).expect("failed to read input"); + + let trim_m = input_m.trim(); + let trim_f = input_f.trim(); + let trim_n = input_n.trim(); + + let trims = vec!(trim_m, trim_f, trim_n); + let mut mf: Vec = vec!(10, 10, 10000); + for (i, trim) in trims.iter().enumerate() { + let g = &trim.parse::().unwrap_or(20); + mf[i] = *g; + } + + // . up until here + + let (mut m, mut f, n_a) = init(mf[0], mf[1], mf[2]); + let mut sum_male: i128 = m.iter().sum(); + let mut sum_female: i128 = f.iter().sum(); + let mut sum = sum_male + sum_female; + // println!("There are now {} rabbits alive!", sum); + let mut i = 0; + while sum < n_a { + // this handling of tuples sucks, there's gotta be a better way... + let (r_m, r_f) = reproduce(m, f); + let (r_m, r_f) = age(m, f, r_m, r_f); + m = r_m; + f = r_f; + sum_male = m.iter().sum(); + sum_female = f.iter().sum(); + sum = sum_male + sum_female; + println!("There are now {} rabbits alive! {} males and {} females", sum, sum_male, sum_female); + thread::sleep(time::Duration::from_millis(500)); + i += 1; + } + println!("It took {} months!", i); +} diff --git a/rust/tally/.idea/misc.xml b/rust/tally/.idea/misc.xml new file mode 100644 index 0000000..d6cdb7a --- /dev/null +++ b/rust/tally/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/rust/tally/.idea/modules.xml b/rust/tally/.idea/modules.xml new file mode 100644 index 0000000..e06f945 --- /dev/null +++ b/rust/tally/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/rust/tally/.idea/tally.iml b/rust/tally/.idea/tally.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/rust/tally/.idea/tally.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/rust/tally/.idea/workspace.xml b/rust/tally/.idea/workspace.xml new file mode 100644 index 0000000..f8aefbc --- /dev/null +++ b/rust/tally/.idea/workspace.xml @@ -0,0 +1,267 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + +