9.15. Exercises

  1. For each word in the list verbs, add an -ing ending. Overwrite the old list so that verbs has the same words with ing at the end of each one.

  2. In XYZ University, upper level math classes are numbered 300 and up. Upper level English classes are numbered 200 and up. Upper level Psychology classes are 400 and up. Create two lists, upper and lower. Assign each course in classes to the correct list, upper or lower. HINT: remember, you can convert some strings to different types!

  3. Starting with the list myList = [76, 92.3, ‘hello’, True, 4, 76], write Python statements to do the following:

    1. Append “apple” and 76 to the list.

    2. Insert the value “cat” at position 3.

    3. Insert the value 99 at the start of the list.

    4. Find the index of “hello”.

    5. Count the number of 76s in the list.

    6. Remove the first occurrence of 76 from the list.

    7. Remove True from the list using pop and index.

    Show Comments
  4. The module keyword determines if a string is a keyword. e.g. keyword.iskeyword(s) where s is a string will return either True or False, depending on whether or not the string is a Python keyword. Import the keyword module and test to see whether each of the words in list test are keywords. Save the respective answers in a list, keyword_test.

  5. The string module provides sequences of various types of Python characters. It has an attribute called digits that produces the string ‘0123456789’. Import the module and assign this string to the variable nums. Below, we have provided a list of characters called chars. Using nums and chars, produce a list called is_num that consists of tuples. The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.

9.15.1. Contributed Exercises

The transpose of a matrix is flipped along its diagonal. E.g., an input matrix that looks like

\[\begin{split}\left[\begin{array}{ccc} 0 & 1 & 2\\ 3 & 4 & 5 \end{array}\right]\end{split}\]

and will have a list representation of

[[0, 1, 2], [3, 4, 5]]

Its transpose will be

\[\begin{split}\left[\begin{array}{ccc} 0 & 3\\ 1 & 4\\ 2 & 5 \end{array}\right]\end{split}\]

and will have a list representation of

[[0, 3], [1, 4], [2, 5]]

Compute the transpose of mat_a and mat_b below and store them in mat_at and mat_bt respectively.

Create a new list called sqrts that contains the square root of each number in the list numbers. Your solution should work for any number of elements in the list.

First, create an empty list, then add each value using append().

Pig latin is a language game or argot. The rules are

  1. All consonants at the start of the string are moved to the end. (Treat ‘y’ is a vowel here.)

  2. An ‘a’ is added to the end of the string.

For example, ‘string’ becomes ‘ingstra’.

Copy the list english into a new list called pig_latin and translate each word into pig latin. You may use + to create new strings but do not use append or + to build the list pig_latin.

Create an alias named cities that points to the same list as the existing municipalities variable. Then, create a list named towns that is a clone of cities. towns and cities should be independent objects and not aliases.

Once you have created the lists, change “Blacksville” in municipalities to “Town of Blacksville”. Print out the content of element 4 in each list.

Consider the code given to you below. It iterates through a list of 5 integers, and appends them to a list. Run the code first and see the result. Now, by only modifying line 3 change the code to instead of just adding an element to the list, it adds each number multiplied by two.

    Q-1: What will be the value of a after the following code has executed?

    a = ["holiday", "celebrate!"]
    b = a
    b.append("company")
    
  • ["holiday", "celebrate", "company"]
  • This is true. It is called aliasing (Section 9.5).
  • Nothing. There's an error.
  • There is no error in this code. Please check Section 9.5 (Aliasing) to understand why not.
  • ["holiday", "celebrate"]
  • That's incorrect. What we used here is something called aliasing. Both a and b variables refer to the same list. So either you append to a or b, you append to the same list.

You are supplied with a string s below. Using python and not hard coding…Remove white space from the beginning and end, make the string lower case and replace all the words ‘cat’ with ‘dog’. This new string should be saved as newS. The final string should look like newS=’the dog ate the dog food, it was yummy!’

Write a program that will accept words from a user, one at a time, until the user types the word ‘quit’. Start with an empty list, and append each new word to that list. After the user types quit, print out the list of words.

First copy your code from the previous problem. This problem will again ask the user for a bunch of words and print out the words. Then it will sort the words using the bubble sort technique we learned in class (no other sort methods allowed!) Print out the sorted list.

    Q-1: Program berikut ini akan menghasilkan output:

    a = [0]
    b = a
    a[0] = 2
    print(a, b)
    
  • [0] [0]
  • nilai a[0] sudah diubah
  • [0] [2]
  • nilai a[0] sudah diubah
  • [2] [0]
  • a dan b sama lho
  • [2] [2]
  • tepat, karena a dan b sama mengubah isi dari a juga mengubah b

Test

Write a list myfriends with e friend names. Clone myfriends to mypeople. Add 2 family names to mypeople and print both lists.

Write a list named myfriends 3 friend names. Clone myfriends to a new list called mypeople. Add 2 new names of family members to mypeople. Print both myfriends and mypeople.

Write one line of code to combine the list below into a word and then print it.

Consider the list below. Using the del Python statement, delete the “Yoda” element from the list. Do not create a new list. You’re supposed to directly modify the star wars list.

Consider the list below. Do the following task:

  1. Create a new list: star_wars_copy that is a copy of star_wars (use Cloning, Section 9.6).

  2. Add a new value “Darth Vader” to star_wars_copy after the “Han Solo” entry (see Section 9.2). Leave the original list star_wars intact.

Consider the list below called words. Using the accumulator pattern on the lists (Section 9.10), create a new list called wordings that adds the ending -ing to each of the words in the “words” list. So your resulting wordings list should be: [“reading”, “working”, “walking”, “watching”, “drinking”, “surfing”]

For each word in the list verbs, add an -ing ending. Overwrite the old list so that verbs has the same words with ing at the end of each one.

Pig Latin is a language game or argot. Each word in English can be transformed into a corresponding word in Pig Latin. The rules are:

  1. All consonants at the start of the word are moved to the end. (Treat ‘y’ as a vowel here - so the vowels are a, e, i, o, u, and y.)

  2. An ‘a’ is then added to the end of the word.

For example, ‘string’ becomes ‘ingstra’. Another example, ‘apple’ becomes ‘applea’.

Copy the list english into a new list called pig_latin and write code which translates each word into Pig Latin. Do not hardcode the results. You may assume the original list contains lower-case words.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, write a single Python program that

  1. Stores the number of times the letter ‘e’ (lowercase) appears in the string my_str into a variable named e_count.

  2. Stores the numeric average of the positive numbers appearing the the list my_list into a variable named pos_average.

  3. Prints out the value in variable e_count on a single line by itself.

  4. Prints out the value in variable pos_average on a single line by itself.

  5. Assigns to a variable named name_str your first name (e.g. “Jane”).

  6. Prints out the value in variable name_str in reverse order, one line per letter.

Solve these pieces in the order listed above. Do NOT hard code any of your answers. This is not an auto-graded question, so you will need to determine whether your responses are correct. You do not need to write any functions to solve this problem unless you’d like to do so. This question is worth 6 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, write a single Python program that

  1. Stores the number of times the letter ‘t’ (lowercase) appears in the string my_str into a variable named t_count.

  2. Stores the numeric average of the positive numbers appearing the the list my_list into a variable named pos_average.

  3. Prints out the value in variable t_count on a single line by itself.

  4. Prints out the value in variable pos_average on a single line by itself.

  5. Assigns to a variable named name_str your last name (e.g. “Richards”).

  6. Prints out the value in variable name_str in reverse order, one line per letter.

Solve these pieces in the order listed above. Do NOT hard code any of your answers. This is not an auto-graded question, so you will need to determine whether your responses are correct. You do not need to write any functions to solve this problem unless you’d like to do so. This question is worth 6 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, write a single Python program that

  1. Stores the number of times the letter ‘p’ (lowercase) appears in the string my_str into a variable named p_count.

  2. Stores the numeric average of the negative numbers appearing the the list my_list into a variable named neg_average.

  3. Prints out the value in variable p_count on a single line by itself.

  4. Prints out the value in variable neg_average on a single line by itself.

  5. Assigns to a variable named name_str your first name (e.g. “Jane”).

  6. Prints out the value in variable name_str in reverse order, one line per letter.

Solve these pieces in the order listed above. Do NOT hard code any of your answers. This is not an auto-graded question, so you will need to determine whether your responses are correct. You do not need to write any functions to solve this problem unless you’d like to do so. This question is worth 6 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, write a single Python program that

  1. Stores the number of times the letter ‘p’ (lowercase) appears in the string my_str into a variable named p_count.

  2. Stores the numeric average of the negative numbers appearing the the list my_list into a variable named neg_average.

  3. Prints out the value in variable p_count on a single line by itself.

  4. Prints out the value in variable neg_average on a single line by itself.

  5. Assigns to a variable named name_str your first name (e.g. “Jane”).

  6. Prints out the value in variable name_str in reverse order, one line per letter.

Solve these pieces in the order listed above. Do NOT hard code any of your answers. This is not an auto-graded question, so you will need to determine whether your responses are correct. You do not need to write any functions to solve this problem unless you’d like to do so. This question is worth 6 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, write a single Python program that

  1. Stores the number of times the letter ‘i’ (lowercase) appears in the string my_str into a variable named i_count.

  2. Stores the numeric average of the positive numbers appearing the the list my_list into a variable named pos_average.

  3. Prints out the value in variable i_count on a single line by itself.

  4. Prints out the value in variable pos_average on a single line by itself.

  5. Assigns to a variable named name_str your last name (e.g. “Richardson”).

  6. Prints out the value in variable name_str in reverse order, one line per letter.

Solve these pieces in the order listed above. Do NOT hard code any of your answers. This is not an auto-graded question, so you will need to determine whether your responses are correct. You do not need to write any functions to solve this problem unless you’d like to do so. This question is worth 6 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, define a Python function named magic_function that takes as a single parameter a list of integers named starting_list.

