Codepath

NFT Collection Statistics

Unit 4 Session 1 Standard (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What is the structure of the input?

    • A: The input is a list of dictionaries, where each dictionary represents an NFT and contains keys such as "name", "creator", and "value".
  • Q: What is the output?

    • A: The output is the average value of the NFTs in the collection, or 0 if the collection is empty.
  • Q: How should the function handle an empty collection?

    • A: The function should return 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?

    • A: It is assumed that each dictionary in the list will have a "value" key with a corresponding numeric value.

P-lan

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.

I-mplement

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
Fork me on GitHub