Learning outcome - Thur 12th March, 2020
What learning outcomes did you focus on today? What did you learn?
I moved up a level, and began solving the mid-level ruby Algorithm on Exercism.io. I must confess that this level is a bit tougher but it is also very Interesting at the same time. I have had the chances to crack by brains and solve Algorithms with a test first approach. I also got familiar with wide range of syntax in ruby and its usage.
The Set Intersection (&) symbol - Returns a new array containing unique elements common to the two arrays. The order is preserved from the original array.
Example:
arr_1 = [1,2,3,4,6,8]
arr_2 = [2,3,9,0,5,4] arr_1 & arr_2 => [2, 3, 4]
.gsub (pattern, replacement) - Returns a copy of str with all occurrences of pattern substituted for the second argument. The pattern is typically a
Regexp
; if given as aString
, any regular expression meta-characters it contains will be interpreted literally, It works for digits too, all you have to do is gExample:
- If you want to eliminate symbols from a string and return a new string with just letters
string = O-W-R-C
new_string = string.gsub(/\W/, ' ') => OWRC
.sum enumerable - Returns the sum of elements in an Enumerable. If a block is given, the block is applied to each element before addition.
Example:
- Return the total sum of the value in the giving array multiplied by 2?
- normally I will solve it like this
array = [1,2,3,4,5,6]
def array
total = 0
array.each do | i |
result = i * 2
total += result
end
total
end
- New Approach using the sum method
array = [1,2,3,4,5,6]
def array
result = array.each.sum
end
result
With the second solution, you are allowing ruby do the sum for you while looping and append the total in the result variable
Splat Operator (*) - Splat operator allows us to work with an undefined number of arguments. It also converts the arguments to an array within a method.
What's something that surprised you today?
- There’s always a better or simpler way to solve a problem, thats certain.
- Solving Katas is becoming fun for me.
- The moment where I get stuck and start cracking my head for ideas to a solution or when i have go back to the documentation to lookup a method or ask my colleague for an idea, is the process where a lot of learning happens.
What's something that frustrated you today?
Started solving the medium level Katas, and I must confess that there’s a lot of head cracking involve 😀
Reference