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 / Mandelbrot
Created March 23, 2015 14:03
Mandelbrot in Swift
for line in 0...32 {
println("".join((0...78).map { (col: Int) -> String in
var x = 0.0, y = x, i = 0
do {
x = x * x - y * y + Double(col) / 20.0 - 2.3
y = 2 * x * y + Double(line) / 10.0 - 1.5
i++
} while (x * x + y * y) < 4 && i < 78
return "\(UnicodeScalar(0x1f35b + i))"
@jonelf
jonelf / standard_deviation.swift
Last active February 15, 2024 21:45
Standard Deviation in Swift
func standardDeviation(arr : [Double]) -> Double
{
let length = Double(arr.count)
let avg = arr.reduce(0, {$0 + $1}) / length
let sumOfSquaredAvgDiff = arr.map { pow($0 - avg, 2.0)}.reduce(0, {$0 + $1})
return sqrt(sumOfSquaredAvgDiff / length)
}
let responseTimes = [ 18.0, 21.0, 41.0, 42.0, 48.0, 50.0, 55.0, 90.0 ]
@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 / gist:3743071
Created September 18, 2012 13:23
10 longest user-agent strings from my access log
The 10 longest user-agent strings from my access log.
All of the are from Internet Explorer and the longest is 370 characters. This is silly!
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS129735; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; WWTClient2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; InfoPath.2; InfoPath.3; .NET4.0C; .NET4.0E)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS125042; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; BO1IE8_v1;ENUS)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; YPC 3.2.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.0.3705; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; msn OptimizedIE8;ENGB)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.
@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 / gist:3916414
Created October 19, 2012 05:42
Merge lists in C#
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
namespace test
{
class Person
{
public int Number { get; set; }