Skip to content

Instantly share code, notes, and snippets.

View 46bit's full-sized avatar
🏳️‍🌈

Miki Mokrysz 46bit

🏳️‍🌈
View GitHub Profile
@46bit
46bit / duck-type-vs-polymorphism.txt
Last active August 30, 2017 22:04
An explanation of Duck Typing vs Polymorphism
// When using Polymorphism, you might define an abstract class to send a message like
// follows, then fully implement it several times in different classes.
abstract class BaseMessage
public void setMessageText messageText
@messageText = messageText
end
public void send;
end
@46bit
46bit / secrand.rb
Created January 18, 2013 18:36
Generate some SecureRandom data with a simple command. Handy when you don't want to run off to a CLI or dive into messy libs.
#!/usr/bin/env ruby
require "securerandom"
length = 24
if ARGV[0]
length = ARGV[0].to_i
end
@46bit
46bit / policeuk-datasets.rb
Created April 15, 2013 11:36
This is a mass downloader of all http://www.police.uk/data datasets. I'm tidying the data into JSON files, one per year, so that we can dive into the full data quickly.
require "httparty"
data_index_url = "http://www.police.uk/data"
data_index_response = HTTParty.get data_index_url
if data_index_response.code != 200
throw "Fetching URL '#{data_index_url}' failed: '#{data_index_response.code}', '#{data_index_response.message}'"
end
zip_match_regex = /\/\/policeuk.s3.amazonaws.com\/frontend\/crime-data\/([0-9]+)-([0-9]+)\/[0-9]+-[0-9]+-([a-z-]+)-([a-z]+).zip/
@46bit
46bit / prom.rb
Created July 22, 2013 17:42
A BBC Prom information service for Yoleaux (https://github.com/dpk/yoleaux), available at http://photos.46bit.com. Add to yoleaux with: .add-command prom http://photos.46bit.com/{$args} Egs: .prom (today's proms), .prom 1 (tomorrow's proms), .prom 7 (proms this day next week)
require 'nokogiri'
require 'httparty'
require 'active_support/time'
require 'sinatra'
def prom_string days_time
headers = {"User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36"}
response = HTTParty.get "http://www.bbc.co.uk/proms/whats-on/2013/print-season", :headers => headers
@46bit
46bit / charitychoice_scrape.rb
Created August 7, 2013 14:55
Scraping Charity Choice for a YRS UOS group :)
require 'httparty'
require 'nokogiri'
require 'json'
pages = 1..64
def get_page page
$stderr.puts "get_page #{page}"
return HTTParty.get("http://www.charitychoice.co.uk/charities/england/#{page}?onlinedonations=1")
end
a = 5
b = 10
if a < b
puts "5 is less than 10"
else
puts "5 is not less than 10"
end
@46bit
46bit / p15.c
Last active December 26, 2015 02:29
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <gmp.h>
// http://projecteuler.net/problem=15
//
@46bit
46bit / deriving-mersenne-twister-state-from-outputs.txt
Last active October 14, 2023 10:59
Deriving how to reverse the output of a Mersenne Twister PRNG into the internal state, as used in https://github.com/46bit/pinocchio.
func (m *MersenneTwister) Urand32() uint32 {
if m.index == 0 {
m.generate_numbers()
}
y := m.State[m.index]
y = y ^ (y >> 11) //
y = y ^ ((y << 7) & 2636928640) //
y = y ^ ((y << 15) & 4022730752) //
y = y ^ (y >> 18) // "y XOR leftmost 14 bits of y"
@46bit
46bit / 4seg.c
Last active August 29, 2015 13:56
Show a digital clock on a 4-digit 7-segment display (http://r.46b.it/4seg.jpg), when running on an LPC1760 MBED. Use push buttons to set hour and minute. Not openly licensed, may not be mine to give away.
// Serial code
#include "stdhapr.h" // https://gist.github.com/46bit/8890761
#include "lpc17xx_timer.h"
TIM_TIMERCFG_Type TIM_ConfigStruct;
TIM_MATCHCFG_Type TIM_MatchConfigStruct;
#define SEG7_0 0b11111100
#define SEG7_1 0b01100000
#define SEG7_2 0b11011010
@46bit
46bit / stdhapr.h
Created February 8, 2014 21:41
Library to make using LPC17xx easier. Not freely licensed, may not be mine to give away. Here for https://gist.github.com/46bit/8890681
#include "lpc17xx_i2c.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_uart.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_nvic.h"
#include "lpc17xx_gpdma.h"
#include "lpc17xx_adc.h"
#include "lpc17xx_dac.h"
#include <math.h>
#include <string.h>