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.

Revisions

  1. artificemm revised this gist Mar 23, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tinnova.rb
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ def add_hash(hash)
    @data[hash[:parent]].add(hash[:category])
    end

    # hesitated in Programming, logic explained:
    # Search method as requested, with error message
    def search_by_parent(parent_letter)
    if @data[parent_letter].nil?
    "Parent '#{parent_letter}' does not exist."
  2. artificemm created this gist Mar 23, 2021.
    59 changes: 59 additions & 0 deletions tinnova.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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

    # hesitated in Programming, logic explained:
    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")