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?
Q: What should the function return if no NFTs match the tag?
Q: Can an NFT have multiple tags?
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.
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