InsertSortTDD
TDD Approach to grokking InsertSort
0 Create a sorted list containing numbers from 0 to 6.
Draw a representation diagram of the list, indicating values and indexes corresponding to the values
Take the 3rd element and the last element and multiply them. Write code that does this and prints the actual value of the multiplication.
Write a python program to create a list containing numbers 0 through 6. The first 3 elements must be in sorted in ascending order. The last 3 elements must be unsorted.
example:
[4, 5, 6, 0, 2, 1, 3]
or[4, 5, 6, 1, 3, 2, 0]
1 Insert the element 3.5
into the sorted list at index 4
element 3.5
into the sorted list at index 4Draw a representation diagram of the list, indicating values and indexes corresponding to the values
What is the python code which will achieve this? Print the list before the insertion and print the list after the insertion
Try doing the above step using
enumerate
to display the index and value pairs and validate your diagram with it
2 Place the last element 3.5
in an almost
sorted list into the list at index 4
3.5
in an almost
sorted list into the list at index 4Build a sorted list containing numbers 0 to 6. Append 3.5 into the list at the very end.
Draw a representation diagram for the above list
Write Python code to remove
3.5
from its current location and then insert it at index 4. Print the list before and after insertion.
3 Place the last element 2.5
in an almost
sorted list into the list at the appropriate index so it fully sorted
2.5
in an almost
sorted list into the list at the appropriate index so it fully sortedBuild a sorted list containing 0 to 6. Append
2.5
into the list at the very end.In your notebook, write pseudo-code for determining how to find the index at which it needs to be inserted.
Convert the pseudo code into Python code.
Print the list before and after your insertion has happened
4 Place the 4th element in the list into the first part of the list [0:3]
at the appropriate location
[0:3]
at the appropriate locationBuild a list in this order
[4, 5, 6, 0, 3, 1, 2]
- the 4th element being0
. Represent this using a diagram in your notebookInitialize
idx = 4
and iterate through the first part of the list (which is already sorted) and insert it into the array at the appropriate location. Write the code for this.
5 Place the last four elements in the list so that the list is fully sorted
Build a list in this order
[4, 5, 6, 0, 3, 1, 2]
Iterate through the last four elements and perform the same operations you did in the previous exercise
Validate through proper print statements that your list is actually properly sorted
6 Place your code inside a function insertsort
and write test cases to test the functionality
insertsort
and write test cases to test the functionality7 Replace the list method calls and instead use slicing
to achieve the same sorting operation
slicing
to achieve the same sorting operationNotes
Bring your code and notes to the FDP session in a notebook of your own.
Use appropriate names for the variables so the code is self-explanatory. Additional comments may also be included to further explain the code
Share with me your code through
repl.it
even prior to coming to classIn the same file, clearly mark the code corresponding to the various steps that I have detailed above
Alternatively, you may use CyberDojo to document your progress and write test cases for validation
Pre-requisites
Familiarity with List methods and operations
pop()
insert()
slicing
Verbose Mode Output
Activities
Use a set of playing cards and demonstrate how they actually know insert_sort already 2. Ask for a video contribution for mergesort since there is good one available on the web. Ask them to be creative and make it engaging video, unlike mine below! 3. Sample is available at http://j.mp/compareSort
Use repl.it or pythonanywhere (even better since it comes with Unix shell prompt as well) - form a list containing some random numbers of their choice. 3. Use alist.pop() and alist.insert alternatively and manually ask them to sort their own list using the insertsort technique 4. There is a piece of code that they have to write to reflect what they are doing in their head 5. SPOILER ALERT! - http://bit.ly/manInsert
Write code on CloudCoder http://j.mp/insertionSortCC
Last updated