Unit 4 Session 1 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the structure of the input?
Q: What is the output?
0
if the collection is empty.Q: How should the function handle an empty collection?
0
if the input list is empty.Q: Are there any constraints on the input, such as the presence of the "value" key in each dictionary?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Calculate the sum of the values of all NFTs in the collection and divide by the number of NFTs to find the average. If the collection is empty, return 0
.
1) Check if the collection is empty. If it is, return `0`.
2) Initialize a variable `total_value` to `0` to store the sum of the NFT values.
3) Iterate through each NFT in the collection:
a) Add the value of the NFT to `total_value`.
4) Calculate the average value by dividing `total_value` by the number of NFTs in the collection.
5) Return the average value.
**⚠️ Common Mistakes**
- Forgetting to check if the collection is empty before performing calculations.
- Miscalculating the average by using incorrect numerator or denominator.
def average_nft_value(nft_collection):
if not nft_collection:
return 0
total_value = 0
for nft in nft_collection:
total_value += nft["value"]
average_value = total_value / len(nft_collection)
return average_value