Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / validatePersonnummer.java
Created November 4, 2021 08:25
Validates a Swedish personal identity number.
View validatePersonnummer.java
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
View balanced2.rb
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.
View balanced.rb
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!"
View collatz_conjecture.rb
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
View fib.rb
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
View script.md

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.

View fra.rb
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
View x.js
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);
View The X-variables.js
var xᅠ = 37;
var x = 41;
var ᅠx = 43;
if(xᅠ== 37 && x == 41 && ᅠx == 43) {
document.writeln("The truth is out there!")
} else {
document.writeln("<a href='https://goo.gl/yoX4Az'>WAT?</a>")
}
View gist:4db13fd3ae1dff2895dcd7db51e13a54
ruby -e "32.times{|l|puts (0..78).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*''}"