In 18 withcommas joinbooktitlewords withoutcommas

This preview shows page 3 - 7 out of 17 pages.

In [18]: with_commas = ", " . join(book_title_words) without_commas = " " . join(book_title_words) # These lines are provided just to print out your answers. print ( ' with_commas: ' , with_commas) print ( ' without_commas: ' , without_commas) with_commas: Eats, Shoots, and Leaves without_commas: Eats Shoots and Leaves In [19]: _ = ok . grade( ' q1_3 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed
1.2 2. Indexing Arrays These exercises give you practice accessing individual elements of arrays. In Python (and in many programming languages), elements are accessed by index , so the first element is the element at index 0. Question 1. The cell below creates an array of some numbers. Set third_element to the third element of some_numbers .
3
In [22]: _ = ok . grade( ' q2_1 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 2. The next cell creates a table that displays some information about the elements of some_numbers and their order. Run the cell to see the partially-completed table, then fill in the missing information in the cell (the strings that are currently "???") to complete the table. In [24]: elements_of_some_numbers = Table() . with_columns( "English name for position" , make_array( "first" , "second" , "third" , "fourth" , "fif "Index" , make_array( "0" , "1" , "2" , "3" , "4" ), "Element" , some_numbers) elements_of_some_numbers Out[24]: English name for position | Index | Element first | 0 | -1 second | 1 | -3 third | 2 | -6 fourth | 3 | -10 fifth | 4 | -15 In [25]: _ = ok . grade( ' q2_2 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 3. You’ll sometimes want to find the last element of an array. Suppose an array has 142 elements. What is the index of its last element?
4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed More often, you don’t know the number of elements in an array, its length . (For example, it might be a large dataset you found on the Internet.) The function len takes a single argument, an array, and returns the len gth of that array (an integer). Question 4. The cell below loads an array called president_birth_years . The last ele- ment in that array is the most recent birth year of any deceased president. Assign that year to most_recent_birth_year . In [42]: president_birth_years = Table . read_table( "president_births.csv" ) . column( ' Birth Year ' ) most_recent_birth_year = max (president_birth_years) most_recent_birth_year Out[42]: 1917 In [43]: _ = ok . grade( ' q2_4 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 5. Finally, assign sum_of_birth_years to the sum of the first, tenth, and last birth year in president_birth_years In [47]: sum_of_birth_years = president_birth_years . item( 0 ) + president_birth_years . item( 9 ) + p sum_of_birth_years Out[47]: 5433 In [48]: _ = ok . grade( ' q2_5 ' ) 5
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed 1.3 3. Basic Array Arithmetic Question 1. Multiply the numbers 42, 4224, 42422424, and -250 by 157. For this question, don’t use arrays.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture