Rank hash keys based on value - ruby

I'm trying to determine a rank for each key in a hash against the other keys based on it's value. The value is numeric. Ranks can be repeated (i.e. 3 keys can tie for first place). This works, but is ugly.
standings.sort_by {|k, v| v}.reverse!
prev_k = nil
standings.each_with_index do |(k, v), i|
if i == 0
k.rank = 1
elsif v == standings[prev_k]
k.rank = prev_k.rank
else
k.rank = prev_k.rank + 1
end
prev_k = k
end

Give this a try:
ranks = Hash[standings.values.sort.uniq.reverse.each_with_index.to_a]
standings.each { |k, v| k.rank = ranks[v] + 1 }
I'm not sure it's any prettier, but it's a bit more compact, carries fewer loop variables, and has no conditionals.

Related

Effective way to memoize a combination of two numbers

I'm working on an algorithm to count the number of ways to build 100 cents given an infinite amount of pennies, dimes, nickels, and quarters.
I ended up coming with the above (which AFAIK works):
def count_ways(amount)
num_ways(amount, 0)
end
def num_ways(amount, index)
return 1 if amount == 0
return 0 if index >= COINS.length || amount < 0
num_ways(amount - COINS[index], index) +
num_ways(amount, index + 1)
end
Now, I'd like to memoize this algorithm. An effective way I've found to think about memoizing is to consider what inputs do we pass into this function repeatedly. In this case, I'd like to memoize the combination of amount & index parameters.
Typically when I have two parameters, I build a two D array as a way to memoize but here that makes a lot less sense. Consequently, how can you memoize off of these two parameters? Does it make sense to do something like this?
def count_ways(amount)
memo = Hash.new { |h, k| h[k] = [] }
num_ways(amount, 0, memo)
end
def num_ways(amount, index, memo)
return 1 if amount == 0
return 0 if index >= COINS.length || amount < 0
memo[amount - COINS[index], index] ||= num_ways(amount - COINS[index], index)
memo[amount, index + 1] ||= num_ways(amount, index + 1)
memo[amount - COINS[index], index] +
memo[amount, index + 1]
end
I believe there is no common way to implement memoization when you solve algo task. Because of speed. Way you have to choose depends on you algo, input data and so on.
Few imperative rules:
avoid creation of many data structure instances: h = {}; h[ [1,2] ]
= 3 will produce COINS.size * amount Arrays just for keys
use Array for contiguous data and Hash for gapped one
use Hash instead of Array when you can't predict data size
create Array with needed values when you can predict your
contiguous data size
Using that rules, memoization (just in your case, where COINS.size << amount and both datas are contiguous) might look like:
COINS = [25, 10, 5, 1]
def count_ways(amount)
# memo is just COINS.size + 1 Array instances
# a[0] = 1 instead of return 1 if amount == 0
memo = Array.new(COINS.size){ Array.new(amount).tap{ |a| a[0] = 1 } }
num_ways(amount, 0, memo)
end
def num_ways(amount, index, memo)
return 0 if index >= COINS.size || amount < 0
memo[index][amount] ||=
num_ways(amount - COINS[index], index, memo) +
num_ways(amount, index + 1, memo)
end
P.S. dynamic-programming tag is unnecessary :)
Since identical arrays have different object ids, you couldn't just put the values as an array in a hash key. However you can convert to string to make a simple lookup structure, for example converting to YAML/JSON
require 'json'
memo = {}
def memoize(*args, &blk)
memo[args.to_json] ||= blk.call
end
def num_ways(amount, index)
memoize(amount, index) do
# original method body
end
end
edit
nevermind, I guess I am wrong about the need to use to_json. You could just use args inside memoize and not args.to_json.

Given an array of integers and target, return true if any two elements sum to target

As the title states, I'm looking to find two elements whose sum is equal to the target integer. I've already worked out the solution below, but this works only for unique integers. I'm having an issue when it comes to duplicate values. Of course, I can use multiple loops that keep track of two separate indices and, in this way, make sure those two indices are the same. However, I wanted to see if there was a way to solve this with one iteration.
two_sum([1,2,3,4,5,6], 8) = true
two_sum([4,4], 8) = false
def two_sum(array, target)
idx = 0
while idx < array.length
complement = target - array[idx]
if array.include?(complement) && complement != array[idx]
return true
end
idx+=1
end
return false
end
require 'set'
def two_sum(arr, tot)
st = Set.new
arr.each { |n| st.include?(tot-n) ? (return true) : st << n }
false
end
two_sum [1,2,3,4,5,6], 8 #=> true
two_sum [4,4], 8 #=> true
two_sum [1,2,3,4,5,6], 3421 #=> false
If you'd prefer to return the two values that sum to tot (when true), replace true with [n, tot-n] (or just return n, the other being tot - n).
I used a set rather than an array for faster lookups.
def two_sum(array, target)
array.combination(2).each { |pair| return true if pair.inject(:+) == target }
return false;
end

Finding the mode of an array in Ruby

When creating a method to find the mode of an array, I see people iterating over the array through a hash with default value 0:
def mode(array)
hash = Hash.new(0)
array.each do |i|
hash[i]+=1
end
end
or
freq = arr.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
Can someone explain the following part of the block?
hash[i] = hash[i] + 1 or h[v] = h[v] + 1
How does the iterator know to add +1 to each unique key of the hash? For example:
array = [1,1,1,2,3]
freq = arr.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
#=> {1:3, 2:1, 3:1}
If someone can explain how to find the mode of an array, I would be grateful.
In you first example, you need the method to return the hash that is created, or do some manipulation of the hash to compute the mode. Let's try it, just returning the hash (so I've added hash as the last line):
def hash_for_mode(array)
hash = Hash.new(0)
array.each do |i|
hash[i]+=1
end
hash
end
array = [1,3,1,4,3]
hash_for_mode(array) #=> {1=>2, 3=>2, 4=>1}
With hash_for_mode you can easily compute the mode.
By defining the hash h = Hash.new(0), we are telling Ruby that the default value is zero. By that, we mean that if a calculation is performed that depends on h[k] when k is not a key of h, h[k] will be set equal to the default value.
Consider, for example, when the first value of array (1 in my example) is passed into the block and assigned to the block variable i. hash does not have a key 1. (It has no keys yet.) hash[1] += 1 is shorthand for hash[1] = hash[1] + 1, so Ruby will replace hash[1] on the right side of the equality with the default value, zero, resulting in hash[1] => 1.
When the third value of array (another 1) is passed into the block, hash[1] already exists (and equals 1) so we just add one to give it a new value 2.
In case you were wondering, if we have:
hash = Hash.new(0)
hash[1] += 1
hash #=> {1=>1}
puts hash[2] #=> nil
hash #=> {1=>1}
That is, merely referencing a key that is not in the hash (here puts hash[2]), does not add a key-value pair to the hash.
Another common way to do the same thing is:
def hash_for_mode(array)
array.each_with_object({}) { |i,hash| hash[i] = (hash[i] || 0) + 1 }
end
hash_for_mode(array) #=> {1=>2, 3=>2, 4=>1}
This relies on the fact that:
(hash[i] || 0) #=> hash[i] if hash already has a key i
(hash[i] || 0) #=> 0 if hash does not have a key i, so hash[k]=>nil
(This requires that your hash does not contain any pairs k=>nil.)
Also, notice that rather than having the first statement:
hash = {}
and the last statement:
hash
I've used the method Enumerable#each_with_object, which returns the value of the hash. This is preferred here to using Enumerable#inject (a.k.a reduce) because you don't need to return hash to the iterator (no ; h needed).
array = [1,3,1,4,3]
array.group_by(&:itself).transform_values(&:count)
# => {1=>2, 3=>2, 4=>1}

Ruby - Return duplicates in an array using hashes, is this efficient?

I have solved the problem using normal loops and now using hashes, however I am not confident I used the hashes as well as I could have. Here is my code:
# 1-100 whats duplicated
def whats_duplicated?(array)
temp = Hash.new
output = Hash.new
# Write the input array numbers to a hash table and count them
array.each do |element|
if temp[element] >= 1
temp[element] += 1
else
temp[element] = 1
end
end
# Another hash, of only the numbers who appeared 2 or more times
temp.each do |hash, count|
if count > 1
output[hash] = count
end
end
# Return our sorted and formatted list as a string for screen
output.sort.inspect
end
### Main
# array_1 is an array 1-100 with duplicate numbers
array_1 = []
for i in 0..99
array_1[i] = i+1
end
# seed 10 random indexes which will likely be duplicates
for i in 0..9
array_1[rand(0..99)] = rand(1..100)
end
# print to screen the duplicated numbers & their count
puts whats_duplicated?(array_1)
My question is really what to improve? This is a learning excercise for myself, I am practising some of the typical brain-teasers you may get in an interview and while I can do this easily using loops, I want to learn an efficient use of hashes. I re-did the problem using hashes hoping for efficiency but looking at my code I think it isn't the best it could be. Thanks to anyone who takes an interest in this!
The easiest way to find duplicates in ruby, is to group the elements, and then count how many are in each group:
def whats_duplicated?(array)
array.group_by { |x| x }.select { |_, xs| xs.length > 1 }.keys
end
whats_duplicated?([1,2,3,3,4,5,3,2])
# => [2, 3]
def whats_duplicated?(array)
array.each_with_object(Hash.new(0)) { |val, hsh| hsh[val] += 1 }.select { |k,v| v > 1 }.keys
end
I would do it this way:
def duplicates(array)
counts = Hash.new { |h,k| h[k] = 0 }
array.each do |number|
counts[number] += 1
end
counts.select { |k,v| v > 1 }.keys
end
array = [1,2,3,4,4,5,6,6,7,8,8,9]
puts duplicates(array)
# => [4,6,8]
Some comments about your code: The block if temp[element] == 1 seems not correct. I think that will fail if a number occurs three or more times in the array. You should at least fix it to:
if temp[element] # check if element exists in hash
temp[element] += 1 # if it does increment
else
temp[element] = 1 # otherwise init hash at that position with `1`
end
Furthermore I recommend not to use the for x in foo syntax. Use foo.each do |x| instead. Hint: I like to ask in interviews about the difference between both versions.

optimize this ruby code

So this code will count the total number of pairs of numbers whose difference is K. it is naive method and I need to optimize it. suggestions?
test = $stdin.readlines
input = test[0].split(" ")
numbers = test[1].split(" ")
N = input[0]
K = input[1]
count = 0
for i in numbers
current = i.to_i
numbers.shift
for j in numbers
difference = (j.to_i - current).abs
if (difference == K)
count += 1
end
end
end
puts count
Would have been nice for you to give some examples of input and output, but I think this is correct.
require 'set'
def count_diff(numbers, difference)
set = Set.new numbers
set.inject 0 do |count, num|
set.include?(num+difference) ? count+1 : count
end
end
difference = gets.split[1].to_i
numbers = gets.split.map { |num| num.to_i }
puts count_diff(numbers, difference)
Untested, hopefully actual Ruby code
Documentation for Set: http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html
require 'set'
numbers_set = Set.new
npairs = 0
numbers.each do |number|
if numbers_set.include?(number + K)
npairs += 1
end
if numbers_set.include?(number - K)
npairs += 1
end
numbers_set.add(number)
end
Someone deleted his post, or his post was deleted... He had the best solution, here it is :
test = $stdin.readlines
input = test[0].split(" ")
numbers = test[1].split(" ")
K = input[1]
count = 0
numbers.combination(2){|couple| couple.inject(:-).abs == K ? count++}
puts count
You don't even need N.
I do not know Ruby so I'll just give you the big idea:
Get the list
Keep a boolean array (call it arr), marking off numbers as true if the number exists in the list
Loop through the list and see if arr[num-K] and/or arr[num+K] is true where num is a number in your list
This uses up quite a bit of memory though so another method is to do the following:
Keep a hash map from an integer n to an integer count
Go through your list, adding num+K and num-K to the hash map, incrementing count accordingly
Go through your list and see if num is in the hash map. If it is, increment your counter by count

Resources