Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / superpermutation
Created March 13, 2024 17:17
Superpermutation in Ruby
# Naive implementation for finding the superpermutation of n symbols
# https://en.wikipedia.org/wiki/Superpermutation
n = 4
permutations = (1..n).to_a.permutation.map{|a| a.join}
s = permutations.first
permutations.shift
while (permutations.length > 0)
idx = nil
@jonelf
jonelf / test.ts
Created November 13, 2023 12:21
TypeScript any problem
interface Test {
done: boolean,
name: string,
}
function fn(input: Test) {
console.log(input.name)
}
let param1 : Test = {"done": true, "name": "Billie"}
@jonelf
jonelf / validatePersonnummer.java
Created November 4, 2021 08:25
Validates a Swedish personal identity number.
package com.example;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.*;
public class Main {
final static String DATE_FORMAT = "yyMMdd";
@jonelf
jonelf / balanced2.rb
Created November 1, 2021 12:38
JSON strings can contain braces
JSON strings can contain {}.
test_data = ["{dsfgsdf}", "{asdf}, {sdfsdf}", "{{dfgd}, {sadf}}", "sdfd}{", "{asdfsad}, {sdfs}}", "{{asdf},{sdfsdf}", '{"test{{{"}, {"{{{fest"}']
test_data.each { |s|
instring = false;
res = s.split(//).reduce(0) { |sum, c|
break sum if sum < 0
instring = !instring if c == '"'
if !instring
@jonelf
jonelf / balanced.rb
Created November 1, 2021 12:20
Checks if the {} are balanced.
test_data = ["{dsfgsdf}", "{asdf}, {sdfsdf}", "{{dfgd}, {sadf}}", "sdfd}{", "{asdfsad}, {sdfs}}", "{{asdf},{sdfsdf}"]
test_data.each { |s|
res = s.split(//).reduce(0) { |sum, c|
break sum if sum < 0
sum +=1 if c == "{"
sum -=1 if c == "}"
sum
}
puts s + " is #{res != 0 ? "no ":""}good!"
def collatzConjecture(n)
steps = 0
max = 0
until n == 1
steps += 1
n = n.odd? ? n * 3 + 1 : n / 2
max = n if max < n
end
[steps, max]
end
@jonelf
jonelf / fib.rb
Created April 29, 2020 12:59
Fibbooonnnnnaaaaaaaacccccccccccccccccccccccccccccccccciiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
n = 0; m = 1; puts "Fibonacci".split(//).map{|c| n, m = m, n + m; c * n}.join
@jonelf
jonelf / script.md
Last active September 24, 2019 11:46
Searching for and deleting a number of AWS CloudWatch Log Groups

If you ever want to delete a bunch of log groups:

$ aws --profile ProfileName logs describe-log-groups --output table | awk '{print $6}' | grep lambdaNamePrefix | sed -E "s/(.*)/aws --profile ProfileName logs delete-log-group --log-group-name \1/"

PS. This only returns a number of delete-log-group commands but does not execute them. DS.

arr = "Pwzqemuözrözvykvvgt".split(//)
0.upto(28) {|n|
puts arr.map {|c| (c.ord - n).chr}.join
}
@jonelf
jonelf / x.js
Created September 28, 2018 14:11
x's in JavaScript
var p = (xᅠ, x, ᅠx) => console.log(xᅠ + ", " + x + ", " + ᅠx)
var xᅠ = 37;
p(xᅠ, x, ᅠx);
var x = 41;
p(xᅠ, x, ᅠx);
var ᅠx = 43;
p(xᅠ, x, ᅠx);