Skip to content

Instantly share code, notes, and snippets.

@jacquesattack
jacquesattack / bs.py
Created November 22, 2022 02:50
Bucket sort of List[int] in python
"""
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)
@jacquesattack
jacquesattack / stauf.clj
Created December 30, 2019 20:23
Solve Stauf Painting Problem
;; 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)
@jacquesattack
jacquesattack / collatz.clj
Last active December 16, 2019 19:36
Create Collatz sequences
;; 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
#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)
@jacquesattack
jacquesattack / fibmorse.rkt
Created July 9, 2019 16:32
Create fibmorse sequence in racket
#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)))
; 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)))))
;; 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)))))
@jacquesattack
jacquesattack / kmpar.R
Last active November 8, 2018 19:22
kmeans||
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)
}
@jacquesattack
jacquesattack / bell_numbers.R
Created July 31, 2018 18:35
Calculate Bell numbers.
# 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)
}
#
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
}