NFT Tag Search - codepath/compsci_guides GitHub Wiki

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 nested list where each sublist represents an NFT collection, and each NFT is a dictionary containing a name and a list of tags.
  • Q: What is the output?

    • A: The output is a list of NFT names that contain the specified tag.
  • Q: What should the function return if no NFTs match the tag?

    • A: The function should return an empty list.
  • Q: Can an NFT have multiple tags?

    • A: Yes, an NFT can have multiple tags, and the function should return the NFT name if any of its tags match the specified tag.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Iterate through each collection and each NFT within the collection, checking if the specified tag is in the NFT's list of tags. If it is, add the NFT's name to the result list.

1) Initialize an empty list called `matching_nfts` to store the names of NFTs that match the tag.
2) For each `collection` in `nft_collections`:
   a) For each `nft` in `collection`:
      i) If `tag` is in `nft["tags"]`, append `nft["name"]` to `matching_nfts`.
3) Return the `matching_nfts` list.

**⚠️ Common Mistakes**

- Forgetting to check all collections for the specified tag.
- Not handling the case where the list of tags is empty or missing.

I-mplement

def search_nft_by_tag(nft_collections, tag):
    matching_nfts = []
    for collection in nft_collections:
        for nft in collection:
            if tag in nft["tags"]:
                matching_nfts.append(nft["name"])
    return matching_nfts