Python Data Representations Week2

docx

School

Grand Rapids Community College *

*We aren’t endorsed by this school

Course

117

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

4

Uploaded by ConstableWildcatMaster401

Report
Congratulations! You passed! Grade received 82.14% Latest Submission Grade 82.14% To pass 70% or higher Go to next item 1. Question 1 Given the list my_list = [1, 3, 5, 7, 9] my_list = [1, 3, 5, 7, 9] , which of the following slices returns the list [3, 5, 7, 9] [3, 5, 7, 9] ? 1 / 1 point my_list[ 1 : 4] my_list[ 1 : 4] my_list[ 1 : -1] my_list[ 1 : -1] my_list[ 1 : ] my_list[ 1 : ] Correct This slice returns the list [3, 5, 7, 9] [3, 5, 7, 9] . my_list[ 2 : 4] my_list[ 2 : 4] 2. Question 2 While of the following expressions returns a tuple of length one? 0.75 / 1 point ([1]) ([1]) (1, ) (1, ) Correct This expression returns the tuple (1, ) (1, ) . ((1, )) ((1, )) ((1)) ((1)) You didn’t select all the correct answers 3. Question 3 Why does following code snippet raise an error in Python? 1 2 3 instructors = ( "Scott" , "Joe" , "John" , "Stephen" ) instructors[ 2 : 4 ] = [] print (instructors) 1 / 1 point The tuple doesn't contain an element with index 4. Slices cannot be used with tuples. John and Stephen are irreplaceable. Tuples are immutable. Correct 4. Question 4 Given a non-empty list my_list my_list , which item in the list does the operation my_list.pop() my_list.pop() remove?
1 / 1 point The item my_list[len(my_list) ] my_list[len(my_list) ] The item my_list[-1] my_list[-1] The item my_list[1] my_list[1] The item my_list[0] my_list[0] Correct The method pop() pop() removes the last item in the list. 5. Question 5 What output does the following code snippet print to the console? 1 2 3 my_list = [ 1 , 3 , 5 , 7 , 9 ] my_list.reverse() print (my_list.reverse()) Note that this question is easily answered by running this snippet in Python. Instead, carefully evaluate this code snippet mentally when you attempt this problem. 1 / 1 point Executing this code snippet raises an error. [1, 3, 5, 7, 9] [1, 3, 5, 7, 9] None None [9, 7, 5, 3, 1] [9, 7, 5, 3, 1] Correct Since reverse() reverse() is a method, it mutates my_list my_list and returns None None . 6. Question 6 Given a list fib = [0, 1] fib = [0, 1] , write a loop that appends the sum of the last two items in fib fib to the end of fib fib . What is the value of the last item in fib fib after twenty iterations of this loop? Enter the answer below as an integer. As a check, the value of the last item in fib fib after ten iterations is 8989 . 0 / 1 point 4181 Incorrect 7. Question 7 One of the first examples of an algorithm was the Sieve of Eratosthenes . This algorithm computes all prime numbers up to a specified bound. The provided code below implements all but the innermost loop for this algorithm in Python. Review the linked Wikipedia page and complete this code. 1 2 3 4 5 6
7 8 9 10 11 12 13 14 15 16 17 18 """ Implement the Sieve of Eratosthenes https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes """ def compute_primes(bound): """ Return a list of the prime numbers in range(2, bound) """ answer = list ( range ( 2 , bound)) for divisor in range ( 2 , bound): # Remove appropriate multiples of divisor from answer pass return answer print ( len (compute_primes( 200 ))) print ( len (compute_primes( 2000 ))) Running your completed code should print two numbers in the console. The first number should be 4646 . Enter the second number printed in the console as the answer below. 1 / 1 point 303 Correct There are exactly 303303 prime numbers less than 20002000 . Here is our solution: 1 2 3 4 5 6 7 8 9 10 11
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12 13 14 15 16 17 18 19 """ Implement the Sieve of Eratosthenes https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes """ def compute_primes(bound): """ Return a list of the prime numbers in range(2, bound) """ answer = list ( range ( 2 , bound)) for divisor in range ( 2 , bound): for stride in range ( 2 * divisor, bound, divisor): if stride in answer: answer.remove(stride) return answer print ( len (compute_primes( 200 ))) print ( len (compute_primes( 2000 ))) Note that our code is not particularly efficient and can be optimized in several ways.