Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / gist:3660240
Created September 6, 2012 20:44
Ethiopian Multiplication
# Ethiopian Multiplication
# usage: ruby em.rb 673 7
m, n = ARGV.map(&:to_i)
product = 0
while m >= 1
puts "%4d : %4d %s" % [m, n, m.even? ? "Ignore" : ""]
product += n unless m.even?
m = m / 2
n = n * 2
end
@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 / gist:3826257
Created October 3, 2012 10:21
Enums in C#
class EnumTest
{
public enum Cities { AnkhMorpork = 1, Ampridatvir, Lankhmar };
public Cities cities { get; set; }
public void WriteCities()
{
Cities test = (Cities)4711;
Console.WriteLine(test);
Console.WriteLine(cities);
@jonelf
jonelf / gist:3842488
Created October 5, 2012 21:21
XML abuse
<?xml version="1.0"?>
<!DOCTYPE b [
<!ENTITY a "na ">
<!ENTITY t "&a;&a;&a;&a;&a;&a;">
<!ENTITY na "&t;&t;&t;&t;&t;&t;">]>
<b>&na; BATMAN!</b>
@jonelf
jonelf / gist:3905316
Created October 17, 2012 12:36
LLINQ
var numbers = new[] { 8, 12, 21 };
var biggerThan12 = numbers.Where(n => n > 12);
numbers[1] = 13;
foreach (var number in biggerThan12)
{
Console.WriteLine(number);
@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; }
@jonelf
jonelf / gist:3930708
Created October 22, 2012 09:55
Merge lists in C# added MergePreserveOrder
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
namespace test
{
class Person
{
public int Number { get; set; }
@jonelf
jonelf / gist:3931476
Created October 22, 2012 13:21
Blasted Mentor
b,a=$*;a.to_i.times{|l|puts (0..b.to_i).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}*""}
@jonelf
jonelf / gist:4017648
Created November 5, 2012 15:07
Baidu likes robots
baiduspider-180-76-5-49.crawl.baidu.com plea.se - [05/Nov/2012:15:06:34+0100] GET /robots.txt HTTP/1.1 200 37 - Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
baiduspider-180-76-5-156.crawl.baidu.com plea.se - [05/Nov/2012:15:06:35+0100] GET /robots.txt HTTP/1.1 200 37 - Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
119.63.196.76 plea.se - [05/Nov/2012:15:15:36+0100] GET /robots.txt HTTP/1.1 200 37 - Baiduspider+(+http://www.baidu.com/search/spider.htm)
119.63.196.14 plea.se - [05/Nov/2012:15:15:41+0100] GET /robots.txt HTTP/1.1 200 37 - Baiduspider+(+http://www.baidu.com/search/spider.htm)
@jonelf
jonelf / gist:4065600
Created November 13, 2012 12:49
Use Fisher-Yates instead
var results = [];
for (var i = 0; i < 60000; i++) {
var arr = ['A', 'B', 'C'];
arr.sort( function() { return 0.5 - Math.random() } );
results.push(arr);
};
var dict = {};
for (var i = results.length - 1; i >= 0; i--) {