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

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...
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 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 simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Instructions: Please write all answers in java Each problem should be completed as a single separate...
Instructions: Please write all answers in java Each problem should be completed as a single separate .java file, each with its own main(). Inputs should be read using a Scanner object and output should be printed using System.out.println. As you finish each question, submit your code to the autograder at: http://162.243.28.4/grader/homework2.html Make sure to include your name at the top as a single word. The submission utility will test your code against a different input than the sample given. When...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT