Estás en:   ArielOrtiz.com > Fundamentos de programación > Práctica 6: Manipulación de texto

Práctica 6: Manipulación de texto

Objectives

During this activity, students should be able to:

This activity helps students develop the following skills, values and attitudes: proficiency in English, ability to analyze and synthesize, capacity to identify and solve problems, and efficient use of computer systems.

Activity Description

Individually or in pairs, solve the following set of problems using Python 3.5. Run and test each of your programs to make sure they work as expected.

Each source file must include at the top the authors’ personal information (student ID and name) within comments. For example:

# Authors: 
#          A01166611 Pepper Pots
#          A01160611 Anthony Stark
#
# Description of problem being solved.
#
# March 15, 2017.

    .
    . (The rest of the program goes here)
    .
  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 a bed, and if they were shorter than the bed, he stretched their limbs until they would fit; if their limbs were longer, he chopped them off.

    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("Programming is fun!"))
        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
    80 114 111 103 114 97 109 109 105 110 103 32 105 115 32 102 117 110 33
    84 72 69 32 69 78 68

Deliverables

Create a ZIP file called strings.zip containing only the six programs you wrote (duplicate.py, vowelless.py, palindrome.py, username.py, procrustean.py, and encode.py).

✔ Upload Instructions

To deliver the strings.zip file, please provide the following information:

Request PIN

If this activity was developed by a team of two people, only one person is required to deliver it. No activity will be accepted through e-mail or any other means.

Due date is Wednesday, March 15.

Evaluation

This activity will be evaluated using the following criteria:

-10 One or more programs don’t contain within comments the authors’ personal information.
DA One or more programs were plagiarized.
10-100 Depending on the amount of problems that were solved correctly.