3 more files

Signed-off-by: Valentin Gehrke <madmaurice@zom.bi>
This commit is contained in:
madmaurice 2016-11-09 02:02:03 +01:00
parent 5f6d4d13f0
commit b8b6518057
3 changed files with 109 additions and 0 deletions

78
markovfun.py Normal file
View file

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

17
palindrome.clj Normal file
View file

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

14
primesfactors.clj Normal file
View file

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