PHP, OOP, SQL, noSQL and those kind of things

Main menu

Tag Archives: TDD

For quite a while now I’ve wanted to get stuck into TDD but never really knew where to start. Do I roll up for work one morning and announce I’m doing TDD from here on in? Hardly! Maybe I should go along to one of those Code Kata? Good idea but the nearest one to me is miles away plus I missed the frist few already.

Then I came acorss this amazing tool from Jon Jagger called Cyber Dojo. This was exactly what I was looking for. An on-line tool which allows you to do coding katas without any setup . And because its on-line you can do it at work, at home or wherever. Awesome.

So here is the challenge. Starting Wednesday 26th Dec and for the following 12 days I’m going to do just one kata per day. I’m calling it the 12 TDDs of Christmas and it would be pretty cool if a few other people out there took up the challenge with me – fancy it?

27th December (Day 2): Number Names

Spell out a number. For example

99 –> ninety nine
300 –> three hundred
310 –> three hundred and ten
1501 –> one thousand, five hundred and one
12609 –> twelve thousand, six hundred and nine
512607 –> five hundred and twelve thousand,
six hundred and seven
43112603 –> forty three million, one hundred and
twelve thousand,
six hundred and three

[Source http://rosettacode.org]

28th Dec (Day 3): Mine Field

A field of N x M squares is represented by N lines of exactly M characters each. The character ‘*’ represents a mine and the character ‘.’ represents no-mine.

Your task is to write a program to accept this input and produce as output a hint-field of identical dimensions where each square is a * for a mine or the number of adjacent mine-squares if the square does not contain a mine.

Example output (for the above input)

*211
12*1
0111

29 Dec (Day 4): Monty Hall

Suppose you’re on a game show and you’re given the choice of three doors. Behind one door is a car; behind the others, goats. The car and the goats were placed randomly behind the doors before the show.

The rules of the game show are as follows:

After you have chosen a door, the door remains closed for the time being. The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it. If both remaining doors have goats behind them, he chooses one randomly. After Monty Hall opens a door with a goat, he will ask you to decide whether you want to stay with your first choice or to switch to the last remaining door.

For example:
Imagine that you chose Door 1 and the host opens Door 3, which has a goat. He then asks you “Do you want to switch to Door Number 2?” Is it to your advantage to change your choice?

Note that the player may initially choose any of the three doors (not just Door 1), that the host opens a different door revealing a goat (not necessarily Door 3), and that he gives the player a second choice between the two remaining unopened doors.

Simulate at least a thousand games using three doors for each strategy and show the results in such a way as to make it easy to compare the effects of each strategy.

[Source http://rosettacode.org]

30th Dec (Day 5): Fizz Buzz

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

31st Dec (Day 6): Recently-Used List

The most recently added item is first, the leastrecently added item is last.

Items can be looked up by index, which counts from zero.

Items in the list are unique, so duplicate insertions are moved rather than added.

Optional extras:

Null insertions (empty strings) are not allowed.

A bounded capacity can be specified, so there is an upper limit to the number of items contained, with the least recently added items dropped on overflow.

1st Jan (Day 7): Template Engine

Write a “template engine” meaning a way to transform template strings, “Hello {$name}” into “instanced” strings. To do that a variable->value mapping must be provided. For example, if name=”Cenk” and the template string is “Hello {$name}” the result would be “Hello Cenk”.

9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||
Nine pins hit on the first ball of all ten frames.
Second ball of each frame misses last remaining pin.
No bonus balls.
Score for each frame == 9
Total score == 10 frames x 9 == 90

5/|5/|5/|5/|5/|5/|5/|5/|5/|5/||5
Five pins on the first ball of all ten frames.
Second ball of each frame hits all five remaining
pins, a spare.
One bonus ball, hits five pins.
Score for each frame == 10 + score for next one
ball == 10 + 5 == 15
Total score == 10 frames x 15 == 150

4th Jan (Day 10): Phone Numbers

Given a list of phone numbers, determine if it is consistent. In a consistent phone list no number is a prefix of another. For example:

o) Bob 91 12 54 26
o) Alice 97 625 992
o) Emergency 911

In this case, it is not possible to call Bob because the phone exchange would direct your call to the emergency line as soon as you dialled the first three digits of Bob’s phone number. So this list would not be consistent.

5th Jan (Day 11): Poker Hands

A poker deck contains 52 cards – each card has a suit which is one of clubs, diamonds, hearts, or spades (denoted C, D, H, and S in the input data).

For scoring purposes, the suits are unordered while the values are ordered as given above, with 2 being the lowest and ace the highest value.

A poker hand consists of 5 cards dealt from the deck. Poker hands are ranked by the following partial order from lowest to highest.

High Card: Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on.

Pair: 2 of the 5 cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order.

Two Pairs: The hand contains 2 different pairs. Hands which both contain 2 pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card.

Three of a Kind: Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the 3 cards.

Straight: Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card.

Flush: Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.

Full House: 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.

Four of a kind: 4 cards with the same value. Ranked by the value of the 4 cards.

Straight flush: 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand.

Your job is to rank pairs of poker hands and to indicate which, if either, has a higher rank.

6th Jan (Day 12): Harry Potter

To try and encourage more sales of the 5 different Harry Potter books they sell, a bookshop has decided to offer discounts of multiple-book purchases.

One copy of any of the five books costs 8 EUR.

If, however, you buy two different books, you get a 5% discount on those two books.

If you buy 3 different books, you get a 10% discount.

If you buy 4 different books, you get a 20% discount.

If you go the whole hog, and buy all 5, you get a huge 25% discount.

Note that if you buy, say, four books, of which 3 are different titles, you get a 10% discount on the 3 that form part of a set, but the fourth book still costs 8 EUR.

Your mission is to write a piece of code to calculate the price of any conceivable shopping basket (containing only Harry Potter books), giving as big a discount as possible.

For example, how much does this basket of books cost?

2 copies of the first book
2 copies of the second book
2 copies of the third book
1 copy of the fourth book
1 copy of the fifth book

One way of group these 8 books is:
1 group of 5 –> 25% discount (1st,2nd,3rd,4th,5th)
+1 group of 3 –> 10% discount (1st,2nd,3rd)
This would give a total of
5 books at a 25% discount
+3 books at a 10% discount
Giving
5 x (8 – 2.00) == 5 x 6.00 == 30.00
+3 x (8 – 0.80) == 3 x 7.20 == 21.60
For a total of 51.60

However, a different way to group these 8 books is:
1 group of 4 books –> 20% discount (1st,2nd,3rd,4th)
+1 group of 4 books –> 20% discount (1st,2nd,3rd,5th)
This would give a total of
4 books at a 20% discount
+4 books at a 20% discount
Giving
4 x (8-1.60) == 4 x 6.40 == 25.60
+4 x (8-1.60) == 4 x 6.40 == 25.60
For a total of 51.20