-
-
Save nguyendo24/82e613fa3e47d6e1a6f398b8a6eeb3d2 to your computer and use it in GitHub Desktop.
*Write function that takes two args: list of strings and a prefix (string). The function should iterate over the list and find all strings which start with the prefix, then return list of such strings
This file contains hidden or 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
| # https://www.facebook.com/groups/pythonsnake/ | |
| """ | |
| Write function that takes two args: list of strings and a prefix (string). | |
| The function should iterate over the list and find all strings which start | |
| with the prefix, then return list of such strings | |
| """ | |
| def return_prefixed_strings(strings, prefix): | |
| results = [] | |
| for string in strings: | |
| if string.startswith(prefix): | |
| results.append(string) | |
| return results | |
| def test_return_prefixed_strings(): | |
| test_cases = [ | |
| ((["Jamal", "Jaden", "Fahad", "Murtaza"], "Ja"), ["Jamal", "Jaden"]), | |
| ((["Jamal", "Jaden", "Fahad", "Murtaza"],"Fa"), ["Fahad"]) | |
| ] | |
| for (args, answer) in test_cases: | |
| result = return_prefixed_strings(*args) | |
| # print "Running Test with data:", args, "gave = ", result | |
| if result != answer: | |
| print("\x1b[0;30;41m" + "Test with data:", args, "failed" + "\x1b[0m") | |
| else: | |
| print("\x1b[6;30;42m" + "Test with data:", args, "passed" + "\x1b[0m") | |
| test_return_prefixed_strings() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment