This file contains 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
""" | |
Bucket sort (of type List[int]) | |
""" | |
from itertools import chain | |
from typing import List | |
def bucket_sort(somelist: List[int], n_buckets: int = None, approximate: bool = False): | |
if n_buckets is None: | |
n_buckets = int(len(somelist) ** 0.5) |
This file contains 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
;; stauf painting problem | |
(defn get-move [x] | |
(case x | |
0 '(1 1 0 | |
1 1 0 | |
0 0 0) | |
1 '(1 1 1 | |
0 0 0 | |
0 0 0) |
This file contains 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
;; see https://www.quantamagazine.org/mathematician-terence-tao-and-the-collatz-conjecture-20191211/ | |
;; for problem statement and definition of the sequence | |
(defn collatz [x] | |
(if (even? x) (/ x 2) (+ (* x 3) 1))) | |
(defn collatz-seq [x] | |
(lazy-cat [x] (collatz-seq (collatz x)))) | |
;; test |
This file contains 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
#lang racket | |
(define (values-in-row pz row) | |
(filter | |
identity | |
(map | |
(lambda (col) | |
(let ([key (list row col)]) | |
(if (hash-has-key? pz key) | |
(hash-ref pz key) |
This file contains 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
#lang racket | |
(define (next-value current-value) | |
(if (= current-value 0) | |
(if (= (random 2) 0) '(1) '(1 0)) | |
'(0 1))) | |
(define (do-sub myl) | |
(apply append | |
(map next-value myl))) |
This file contains 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
; things are looking up; you might see the solution in your lifetime | |
(def digits (range 9)) | |
(defn find-open-position [puzzle] | |
(first | |
(remove nil? | |
(for [i digits j digits] | |
(cond (nil? (puzzle [i j])) [i j] | |
:else nil))))) |
This file contains 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
;; I think technically this works, but the algorithm is very slow and you won't see the solution in your lifetime. | |
(def digits (range 9)) | |
(defn find-open-position [puzzle] | |
(first | |
(remove nil? | |
(for [i digits j digits] | |
(cond (nil? (puzzle [i j])) [i j] | |
:else nil))))) |
This file contains 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
library(tidyverse) | |
dist = function(v1,v2) { | |
sqrt(sum((v1-v2)^2)) | |
} | |
# point and mu are numerical vectors of the same length | |
sum_of_squares = function(point,mu){ | |
sum((point - mu)^2) | |
} |
This file contains 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
# https://en.wikipedia.org/wiki/Partition_of_a_set#Counting_partitions | |
bell = function(n) { | |
ifelse(n > 0, | |
sum(Vectorize(function(n, k) { | |
choose(n - 1, k) * bell(k) | |
}, "k")(n, 0:(n - 1))), | |
1) | |
} |
This file contains 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
# | |
add = function(a,b,add_func="add_func"){ | |
add_func(a,b) | |
} | |
# 0,1 with addition mod 2 | |
add_func = function(a,b){ | |
(a + b) %% 2 | |
} |
NewerOlder