# EAE 2420 - HW1 testList1 = [1, 3, 2, 4] testList2 = [9, 8, 7, 6, 5, 4, 3, 2, 1] testList3 = [5, 4, 9, 2, 5, 1] # 1.1 - Swap 2 numbers in a list def numSwap(theList, index1, index2): temp = theList[index1] theList[index1] = theList[index2] theList[index2] = temp numSwap(testList1, 1, 2) print(testList1) # 1.2 - Find smallest number in a list def smallestNum(theList): smallest = theList[0] for num in theList: if num < smallest: smallest = num return smallest print(smallestNum(testList1)) # 1.3 - Sum all values in a list def sumList(theList): theSum = 0 for num in theList: theSum += num return theSum print(sumList(testList1)) # 2.1 - Selection Sort def selectionSort(theList): length = len(theList) return theList selectionSort(testList1) print("Selection Sort - Test 1: ", testList1) selectionSort(testList2) print("Selection Sort - Test 2: ", testList2) selectionSort(testList3) print("Selection Sort - Test 3: ", testList3) # 2.2 - Insertion Sort def insertionSort(theList): length = len(theList): return theList insertionSort(testList1) print("Insertion Sort - Test 1: ", testList1) insertionSort(testList2) print("Insertion Sort - Test 2: ", testList2) insertionSort(testList3) print("Insertion Sort - Test 3: ", testList3) # 3.1 - Use assert() to prove Selection & Inserstion Sorts work # 3.2 - Invariants of both sorts '''After each iteration of the outer for loop, the sorted part of the list is sorted''' # 4.1 - Dispaly an Integer value in its Binary form # 4.2 - Add 2 lists in Binary form (each list holding ints that are either 1 or 0) # 5.1 - Add 2 numbers in Binary form without using + operator # 5.2 - Multiply 2 numbers without using * operator # 5.3 - How many bits are necessary to store a positive value x? '''asdf'''
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question