D522 - Coding Problems Question with Verified
Correct Answers
Code Problem: Create a function named add_numbers that returns the sum of a and b.
Answer: def add_numbers(a, b):\n return a + b
Code Problem: Create a function named minutes_to_hours that converts minutes to
hours.
Answer: def minutes_to_hours(minutes):\n return minutes / 60
Code Problem: Create a function named is_adult that returns True if age is 18 or older.
Answer: def is_adult(age):\n return age >= 18
Code Problem: Create a function named check_temp that returns "Hot" if temp is
greater than 80, otherwise return "Not hot".
Answer: def check_temp(temp):\n if temp > 80:\n return "Hot"\n else:\n return "Not hot"
Code Problem: Create a function named check_access that returns "Allowed" only if
has_id is True and age is 18 or older.
Answer: def check_access(age, has_id):\n if age >= 18 and has_id == True:\n return
"Allowed"\n else:\n return "Denied"
Code Problem: Create a function named count_to_five that prints numbers 1 through 5
using a while loop.
Answer: def count_to_five():\n count = 1\n while count <= 5:\n print(count)\n count += 1
Code Problem: Create a function named get_high_values that returns values greater
than 90 from a list.
, Answer: def get_high_values(nums):\n high_values = []\n for num in nums:\n if num > 90:\n
high_values.append(num)\n return high_values
Code Problem: Create a function named get_high_indices that returns the indexes of
values greater than 90.
Answer: def get_high_indices(nums):\n indices = []\n for i in range(len(nums)):\n if nums[i]
> 90:\n indices.append(i)\n return indices
Code Problem: Create a function named first_item that returns the first item in a list.
Answer: def first_item(items):\n return items[0]
Code Problem: Create a function named last_item that returns the last item in a list.
Answer: def last_item(items):\n return items[-1]
Code Problem: Create a function named add_item that adds item to a list and returns
the updated list.
Answer: def add_item(items, item):\n items.append(item)\n return items
Code Problem: Create a function named update_name that changes the "name" value
in a dictionary.
Answer: def update_name(user, new_name):\n user["name"] = new_name\n return user
Code Problem: Create a function named get_status that returns the value stored under
the "status" key.
Answer: def get_status(log):\n return log["status"]
Code Problem: Create a function named format_rgb that accepts a list like [255, 0, 100]
and returns "rgb(255,0,100)".
Correct Answers
Code Problem: Create a function named add_numbers that returns the sum of a and b.
Answer: def add_numbers(a, b):\n return a + b
Code Problem: Create a function named minutes_to_hours that converts minutes to
hours.
Answer: def minutes_to_hours(minutes):\n return minutes / 60
Code Problem: Create a function named is_adult that returns True if age is 18 or older.
Answer: def is_adult(age):\n return age >= 18
Code Problem: Create a function named check_temp that returns "Hot" if temp is
greater than 80, otherwise return "Not hot".
Answer: def check_temp(temp):\n if temp > 80:\n return "Hot"\n else:\n return "Not hot"
Code Problem: Create a function named check_access that returns "Allowed" only if
has_id is True and age is 18 or older.
Answer: def check_access(age, has_id):\n if age >= 18 and has_id == True:\n return
"Allowed"\n else:\n return "Denied"
Code Problem: Create a function named count_to_five that prints numbers 1 through 5
using a while loop.
Answer: def count_to_five():\n count = 1\n while count <= 5:\n print(count)\n count += 1
Code Problem: Create a function named get_high_values that returns values greater
than 90 from a list.
, Answer: def get_high_values(nums):\n high_values = []\n for num in nums:\n if num > 90:\n
high_values.append(num)\n return high_values
Code Problem: Create a function named get_high_indices that returns the indexes of
values greater than 90.
Answer: def get_high_indices(nums):\n indices = []\n for i in range(len(nums)):\n if nums[i]
> 90:\n indices.append(i)\n return indices
Code Problem: Create a function named first_item that returns the first item in a list.
Answer: def first_item(items):\n return items[0]
Code Problem: Create a function named last_item that returns the last item in a list.
Answer: def last_item(items):\n return items[-1]
Code Problem: Create a function named add_item that adds item to a list and returns
the updated list.
Answer: def add_item(items, item):\n items.append(item)\n return items
Code Problem: Create a function named update_name that changes the "name" value
in a dictionary.
Answer: def update_name(user, new_name):\n user["name"] = new_name\n return user
Code Problem: Create a function named get_status that returns the value stored under
the "status" key.
Answer: def get_status(log):\n return log["status"]
Code Problem: Create a function named format_rgb that accepts a list like [255, 0, 100]
and returns "rgb(255,0,100)".