This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; Word Count | |
| (ns word-count) | |
| (defn word-count [string] | |
| (let [remove-punc (fn [s] (clojure.string/replace s #"\W" ""))] | |
| (-> string (clojure.string/lower-case) (clojure.string/split #" ") | |
| (#(map remove-punc %)) (#(remove empty? %)) (frequencies)))) | |
| ;; RNA Transcription | |
| (ns rna-transcription) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; zuzukin's solution to A nil key | |
| ;; https://4clojure.com/problem/134 | |
| (fn [k dict] | |
| (if (contains? dict k) | |
| (nil? (dict k)) | |
| false)) | |
| ;; zuzukin's solution to Map Defaults | |
| ;; https://4clojure.com/problem/156 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {- Replacer function via recursion. Replaces old element x with new element e at specified index in a list. -} | |
| replace :: Int -> a -> [a] -> [a] | |
| replace _ _ [] = [] | |
| replace 0 e (x:xs) = e : xs | |
| replace i e (x:xs) = x : replace (i-1) e xs | |
| {- Swapper function via concatenation. Takes element in a list at initial index i and returns new list at desired index j. -} | |
| swap :: Int -> Int -> [a] -> [a] | |
| swap _ _ [] = [] | |
| swap i j xs | i == j || i >= length xs || j >= length xs = xs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| nthPrime :: Integral a => Int -> a | |
| nthPrime = (!!) listOfPrimes . pred | |
| listOfPrimes :: Integral a => [a] | |
| listOfPrimes = 2:3:5:7:filter testPrime [9,11..] | |
| testPrime :: Integral a => a -> Bool | |
| testPrime 2 = True | |
| testPrime x | x < 2 || even x = False | |
| | otherwise = all (\i -> mod x i /= 0) [3,5..floor.sqrt.fromIntegral $ x] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {- Project Euler Problem 1: | |
| Find the sum of all natural numbers below z that are factors of x or y -} | |
| sumFactors :: Integral a => a -> a -> a -> a | |
| sumFactors x y z = sum $ filter (\n -> mod n x == 0 || mod n y == 0) [1.. pred z] | |
| {- Project Euler Problem 2: | |
| Find the sum of all even Fibonacci numbers below upper value lim -} | |
| sumList :: Integral a => a -> a | |
| sumList = sum . fiboEvenLazyList | |
| where fibo n = if n < 4 then n else fibo (n - 1) + fibo (n - 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from math import sqrt | |
| def set_div(n): | |
| #screens for 0, 1, 2, and screens out all other evens greater than 2 | |
| #then passes a maximum odd divisor up to sqrt(number) to test_prime(div) | |
| if n == 2: return True | |
| elif n < 2 or not n % 2: return False | |
| else: return test_prime(int(sqrt(n))) if int(sqrt(n)) % 2 else test_prime(int(sqrt(n))-1) | |
| def test_prime(div): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '''Each function below is an independent test for prime number given any number n that returns True or False. | |
| The code is designed to be used as a test function in your code, like within filter(test_prime, [list of integers]) | |
| or within list comprehension prime_list = [num for num in nums if test_prime(num)]. | |
| Each test function assumes positive integers so validate inputs with abs(), int(), etc. separately.''' | |
| from math import sqrt | |
| def test_prime(n): | |
| #probabilistic test for prime with no false negatives (all real primes return True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '''x = r * x * (1 - x) expressed as a Python function four different ways. | |
| x is a population fraction as a rounded float, where each x is a successive | |
| iteration or base case (initial population fraction). r is rate of growth. | |
| gen is generation, where gen = 0 is initial state with x = x0 and gen = 20 is 20th | |
| generation with x being population after 20 iterations of the above formula. | |
| Call each function below as new_pop(gen, x=?, r=?) or simply as new_pop(gen) with | |
| default values for initial x and r set at 0.02 and 2.7 as per example in Gleick's book.''' | |