NFT Collection Review - 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 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 a list of names (strings) extracted from each dictionary in the input list.
-
Q: What should the function return if the input list is empty?
- A: The function should return an empty list.
-
Q: What is the problem in the provided code?
- A: The issue is with the use of the
+=
operator, which concatenates each character of the name string individually to the list, resulting in a list of characters instead of a list of full names.
- A: The issue is with the use of the
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Correct the method used to add names to the list. Instead of concatenating characters, we should append the entire string to the list.
1) Identify the incorrect usage of `+=` in the code.
2) Replace it with the `append()` method to ensure the full name string is added to the list.
3) Test the function with the provided examples to ensure correctness.
**⚠️ Common Mistakes**
- Using `+=` with a list and a string can lead to unintended behavior where each character of the string is added separately to the list.
I-mplement
def extract_nft_names(nft_collection):
nft_names = []
for nft in nft_collection:
nft_names.append(nft["name"])
return nft_names