Homework 2
CS2110 Programming for Informatics - Spring 2021
Due Thursday, Feb. 11, by 10:00pm
6 points
Note: you may not use lists, dictionaries, list comprehensions, or similar Python constructs not taught yet in this course.
1. Write function q1(origString, lettersToRepeat, count) that takes as input a string, origString, a string of lower case letters, lettersToRepeat, and a positive integer, count, and returns a new string such that each letter in origString that occurs in lettersToRepeat (ignoring case!) is replaced by n consecutive copies of that letter (with the same case as in the original).
NOTE: You may not use any string methods except for lower().
For example,
>>> q1("Our cat is funny.", "aeiou", 5 )
'OOOOOuuuuur caaaaat iiiiis fuuuuunny.'
>>> q1("Our cat is funny.", "zu", 3 )
'Ouuur cat is fuuunny.'
>>> q1("Hi there, Jim!!!", "jix", 2)
'Hii there, JJiim!!!'
2. Implement function q2(string1, string2, num) so that it returns True if the input strings are the same length and
differ at exactly num character positions, and returns False otherwise.
NOTE: Use a simple loop comparing corresponding characters direclty. You may not use any string methods.
For example
>>> q3("bat", "bet", 1)
True
>>> q3("art", "rat", 1)
False
>>> q3("art", "ran", 1)
False
>>> q3("art", "rat", 2)
True
>>> q3("art", "ran", 3)
True
>>> q3("abcde", "abada", 2)
True
3.
Implement q3(inputString, minLetter, lettersIgnore) so that it returns two things:
- the smallest letter in inputString that is both greater than minLetter and not in lettersToIgnore
- the highest index at which that letter occurs.
If there is no such letter return None for both values. i.e. (None None)
inputString is a string a zero or more lower case letters, minLetter is a lower case letter, and lettersToIgnore is a string
of zero or more lower case letters.
Use a simple while loop. You may not use any string methods, but you may use the 'in' or 'not in' operators.
E.g.
>>> q3("bccacbd", "a", "eb")
('c', 4)
>>> q3("bccacbd", "a", "aefg")
('b', 5)
>>> q3("abc", "d", "")
(None, None)
Remember, as in HW1, to return two values, simply list both in the return statement with a comma separating them.
E.g. return (smallestLetter, indexOfSmallestLetter) or return smallestLetter, indexOfSmallestLetter
Submit to ICON one python file containing the required functions. The file must not contain any code (other than, perhaps, "import math") that is not part of a function definition.