Practice Code with Katas!


#1

A fun way to practice code is with Code Wars. Available with many programming languages. :smiley: You will also see answers people have given after you solve your kata and learn from them! And what’s even better about this site is you can follow people you know on here. Fun coding for all! :smile:


#2

I wrote my first kata challenge!!! Base64 Numeric Translator Please try out my challenge and let me know what you think! :slight_smile:


#3

These weeks I’ve been working on an api wrapper for CodeWars. Next step is create a command line client (as a separate gem that will be using the wrapper).
There are some clients on GitHub but they’re quite simple and don’t allow to me to do things that I want to (and they’re not in Ruby :laughing:).


#4

I already have 510 points (3 kyu) on Code Wars. I’ll give your problem a spin after work :smirk:

Edit: Okay, I did it. I must say that I think my solution is much cleaner than your own :wink:


#5

Oh nice - will have a good look later :023:


#6

Beautiful code dude!

Yes. I originally wrote my solution in my BaseCustom gem where you could define any custom base numbering system. Once you get into multiple characters being one numeric value then you need a Hash.

Your answer is definitely cleaner. I’m using it as my model for translating it to other languages.


#7

There. I have it in 4 languages now. Ruby, Python, JavaScript, and CoffeeScript. :smiley:

Ruby is definitely the most enjoyable language to figure things out in. Translating proves it to me. I actually forgot my Ruby syntax after translating… kept trying to add {} or do end onto if and else.


#8

@Ohm I went ahead and solved the kata puzzle you made. Took some time. Did you really take the time to undefine every sort method in Array, Hash, and Set?

I like jacobb’s answer best on that one.


#9

Some guy @timp just answered my puzzle in all 4 languages in code golf format.


#10

Haha, I made that over a year ago. I had totally forgot about it!

I did the following to disable built-in sorting:

module Enumerable
  undef :sort
  undef :sort_by
end

class Array
  undef :sort, :sort!
  undef :sort_by!
end

The whole task was to get people to build their own, like a quick-sort or similar, instead of relying on the built-ins. :smiley:


#11

I just taught myself some math principles by experimentation. Solving how many trailing zeros are on the end of the total of a factorial Number of trailing zeros of N!

Of course I ended up with a really long solution and I can see there are short ones. But improving upon my mathematical understanding is, and has been, most enjoyable. I haven’t practiced much math since 2003.


#12

I just looked up how to do it. Like you would a normal problem in your day-to-day job, I Googled and found a math page showing how to solve it, then I coded it. Elegant solution: http://www.codewars.com/kata/reviews/52fd3f6860f49c674d000f3c/groups/5635cf2c9d663479180000a1


#13

Yes… but did you learn in the process that the number of zeros is based not only upon the multiples of 5’s… but the multiples of those multiples… and the multiples of those multiples… ad infinitem?

BTW… I’m about to publish a really complex problem. I’d like you to give it a try once it’s up.


#14

Alright @Ohm. Get your challenge hat on! Here’s my big one: Tonk Card Set Determination. This was a lot of work for me to make. Let me know what you think! :smiley:

This was a method I’ve been meaning to write for months now. Code War was great incentive to finally implement it. Now when other people put in their answers I’ll get the advantage of refactoring by example. It’s a win win!


#15

Done: http://www.codewars.com/kata/reviews/5635e6577bde86d0860000b4/groups/5635fd49ae4fc86a49000017

I had some trouble with your last test-case, because I didn’t think about the cases where there were multiple sets in the same suit. :angry:


#16

Wow! That was fast! Great job! :smiley: Color me impressed.


#17

Did you have a chance to see how I implemented it? The piece of code I’m most proud of is the “moving_slice_enumerator”. http://www.codewars.com/kata/reviews/5635e6577bde86d0860000b4/groups/5635e6577bde86d0860000b6

define_method :moving_slice_enumerator do |cards_string, min|
  Enumerator.new do |y|
    progression = 0
    length = cards_string.length
    loop do
      (progression.abs+1).times do |i|
        y << cards_string[i..length+progression-1+i]
      end
      progression -= 1
      break if length + progression < min
    end
  end
end 

It makes an iterator and gradually chunks the string down. Example:

moving_slice_enumerator("A23456",3).to_a
# ["A23456","A2345","23456","A234","2345","3456","A23","234","345","456"]

Your code is definitely cleaner than mine, again. I’ll take time to get to understand how you’ve implemented it. Thanks for trying it out! And yes that edge case caught me initially as well.


#18

Yeah, I thought about building it like that as well, however I ended up doing it much simpler with the built-ins of Ruby :smirk:

However, my solution looks at many more possibilities than yours, as mine doesn’t use the fact that the solutions are sorted.

cards = "A123456".split(//)
cards.length.downto(3).map {|i| cards.combination(i).map(&:join) }

# => [["A23456"], ["A2345", "A2346", "A2356", "A2456", "A3456", "23456"], ["A234", "A235", "A236", "A245", "A246", "A256", "A345", "A346", "A356", "A456", "2345", "2346", "2356", "2456", "3456"], ["A23", "A24", "A25", "A26", "A34", "A35", "A36", "A45", "A46", "A56", "234", "235", "236", "245", "246", "256", "345", "346", "356", "456"]]

#19

Nice! What if you had

Ah, 3h, 2c, 5h, 4c, 6c

Wouldn’t A35 and 246 match this? Do you have another method(s) to verify consecutive sequence?

[EDIT] Just tested your code to this myself. It works. I guess you do have a consecutive check. Haven’t figured out which part it is.


#20

By your rules there isn’t any sets in that sequence, is there? Or are you looking for the code that removes those as matches? (That would be the if (card_values[0]..card_values[-1]).to_a == card_values part)