# Week 4 of Practicing Leetcode

This week I decided I am going to go over my schooling content for Ruby again. I am too dependent on documentation in my opinion, which may be high standards, but that is a good thing to try and practice. My friend from school Steve reached out to me and it was great catching up and we will do some dual coding together. But as far as this week goes, leetcode was not that difficult, which means I am improving!

My first challenge was finding even digits, and that was extremely simple. It was a lot like another leetcode challenge I had done earlier so this came back from memory. The final result was just counting the digits and using the even? call on them.

#find even digits# @param {Integer[]} nums# @return {Integer}def find_numbers(nums)nums.count { |x| x.digits.size.even? }end

My next challenge was removing outermost parenthesis from a problem, and that was somewhat difficult and required a little bit of googling and a counter which was a suggestion I had found online. I defined a counter, and started an array, split the array, and then iterated over it. This counter finds a ( or ) and counts up or down depending on what is located. The final result was -

#remove outermost parenthesis# @param {String} s# @return {String}def remove_outer_parentheses(s)counter = 0output = Array.news.split(//).each do |c|output << c unless (counter == 0 && c == "(") || (counter == 1 && c == ")")counter += 1 if c == "("counter -= 1 if c == ")"endoutput.joinend

Next was Final Prices with Discount. This problem I created two arrays, and shuffled different variables into each array, and found where the output had a discount to the price. The final result for this problem was -

#final price w/ discount# @param {Integer[]} prices# @return {Integer[]}def final_prices(prices)output = []new_a = []for i in 0...prices.size - 1min = nilprices[i+1..-1].select{|v| break min = v if v <= prices[i]}min.nil? ? output << prices[i] : output << prices[i] - minendoutput << prices[-1]p outputend

My second to last problem I ended up looking at the discussion for, and was intrigued by the “one line” (it was actually two with defining the array I guess), so I used that and wanted to learn some more about it. Breaking it down, it was not that difficult but this is something I want to practice more to get better at. Being able to chain your code to fit in one line may not be the most efficient, but it shows the knowledge that you have and the grasp of overall Ruby which is something I admire.

#self dividing mumbers# @param {Integer} left# @param {Integer} right# @return {Integer[]}def self_dividing_numbers(left, right)result = [](left..right).each { |num| result << num if num.to_s.chars.all? { |digit| digit != '0' and num % digit.to_i == 0 }}return resultend

Last, I had to find the number of students doing homework in the given data set, where I just mapped over the data, found the start, end, and query time, and found the sum of those who met the given criteria. Our end result was -

#number of students doing homework# @param {Integer[]} start_time# @param {Integer[]} end_time# @param {Integer} query_time# @return {Integer}def busy_student(start_time, end_time, query_time)start_time.each_with_index.map { |st, i| st <= query_time && end_time[i] >= query_time ? 1 : 0 }.sumend