This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def house_numbers_sum(input_array) | |
| input_array.take_while { |n| n > 0 }.sum | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_missing_letter(arr) | |
| (arr = (arr.min..arr.max).to_a - arr).first | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_it(seq) | |
| seq.select {|n| seq.count(n).odd? }.first | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def remove_smallest(numbers) | |
| return numbers if numbers.empty? | |
| numbers.dup.tap { |i| i.delete_at(numbers.index(numbers.min)) } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def pyramid(numbers) | |
| (1..numbers).collect { |n| [1]*n } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def min_min_max(array) | |
| (array.minmax << ((array.min..array.max).to_a - array).min).sort | |
| end |