-
Notifications
You must be signed in to change notification settings - Fork 253
Concatenate
kyra-ptn edited this page Aug 25, 2024
·
6 revisions
TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 5 mins
- 🛠️ Topics: String Manipulation, List Iteration, Return Statements
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- The function
concatenate()
should take a list of strings and concatenate them into a single string. If the list is empty, it should return an empty string.
HAPPY CASE
Input: ["vengeance", "darkness", "batman"]
Output: "vengeancedarknessbatman"
EDGE CASE
Input: []
Output: "
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Define the function to iterate through the list and concatenate each string.
1. Define the function `concatenate(words)`.
2. Initialize an empty string `concatenated` to store the result.
3. Iterate through each word in `words`:
a. Add the word to the `concatenated` string.
4. Return the `concatenated` string.
- Forgetting to handle the case when the list is empty.
Implement the code to solve the algorithm.
def concatenate(words):
concatenated = " # Initialize an empty string to store the result
# Iterate through each word in the list and add it to the result string
for word in words:
concatenated += word
return concatenated