Giter Site home page Giter Site logo

key-for-min-value's Introduction

Key For Minimum Value

Objectives

  1. Build a method that accepts an argument of a hash and iterates over it.

Instructions

  1. Fork and clone this lab
  2. Build a method key_for_min_value that accepts an argument of a hash. This method should iterate over the hash and return the key (not the value!) that points to the smallest value of the set. If the method is called and passed an argument of an empty hash, it should return nil.

Here are some examples:

ikea = {:chair => 25, :table => 85, :mattress => 450}
key_for_min_value(ikea)
# => :chair

veggies = {"apple" => -45, "banana" => -44.5, "carrot" => -44.9}
key_for_min_value(veggies)
# => "apple"

A Few Restrictions: We want you to build this on your own. Some of the following methods are helpful but off limits for this exercise. (We'll cover a few below in more depth in subsequent lessons).

  • #key
  • #keys
  • #values
  • #min
  • #sort
  • #sort_by
  • #min_by

A Helpful Hint:

  • Think about how to determine which value is the lowest. Do you need to compare each value to something as you iterate?
  • Think about how to collect or store the correct key. Remember that you need your method to return just this key.

key-for-min-value's People

Contributors

ahimmelstoss avatar annjohn avatar arelenglish avatar aturkewi avatar aviflombaum avatar curiositypaths avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar ihollander avatar kthffmn avatar markedwardmurray avatar maxwellbenton avatar pletcher avatar sarogers avatar sgharms avatar sophiedebenedetto avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

key-for-min-value's Issues

discuss manip/iterating over hashes

use this lab to intro how to iterate over hashes and some helpful methods on hashes. Add more tests to lab: they can solve this problem first just w/ iteration, then intro methods like .keys, .values, .sort, etc.

Local test failed

Hi,

I'm passing all my tests on terminal and the app on the computer but can't seem to get the darn red circle on the site to turn green. Raising this as an issue but moving on to the next lesson!

fix lab spec

  • make test fail in incremental order (reverse the order of failures)
  • first failure should be undefined methodkey_for_min_value'`

Restrictions

I would also suggest limiting sort_by as it's another workaround to the problem.

missing sort_by

The read me file says dont use these ruby methods
#key #keys #values #min #sort #sort_by #min_by

however the Rspec file doesnt exclude #sort_by and the test will pass

Thank you
sim

Help please...

def key_for_min_value(hash)
lowest_key = nil
lowest_value = Float::INFINITY
hash.each do |key, value|
if value < lowest_value
lowest_value = value
lowest_key = key
end
end
lowest_key
end

so i found this code for returning the key with the lowest value but i guess i don't understand the lowest_key = key part.... iterations are hard for me to understand as i don't get how they fully work. So can someone break this down for me? thanks

Consider Explaining this Lab better

I just looked at the solution branch for this lab and I think that this lab should be dialed down a bit, because I felt like I had an arm tied behind my back.

I used a bunch of searches like the following:
"find the lowest value in a hash ruby using each"
"iterate over a hash to return the lowest value without using .sort ruby"
"find lowest value in a hash without using sort ruby"
"lowest value in a hash without using .min"
"compare values in an array to find the lowest ruby"

As many of my search results began to overlap I really had no idea how to complete this lab without using .keys , .values ,.min , .sort , or .min_by. All of the searches were answered using the "off-limits" methods. Then for the first time I resorted to looking at the solution branch like I mentioned earlier.

Look at the solution code:

def key_for_min_value(hash)
  lowest_key = nil
  lowest_value = Float::INFINITY
  hash.each do |k, v|
    if v < lowest_value
      lowest_value = v
      lowest_key = k
    end
  end
  lowest_key
end

Bringing your attention to the use of Float::INFINITY. That's great that this method works it's why I can go on to learn more about hashes and not be stuck in "Ask" limbo; however, being someone who has taken up coding just two months ago classes aren't in my wheelhouse just yet and I wouldn't have ever thought to use something like Float::INFINITY. This is something that has not been covered in the curriculum and it's a real curve ball for people doing this for the first time. The file key_for_min.rb gives us this line: # prereqs: iterators, hashes, conditional logic, never mentioning classes or what Float even does.

I found the most relevant lab to this one was this one on sorting, yet it teaches us how to use a method that is off limits in this lab. I constructed this method just to see if I could achieve the goal of this lab:

def completes(name_hash) # This also works for this lab.
  name_hash.sort do |x,y|
     x <=> y
  end.first[0]
end

It passed the first test and failed the last two which surprised me.

Take aways here:

  1. This lab is placed in a really odd spot in the flow of the curriculum. I feel like it's saying "before you learn about all these methods we won't let you use, use your previous knowledge to complete this lab. Which according to how we have solved it the solution relies on something never covered.
  2. Dial down the difficulty This can be accomplished by simply taking away the restrictions or even adding a third "Helpful Hint" that links to a real helpful hint, introducing the Float class
  3. Fix the tests. I looked at both files in the spec folder and I did not see any indication that the "off-limits" methods were indeed off limits which makes me wonder why the completes method I copy and pasted above only passed one test, I at least expected it to pass the second.

Specs don't test for restrictions

The README says

A Few Restrictions: We want you to build this on your own. Some of the following methods are helpful but off limits for this exercise. (We'll cover a few below in more depth in subsequent lessons).

  • #keys
  • #values
  • #min
  • #sort
  • #min_by

but the tests don't prevent the use of these methods.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.