Skip to content

Instantly share code, notes, and snippets.

View lantoli's full-sized avatar

Leo Antoli lantoli

View GitHub Profile
@lantoli
lantoli / fibonacci.rb
Created January 11, 2011 11:27
Fibonacci numbers using 4 different algorithms
require 'rspec'
class Integer
def to_fib
return to_fib_formula unless self < 10
to_fib_tail
end
def to_fib_recursive
@lantoli
lantoli / roman_numerals.rb
Created January 17, 2011 13:14
roman numerals in ruby using inject
require 'rspec'
class Fixnum
ROMANS = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }
def to_roman
remaining_number = self
ROMANS.inject ("") do | roman_str, current_number |
times,remaining_number = remaining_number.divmod current_number[1]
roman_str + current_number[0].to_s * times
@lantoli
lantoli / bingo.rb
Created May 22, 2011 15:11
Bingo number generation
class Bingo
MAX_NUM = 50
NUMBERS_IN_CARDBOARD = 10
CARDBOARD_IN_GAME = 15
def with_repetitions? cardboard
cardboard.inject do |prev, current|
return true if prev == current
// Leo Antoli solution to reto 2 MSDN: http://blogs.msdn.com/b/esmsdn/archive/2014/09/19/retosmsdn-reto-2-161-esos-eventos.aspx
using System;
using System.Linq;
namespace Reto2ClassLibrary
{
public class Reto2 : IReto2
{
public event EventHandler EventFired;
@lantoli
lantoli / Reto.cs
Last active August 29, 2015 14:06
Leo Antoli Solution to reto 2 MSDN defining add and remove http://blogs.msdn.com/b/esmsdn/archive/2014/09/19/retosmsdn-reto-2-161-esos-eventos.aspx
// Leo Antoli solution to reto 2 MSDN using add and remove: http://blogs.msdn.com/b/esmsdn/archive/2014/09/19/retosmsdn-reto-2-161-esos-eventos.aspx
// NOTE: It's not thread-safe, locking should be added.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Reto2ClassLibrary
{
@lantoli
lantoli / ObjectDumper.cs
Last active August 29, 2015 14:07
Refactored ObjectDumper from @eiximenis
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Dict = System.Collections.Generic.Dictionary<string, System.Delegate>;
using Pair = System.Collections.Generic.KeyValuePair<string, string>;
namespace ObjectDumper
{
public class ObjectDumper<T> where T: class
/*
We are asked to give a random derangement: http://en.wikipedia.org/wiki/Derangement
We use a slight modification of Fisher-Yates shuffle algorithm to make sure it's a derangement.
This algoritm works because all elements are moved one or more times to the left so at the end
all elements are in different positions than the original one.
But with this modification, random properties are not great. For example some permutations are more
likely than others, and there are some permutatations which can never happen.
using System.Collections.Generic;
using System.Linq;
namespace Reto5
{
public class DictionaryPlus<TK, TV> : Dictionary<TK, TV>
{
public IEnumerable<TV> this[params TK[] keys] {
get {
return keys.Select(key => base[key]);
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Reto_6
{
[DebuggerDisplay("Count = {Count}, ActiveCount = {ActiveCount}")]
[DebuggerTypeProxy(typeof(CacheDebugView))]
@lantoli
lantoli / 1StudiousStudentWrong.java
Last active September 25, 2015 13:13
Studious Student
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
public class StudiousStudentWrong {
public static void main(String[] args) throws Exception {