Inducción al uso de Python

Práctica 6: Manipulación de texto

Objective

During this activity, students should be able to:


Activity Description

Solve the following set of problems using Python 3. Run and test each of your programs to make sure they work as expected.

  1. 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
  2. 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.

  3. 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
  4. 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:

    • The first character of the user name should be the first letter of the first name.
    • The second character of the user name should be the first consonant of the middle name.
    • The third character onwards should be the last name.
    • The complete user name should not be longer than eight characters. Any superfluous characters should be chopped off from the end.
    • Every character in the user name should be in all lower case letters.

    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
  5. 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
    
  6. (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