Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@maxlath
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxlath/10978750 to your computer and use it in GitHub Desktop.
Save maxlath/10978750 to your computer and use it in GitHub Desktop.
# Kata: http://wiki.agile-france.org/cgi-bin/wiki.pl?KataPotter
def calculateur_prix(livres)
# comptons combien d'exemplaires de chaque tomes d'Harry Potter la liste
# compte en incrémentant la case du tableau associé au numéro du tome
tableau_tome = [0,0,0,0,0]
livres.each { |tome| tableau_tome[tome] += 1 }
# trions le tableau optenu (comptant le nombre de chaque tome) par ordre
# décroissant, de sorte que le livre le plus demandé soit à
# tableau_tome[0], le deuxième le plus demandé à tableau_tome[1] etc
tableau_tome.sort!.reverse!
#là, il y a une astuce : on va déduire le nombre de fois qu'un tarif
# va pouvoir être appliqué par soustractions entres les nombres de livres
# demandés:
# la promo "un seul type de livre donc chaque exemplaire = 8€" sera promo[0]
# ce tarif, promo[0], peut être trouvé en soustrayant au livre le plus
# demandé le second livre le plus demandé
# soit tableau_tome[0] - tableau_tome[1]
tableau_promo = [0,0,0,0,0]
for i in 0..4
if i != 4
tableau_promo[i] = tableau_tome[i] - tableau_tome[i+1]
else
tableau_promo[i] = tableau_tome[i]
end
end
# le plus dur est fait, reste à calculer le prix final en appliquant aux
# quantités dans tableau_promo le tarif adapté
prix = 0
for i in 0..4
prix += tableau_promo[i] * (i+1) * (1-(i*0.05))
end
prix *= 8
return prix
end
describe 'KataPotter' do
describe '3 tomes identiques' do
it {expect(calculateur_prix([1, 1, 1])).to eq(24)}
end
describe 'le tome 2 et le tome 3' do
it {expect(calculateur_prix([1, 2])).to eq(15.2)}
end
describe 'le tome 1 et le tome 2' do
it {expect(calculateur_prix([0, 1])).to eq(15.2)}
end
describe 'deux tomes 1' do
it {expect(calculateur_prix([0, 0])).to eq(16)}
end
describe 'tome 2' do
it {expect(calculateur_prix([1])).to eq(8)}
end
describe 'zombo nombre de tomes différents' do
arr = [0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4]
it {expect(calculateur_prix(arr)).to eq(468.8)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment