Skip to content

Instantly share code, notes, and snippets.

@artificemm
Last active March 31, 2021 22:08
Show Gist options
  • Select an option

  • Save artificemm/ff02d0be2c13a3b182cacb07d49fa253 to your computer and use it in GitHub Desktop.

Select an option

Save artificemm/ff02d0be2c13a3b182cacb07d49fa253 to your computer and use it in GitHub Desktop.
Tinnova first set of exercises
require 'set'
# Solved exercises
# Roberto Ruiz
# For Tinnova
# 1:
input = [{ "name": 'eggs', "price": 1 }, { "name": 'coffee', "price": 9.99 }, { "name": 'rice', "price": 4.04 }]
def sort_hash_by_price(ary)
ary.sort_by { |x| x[:price] }
end
puts "----- Exercise 1 output"
puts sort_hash_by_price(input)
#2:
def present_elements(ary1, ary2)
(ary1 - ary2).to_s
# to_s for cosmetic purposes on console
end
puts "----- Exercise 2 output"
puts present_elements([1,2,3,4], [1,2,3,4])
puts present_elements([1,2,3,4], [5,6,7,8])
puts present_elements([1,2,3,4], [3,4,5,6])
#3:
@data = Hash.new
#method to add as requested
def add_hash(hash)
if @data[hash[:parent]].nil?
@data[hash[:parent]] = Set.new
end
@data[hash[:parent]].add(hash[:category])
end
# Search method as requested, with error message
def search_by_parent(parent_letter)
if @data[parent_letter].nil?
"Parent '#{parent_letter}' does not exist."
else
@data[parent_letter].to_a.join(", ")
end
end
add_hash({:category=>"A", :parent=>"Z"})
add_hash({:category=>"B", :parent=>"A"})
add_hash({:category=>"C", :parent=>"A"})
add_hash({:category=>"D", :parent=>"V"})
puts "----- Exercise 3 output"
puts search_by_parent("A")
puts search_by_parent("H")
@artificemm
Copy link
Copy Markdown
Author

No feedback. Archiving for personal purposes.

Ineligible pow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment