tdd/gcd.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 31 Aug 2010 20:13:49 +0530
changeset 113 38ba17aa202f
parent 111 a6a442d1bbd9
child 114 83b3e357ed08
permissions -rw-r--r--
Added the test cases for GCD function and the Python script that was used to generate it.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
111
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
def gcd(a, b):
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
    while b != 0:
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
        a, b = b, a % b
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
    return a
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
if __name__ == '__main__':
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
    tc1 = gcd(48, 64)
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
    if tc1 != 16:
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
        print "Test failed for the case a=48 and b=64. Expected 16. Obtained %d instead." % tc1
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
        exit(1)
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
    tc2 = gcd(44, 19)
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
    if tc2 != 1:
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
        print "Test failed for the case a=44 and b=19. Expected 1. Obtained %d instead." % tc2
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
        exit(1)
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
    print "All tests passed!"