Skip to content

Instantly share code, notes, and snippets.

@kduraiswami
Last active August 29, 2015 14:24
Show Gist options
  • Save kduraiswami/7415a8267b00806e8338 to your computer and use it in GitHub Desktop.
Save kduraiswami/7415a8267b00806e8338 to your computer and use it in GitHub Desktop.
people = ['John', 'Amelia', 'Steph']
units = (1..10).to_a
record = []
# this is making our data
500.times do
record << { "#{people.sample}" => "#{units.sample}".to_i }
end
=begin
STORY:
We used the gem 'HTTParty' to make a GET request to fetch some data.
It returned us an array of Hashes (like JSON) but in Ruby, into the variable RECORD.
We could turn them into objects but we just want to iterate over them
to return some data...
Each hash represents a sale and is structured as:
{
'salesperson' => units_sold_this_sale
}
TASK 1:
return who sold the most units.
Your answer should look like: "Amelia sold the most with X units"
TASK 2:
return who had the most individual sales.
Remember, each Hash represents a sale. So even though one salesperson may sell more units,
they may not neccesarily make the most sales.
Your answer should look like: "Steph had the most sales with X"
NOTE:
You are not allowed to use Ruby enumerators such as .each, .collect, .select.
Instead use a WHILE or FOR loop.
You can use .length, .first, .last of the records array.
TAKEAWAY:
Being able to prove that that we can iterate over a large series of data and
return proper data, in something that could/would be relevant, such as data
retreived via a GET request... but using FORs and WHILEs as they are
pretty much standard across all programming languages.
TIPS:
Print the record variable. What does it look like? What would be a good way to iterate over it?
What are you trying to get with each pass?
Good luck!
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment