Question

In: Computer Science

Question 6 Which of the following for loops will find the largest element in the array...

Question 6

Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values?

Question 6 options:

largest = None
for i in range(len(numbers)):
if largest is None and numbers[i] > largest:
largest = numbers[i]

largest = None
for i in range(numbers):
if largest is None and numbers[i] > largest:
largest = numbers[i]

largest = None
for i in range(len(numbers)):
if largest is None or numbers[i] > largest:
largest = numbers[i]

largest = None
for i in range(numbers):
if largest is None or numbers[i] > largest:
largest = numbers[i]

Question 7

Which of the following is the correct output from this sequence of statements?

array1 = [1, 2, 3]
array2 = [2, 3]
print((array1 + array2) * 2)

Question 7 options:

[1, 2, 3, 2, 3, 1, 2, 3, 2, 3]

[1, 2, 3, 2, 3]

[1, 2, 3, 2, 3, 2, 3]

[1, 2, 3]

Question 8

Which of the following is the correct output from this sequence of statements?

primes = [2, 3, 5, 7, 11, 13]
primes[3:] = [1, 2, 3]
print(primes[:4])

Question 8 options:

[2, 3, 5, 7]

[2, 3, 5]

[3, 5, 1]

[2, 3, 5, 1]

Question 9

Which of the following is the correct output from the following sequence of statements?

sequence = [1, 3, 5, 7, 9]
for i in range(len(sequence)):
sequence[i] = i + sequence[i]
print(sequence)

Question 9 options:

[2, 7, 14, 23, 34]

[1, 4, 7, 10, 13]

[2, 6, 10, 14, 18]

[1, 4, 9, 16, 25]

Question 10

Which of the following is the correct output from this sequence of statements?

numbers = [18, 27, 42, 13, 21, 8, 11]
print(max(numbers))

Question 10 options:

42

140

7

8

Solutions

Expert Solution

Question 6:

largest = None
for i in range(len(numbers)):
if largest is None or numbers[i] > largest:
largest = numbers[i]

The for loop will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values.

In every iteration it checks for largest and assign a list number if the condition is true.

Sample Code:

Sample run:

Code to Copy (Refer above images of code for indentation):
#example list
numbers=[1,2,10,2,4,15,8,5]
largest = None
#for loop iterate through list
for i in range(len(numbers)):
#compare and assign largest
if largest is None or numbers[i] > largest:
largest = numbers[i]
#print largest
print(largest)

Question 7:

[1, 2, 3, 2, 3, 1, 2, 3, 2, 3]

The above list is generated with the given code.

array1 = [1, 2, 3]
array2 = [2, 3]
print((array1 + array2) * 2)

(array1+array2)=[1,2,3,2,3]*2=[1,2,3,2,3,1,2,3,2,3]

So the above option is correct.

Sample run:

Question 8:

[2, 3, 5, 1]

The above list is printed with the given code. Because in the code slicing is used.

#list initialization
primes = [2, 3, 5, 7, 11, 13]
#modify above list from 3 rd index element
primes[3:] = [1, 2, 3]
#print list upto index 3
print(primes[:4])

initial list -[2,3,5,7,11,13]

primes[3:]=[1,2,3] so primes list modified as [2,3,5,1,2,3]

print(primes[:4]) prints upto index 3.

so it prints 2,3,5,1.

So the above list is printed.

Sample run:

Question 9:

[1, 4, 7, 10, 13]

The above list is printed by given code

#list initialization

sequence = [1, 3, 5, 7, 9]
for i in range(len(sequence)):

#using loop add index to value
sequence[i] = i + sequence[i]
print(sequence)

In the code, the index value is added to respective value.

1 index 0 =1+0 =1

3 index 1= 3+1=4

5 index 2=5+2=7

7 index 3=7+3=10

9 index 4=9+4=13

The final list printed is [1,4,7,10,13]

Sample run:

Question 10:

42

42 is printed because max() function return maximum value.

So in the given list 42 is highest.

numbers = [18, 27, 42, 13, 21, 8, 11] -42 maximum

Sample run:


Related Solutions

Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that is * less than or equal to the limit parameter * if all values are greater than theVal, return -1; * * Precondition: the array is nonempty and all elements are unique. * Your solution must go through the array exactly once. * * <pre> * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0 * 5 == posOfLargestElementLtOeT(3,...
Consider the following algorithm to find the kth largest elementof a given array A of...
Consider the following algorithm to find the kth largest element of a given array A of n numbers. We pick every fifth element of A and place them in the array B. We find the median of B recursively, and use this median of B as a pivot to partition the array A. Depending on k and the number of elements that are smaller than the chosen pivot, we recurse into an appropriate subproblem of A.Answer the following questions.Write the...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Given an array, return the element at which the "pattern" breaks. For example, in the array...
Given an array, return the element at which the "pattern" breaks. For example, in the array [2,4,6,8,11,13,15,17], the algorithm should return 11 because the difference between every previous node was 2, and the difference between 8 and 11 is 3. PLEASE USE DIVIDE AND CONQUER. Thank you. Python please or just a description of the algorithm
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT