Skip to content

Instantly share code, notes, and snippets.

@urokuta
urokuta / gist:1284026
Created October 13, 2011 11:37
fizz buzz
num = ARGV[0]
1.upto(num.to_i) {|i|
if i % 3 == 0 && i % 5 == 0 then
puts "fizzbuzz"
elsif i%3 == 0 then
puts "fizz"
elsif i%5 == 0 then
puts "buzz"
else
puts i
@urokuta
urokuta / rpn.rb
Last active August 29, 2015 14:01
rpn
def expr(e, stack=[], rpn=[])
case e
when "="
rpn += stack.reverse
return rpn.inject([]) do |arr, token|
case token.to_s
when /[-+*\/]/ then arr << arr.pop(2).inject($&)
when /\d+/ then arr << $&.to_f
end
end.shift
@urokuta
urokuta / simple_calc.rb
Last active August 29, 2015 14:01
simple_calc
def expr(e) e.to_s.include?("=") ? e.split[0..-2].inject([]){|arr,e| (!arr.empty? && arr.last =~ /[^\d]/) ? [eval([*arr,e].join)] : arr << e } : ->(_e){expr("#{e} #{_e}")} end
puts expr(4)["+"][3]["*"][2]["-"][1]["="]
@urokuta
urokuta / ad_ab_test.php
Last active August 29, 2015 14:02
ad_ab_test.php
<?php
class AdAbTest {
private $templates = array();
public function addTemplate($template, $ratio) {
$this->templates[] = array(
"template" => $template,
"ratio" => $ratio,
);
}
@urokuta
urokuta / attacker_success_probability.c
Last active October 18, 2024 04:03
Bitcoin AttackerSuccessProbability
#include <math.h>
double AttackerSuccessProbability(double q, int z){
double p = 1.0 - q;
double lambda = z * (q / p);
double sum = 1.0;
int i, k;
for (k = 0; k <= z; k++){
double poisson = exp(-lambda);
for (i = 1; i <= k; i++)poisson *= lambda / i;
sum -= poisson * (1 - pow(q / p, z - k));