-
-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5854d9a
commit 286d73f
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Mentoring | ||
|
||
## Reasonable Solutions | ||
|
||
### Solution 1: Straightforward Iteration | ||
|
||
```Python | ||
def recite(start_verse, end_verse): | ||
verses = [] | ||
for i in range(start_verse - 1, end_verse): | ||
clause = " ".join(VERSES[i::-1]) | ||
verses.append(f"This is {clause}") | ||
return verses | ||
``` | ||
#### Explanation: | ||
- Directly starts the loop at start_verse - 1 to avoid unnecessary iterations. | ||
- Uses slicing VERSES[i::-1] to efficiently reverse the order of verses within the clause. | ||
|
||
|
||
### Solution 2: List Comprehension | ||
|
||
```Python | ||
def recite(start_verse, end_verse): | ||
return [f"This is {' '.join(VERSES[i::-1])}" for i in range(start_verse - 1, end_verse)] | ||
``` | ||
#### Explanation: | ||
- Uses list comprehension for a concise way to build the verses list. | ||
|
||
## Common Suggestions | ||
- Optimize the Loop:By starting the loop at start_verse - 1 instead of 0, we can avoid unnecessary iterations and directly jump to the relevant part of the rhyme. | ||
- Slicing: Python's list slicing offers an elegant and efficient way to extract a portion of a list. | ||
- Naming Conventions: Using all capital letters for constants like VERSES is a good practice in Python. | ||
- Error Handling: It's a good practice to add checks for invalid inputs, such as: | ||
``` | ||
start_verse being less than 1 or greater than the length of VERSES | ||
end_verse being less than start_verse or greater than the length of VERSES. | ||
``` | ||
|
||
## Talking Points | ||
- Alternative Approaches: Potential use of recursion. | ||
- The "House that Jack Built" rhyme has a recursive structure. We can use recursion to build the clauses | ||
|