Python (Fall 2026) Module 9-5: Algorithms and Solving A8 Part 2
Watch on YouTube →
Overview
Bill Moseley refactors email-address cleanup in Python by creating a reusable `cleanHalf` function that keeps alphanumeric characters plus periods, hyphens, and underscores, stopping at the first invalid character. He applies it to both halves of an address, restores the reversed first half, joins the halves with `@`, and returns the cleaned string from `cleanEmail`.
Key takeaways
- A helper such as `cleanHalf` avoids duplicating character-filtering code when the same operation must be applied to both sides of an email address.
- The `cleanHalf` character policy accepts alphanumeric characters plus `.`, `-`, and `_`; an invalid character ends processing with `break`.
- Cleaning a reversed string does not restore its order, so the first half must be reversed again before the address is reassembled.
- Joining the cleaned halves with `@` directly is simpler here than rebuilding a list and using `join`.
- Returning `strClean` makes `cleanEmail` usable by other code, while printing is only useful for temporary inspection.
- The demonstrated filter can leave a trailing period in some results, so the cleanup algorithm still needs an additional rule for that case.
Chapters
0:00
Build `cleanHalf` to Filter Email-Address Characters
- Bill Moseley extracts repeated validation work into a `cleanHalf` function called from `cleanEmail`.
- The function loops through a half-string and keeps alphanumeric characters and the permitted punctuation `.`, `-`, and `_`.
- When a character is invalid, `break` stops the loop so no later characters are added; the function returns the cleaned string.
4:00
Clean Both Halves and Restore the Reversed First Half
- `cleanEmail` passes `strFirstHalf` and `strSecondHalf` to `cleanHalf`, replacing each variable with the returned cleaned text.
- Because the first half was reversed earlier in the algorithm, Moseley reverses it again after cleaning to restore its original order.
- The reusable function keeps the filtering logic in one place rather than duplicating the character-checking loop.
6:30
Reassemble and Return the Cleaned Email Address
- The two cleaned halves are joined with `strFirstHalf + "@" + strSecondHalf` to form `strClean`.
- Moseley prints the assembled result for a check, then changes `cleanEmail` to return `strClean` instead of printing it.
- He notices some sample results end in a period and flags that trailing punctuation still needs to be handled.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.