Skip to content

Instantly share code, notes, and snippets.

@nguyendo24
Forked from fahdi/python_snake_fb.py
Created November 28, 2019 19:24
Show Gist options
  • Save nguyendo24/82e613fa3e47d6e1a6f398b8a6eeb3d2 to your computer and use it in GitHub Desktop.
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
# 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