Skip to content

Instantly share code, notes, and snippets.

View dermesser's full-sized avatar
🐢
a bit of time :-)

Lewin Bormann dermesser

🐢
a bit of time :-)
View GitHub Profile
@dermesser
dermesser / jupyter_julia_lsp.json
Created March 22, 2023 16:33
Place this in e.g. ~/.local/share/etc/jupyter/jupyter_server_config.d/julialsp.json - that should be enough for julia-ls to work. (obviously install LanguageServer first)
{
"LanguageServerManager": {
"language_servers": {
"julia-lsp": {
"version": 2,
"argv": ["/home/lbo/bin/julia", "-e", "using LanguageServer; runserver()"],
"languages": ["julia"],
"mime_types": ["text/julia", "text/x-julia"],
"display_name": "JuliaLSP"
}
@dermesser
dermesser / magicsquare_4x4.jl
Created November 13, 2022 19:49
Solve magic squares (a.k.a. Sudoku) of size 4x4 in Julia. Naive implementation!
function testsquare1()::Matrix{Int}
[1 0 4 0;
0 2 0 3;
0 0 3 0;
0 0 0 4]
end
function testsquare2()::Matrix{Int}
[1 0 0 0;
0 2 0 0;
0 0 3 0;
@dermesser
dermesser / lcss.jl
Created November 13, 2022 17:15
Standard algorithm for longest common subsequence in Julia.
function longest_common_subsequence(x::Vector{T}, y::Vector{T})::Vector{T} where {T}
m, n = length(x), length(y)
C = zeros(Int, m+1, n+1)
# Not necessary:
for i = 1:m
C[i,1] = 0
end
for j = 1:n
C[1,j] = 0
end
@dermesser
dermesser / liss.jl
Last active February 28, 2023 12:44
Standard algorithm for longest increasing subsequence in Julia.
function longest_increasing_subsequence(v::Vector{Int})::Vector{Int}
M = zeros(Int, length(v)) # M[L] stores last index of sequence with length L.
P = zeros(Int, length(v)) # P[i] stores predecessor of element i in longest sequence.
Longest = 0
for i = eachindex(v)
# Binary search for position in longest-sequence-so-far
lo, hi = 1, Longest+1
while lo < hi
mid = round(Int, floor((lo+hi)/2))
if v[i] < v[M[mid]]
@dermesser
dermesser / find_in_sorted_matrix.jl
Created November 11, 2022 18:32
Find an integer in a sorted matrix of integers (but generalizable to any orderable type)
# > X = generate_sorted_matrix(10, 10)
# 10×10 Matrix{Int64}:
# 2 139 194 248 337 428 544 625 736 873
# 11 141 195 251 340 454 564 644 743 892
# 39 153 200 272 350 454 582 649 786 900
# 58 163 203 284 357 462 586 652 799 912
# 66 164 219 295 359 473 599 654 806 930
# 73 170 220 302 361 482 604 677 813 947
# 74 177 243 303 364 488 609 681 815 957
# 78 187 245 323 409 504 611 696 845 961
@dermesser
dermesser / wordrectangle.cc
Created November 5, 2022 09:35
Interview question: how to find the largest "word rectangle" (each word and each column is a word) from a dictionary.
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <string>
#include <vector>
#include <memory>
#include <cctype>
using namespace std;
@dermesser
dermesser / packing.py
Created October 23, 2022 06:55
Knapsack and binpacking in traditional DP manner. Knapsack should be correct - binpacking seems correct (but not verified)
import numpy as np
def knapsack(items=[4,3,1,2], profits=[60, 50, 20, 20], capacity=7):
table = np.zeros((len(items)+1, capacity+1))
table[:, 0] = 0
table[0, :] = 0
for item in range(1, len(items)+1):
for cap in range(1, capacity+1):
y = 0 if items[item-1] > cap else profits[item-1]+table[item-1, cap-items[item-1]]
@dermesser
dermesser / names_classifier.jl
Created September 20, 2022 14:14
An experiment classifying (separating) words that look like names from non-names. RNN Classifier
using MKL
using DataFrames
using Flux
import ChainRulesCore: ignore_derivatives
import Distributions: Bernoulli
import CSV
import Random: Sampler
import BSON
import Flux.MLUtils: DataLoader
@dermesser
dermesser / gibbs.jl
Created September 7, 2022 21:06
Primitive Gibbs sampler, with two demos.
using Distributions
function mysampler(previous::Union{NamedTuple, Nothing})::NamedTuple
µ = rand(Normal(3, 2))
σ = rand(TruncatedNormal(1, 3, 0, Inf))
(µ=µ, σ=σ)
end
function myloglikelihood(θ::NamedTuple)::Float64
@dermesser
dermesser / hmc.jl
Last active September 6, 2022 09:06
Primitive Hamiltonian Monte Carlo (HMC) sampler
using Plots
using Random
using Distributions, DistributionsAD
using LinearAlgebra
import Zygote: gradient
struct HMC{T,F}
sup::AbstractArray{Tuple{T,T}}
invM::Matrix{T}
pdist::AbstractMvNormal