During this activity, students should be able to:
Solve the following set of problems using Python 3. Run and test each of your programs to make sure they work as expected.
Write a program called duplicate.py
. Define in this program a function called duplicate(s)
that returns a new string in which every character of s
is duplicated. For example, duplicate("Hello")
should return "HHeelllloo"
.
Test your program with the following main()
function:
def main(): print(duplicate("Hello")) print(duplicate("")) print(duplicate("Programming is fun!")) print(duplicate("THE END"))
The expected program output should be:
HHeelllloo PPrrooggrraammmmiinngg iiss ffuunn!! TTHHEE EENNDD
Write a program called vowelless.py
. Define in this program a function called vowelless(word)
that returns a new string that contains the exact same characters contained in word
but without any vowels. Use the following main()
function to test your vowelless()
function:
def main(): print(vowelless('<AEIOUaeiou>')) print(vowelless('Hello!')) print(vowelless('This is a little test.')) print(vowelless('UMPA-LUMPA'))
The program’s output should look like this:
<> Hll! Ths s lttl tst. MP-LMP
Note that the original order and case of the characters should be preseved.
A palindrome is a word or sequence that reads the same backward as forward. For example: ana, civic, or madam.
Write a program called palindrome.py
. Define in this program a function called palindrome(s)
that returns True
if s
is a palindrome, otherwise returns False
.
def main(): print(palindrome("ana")) print(palindrome("civic") and palindrome("madam")) print(palindrome("sexes madam sexes")) print(palindrome("what is this?")) print(palindrome("not a palindrome")) print(palindrome("annas") or palindrome("caiaphas"))
The expected program output should be:
True True True False False False
The food chain company Juan In A Million has certain rules to generate their employees’ user names required for their internal information systems. Assuming that every employee has a first, middle and last name (for example: Scarlett Ingrid Johansson), these rules are as follows:
So, the user name for employee Scarlett Ingrid Johansson should be: snjohans
.
Write a program called username.py
. Define in this program a function called username(first, middle, last)
that takes the first
name, middle
name, and last
name of a person and generates her user name given the above rules. You may assume that every first name has at least one letter and every middle name has at least one consonant. Use string.lower()
to convert string
into lower case letters.
Test your program with the following main()
function:
def main(): print(username('Scarlett', 'Ingrid', 'Johansson')) print(username('Donald', 'Ervin', 'Knuth')) print(username('Alan', 'Mathison', 'Turing')) print(username('Martin', 'Luther', 'King')) print(username('Stephen', 'William', 'Hawking')) print(username('Alejandro', 'Gonzalez', 'Inarritu'))
The expected program output should be:
snjohans drknuth amturing mlking swhawkin aginarri
Procrustes was a legendary robber of ancient Attica. He bound his victims to an iron bed, and if they were shorter or longer than the bed, he stretched or chopped off their limbs so that they would fit precisely in it.
Write a program called procrustean.py
. Define in this program a function called procrustean(string, size, padding)
that takes string
and “procrusteans” it to size
number of characters, using padding
if necessary. This means that if string
has a length greater than size
, the function should return the original string truncated to size
characters. On the other hand, if the length of string
is less than size
, the function should return string
concatenated to padding
repeated as many times as necessary in order to produce a resulting string with exactly size
characters. Lastly, if string
has a length which happens to be equal to size
, the function should return string
with no alterations.
Use the following main()
function to test your code:
def main(): print(procrustean('Hello', 10, '*')) print(procrustean('Hello world!', 10, '*')) print(procrustean('Hello', 5, '*')) print(procrustean('AB', 20, 'test'))
The expected program output is:
Hello***** Hello worl Hello ABtesttesttesttestte
(This is problem 13.6 from [JOHNSON]) Write a program called encode.py
. Define in this program a function called encode(msg)
that returns a string containing
the ASCII codes of each character in msg
separated by spaces. For example, encode("ABC")
should return the string "65 66 67"
.
Test your program with the following main()
function:
def main(): print(encode("ABC")) print(encode("")) print(encode("Hello World!")) print(encode("THE END"))
The expected program output should be:
65 66 67 72 101 108 108 111 32 87 111 114 108 100 33 84 72 69 32 69 78 68