06.
Consider the following function:
def rate(n): total = 0 i = 0 while i < n: j = 0 while j < n: total += j j += 1 i += 1 return total
07.
Modify the function so it will print out the total number of statements that are executed inside the rate function shown. Do not count any additional statements that you add to the code when you modify it. You should count the lines containing loop conditions as being executed each time the condition is checked. Note that a loop condition is checked 1 time more than the loop body is executed. Your output should be in the format:
Number of operations: <count>
def rate(n):
total = 0
i = 0
count = 3
while i < n:
count += 4
j = 0
while j < n:
count += 3
total += j
j += 1
i += 1
count += 1
print("Number of operations: {}".format(count))
return total
Consider the following function:
def rate(n): total = 0 i = 1 while i < n: j = 0 while j < n: total += j j += 1 i *= 2 return total
Modify the function so it will print out the total number of operations that are executed inside the rate()
function shown. Do not count any additional statements that you add to the code when you modify it. Your output should be in the format:
Number of operations: <count>
def rate(n):
total = 0
i = 1
count = 3
while i < n:
j = 0
count += 4
while j < n:
count += 3
total += j
j += 1
i *= 2
count += 1
print("Number of operations: {}".format(count))
return total
09.
Consider the following function:
def rate(n): total = 0 i = 0 while i < 4: j = 0 while j < i: total += j j += 1 i += 1 return total
Modify the function so it will print out the total number of statements that are executed inside the rate function shown. Do not count any additional statements that you add to the code when you modify it. You should count the lines containing loop conditions as being executed each time the condition is checked. Note that a loop condition is checked 1 time more than the loop body is executed. Your output should be in the format:
Number of operations: <count>
where <count> is an integer value.
def rate(n):
total = 0
i = 0
count = 3
while i < 4:
count += 4
j = 0
while j < i:
count += 3
total += j
j += 1
i += 1
count += 1
print("Number of operations: {}".format(count))
return total
02-03.
def bubble_row(data, index):
c=index+1
for i in range(0,index):
for j in range(i,i+2):
if data[i]>data[j]:
tem=data[i]
data[i]=data[j]
data[j]=tem
return data
def my_bubble_sort(data):
index=len(data)-1
for i in range(index):
bubble_row(data,index)
index-=1
05.
Write a function called get_position_of_highest(data, index)
which takes a list of letters and an index integer as parameters. The function returns the position of the highest element from the beginning of the list up to and including the index position. You can assume that the list is not empty.
def get_position_of_highest(data,index):
ma=0
for i in range(1,index+1):
if data[ma]<data[i]:
ma=i
return ma
06.
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the right end and the unsorted part at the left end. Initially, the sorted part is empty and the unsorted part is the entire list. The highest element is selected from the unsorted list and swapped with the rightmost element, and that element becomes a part of the sorted list. This process continues moving the unsorted list boundary by one element to the left.
Write a function called selection_row(data, index)
which takes a list of letters and an index integer as parameters. The highest element is selected from the beginning up to and including the index position. Then, the highest element is
swapped with the element at the index position. You can assume that the list is not empty. You can copy the get_position_of_highest() function from the previous question into the answer box, and call that function to obtain the position of the highest letter.
def get_position_of_highest(data,index):
ma=0
for i in range(1,index+1):
if data[ma]<data[i]:
ma=i
return ma
def selection_row(data,index):
c=index
m=get_position_of_highest(data,c)
tem=data[c]
data[c] =data[m]
data[m]=tem
c-=1
07.
Continuing on from the previous question, write a function called my_selection_sort(data)
which takes a list of letters and sorts the list using the selection sort algorithm. For example, if there are 5 elements in the list, the idea is calling the selection_row() function 4 times:
selection_row(data, 4) selection_row(data, 3) selection_row(data, 2) selection_row(data, 1)
Note: You don't have to use the selection_row() function defined previously. You can simply use nested loops to solve this problem.
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the right end and the unsorted part at the left end. Initially, the sorted part is empty and the unsorted part is the entire list. The highest element is selected from the unsorted list and swapped with the rightmost element, and that element becomes a part of the sorted list. This process continues moving unsorted list boundary by one element to the left.
def get_position_of_highest(data,index):
ma=0
for i in range(1,index+1):
if data[ma]<data[i]:
ma=i
return ma
def selection_row(data,index):
c=index
m=get_position_of_highest(data,c)
tem=data[c]
data[c] =data[m]
data[m]=tem
c-=1
def my_selection_sort(data):
c=len(data)-1
while c>0:
selection_row(data,c)
c-=19
09.
Write a function called shifting(data, index)
which takes a list of letters and an index integer as parameters. The function checks for the correct placement for the element starting from the index position and shifts all elements one place to the right until a suitable position is found for the new element. For example: consider the following letters:
data = ['h', 'w', 't', 'c', 'q', 'e'] shifting(data, 3)
The element at index 3 is "c", the correct placement for "c" in the data list is at index 0. Therefore, the function will shift all elements up to (but not including) position 3 by one place to the right and the resulting list will be:
['h', 'h', 'w', 't', 'q', 'e']
Note: This function just handles the shifting to the right. You don't need to replace the first element in this question. Also, you can assume that the list is not empty.
def shifting(data,index):
x=data[index]
lis=[]
for i in range(len(data)):
lis.append(data[i])
global ans
ans=0
for i in range(index):
if x>data[i]:
ans+=1
data.pop(index)
data.insert(ans,data[ans])
10.
Continuing on from the previous question, write a function called insertion_row(data, index)
which takes a list of letters and an index integer as parameters. The function checks for the correct placement of the element starting from the index position and shifts all elements one place to the right until the correct position is found for the new element and then replaces the element at that position.
def shifting(data,index):
global x
x=data[index]
lis=[]
for i in range(len(data)):
lis.append(data[i])
global ans
ans=0
for i in range(index):
if x>data[i]:
ans+=1
data.pop(index)
data.insert(ans,data[ans])
def insertion_row(data,index):
shifting(data,index)
data[ans]=x
11.
Continuing on from the previous question, write a function called my_insertion_sort(data)
which takes a list of letters as a parameter. The function implements the insertion sort algorithm. Note: you don't have to use the insertion_row() function defined previously. You can simply use nested loops to solve this problem.
def shifting(data,index):
global x
x=data[index]
lis=[]
for i in range(len(data)):
lis.append(data[i])
global ans
ans=0
for i in range(index):
if x>data[i]:
ans+=1
data.pop(index)
data.insert(ans,data[ans])
def insertion_row(data,index):
shifting(data,index)
data[ans]=x
def my_insertion_sort(data):
for i in range(0,len(data)):
insertion_row(data,i)
12.
Write a function named binary_search(numbers, value)
which takes a list of SORTED numbers and an integer value as parameters. The function searches the list of numbers with the given target value using a Binary Search and returns its position. If the same element is present more than once, the method returns the index of the first occurrence of the element. If the list does not contain the given value, the function should return -1.
Consider the pseudocode for a binary search:
- Let min_index = 0 and max_index = n-1.
- If max_index < min_index, then stop: target is not present in the list. Return -1.
- Compute mid_index as the average of max_index and min_index, rounded down (so that it is an integer).
- If numbers[mid_index] equals the target value, then stop. You found it! Return mid_index.
- If the mid_index was too low, that is, numbers[mid_index] < target, then set min_index = mid_index + 1.
- Otherwise, the mid_index was too high. Set max_index = mid_index - 1.
- Go back to step 2.
def binary_search(numbers,value):
max_index=len(numbers)-1
min_index=0
flag=1
while (min_index<=max_index):
if numbers[max_index]==value:
flag=0
return max_index
if numbers[min_index]==value:
flag=0
return min_index
min_index+=1
max_index-=1
return -1