« Kata Three: How Big, How Fast? | Main | Code Kata One - Supermarket Pricing »

January 28, 2007

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e200d83572b26e69e2

Listed below are links to weblogs that reference Kata Two -- Karate Chop:

Comments

grow taller

key was not in the list should return the location where the item would be inserted as a negative value.

ClubPenguinCheats

I'm thinking of optimising for code space and for memory for the next two, and seeing where it takes me. Perhaps have self modifying code.

outis

@Mattias: other options include:

1. concurrency: for a slice, spawns threads to check each case for the slice. The successful thread will go on to spawn more threads. This will create O(lg(len(array))) threads)
2. continuations, which turn your brain inside-out. Here's a partial solution in Scheme, so as not to spoil it:

    (define (chop x items)
      (define (return x) x)
      (let* ((lohi (call/cc (lambda (k)
                             (set! return k)
                             (cons 0 (- (vector-length items) 1)))))
             (lo (car lohi))
             (hi (cdr lohi)))
        ...
    ) )

Mattias

Has anyone been able to come up with another approach to solving this problem other than iterative and recursive?

I have three solutions where two of them are rather similar.

1. Iterative.
2. Split array in two, lower and upper half. If the value we're looking for is in the lower part, split that array in two and continue looking. If the value isn't in the lower part but is in the upper part, split the upper part in two and continue looking. If not found in either part, return -1.
I've implemented this algorithm both as a loop that iterates until there is only one element in the list and recursively where a method keeps calling itself until there is only one element left in the list.

I don't have a blog or anything where I can post my solutions and I assume you don't want me to post it here as it would totally clutter the blog.

My solutions are in Java by the way.

It would be fun if someone found another way of impelementing this algorithm.

Regards, Mattias

www.facebook.com/profile.php?id=505631250

Here's an iterative solution of mine that I don't entirely hate... http://gist.github.com/224727

Matt Ridenour

I was reading Chad Fowler's "The Passionate Programmer" and he mentions this site in the book. It's a great idea; I thought I'd give it a go.

Here's a recursive solution in Ruby:

http://blog.mattbot.net/2009/09/19/codekata-practice-for-programmers/

Emtucifor

Kent Mentolado,

Look again, that would be -1. His answer is one-based.

Kent Mentolado

SenseiJae,

What if the value should be inserted at the first place? You return -0?

SenseiJae

Academically, I was taught that a binary search on a list where the key was not in the list should return the location where the item would be inserted as a negative value.

For example, bsearch(4, [0, 1, 2, 3, 5, 6, 7])
Should return -5

A subtly more challenging problem.

Raul Jara

These are great katas, and I really like the concept of katas in general. I'm also pretty new to programming, so solving this was my first experience with Test::Unit. It was _very_ helpful.

On my first go, however, I was able to write a method that passed the tests without being a true solution. I wrote an additional test with an arbitrarily large array that would be much harder to fool. Here it is, for any other ruby beginners out there

def test_chop_with_large_array
large_array_length = 34519 #or any arbitrary large number
count_by = 4 #or any even, non-zero number
large_array = []
(1..large_array_length).each{|x| large_array << x * count_by}
(1..large_array_length).each{|x| assert_equal( (x - 1), chop(x * count_by, large_array))}
(1..large_array_length).each{|x| assert_equal( -1, chop(x * count_by + 1, large_array))}
end

nazlfrag

Nice challenge, I've done two implementations so far, iterative & recursive. I'm thinking of optimising for code space and for memory for the next two, and seeing where it takes me. Perhaps have self modifying code. It's such a deceptively simple algorithm but I'm sure theres dozens of unique approaches.

Rulas

This is a nice one, I'm using C# and I just get one solution right now, as soon as I have a little time I´ll continue...This is very fun, nice idea this codekata blog

The comments to this entry are closed.