GET A CODE OF THIS! &block, .chars, .downto and .each_cons

Time for another round of juicy Codewars learnings. Reaching for 100 points this week.

And by the way, if you’re doing the Pre-Course and no one’s told you this already, the fastest way to rack up cheap honour on Codewars, short of murdering your rivals and assuming their identities in the name of the Many Faced God, is to search for katas that are still in Beta (just modify the filter in the kata search).

Let’s start with something easy: the notation for calling procs.

Filter That Array!

Instructions

Create a method select that accepts a list and a block, and returns a new array of elements for which the given block returns true.

My solution:

def select list, &block      
list.select {|x| block.call(x)}
end

Said block would actually be a proc: a block that works as an object and therefore can be called within other methods. When you’re using it as a place-mat parameter, you add an ampersand(&) in front of it.

This also works, apparently:

def select list, &block   
list.select(&block)
end

Return Length of Longest Palindrome in String

(I’ve been saving this one for a few days now)

Instructions

Write a method that takes a string as its parameter — and finds the longest palindrome within it.

I found this extremely slick solution hanging around on StackOverflow, and I’ll break down the new methods from it and how the method works.

1 def longest_palindrome(str)
2 arr = str.downcase.chars
3 str.length.downto(1) do |n|
4 ana = arr.each_cons(n).find { |b| b == b.reverse }
5 return ana.join.length if ana
6 end
7 end

.chars Takes a string and puts it into an array with each character separated.

.downto() Called on an integer, with a parameter that you want to iterate down to. Quicker than using a range, perhaps?

.each_cons Takes an integer or range of integers and applies a block to the next (n) elements of each one. So:

(1..10).each_cons(3) { |a| p a }
# outputs below
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]

So the genius of this solution, I think, is the way it applies two different iterations. The first is in line 3 with .downto, and it enables us to search 5, 4, 3, 2 etc. consecutive letters in the string. Another thing I like about this is that by counting down, you ensure that you’re returning the longest palindrome first.

The second iteration code is in line 4, and it actually applies the searching code to each of these string combinations with .each_cons. If it’s equal to itself in reverse, we .join the characters and return them as the answer.

At this stage, I would not have thought of that.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.