Practice Code with Katas!


#21

I just finished http://www.codewars.com/kata/befunge-interpreter/ruby. That was fun. It’s a “build an interpreter for Befunge”-problem. I did it with 85 lines of code. :+1:


#22

Ah, thanks. Yes this is a :none scenario

Wow! Definitely going to have to check that out. Have you considered making more katas?


#23

Since I loved the challenge of implementing the moving_slice_enumerator, I made it a Kata: Moving Slice Enumerator


#24

I found two people just built an Array for the moving slice method and then called either .each or .to_enum on it. I didn’t like this as it didn’t meet my criteria for matching “All strings greater than min”. So I’ve generated a string 18,667,188 characters long. Now they HAVE to create a Lazy Enumerator. :smile:


#25

Sure, I just need the time and inspiration to do it :smile:


#26

Just wrote a Sudoku validator solution in about 10 lines of code 10LOC :smiley: Sudoku Solution Validator


#27

Hahaha, that might be only 10 LOC, but that is also unreadable :wink:

I used 23 LOC: http://www.codewars.com/kata/reviews/5593733c289bb3285000000b/groups/56386e01c8d77fd696000093


#28

Depends on one’s literacy. :wink:

I’ve never used the transpose method before. I’ll have to add that one to my vocabulary.


#29

Yeah, I’ve also only used it for problems like this - I don’t have any production code running using #transpose let alone #each_slice :smiley_cat: Then again, I work in Real Estate, not Sudoko solving.


#30

I came up with turning a Hash into a Proc today. I love it. I think I’ll be using it this way from now on.

my_hash = ->key{{
  a: 1, b: 2, c: 3, d: 4, e: 5, f: 6
}[key]}

my_hash[:a] # This is the same as my_hash.call(x)
# => 1

[:e, :a, :b, :f, :c, :d].map(&my_hash) # hash is now mappable
# => [5, 1, 2, 6, 3, 4]

#31

I’ve proposed it as a feature for the Ruby language: Add to_proc on Hash


#32

Dude. Insti on codewars.com answered the Tonk Card Set Determination challenge with Ruby code that’s as smooth as silk. Tonk Card Set Determination (Ruby) solution by Insti He practices best practices. Perfect for a kata. I want to code like him.


#33

I thought this one was going to be easy http://www.codewars.com/kata/next-bigger-number-with-the-same-digits/ruby … after many hours and 3 solutions that miss edge cases I went ahead and looked at existing solutions. The manual way of doing it doesn’t make sense to me. I’ve seen one solution that I could have come up with… that at least makes me feel good.


#34

Hahaha, I sat with that one the other day as well, thinking I’d be able to do it in 20 minutes or so. Had to just put it away again after an hour and a half. :anguished:

(Edit: My problem is that I have a solution that works, but it uses all of their RAM :wink:)


#35

I think I know the one you mean. Shall we discuss various attempted solutions?


#36

This one’s really kicked my arse (like saying it this way makes it more acceptable ;-)). 4 recorded attempts … even though I’ve done many variations on them each. I’m sure the solution is some combination of the answers I’ve given.


#37

I’ve just solved it. Wrote the algorithm down on paper and then actually just implemented it.

I’ll sketch out the algorithm below.

SPOILERS BELOW, DON’T SCROLL IF YOU DON’T WANT TO SEE THE SOLUTION






Take a number, e.g. 20573. We maintain two lists, a prefix and a suffix, and keep a current element.
The next number bigger than 20573 using the same digits can be found thusly:

Start with everything in the prefix list.

Prefix: [2, 0, 5, 7, 3], Suffix: []

Now pop from the prefix, and add to the suffix and pop again from prefix and keep that as current element.

Prefix: [2, 0, 5], Suffix: [3], Current: 7

Our check for whether we should pop more are “Is all the numbers in the suffix lower than or equal to the current element and are the prefix non-empty?”

In this case, yes, so we pop another from prefix, adding current to suffix and making the newly popped the new current element:

Prefix: [2, 0], Suffix: [3, 7], Current: 5

Same question, but this time 5 < 7, so we don’t need to look at more digits. To get the final number, we sort the suffix and take the first number larger than the current, which in this case is 7. This is pushed to prefix and then the rest of the sorted suffix, yielding 20735.

Another quick example: 717743:

Prefix: [7, 1, 7, 7], Suffix: [4], Current: 3
Prefix: [7, 1, 7], Suffix: [4, 3], Current: 7
Prefix: [7, 1], Suffix: [4, 3, 7], Current: 7
Prefix: [7], Suffix: [4, 3, 7, 7], Current: 1

Reassemble: 3 is the lowest number bigger than 1, so we reassemble push that to prefix and then the sorted suffix yielding 731477.


#38

I felt bad about my solution to “Evaluate mathematical expression” however, after seeing the other solutions, I actually find my own more readable and nicer.

I’m using regexps to find each operator and its operands and then gsub in the evaluated expression.


#39

My request to add :to_proc on Hash has been accepted into the Ruby language :smiley: Feature #11653


#40

Keep getting people trying to cheat the answers by hard-coding solutions in based on how many times the method is called. This is a flaw in the CodeWars system that has tests ordered. After I changed the JavaScript tests to be random, and added some additional tests, I invalidated more than just the one cheater. Anyone who didn’t use “to the power of” ends up failing now. Eg. 64**i or Math.pow(64,i). I guess their accumulative reduce method uses too much CPU?

http://www.codewars.com/kata/5632e12703e2037fa7000061/solutions/javascript