Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Hello algorithms

Binimate

#
# binimate.py
#


# Full example with unit tests


# binimate(s): kill every other character and return the result

def binimate(s):
    result = ""
    for i in range(len(s)):
        if i%2==0:
            result += s[i]
    return result


# unit test function: run the function binimate() and verify the
# output is what you expect

def test_binimate(s, expected):
    result = binimate(s)
    print("s:", s, "expected:", expected, "result:", result)
    if result == expected:
        print("Woohoo!")
    else:
        print("Boohoo!")


# multiple unit tests

test_binimate("Dr. Kessner", "D.Ksnr");
test_binimate("Briley", "Bie");
test_binimate("Jasmine", "Jsie");
test_binimate("Sophia", "Spi");

Output:

s: Dr. Kessner expected: D.Ksnr result: D.Ksnr
Woohoo!
s: Briley expected: Bie result: Bie
Woohoo!
s: Jasmine expected: Jsie result: Jsie
Woohoo!
s: Sophia expected: Spi result: Spi
Woohoo!

Next: