This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
class Integer | |
def to_fib | |
return to_fib_formula unless self < 10 | |
to_fib_tail | |
end | |
def to_fib_recursive |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace Reto_6 | |
{ | |
[DebuggerDisplay("Count = {Count}, ActiveCount = {ActiveCount}")] | |
[DebuggerTypeProxy(typeof(CacheDebugView))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
OlderNewer