Skip to content

Instantly share code, notes, and snippets.

@misstonbon
Created November 16, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misstonbon/c48ef6c3f9c3d3dc061a3113cefd252b to your computer and use it in GitHub Desktop.
Save misstonbon/c48ef6c3f9c3d3dc061a3113cefd252b to your computer and use it in GitHub Desktop.
Whiteboarding practice
def compress(string)
result = ""
num_repeated = 1
current = nil
return "" if string.nil? || string.empty?
string.length.times do |i|
letter = string[i]
if current == letter
num_repeated += 1
else
if current != nil
# fancy one liner: result += current + ((num_repeated > 1) ? num_repeated.to_s : "")
if num_repeated > 1
result += current + num_repeated.to_s
else
result += current
end
end
current = letter
num_repeated = 1
end
end
# fancy one liner : result += current + ((num_repeated > 1) ? num_repeated.to_s : "")
if num_repeated > 1
result += current + num_repeated.to_s
else
result += current
end
return result
end
# p compress("abbbbccaa")
# p compress("a")
# p compress("")
# p compress(nil)
# p compress("gggggllllomppolotttttt")
# p compress("bbb")
# p "__________________________"
class String
def numeric?
/\A[-+]?\d+\z/ === self
end
end
def decompress(string)
result = ""
current = nil
return "" if string.nil?
# string.each_char do |letter| .each_char is Ruby magic. I avoid it because other languages don't have it .
string.length.times do |i|
letter = string[i]
if !letter.numeric?
result += letter
current = letter
else
# result += current * (letter.to_i - 1)
(letter.to_i - 1).times do
result += current
end
end
end
return result
end
# p decompress(compress("abbbbccaa"))
# p decompress(compress("a"))
# p decompress(compress(""))
# p decompress(compress(nil))
# p decompress(compress("gggggllllomppolotttttt"))
# p decompress(compress("bbb"))
# p decompress("aaacccbbbdddee")
# p decompress("")
# p decompress(nil)
#
#
# Given an array with numbers from 1...n,
# find the missing number and find the duplicate number
# ex: [1, 2, 4, 4, 5] should return 3 and 4
def missing_and_duplicate(arr)
return [] if arr.nil?
result = []
current = nil
count = arr[0]
arr.each do |num|
if num == current # duplicate
if !result.include? num
result << num
end
end
if num != count #missing
if !arr.include? count #checking that it's not really missing if further in the array
result << count
end
end
current = num
count += 1
end
return result
end
# p missing_and_duplicate(arr)
# Given 2 sorted arrays with no repeated integers,
# return the integers that are common to both arrays
# (i.e., find the intersection).
arr1 = [2, 3, 4, 5, 6]
arr2 = [5, 6, 9, 10, 20, 21, 34]
# def intersection(arr1, arr2)
# result = []
# arr1.each do |num|
# if arr2.include? num
# result << num
# end
# end
# return result
# end
# brute force
# def intersection(arr1, arr2)
# result = []
#
# arr1.each do |num|
# arr2.each do |compare_num|
# if num < compare_num
# break
# end
#
# if num == compare_num
# result << num
# end
# end
# end
# return result
# end
def intersection(arr1, arr2)
result = []
compare_num = {}
arr1.each do |num|
compare_num[num] = true
end
arr2.each do |num|
if compare_num.has_key? num
result << num
end
end
return result
end
# p intersection(arr1, arr2)
def palindrome?(str)
is_palidrome = true
str = str.gsub(" ", "")
str.length.times do |i|
j = str.length - (i + 1)
if (str[i] != str[j])
is_palidrome = false
break;
end
end
return is_palidrome
end
# p palindrome?("madam")
# p palindrome?("nurses run")
# p palindrome?("tacocat")
# p palindrome?("poop")
#
# p palindrome?("monkey")
# p palindrome?("banana")
# p palindrome?("a")
def reverse_words (str)
words = str.gsub(" ", "_").split("_")
result = ""
words.length.times do |i|
j = words.length - (i + 1)
result += ((i > 0) ? " " : "") + words[j]
end
return result
end
#
# p reverse_words("I am car")
# p reverse_words(" Me super good ")
# p reverse_words("Me")
# p reverse_words("Me Cool")
# n = 2
# arr = [1, 1, 3]
#
# valids = [1, 1]
#
# result = [[1, 1]]
def get_valids(n, arr)
valids = []
arr.each do |num|
if num <= n
valids << num
end
end
return valids
end
# This does not work at all!
def sum_of_n(n, arr)
valids = get_valids(n, arr)
return [] if valids.empty?
results = []
valids.length.times do |i|
remainder = n - valids[i]
if remainder > 0
dup = valids.dup
dup.delete_at(i)
results << sum_of_n(remainder, dup)
else
results << valids[i]
end
results.each do |set|
set.push(valids[i])
results.push(set)
end
end
return results
end
# p sum_of_n(2, [1, 1, 3])
# def scope_test
# x = 4
#
# if true
# p "x = #{x}"
# y = 5
# end
#
# [1].each do |num|
# z = 5
# p "y = #{y}"
# p "z = #{z}"
# end
#
# p "x = #{x}"
# p "y = #{y}"
# p "z = #{z}"
# end
#
# scope_test
def factors_of(num)
result = []
(1..num).each do |possible_num|
if num % possible_num == 0
result << possible_num
end
end
return result
end
# p factors_of(11000)
# Given an array with numbers from 1...n,
# find the missing number and find the duplicate number
# ex: [1, 2, 4, 4, 5] should return 3 and 4
#
#
# 1, 2, 3, 4, 5 correct value
def missing_and_duplicates(arr)
result = []
previous = nil
correct_value = arr[0]
arr.each do |num|
if previous == num
result << num if !result.include? num
end
if num != correct_value
result << correct_value if !arr.include? correct_value
end
correct_value += 1
previous = num
end
return result
end
arry = [5,7,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2 ,3, 10,100,102,555, -3, 0, 1]
def longest_streak(arry)
return 0 if arry.empty?
result = 0
previous = nil
current_count = 0
arry.each do |num|
if previous != nil && previous < num && previous != num
current_count += 1
if current_count > result
result = current_count
end
end
if previous != nil && previous > num && previous != num
current_count = 0
end
previous = num
end
return result
end
p longest_streak(arry)
numbersstring = ""
def char_count_in(str)
str = str.to_i.to_s
return 0 if (str.to_i.to_s == "0" || str == "")
hash ={}
str.each_char do |char|
if hash.has_key?(char)
hash[char] += 1
else
hash[char] = 1
end
end
return hash
end
p char_count_in(numbersstring)
# Given an array of arrays of unique, ordered integers return the intersecting integers.
# (i.e. [ [1, 3, 5], [0, 1, 5, 10], [-2, 1, 5] ] returns [1, 5] )
arrayofarrays = [[1,3,5],[0,1,5,10],[-2,1,5],[3, 7, 5, 4, 8, 9,],[10, 11], [11]]
def intersecting_nums(arr)
result = []
i = 0
comparison_nums = {}
arr.length.times do
arr[i].each do |num|
if !comparison_nums.has_key?(num)
comparison_nums[num] = true
elsif !result.include? num
result << num
end
end
i += 1
end
return result
end
p intersecting_nums(arrayofarrays)
# questions to do
# 1. Write a method that returns any integers in array that add up to the value 'n'.
# 2. Wrtie a method for selection sort
# 3. Write a method that returns if a string is a palendrome
# "given a string of words, reverse the words (""hello world"" => ""world hello"")
# given a string of a number (max 1000), return a string of that word in english (ex ""one thousand two hundred and forty five"""
# Given a long string or text file write a method to return the most frequently used word. (Now what about returning the top 10 words? How does .max work in the background?)
# Given an array of lengths of sticks(positive integers), write a method that will remove the length of the smallest stick from the other sticks, and then print out how many sticks are left until there are none left. (Added on - ) What is the O(n) efficiency? How would you improve it? example - [5,3,2,2,7] will print 5 , 3, 2, 1 (first step - [3,1,5] , second [2,4] , last [2])
# Write a method that will return the minimum number of coins to make change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment