8 Replies - 387 Views - Last Post: 12 March 2019 - 01:47 PM Rate Topic: -----

#1 Gh05t_97   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-March 19

Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 09:12 AM

The last method, I literally have no idea what to do. I could use any improvements to improve my code. I think most of the method have the same layout but I am not getting the desired output, so any help would be great. If you want to see the exercise online, its called the Bachelor Nested Iteration. I really have no clue why I am not getting my desired output, to me my working out makes sense I guess.

thebachelor = { 
  "season 30": [
    {
      "name":      "Beth Smalls",
      "age":       "26",
      "hometown":  "Great Falls, Virginia",
      "occupation": "Nanny/Freelance Journalist",
      "status":    "Winner"
    },
    {
      "name":       "Becca Tilley",
      "age":        "27",
      "hometown":   "Shreveport, Louisiana",
      "occupation": "Chiropractic Assistant",
      "status":     "Eliminated Week 8"
    }
  ],
  "season 29": [
    {
      "name":      "Ashley Yeats",
      "age":       "24",
      "hometown":  "Denver, Colorado",
      "occupation": "Dental Assitant",
      "status":    "Winner"
    },
    {
      "name":       "Sam Grover",
      "age":        "29",
      "hometown":   "New York, New York",
      "occupation": "Entertainer",
      "status":     "Eliminated Week 6"
    }
  ]
}

def get_first_name_of_season_winner(data, season)

	#this method returns the first name of that seasons winner
	#pass the season of the show, and then it returns only th FIRST NAME of the winner for that season
	#iterate through the inital hash to access the season number
	#then iterate through the array, to access the hash inside
	#acess the "status" to get the output
  data.each do |season, contestant_Data|
    contestant_Data.each do |a|
      a.each do |attribute, value|
        if value == "Winner"
          return a[:name]
        end
      end 
    end
  end
end

get_first_name_of_season_winner(thebachelor, "season 29") #returns the full name of only "Beth Smalls"
			
def get_contestant_name(data, occupation) #this method takes in the data hash and an occupation string and returns the name of the woman who has that occupation

	#iterate through the initial hash to access the seasons
	#iterate through the seasons to access the arrays inside
	#access the occupation element of the array
	#return the person who has the occupation

	data.each do |season, contestant_data|
		contestant_data.each do |a|
			a.each do |attribute, value|
				if attribute == :occupation
					return a[:name]
				end 
			end 
		end 
	end
end

get_contestant_name(thebachelor, "Chiropractic Assistant" ) #returns the full name of only "Beth Smalls"



def count_contestant_by_hometown(data, hometown) #this method should return the number of contestants from the hometown passed
	#include a counter variable
	#iterate through the hash to access the seasons
	#access the array 
	#access the hometown key in the hash
	#keep count

	counter = 0 
	data.each do |season, contestant_data|
		contestant_data.each do |a|
			a.each do |attribute, value|
				if attribute == :hometown
					counter += 1
				end
			end 
		end 
	end 

	return counter
end 

count_contestant_by_hometown(thebachelor, "Denver, Colorado") #returns the number 4, I have no idea why

def get_occupation(data, hometown) #should return the occupation of of the first contestant who hails from the hometown

	data.each do |season, contestant_data|
		contestant_data.each do |a|
			a.each do |attribute, value|
				if attribute == :hometown
					return a[:name]
				end 
			end 
		end 
	end

end 

def average_age_for_season(data, season) #returns the average age of all contestants for that season
end


Is This A Good Question/Topic? 0
  • +

Replies To: Iteration of nested hashes and arrays to get output

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,188
  • Joined: 12-December 12

Re: Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 09:17 AM

Please provide a clear description and question. Expecting someone to hunt around for an exercise will, in particular, be received poorly.
Was This Post Helpful? 0
  • +
  • -

#3 Gh05t_97   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-March 19

Re: Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 09:20 AM

View Postandrewsw, on 11 March 2019 - 09:17 AM, said:

