Skip to content

Instantly share code, notes, and snippets.

View RaymondWies's full-sized avatar

Raymond Wies RaymondWies

View GitHub Profile
;; 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)
@RaymondWies
RaymondWies / 4Clojure solutions to exercises
Created January 3, 2017 18:57
This is code from my 4Clojure account for solved exercises that passed tests.
;; 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
@RaymondWies
RaymondWies / Haskell Common Algorithms and Helper Functions
Last active September 29, 2021 15:43
Custom implementations of some common functions in Haskell.
{- 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
@RaymondWies
RaymondWies / Prime Number Helper Functions in Haskell
Last active August 29, 2016 09:25
Various functions for generating, testing, and finding prime numbers. This is an optimized brute force algorithm (non-memoized) that takes advantage of a lazy infinite list, odd numbers for search, and upper bound at sqrt (n) for factors.
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]
@RaymondWies
RaymondWies / Project Euler in Haskell
Last active August 29, 2016 02:35
Problem sets from Project Euler implemented in Haskell.
{- 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)
@RaymondWies
RaymondWies / recursive_prime.py
Last active January 2, 2016 21:39
This example is an exercise in testing any given positive integer for prime number utilizing classic recursion of the divisor. The integer to be tested must be defined within the program or set to user input() as this recursive strategy requires a global variable outside the function blocks to work.
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):
@RaymondWies
RaymondWies / test_for_prime.py
Last active January 2, 2016 21:09
This code shows different ways to test a given positive integer for a prime number. Each function is a different strategy to test for prime. Each function simply returns True or False as a boolean test of function(n) whether a given n is a prime number.
'''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)
@RaymondWies
RaymondWies / population_growth.py
Last active January 2, 2016 18:49
This code is taking a population formula from James Gleick's "Chaos" Chapter 3 p.63 for linear population growth approaching steady state expressed as a fraction calculated from each generation to the next: Xnext = RX*(1-X) For example, if R is set at 2.7 and initial X is set at 0.02, then each population fraction X is 0.02, 0.0529, 0.1353, 0.31…
'''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.'''