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.

Revisions

  1. @fahdi fahdi created this gist Aug 11, 2019.
    33 changes: 33 additions & 0 deletions python_snake_fb.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # 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()