Skip to content

Instantly share code, notes, and snippets.

@PRotondo
PRotondo / traceroute.hs
Last active July 30, 2017 15:53
Haskell traceroute over icmp
#!/usr/bin/env runhaskell
-- Haskell traceroute over icmp
-- now with ByteString to avoid new errors produced
-- by the deprecated recvFrom from Network.Socket
import Control.Monad
import Data.Bits(complement)
import Data.ByteString(unpack,pack)
import Data.List
@PRotondo
PRotondo / balanced_smileys.rb
Created February 15, 2013 21:11
My solutions to the problems from Facebook Hackercup 2013 Qualification Round.
$dp = nil
def try(l,r,s)
case s[l]
when '('
((l+1)..r).each do |i|
return true if s[i] == ')' && go(l+1,i-1,s) && go(i+1,r,s)
end
false
when ':'
@PRotondo
PRotondo / card_game.rb
Created February 15, 2013 21:22
My solutions to the problems from Facebook HackerCup 2013 Round 1
MOD = 1000000007
MAX = 10000
$dp = Array.new(MAX+1)
$dq = Array.new(MAX+1)
$dp[0] = 1
$dq[0] = 1
def inv(a,n)
@PRotondo
PRotondo / integral.hs
Last active December 13, 2015 21:28
Haskell : Integration on the unit cube
{-# OPTIONS -XFlexibleInstances -XMultiParamTypeClasses #-}
-- integrate on the unit cube
-- first argument indicates approximate error
class Integrable a b where
int :: b -> (b->a) -> b
instance (Ord a, Floating a) => Integrable a a where
int e f = within (e / 2) (integrate f 0 1)
@PRotondo
PRotondo / chat_client.rb
Created April 17, 2013 02:24
Chat in Ruby using a home-made RSA.
%w(socket net/http thread monitor cmath ./rsa).map(&method(:require))
from_server = RSA.new
n,e = from_server.get_params
puts "Encryption system ready!"
server = TCPSocket.new 'localhost', 2000 # change this part
a = server.gets.to_i
@PRotondo
PRotondo / colors.rb
Created April 21, 2013 17:47
PNG: from colour to black-and-white.
require 'cairo'
#input and output come as arguments
input, output = ARGV
surface = Cairo::ImageSurface.from_png input
goal_data = surface.data
HEIGHT, WIDTH = surface.height, surface.width
@PRotondo
PRotondo / CodeForces.rb
Last active December 16, 2015 18:19
My gedit external tool for compiling and testing in CodeForces problems (C++). Filenames should be formatted like {contest-number}{problem-code}.cpp , for example 294A.cpp
#!/usr/bin/env ruby
# [Gedit Tool]
# Name=CodeForces
# Shortcut=<Control>F6
# Languages=cpp
# Applicability=all
# Output=output-panel
# Input=nothing
# Save-files=document
@PRotondo
PRotondo / basketball.rb
Created November 25, 2013 10:12
My solutions to the problems from Facebook Hackercup 2014 Qualification Round.
gets.to_i.times do |i|
n, m, ps = gets.split.map(&:to_i)
players = (1..n).map do
name, sp, h = gets.split
sp, h = sp.to_i, h.to_i
[sp,h,name]
end
players.sort!
players.reverse!
team1, team2 = [], []
@PRotondo
PRotondo / AAAAAA.cpp
Created December 8, 2013 21:21
My solutions to the problems from Facebook Hackercup 2014 Round 1.
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
#define oo (1<<28)
int solve(vector <string> m)
{
int N = m.size(), M = m[0].length();
@PRotondo
PRotondo / look_and_say.py
Created August 13, 2014 14:07
look-and-say in binary
import itertools
def look_and_say(n) :
a = "0"
for i in xrange(n) :
b = ""
for c,l in itertools.groupby(a) :
b += bin(len(list(l)))[2:] + c
yield a
a = b