From b8b6518057a426c7622258ebc0592df10403b4fd Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Wed, 9 Nov 2016 02:02:03 +0100 Subject: [PATCH] 3 more files Signed-off-by: Valentin Gehrke --- markovfun.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++ palindrome.clj | 17 +++++++++++ primesfactors.clj | 14 +++++++++ 3 files changed, 109 insertions(+) create mode 100644 markovfun.py create mode 100644 palindrome.clj create mode 100644 primesfactors.clj diff --git a/markovfun.py b/markovfun.py new file mode 100644 index 0000000..b4cc1ec --- /dev/null +++ b/markovfun.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +from sys import argv +from random import randint, choice + +class MarkovGenerator: + overall = object() + def __init__(self): + self.distribution = {} + + def add(self, first, second): + if first not in self.distribution: + self.distribution[first] = { MarkovGenerator.overall : 0 } + if second not in self.distribution[first]: + self.distribution[first][second] = 0 + + self.distribution[first][MarkovGenerator.overall] += 1 + self.distribution[first][second] += 1 + + + def getrandomfollower(self,word): + i = randint(0,self.distribution[word][MarkovGenerator.overall]-1) + for follower, occurances in self.distribution[word].items(): + if follower == MarkovGenerator.overall: + continue + if i < occurances: + return follower + else: + i -= occurances + return None + + def scantext(self,text): + prevtoken = None + while len(text) > 0: + parts = text.split(" ",1) + if len(parts) == 1: + text = "" + token = parts[0] + else: + token, text = parts + token = token.strip(".,!?\"()[]{}\n") + if prevtoken is not None: + self.add(prevtoken,token) + prevtoken = token + + def getrandomword(self): + return choice(list(self.distribution.keys())) + + def generate(self, n): + word = self.getrandomword() + text = word + for i in range(1,n): + word = self.getrandomfollower(word) + if word is None: + word = self.getrandomword() + text += ". " + word + else: + text += " " + word + + return text + + def debug(self): + print("\n".join(self.distribution.keys())) + + +def main(): + if len(argv) > 1: + filename = argv[1] + else: + filename = "test.txt" + text = open(filename,"r").read() + mg = MarkovGenerator() + mg.scantext(text) + print(mg.generate(100)) + + + +if __name__ == '__main__': + main() diff --git a/palindrome.clj b/palindrome.clj new file mode 100644 index 0000000..217737f --- /dev/null +++ b/palindrome.clj @@ -0,0 +1,17 @@ +(defn digits [n] + (->> n + (iterate #(quot % 10)) + (take-while pos?) + (mapv #(mod % 10)) + rseq)) +(defn is-palindrome [n] (let [d (digits n)] (= d (reverse d)))) +(defn pairs [s] (for [a s b s] [a b])) +(defn pow [b e] (reduce * 1 (repeat e b))) +(defn largest-palindrome [n] + (->> (range (pow 10 (dec n)) (pow 10 n)) + (pairs) + (map (partial apply *)) + (filter is-palindrome) + (apply max))) + +(println (largest-palindrome 3)) diff --git a/primesfactors.clj b/primesfactors.clj new file mode 100644 index 0000000..d0d629a --- /dev/null +++ b/primesfactors.clj @@ -0,0 +1,14 @@ +(defn prime-factors [n] + (loop [nums (cons 2 (iterate #(+ 2 %) 3)) + factors [] + v n] + (let [f (first nums)] + (cond + (= v 1) factors + (= 0 (mod v f)) (recur nums (conj factors f) (quot v f)) + :else (recur (rest nums) factors v))))) +;(loop [i 1] +; (when (< i 100) +; (println i (prime-factors i)) +; (recur (inc i)))) +(println 123 (prime-factors 123))