Your function should produce and return a new list containing only the items from starting_list that are even integers that are greater than 20. Your function should not contain any print statements. Do not hard code this– your function should work given any list.

This is not an auto-graded question, so you will need to determine whether your responses are correct. This question is worth 5 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, define a Python function named magic_function that takes as a single parameter a list of integers named starting_list.

Your function should produce and return a new list containing only the items from starting_list that are odd integers that are greater than 20. Your function should not contain any print statements. Do not hard code this– your function should work given any list.

This is not an auto-graded question, so you will need to determine whether your responses are correct. This question is worth 5 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, define a Python function named magic_function that takes as a single parameter a list of integers named starting_list.

Your function should produce and return a new list containing only the items from starting_list that are odd integers that are less than 40. Your function should not contain any print statements. Do not hard code this– your function should work given any list.

This is not an auto-graded question, so you will need to determine whether your responses are correct. This question is worth 5 points.

Click the “Show Code” button if the code window does not appear.

In the ActiveCode window below, define a Python function named magic_function that takes as a single parameter a list of integers named starting_list.

Your function should produce and return a new list containing only the items from starting_list that are even integers that are less than 60. Your function should not contain any print statements. Do not hard code this– your function should work given any list.

This is not an auto-graded question, so you will need to determine whether your responses are correct. This question is worth 5 points.

Loading a dynamic question ...
Selecting from: hu_cps201fa21_midterm_essay_3a, hu_cps201fa21_midterm_essay_3b, hu_cps201fa21_midterm_essay_3c, hu_cps201fa21_midterm_essay_3d

Loading a dynamic question ...
Selecting from: hu_cps201fa21_midterm_essay_4a, hu_cps201fa21_midterm_essay_4b, hu_cps201fa21_midterm_essay_4c, hu_cps201fa21_midterm_essay_4d

Consider the list below called words. Using the accumulator pattern on the lists (Section 9.10), create a new list called wordings that adds the ending -ing to each of the words in the “words” list. So your resulting wordings list should be: [“reading”, “working”, “walking”, “watching”, “drinking”, “surfing”]

Write a program that

  1. sorts the given list lst into increasing order stored as lst,

  2. sorts the given list into reverse order stored as rev_lst,

  3. adds the value of number1 to lst at the position with index 7 (increasing the number of lst elements by one),

  4. adds the value of number2 to the end of rev_lst (increasing the number of rev_lst elements by one), and finally

  5. creates the list sum_lst which contains the sums of the corresponding values in the 2 lists lst and rev_lst.

To be eligible to earn full credit:

  1. Do not hard code your answer (i.e., your code should work even if lst, number1, and number2 are modified.)

  2. Use meaningful identifiers for your variables.

  3. Include appropriate comments in your code describing what blocks of your code are doing.

  4. Pass all unittests.

Note: This problem is extra credit.

A list of 2021’s most expensive US Colleges in the form "rank; college; tuition" is provided. Use the format method to print the rank and tuition of each college on a separate line so that the output is exactly the following:

1. In  2021, the tuition and fees were $63,500 at Columbia University.
2. In  2021, the tuition and fees were $63,401 at Franklin & Marshall College.
3. In  2021, the tuition and fees were $62,070 at Kenyon College.
4. In  2021, the tuition and fees were $63,000 at Tufts University.
5. In  2021, the tuition and fees were $62,870 at Vassar College.
6. In  2021, the tuition and fees were $62,404 at Brown University.
7. In  2021, the tuition and fees were $62,070 at Colorado College.
8. In  2021, the tuition and fees were $61,966 at Colgate University.
9. In  2021, the tuition and fees were $61,749 at Wesleyan University.
10. In  2021, the tuition and fees were $61,710 at University of Pennsylvania.

You are supplied with a string original below. Create a new string by

  1. removing white space from the beginning and end,

  2. replacing all the words in original that appear in pets with ‘kittens’, and

  3. make the string upper case.

This new string should be saved as result.

With the given string, the result should be ‘SHE IS FOND OF KITTENS AND PARAKEETS AND ENJOYS YOGA WITH KITTENS, RIDING KITTENS, AND TRAINING KITTENS.’

Do not hardcode!

Write a program that

  1. sorts the elements in the list lst from largest to smallest, then

  2. assigns the number of occurrences of 9 in lst to a variable called x, then

  3. removes the element of lst at index 14 and assigns that element to a variable called y, and then

  4. adds the sum of x and y to the end of lst.

Do not hardcode!

You are supplied with a string old_string below. Create a new string by

  1. removing white space from the beginning and end,

  2. making the entire string upper case, and then

  3. replacing all the strings that appear in numbers with the string ‘5’.

This new string should be saved as new_string.

With the given old string, the new string should be ‘5 TIMES 5 IS 25, AND 10 MINUS 5 IS 5.’

Do not hardcode!

You have attempted of activities on this page