Abruptly Goblin planner step 8

hello i am struggling to comprehend this bit of the exercise, which i will mention below, how should i think of this problem, it’s literally confusing me

Lastly we need a way to pick the day with the most available people to attend so that we can schedule game night on that night.

Instructions

Write a function find_best_night that takes a dictionary availability_table and returns the key with the highest number.

off platform project abruptly goblin

gamers = []

# function that will update the 'gamers' list and add a new gamer if they have name and availability
def add_gamer(gamer, gamers_list):
    if gamer.get("name") and gamer.get("availability"):
        gamers_list.append(gamer)
    else:
        print("Missing crucial information")

# Create a dictionary called kimberly with the name and availability given above.
kimberly = {"name": "Kimberly Warner", "availability": ["Monday", "Tuesday", "Friday"]}
add_gamer(kimberly, gamers)
print(gamers)

# Continue creating more dictionaries for more gamers that want to join
add_gamer({'name': 'Thomas Nelson', 'availability': ["Tuesday", "Thursday", "Saturday"]}, gamers)
add_gamer({'name': 'Joyce Sellers', 'availability': ["Monday", "Wednesday", "Friday", "Saturday"]}, gamers)
add_gamer({'name': 'Michelle Reyes', 'availability': ["Wednesday", "Thursday", "Sunday"]}, gamers)
add_gamer({'name': 'Stephen Adams', 'availability': ["Thursday", "Saturday"]}, gamers)
add_gamer({'name': 'Joanne Lynn', 'availability': ["Monday", "Thursday"]}, gamers)
add_gamer({'name': 'Latasha Bryan', 'availability': ["Monday", "Sunday"]}, gamers)
add_gamer({'name': 'Crystal Brewer', 'availability': ["Thursday", "Friday", "Saturday"]}, gamers)
add_gamer({'name': 'James Barnes Jr.', 'availability': ["Tuesday", "Wednesday", "Thursday", "Sunday"]}, gamers)
add_gamer({'name': 'Michel Trujillo', 'availability': ["Monday", "Tuesday", "Wednesday"]}, gamers)
print(gamers)

# Setup weekly table
def build_daily_frequency_table():
    return {"Monday": 0, "Tuesday": 0, "Wednesday": 0, "Thursday": 0, "Friday": 0, "Saturday": 0, "Sunday": 0}

count_availability = build_daily_frequency_table()

# Calculate how many days gamers are available
def calculate_availability(gamers_list, available_frequency):
    for gamer in gamers_list:
        for day in gamer["availability"]:
            available_frequency[day] += 1

calculate_availability(gamers, count_availability)
print(count_availability)

#Write a function `find_best_night` that takes a dictionary `availability_table` and returns the key with the highest number.
def find_best_night(availability_table):
  return availability_table.get() help here plz 


this is where im stuck at I have written the function have put availability_table as an argument
Now the next step is i have to turn availability_table into a dictionary ?
then i have to find the highest key from its dictionairy?

I am confused because we have already the information on how many players we have weekly in count_availability
am i not supposed to use count_availability in my function? I peeked quickly on the solution fast enough so i dont remember the whole code, it doesn’t have count_availability in the function. i want to solve it by myself but just need some guidance. i’m not good with problem solving but i try daily to become better at it but i struggle with the process of thought

def find_best_night(availability_table):
    # body of function goes here
    #

availability_table is the parameter of the find_best_night function. When a function call is made to find_best_night, then the value being provided to the function will be the argument. When the function call is made, the argument will be assigned to the parameter. For Example,

def func(num):
   result = num ** 2 + 1
   print(result)
   return result

i = 12
func(i)

# Output:
# 145

# i is the argument
# After function is called, the argument's value will be assigned to 
# the parameter num. Within the body of the function, we will use 
# num to do our calculations.

func(10)
# Output:
# 101

j = 3
func(j)
# Output:
# 10

# We can call our function with different arguments, and yet won't 
# need to edit/modify our function. Whatever argument is passed 
# during a function call will be assigned to the parameter num, 
# and within the function we will use num to work with the value.

No, you don’t have to create a dictionary. A dictionary will be provided as the argument during a function call. After the function call, the argument will be assigned to the parameter availability_table.

In the code you have posted, count_availability is a dictionary and will have a structure like:

{'Monday': 5, 'Tuesday': 4, 'Wednesday': 4, 'Thursday': 6, 
'Friday': 3, 'Saturday': 4, 'Sunday': 3}

This dictionary will be passed as an argument to the find_best_night function. Within the body of the function, you will use the parameter availability_table to access this dictionary.

# Function call made with count_availability provided as the argument
print(find_best_night(count_availability))

# The find_best_night function will be executed and a reference to the 
# provided count_availability dictionary will be assigned to 
# the parameter availability_table. Within the function body, you 
# will use availability_table to access the provided dictionary.

The goal of find_best_night function is to pick out the night with the highest count. In the sample dictionary, it would be "Thursday" because it has the highest count of 6. There are many ways to accomplish this task. One way would be to loop over availability_table and keep track of the night with the highest count. Once the loop finishes, you can return the best night.

2 Likes

Thank you myman! much appreciated !
I see my error and sometimes still mix the vocabulary like parameter and argument which made me think of the problem differently.

def find_best_night(availability_table):
  highest_count = 0
  best_day = "none"

  for key, value in availability_table.items():
    if value > highest_count:
      best_count = value
      best_day = key
  return best_day
print("\n")
print(find_best_night(count_availability))

this worked just thought of what you said the argument will be passed on later for now i do the paramter which is a like a long placeholder on how it will perform, the dictionary should loop through the items , then just use operators to compare each time the values which count is higher , whenever it encounter a higher one, it becomes the best_count variable since it found the highest count it i can then just change the key to become the best_day and return the value, then i pass the argument count_availability.

thx ser

1 Like