Programming Project: LoopFun
Collaboration: Solo. This is a Programming Project, not a Lab. Complete this project by yourself with optional help from section leaders. Do not work with anyone else, do not copy any code directly, do not copy code indirectly through reading, do not give your code to anyone. Begin early.
• This project asks you to write ten functions related to the Python for loop, strings, and lists.
• Start with LoopFun.py that has all 10 function headings with comments.
• Develop one function at a time, run the module (even thought there is no main), and test it from the Python shell as in
> firstN(3)
> 6
• When done with all 10 functions, run LoopFunTest.py to see if you will get 82/82 for correctness before turn in.
• Turn in one file only, LoopFun.py with the 10 functions specified below and in comments in LoopFun.py
______
1) def firstN(n):
Given an integer argument that represents the number of integers to sum, return the sum of the first n integers. Use a for loop, not a formula. Assume arguments to firstN are non-negative. firstN(0) should return 0. Do not test with firstN(-1) or any negative argument as this is undefined. Rick will not have tests with negative integers.
firstN(1) returns 1
firstN(2) returns 3, which is 1+2
firstN(5) returns 15, which is 1+2+3+4+5
______
2) def factorial(n):
Given an integer argument return n factorial, which is written as n!. 5! = 5*4*3*2*1 or in general, n! = n*(n-1)*(n-2) …*2*1. Use a while loop. By definition factorial(0) is 1.
factorial(0) returns 1
factorial(1) returns 1
factorial(2) returns 2, which is 2 * 1
factorial(3) returns 6, which is 3 * 2 * 1
factorial(4) returns 24, which is 4 * 3 * 2 * 1
______
3) def stringReverse(str):
Given a string, return a new string that has the characters of str in reverse order. Use a for loop.
stringReverse("") returns ""
stringReverse("1") returns "1"
stringReverse("1234") returns "4321"
stringReverse("racecar") returns "racecar"
______
4) def isPalindrome(str):
Return True if str is a palindrome, which is a string that reads exactly the same forward as reverse
isPalindrome("") returns true
isPalindrome("T") returns true
isPalindrome("racecar") returns true
isPalindrome("race car") returns false (space does not match 'e')
isPalindrome("Mm") returns false // case sensitive
______
5) def charPairs(str):
Return the number of times two consecutive characters occur in the given string.
charPairs("") returns 0
charPairs("H") returns 0
charPairs("abc") returns 0
charPairs("aabbcc") returns 3
charPairs("mmm") returns 2
______
6) def stringSplosion(str):
Given a non-empty string let like "Code" stringSplosion return a string like "CCoCodCode" where an increasingly longer substring gets added for each character more than 1.
stringSplosion("") returns ""
stringSplosion("1") returns "1"
stringSplosion("ab") returns "aab"
stringSplosion("abc") returns "aababc"
stringSplosion("Code") returns "CCoCodCode"
______
7) def zipZap(str):
Look for substrings like 'zip' and 'zap' in str, three letter substrings starting with 'z' and ending with 'p'. Return a string where for all such substrings, the middle letter is gone, so 'zipXzap' yields 'zpXzp'.
zipZap('zipXzap') returns 'zpXzp'
zipZap('zopzop')returns 'zpzp'
zipZap('zzzopzop') returns 'zzzpzp'
______
8) def listSum(list):
Given a list of integers, return the sum of all integers in that list. An empty list has a sum of 0.
listSum( [4, 2, 3 ,1] ) returns 10
listSum( [2, -3, 4, -5] ) returns -2
listSum( [ ] ) returns 0
______
9) def listRange(list):
Given a list of integers, return the range of the list elements. Range is defined as the maximum integer minus the minimum. Lists with a range of 0 include those with zero or one elements and a list where all elements are equal.
listRange( [ ] ) returns 0
listRange( [99] ) returns 0
listRange( [5, 5, 5, 5] ) returns 0
listRange( [1, 2] ) returns 1
listRange( [-1, -2] ) returns 1
listRange( [-2, 6, 0, 1, 13] ) returns 15
______
10) def listTrips(list):
Given a list of integers, return how often the same integer appears in three consecutive indexes. Overlapping is allowed so there are three listTrips in the list [9, 9, 9, 9, 9]
listTrips( [1, 2] ) returns 0
listTrips( [1, 2, 3] ) returns 0
listTrips( [1, 1, 1, 2, 2, 2] ) returns 2
listTrips( [2, 2, 2, 2] ) returns 2
Grading Crtieria: Turnin LoopFun.py to D2l Dropbox Project 5: LoopFun, Subject to Change.
___ / 82 The 82 assertions in LoopFunTest.py pass for the 10 functions (1 pt for each passing assertions in 10 Tests)
___ / 10 All function names match so there are no errors when we run our tests (1 pt each).
Suggestion: Run tests stored in LoopFunTest.py which must be in the same folder as your LoopFun.py
___/ 2 Your ten functions are completed in a module named LoopFun.py (not loopFun.py)
___/ 6 Style and Design: You used meaningful names for all identifiers in all functions (subjective)