Please provide a clear description and question. Expecting someone to hunt around for an exercise will, in particular, be received poorly.


I described everything I needed to which are on the comments on the code? What more do you want lol
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15222
  • View blog
  • Posts: 60,931
  • Joined: 12-June 08

Re: Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 09:29 AM

Let's start small. What output are you getting as compared to what it should be?
Was This Post Helpful? 0
  • +
  • -

#5 Gh05t_97   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-March 19

Re: Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 09:42 AM

View Postmodi123_1, on 11 March 2019 - 09:29 AM, said:

Let's start small. What output are you getting as compared to what it should be?


for the get_first_name_of_season_winner method, no matter what arguments I pass through when I call it, I always get "Beth Smalls" as an output when it shouldn't be the case. If I pass "Season 29", the output should be "Ashley Yeats"

for the get_contestant_name method, it's the same thing. It always returns "Beth Smalls" no matter what occupation I pass through. For example if I call it like this

get_contestant_name(thebachelor, "Chiropractic Assistant" )

it should return "Becca Tilley" as an output but it doesn't.

for the count_contestant_by_hometown, it should return the number of contestants which are from the hometown thats passed in the method, however, no matter which argument I pass, I get the number 4 as an output.

for the get_occupation, it should return the name of the person corresponding to the hometown being passed in the method, but I always get "Beth Smalls" no mattet which location I pass through it.

The final method, I have no idea how to do it. It takes in two arguments––the data hash and a string of a season. Iterate through the hash and return the average age of all of the contestants for that season.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 09:47 AM

You never use the value of your season argument. You just loop over the contestants from all seasons and return as soon as the given condition is true for any of them. So you'll always get the same result regardless of which value you pass in as the second argument.

If you want the second argument to make a difference, you'll need to actually use it inside the method.
Was This Post Helpful? 0
  • +
  • -

#7 Gh05t_97   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-March 19

Re: Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 09:50 AM

View Postsepp2k, on 11 March 2019 - 09:47 AM, said:

You never use the value of your season argument. You just loop over the contestants from all seasons and return as soon as the given condition is true for any of them. So you'll always get the same result regardless of which value you pass in as the second argument.

If you want the second argument to make a difference, you'll need to actually use it inside the method.


Okay, where should I put this in my code?
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 979
  • View blog
  • Posts: 3,858
  • Joined: 13-June 14

Re: Iteration of nested hashes and arrays to get output

Posted 11 March 2019 - 12:47 PM

You tell us. How would you use the value passed to the function to get the result you want? Imagine you had this data written down on paper or something and someone asked you the first name of the winner for a particular season. How would you work that out?

Also, there's no need to quote the previous post in its entirety - just use the "Reply" button.
Was This Post Helpful? 0
  • +
  • -

#9 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: Iteration of nested hashes and arrays to get output

Posted 12 March 2019 - 01:47 PM

One of the problems with your question is that you are asking a whole lot of questions at the same time. Furthermore, your way of asking is a bit disorganized. Here's a format that works: I did x, I expected y, I got z. If you leave any of those out, you're asking everyone else to figure out what you're looking for instead of just telling us.

So, let's break it down a bit. Here's your first question. On line 54, why are you returning the full name instead of just the first name? Answer, because your line 49 is returning the entire value of the hash element, and you need to break it apart, and only return that part of the value that precedes a space. I suggest that you look up `#split` in the Ruby doc.

Now, your next question is on line 74, and again, your method is returning exactly what you tell it to. You need to develop a better understanding of hashes and `#each`. In effect, what your method is saying is "return the name value in the first hash that contains the key `:occupation`. I won't tell you exactly why, because you won't learn it for yourself then. But I'll give you a hint: you don't need the `#each` on line 65. Rather, you need to do something analogous with `:occupation` to what you do with `:name` on line 67.

If you figure that one out, you should have the key to the problems you're experiencing with the other methods.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1