FJOM On The Run
Mis Bookmarks
Textpattern Forums
Textpattern
Bloglines
Ask Metafilter
Metafilter
Mi respuesta al problema BritishCoins (para que jale en la Arena de TopCoder solamente se pone la pura clase BritishCoins sin el namespace y obviamente sin la clase Tester.)
en C#
namespace TopCoder
{
class BritishCoins
{
public int[] coins(int pence)
{
int pounds, shillings; //12 pennies in a shilling, 20 shillings in a pound
pounds = pence / (20 * 12);
pence %= (20*12);
shillings = pence / 12;
pence %= 12;
int[] result = new int[3] { pounds, shillings, pence};
return result;
}
}
class Tester
{
static string ToS(int[] array)
{
string resultSt = "";
foreach (int valor in array)
{
resultSt += valor + ",";
}
return resultSt;
}
static void Main(string[] args)
{
BritishCoins testCoins = new BritishCoins();
System.Console.WriteLine("{0}", Tester.ToS(testCoins.coins(533))); // 2,4,5
System.Console.WriteLine("{0}", Tester.ToS(testCoins.coins(0))); // 0,0,0
System.Console.WriteLine("{0}", Tester.ToS(testCoins.coins(6))); // 0,0,6
System.Console.WriteLine("{0}", Tester.ToS(testCoins.coins(4091))); // 17,0,11
System.Console.WriteLine("{0}", Tester.ToS(testCoins.coins(10000))); // 41,13,4
}
}
}
en Ruby (igual, solamente seria la pura Clase BritishCoins (suponiendo que TopCoder tuviera como opcion Ruby) solo para documentar como queda el test con Test::Unit, en el futuro solo pondré las clases)
class BritishCoins
def coins(pence)
#~ int[] coins(int pence)
shillings,pence=pence.divmod 12
pounds,shillings=shillings.divmod 20
[pounds,shillings,pence]
end
end
if __FILE__ == $0
require 'test/unit'
class Test_BritishCoins < Test::Unit::TestCase
def setup
@a=BritishCoins.new
@function='@a.coins(pence)'
end
def test_0
pence=533
expected=2,4,5
assert_equal(expected,eval("#{@function}"))
end
def test_1
pence=0
expected=0,0,0
assert_equal(expected,eval("#{@function}"))
end
def test_2
pence=6
expected=0,0,6
assert_equal(expected,eval("#{@function}"))
end
def test_3
pence=4091
expected=17,0,11
assert_equal(expected,eval("#{@function}"))
end
def test_4
pence=10000
expected=41,13,4
assert_equal(expected,eval("#{@function}"))
end
end
end
Newer: Project Euler 1 | Home | Older: Project Euler - Problema 97
En línea desde hace 4 años, 8 meses, 23 dÃas, 10 horas y 23 minutos.