Skip to content

Instantly share code, notes, and snippets.

View venelrene's full-sized avatar
✝️
Working

Venel venelrene

✝️
Working
  • remote
View GitHub Profile
@venelrene
venelrene / house_numbers_sum.rb
Created January 3, 2020 17:50
For inputArray = [5, 1, 2, 3, 0, 1, 5, 0, 2], the output should be 11. The answer was obtained as 5 + 1 + 2 + 3 = 11.
def house_numbers_sum(input_array)
input_array.take_while { |n| n > 0 }.sum
end
@venelrene
venelrene / find_the_missing_letter.rb
Created December 26, 2019 19:02
Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array. You will always get an valid array. And it will be always exactly one letter be missing. The length of the array will always be at least 2. The array will always contain letters in only one case. Example: ['a','b','c'…
def find_missing_letter(arr)
(arr = (arr.min..arr.max).to_a - arr).first
end
@venelrene
venelrene / replace_with_alphabet_position.rb
Created December 26, 2019 18:12
In this kata you are required to, given a string, replace every letter with its position in the alphabet. If anything in the text isn't a letter, ignore it and don't return it. "a" = 1, "b" = 2, etc.
def alphabet_position(text)
text.upcase.gsub(/[A-Z]/).map {|m| m.ord-64}.join(" ")
end
#### before refactor
# def alphabet_position(text)
# alphabet = ('a'..'z').to_a
# text.downcase.chars.map {|l| alphabet.index(l.next) }.compact.join(" ")
# end
@venelrene
venelrene / sum_of_digits_digital_root.rb
Created December 17, 2019 15:57
A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
def digital_root(n)
return n if n < 10
until n.to_s.size == 1
n = n.to_s.chars.map(&:to_i).reduce(:+)
end
return n
end
@venelrene
venelrene / find_the_odd_int.rb
Created December 10, 2019 22:24
Given an array, find the integer that appears an odd number of times. There will always be only one integer that appears an odd number of times.
def find_it(seq)
seq.select {|n| seq.count(n).odd? }.first
end
@venelrene
venelrene / Remove_the_minimum.rb
Created December 9, 2019 21:43
Given an array of integers, remove the smallest value. Do not mutate the original array/list. If there are multiple elements with the same value, remove the one with a lower index. If you get an empty array/list, return an empty array/list. Don't change the order of the elements that are lef
def remove_smallest(numbers)
return numbers if numbers.empty?
numbers.dup.tap { |i| i.delete_at(numbers.index(numbers.min)) }
end
@venelrene
venelrene / the_supermarket_queue.rb
Created November 21, 2019 17:52
There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
def queue_time(customers, n)
registers = Array.new(n, 0)
while !customers.empty?
idx = registers.index(registers.min)
registers[idx] += customers.shift unless customers.empty?
end
registers.max
# if I had to do it in one line
# customers.each{|time| registers[registers.index(registers.min)] = (time + registers.min)}
@venelrene
venelrene / printer_errors.rb
Created November 21, 2019 17:23
You have to write a function printer_error which given a string will output the error rate of the printer as a string representing a rational whose numerator is the number of errors and the denominator the length of the control string. Don't reduce this fraction to a simpler expression.
def printer_error(s)
"#{s.count('n-z')}/#{s.size}"
end
##### Before refactor #####
def printer_error(s)
error_num = s.split('').count {|s| s =~ /[n-z]/}
"#{error_num}/#{s.size}"
end
@venelrene
venelrene / PyramidArray.rb
Created November 12, 2019 20:15
Write a function that when given a number >= 0, returns an Array of ascending length subarrays.
def pyramid(numbers)
(1..numbers).collect { |n| [1]*n }
end
@venelrene
venelrene / MinMinMax.rb
Created November 12, 2019 19:27
Given an unsorted array of integers, find the smallest number in the array, the largest number in the array, and the smallest number between the two array bounds that is not in the array.
def min_min_max(array)
(array.minmax << ((array.min..array.max).to_a - array).min).sort
end