Question

In: Computer Science

Write all your answers for this problem in a text file named aa.txt – for each...

Write all your answers for this problem in a text file named aa.txt – for each problem write the problem number and your answer.

1.1 What type of object can be contained in a list (write the letter for the best answer)?

a. String b. Integer c. List d. String and Integer only e. String, Integer, and List can all be contained in a list

1.2 Which statement about the debugger is not correct? a. It is a powerful tool in PyCharm and it can help trace the execution of a program. b. You can set breakpoints in the code to indicate where the computer should pause execution. c. The debugger can step through the program execution, but you cannot see the value of a variable without using a print statement. d. The PyCharm debugger highlights the line it will execute next.

1.3 Binary search takes a smaller number of comparisons to run compared to linear search. However, binary search cannot always be used. What is required for binary search to work?

1.4 Write TRUE or FALSE regarding the following statement: “The Merge Sort algorithm we studied in Chapter 5 was implemented with recursion”

1.5 Suppose a list is defined with the following assignment statement: numbers = [1, 2, 3, 4, 5, 6, 7] If we use the binary search algorithm we studied, how many comparisons (i.e. iterations of the while loop) will it take to find the number 5?

1.6 Suppose a list is defined with the following assignment statement: numbers = [33, 22, 55, 11, 44] Explain in 2-4 sentences in your own words how the Merge Sort (msort) function will sort the above list. Be sure to explain how it will merge and sort the groups for each step.

about python

Solutions

Expert Solution

1.1. e. String, Integer, and List can all be contained in a list.

1.2. d. The PyCharm debugger highlights the line it will execute next.

1.3. The list must be sorted in ascending order according to the ordering used by the comparisons in the search function.

1.4. please mention the detail algorithm.

1.5. Two comparisons required.

comaprison 1: 5> middle point 3 so serach in right of the midle point.

comparison 2: middle point is 5 so element found.

1.6. Merge sort in python code has been given below.

def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
  
# create temp arrays
L = [0] * (n1)
R = [0] * (n2)
  
# Copy data to temp arrays L[] and R[]
for i in range(0 , n1):
L[i] = arr[l + i]
  
for j in range(0 , n2):
R[j] = arr[m + 1 + j]
  
# Merge the temp arrays back into arr[l..r]
i = 0 # Initial index of first subarray
j = 0 # Initial index of second subarray
k = l # Initial index of merged subarray
  
while i < n1 and j < n2 :
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
  
# Copy the remaining elements of L[], if there
# are any
while i < n1:
arr[k] = L[i]
i += 1
k += 1
  
# Copy the remaining elements of R[], if there
# are any
while j < n2:
arr[k] = R[j]
j += 1
k += 1
  
# l is for left index and r is right index of the
# sub-array of arr to be sorted
def mergeSort(arr,l,r):
if l < r:
  
# Same as (l+r)//2, but avoids overflow for
# large l and h
m = (l+(r-1))//2
  
# Sort first and second halves
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
  
  
# Driver code to test above
arr = [33,22,55,11,44]
n = len(arr)
print ("Given array is")
for i in range(n):
print ("%d" %arr[i]),
  
mergeSort(arr,0,n-1)
print ("\n\nSorted array is")
for i in range(n):
print ("%d" %arr[i]),

Detailed steps with picture hase been given below:


Related Solutions

For the following, copy the questions into a text file and write your answers for each...
For the following, copy the questions into a text file and write your answers for each question. I. Place the following algorithm time complexities in order from fastest (least number of comparisons) to the slowest: nlogn, n, n2, 2n, logn, 2n II. In your own words, explain the two characteristics that a recursive solution must have. III. Why are divide-and-conquer algorithms often very efficient in terms of time complexity? IV. Write in your own words 3 different examples of cases...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
Create a file named work.sh in your hw5 directory. Give this file read and write permission...
Create a file named work.sh in your hw5 directory. Give this file read and write permission (no execute permissions) for you alone. No other account should have any access privileges to this file. Change the permissions on the work.sh file so you have read and write permissions. Give everybody else, including the group, read permissions only. Give yourself read, write and execute permissions to the file work.sh. Give everyone else, including the group, execute permissions only. Create a directory named...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT