"""
File: ForLoop f.py
This file has functions related by the fact that CSC 101 is
learning to write Python functions using the algorithmic
pattern 'Determinate Loop'. All loops must be Python for loops!
Programmers: YOUR NAME
"""
# ---------------------------------------------------------
# ---------------------------------------------------------
def mopMap(string):
"""
Look for patterns like 'mop' 'map', 'mXp' in the string, starting with lower
case 'm' and ending with lower case 'p'. Return a string where for all
such words, the middle letter is gone, so 'mopXmap' yields 'mpXmp'.
This is case sensitive
mopMap('moP') returns 'moP'
Suggestion: Change the parameter with careful string slicing
the removes any character between 'm' and 'p'
"""
# TODO
# Tests
assert(mopMap('') == '')
assert(mopMap('abc') == 'abc')
assert(mopMap('mp') == 'mp')
assert(mopMap('mop') == 'mp')
assert(mopMap('map') == 'mp')
assert(mopMap('mipXmap') == 'mpXmp')
assert(mopMap('m pm p') == 'mpmp')
assert(mopMap('mmm1pm2p') == 'mmmpmp')
assert(mopMap('mxP') == 'mxP')
# ---------------------------------------------------------
# ---------------------------------------------------------
# Precondition: n >= 1
def addReciprocals(n):
"""
Complete function addReciprocals that takes an integer as a parameter (n)
and returns the sum of the first n reciprocals. In general, addReciprocals(n)
returns 1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ... + 1.0/n.
Note: n will always be greater than 0.
"""
# TODO
# Tests
assert(addReciprocals(1) == 1.0)
assert(addReciprocals(2) == 1.0 + 1.0 / 2.0)
assert(addReciprocals(3) == 1.0 + 1.0 / 2.0 + 1.0 / 3.0)
assert(addReciprocals(6) == 1.0 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6)
# ---------------------------------------------------------
# ---------------------------------------------------------
def vowelCount(string):
"""
'How much wood could a woodchuck chuck' contains 11 vowels. How do we
know? Five of the 26 English alphabet letters are vowels: a, e, i, o,
and u. By looking at each letter in the woodchuck string, we find
11 vowels. Complete function vowelCount that returns the number of vowels
in the given string argument. This is case insensitive. Upper and lower
case vowels count. We do not consider "y" to be a vowel.
"""
# TODO
# Tests
assert(vowelCount("able") == 2)
assert(vowelCount("a e i o u A E I O U") == 10)
assert(vowelCount("y+_x1Y2x!y34") == 0)
assert(vowelCount("How much WOOD could a woodchuck CHUCK") == 11)
# ---------------------------------------------------------
# ---------------------------------------------------------
# Precondition: n >= 0
def sumNints(n):
"""
Return the sum of the first n integers
sumNints(0) returns
sumNints(1) returns 1
sumNints(5) returns 1 + 2 + 3 + 4 + 5 or 15
"""
# TODO
# Tests
assert(sumNints(1) == 1)
assert(sumNints(2) == 3)
assert(sumNints(3) == 6)
assert(sumNints(4) == 10)
assert(sumNints(5) == 15)
# ---------------------------------------------------------
# ---------------------------------------------------------
def charPairs(word):
"""
Return the number of times two consecutive characters occur
in the given string.
"""
# TODO
# Tests
assert(charPairs("") == 0)
assert(charPairs("H") == 0)
assert(charPairs("abc") == 0)
assert(charPairs("aaba") == 1)
assert(charPairs("aabb") == 2)
assert(charPairs("mmm") == 2)
assert(charPairs("aabbccc") == 4)
# ---------------------------------------------------------
# ---------------------------------------------------------
def isPrime(num):
"""
One evening, while listening intently at the Very Large Array
(VLA) radio astronomy observatory 333 miles east of Oro
Valley AZ, Ellie hears a powerful signal: a prime number
pattern emanating from the star Vega, confirmed by others
the world over, undeniable and strong in its pulsing power.
What Ellie recognized was the repeating series of the
first 26 prime numbers. This is someone near Vega trying
to contact anyone who is listening:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
A prime number is a natural number greater than 1 that has
no positive divisors other than 1 and itself. Complete
function isPrime that returns true if the argument is a
prime number. Hint: Consider writing a loop that divides
the argument num by every integer from 2 to num-1,
returning false if num was found to be evenly divisible
by any one of those.
isPrime(2) returns True
isPrime(10) returns False
isPrime(11) returns True
"""
# TODO
# Tests
assert(isPrime(2))
assert(isPrime(3))
assert(not isPrime(4))
assert(isPrime(5))
assert(not isPrime(6))
assert(isPrime(7))
assert(not isPrime(10))
assert(isPrime(13))
assert(not isPrime(33))