The following code does not work. Do you see why? Can you suggest a way to fix it?
def length(alist): len = 1 for i in range(len(alist)): len = len + 1 return len print(length([1,2,3]))
The following code does not work. Do you see why? Can you suggest a way to...
Python Merge Sort
Adjust the following code so that you can create
random lists of numbers of lengths 10, 15,
and 20.
You will run the merge sort 10 times for each
length. Record the run time for the length and then calculate the
average time to sort.
Finally, compare the average run time for each length to the
average time for the Merge Sort.
--------------------------------------------
Initial python code:
import random
import time
def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
mid...
Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...
Using the code below, we need to implement the median-of-three method for selecting a pivot. If it is possible to add the code needed and any pseudo code as comments, that would be helpful. Thank you Code: def quickSort(alist): quickSortHelper(alist,0,len(alist)-1) def quickSortHelper(alist,first,last): if first<last: splitpoint = partition(alist,first,last) quickSortHelper(alist,first,splitpoint-1) quickSortHelper(alist,splitpoint+1,last) def partition(alist,first,last): pivotvalue = alist[first] leftmark = first+1 rightmark = last done = False while not done: while leftmark <= rightmark and \ alist[leftmark] <= pivotvalue: leftmark = leftmark + 1...
this python code below is to find the longest common subsequence from two input string !!! however it give me the wrong answer, can any one fix it thanks def lcs(s1, s2): matrix = [["" for x in range(len(s2))] for x in range(len(s1))] for i in range(len(s1)): for j in range(len(s2)): if s1[i] == s2[j]: if i == 0 or j == 0: matrix[i][j] = s1[i] else: matrix[i][j] = matrix[i-1][j-1] + s1[i] else: matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1], key=len) cs =...
Python Programming: Why do I get UnboundLocalError? What does this mean and how can I fix it? Question: Write a function check_move(column, row) which returns a string describing a chess move to a given row and column on the chess board. Your program must check if the row and column entered are both valid. The column in a chess board is a letter ranging from A to H or a to h (inclusive) and the row is a number between...
i have no lean break. so below is my way. can you fix it.
Test Input Result print (read_number_list()) 999 [] print (read_number_list)) 1 Answer (penalty regime: 0, 10, 20, 30, 40, 50%) 1 def read_number_list(): alist = [] while True: - numberinput("") if number -999: 4 return a listl else: a_list.append (int (number)) 9 1e 12 Precheck Check Test Input Expected Got print (read_number_list()) 999 [] Runtime error** Traceback (most recent call last): File "prog.python3", line 29, in <module>...
Please use Python3. Thanks Find and fix the errors (syntax or logic) in the following code snippet: # this function creates a 2D list using the height, width, and symbol. It does # not contain any shallow copies of rows def createList(height, width, symbol): board = [] for i in range(len(height)): row = [] for j in range(width): row.append(symbol) board.append(row) def main(): # creates 5 rows of 5 @s myList = createList(5,"@") print(board) main()
python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try: f = open("shopping2.txt","r") for line in f: sl.append(line.strip()) f.close() except: pass def mainScreen(): os.system('cls') # for linux 'clear' print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") print(" SHOPPING LIST ") print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") print("\n\nYour list contains",len(sl),"items.\n") print("Please choose from the following options:\n") print("(a)dd to the list") print("(d)elete from the list") print("(v)iew the...
Write a Python Code for a Function: you need to come up with code for shift_char. Write the code in a way in which you can shift the character by writing the number of shifts. Use the ASCII code for this. For example in lie 11, the input for char_to shift is r. U shift it by 1 so it becomes s. below is the function def shift_char(char_to_shift, amount_to_shift): ''' shifts any character by moving it a certain amount on...
What will be printed upon running following code: alist = [3, 50, "Kity", [50, 52, "Rover"], [ ], 3.14, False, 'a'] print(len(alist[3]))alist = [3, 50, "Kity", [50, 52, "Rover"], [ ], 3.14, False, 'a'] print(len(alist[3])) 8 7 2 an error 3 Select the best reason why the code below returns True, False, False respectively? a = 256 b = 256 print(id(a) == id(b)) c = 5.1 d = 5.1 print(id(c) == id(d)) x = 257 y = 257 print(id(x) ==...