print(swapchars("This.sentence.has.a.lot.of.punctuation")) print(swapchars("aaaaaa")) print(swapchars("this uses the intermediate character, ~~ when replacing")) """for the last one we expect the ~~ to be replaced when it shouldn't be because that is the intermediate character used for swapping. I could not think of a way around that. For the 2nd test the punctuation is most common character. I could have created a string devoid off all punctuation and spaces, but that seemed tedious and unnecessary for the assignment. All the other tests behave as expected""" print #tests for question3.py from question3 import sortcat print "sortcat: " print(sortcat(1, "a", "ahhhhh")) print(sortcat(-1, "a", "ahhhhh")) print(sortcat(1, "hello", "world", "ign")) print(sortcat(2, "hello", "world", "ign")) print(sortcat(3, "hello", "world", "ign")) print(sortcat(-1, "hello", "world", "ignore")) print(sortcat(-5, "hello", "world", "ign")) """ All the outputs are as they are expected. First one should be 'ahhhhh', second one should be 'ahhhhha, and so on. I did not include a case where an input was not a string because this creates a TypeError which crashes the test file. To solve this we could test that all arguments entered are strings and if they are not return an error. """ print
if __name__ == '__main__': print "Displaying tests:" print "--- Question 2 ---" test_str = "There were a lot of escopeoples in the elevator on Tuesday." result_str = "Thyry wyry a lot of yseopyoplys in thy ylyvator on Tuysdae." print "Reversing: {}".format(test_str) print "Result: {}".format(swapchars(test_str)) print "Should be: {}".format(result_str) print "" print "Reversing: HeLlO" print "Result: {}".format(swapchars("HeLlO")) print "Should be: {}".format("HeOoL") print "" print "--- Question 3 ---" print "sortcat(1, 'abc', 'bc')" print "Gives: {}".format(sortcat(1, 'abc', 'bc')) print "Should be: {}".format('abc') print "" print "sortcat(2, 'bc', 'c', 'abc')" print "Gives: {}".format(sortcat(2, 'bc', 'c', 'abc')) print "Should be: {}".format('abcbc') print "" print "sortcat(-1, 'abc', 2)" print "Gives: {}".format(sortcat(-1, 'abc', 2)) print "Should be: {}".format("abc2") print "" print "--- Question 4 ---" print "Testing for 2000 trials..." trials = 2000 win_frac = look_away(trials) # Compute win loss counts based on fraction returned
from pprint import pprint # pretty print output formatting, you don't have to use this, can just use "print" from question2 import swapchars # so in the file question2.py, this will look for a function called "swapchars" from question3 import sortcat # so in the file question3.py, this will look for a function called "sortcat" from question4a import lookaway # so in the file question4a.py, this will look for a function called "lookaway" print "==testing question 2==" print "swapchars... ", pprint(swapchars("hiiiiii")) # Prints:"ihhhhhh" print print "==testing question 3==" print "sortcat... ", pprint(sortcat(3, "wordone", "w3", "word2", "4")) # Prints:"wordoneword2w3" pprint(sortcat(2, "dd", "aaaaaaa", "eeee", "bbbbb", "ccc")) # Prints:"aaaaaaabbbbb" print print "==testing question 4==" print "Will luigi win?... ", pprint(lookaway(100)) pprint(lookaway(100)) pprint(lookaway(100)) # Prints three proportions (usually between 20 to 30 wins out of 100 games) print # Make sure you record in comments what the result is! It should be what you expect, or else you know # something's wrong with your solution!