On Mon, Oct 19, 2009 at 4:35 PM, Bobby Johnson <bobby....@gmail.com> wrote:
> thanks for the pointer, Ill give it another go sometime in the coming weeks.
>
> On Mon, Oct 19, 2009 at 4:30 PM, Simone Busoli <simone...@gmail.com>
> wrote:
>>
>> If you were the cashier, how would you calculate it? That's your solution
>> :)
>>
>> On Tue, Oct 20, 2009 at 01:27, Bobby Johnson <bobby....@gmail.com>
>> wrote:
>>>
>>> You are the second person that I have discussed this with to mention
>>> grouping the books. I may be to close to my solution to see it yet. I'll
>>> have to let it percolate for a few days.
This seems to be key to dealing with the optimization side of this
problem (short of doing it brute-force).
Attached.
For erlang newbs (those with slightly less experience than myself),
with erlang installed:
oseiler@oseiler ~/work/priv/katapotter $ erl
Erlang R13B02 (erts-5.7.3) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0]
Eshell V5.7.3 (abort with ^G)
1> c(katapotter).
{ok,katapotter}
2> katapotter:test().
All 29 tests passed.
ok
3> katapotter:price([2, 2, 2, 1, 1]).
38.4
4> q().
It doesn't actually care what the numbers are, and can take strings as
well. Things work fine as long as there aren't more than 5 distinct
values:
9> katapotter:price(['half-blood prince', 'philosophers stone']).
15.2
Note that these aren't strings in Erlang; they're atoms. In this
context using strings (double-quotes) doesn't change the behaviour,
however.
10> 8*5*0.75.
30.0
11> katapotter:price([0,1,2,3,4]).
30.0
12> katapotter:price([1,2,3,4,5]).
30.0
13> katapotter:price([0,1,2,3,4,5]).
48
Based on the problem statement that last result is undefined, but one
could argue that it should be 38. I'm not worrying about this myself,
however, because then I'd need to figure out what [0,1,2,3,4,5,6]
should do (46, I guess).
As for the code, I'm not completely happy with some of the naming. The
logic around sort_by_cost_for_next_book is a bit complicated, but I
hope the intent is clear. This is pretty much the most important part
of this solution.
Cheers
Oliver
PS. Made some edits to the source in the process of typing this, and
I'm not entirely sure that gmail got the attachment correct, but the
important bits should be there.
Here is my solution. It turns out to look a lot like Simone's.
http://github.com/dougbradbury/potter_kata
I realized something in doing this kata. I think there is an
incorrect assumption that when doing TDD, you don't have to think
about the algorithm at all.
I played dumb all the way up to the last test that requires you to
optimize the groupings, and then I completely rewrote the algorithm.
I extract two classes along the way, TDDing each class as I discovered
that I needed it.
What approaching the algorithm test first did for me was to help me
reason about it. I started out with no idea how to solve it. By
working through the easy cases, I got to understand the domain. I
left it overnight and in the morning I had the pieces put together in
my head.
TDD changes the way we THINK about code.
Doug
--You received this message because you are subscribed to the Google Groups "software_craftsmanship" group.To post to this group, send email to software_cr...@googlegroups.com.
To unsubscribe from this group, send email to software_craftsma...@googlegroups.com.
Visit this group at http://groups.google.com/group/software_craftsmanship?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "software_craftsmanship" group.
To unsubscribe from this group and stop receiving emails from it, send an email to software_craftsma...@googlegroups.com.