DEV Community

Cover image for Learning Python-Basic course: Day 17, Summary of the week and Insertion sort.
Aatmaj
Aatmaj

Posted on

Learning Python-Basic course: Day 17, Summary of the week and Insertion sort.

🤟Today we will learn about the insertion sort code, then write a program to change capital and small cases.


Summary of the week

  • Day 14- we covered the try catch statements, and basic exception handling. Advanced exception handling and types of exceptions, user defined exceptions is for later.
  • Day 15- We solved some creative questions based on try catch and learnt about nesting them. We also saw one really challenging question related to try except here.
  • Day 16 We solved more questions on lists now with the added power of try except. We covered questions like fractal lists, alphabetical order of lists and reversing lists.

Insertion sort.

Insertion sort is a simple sorting algorithm. It works similar to the way you sort cards. If you are new to this algorithm, check out this link.

a=[1,4,3,5,2]
for i in range(1, len(a)):
    #key = a[i]
    for j in range(0,i):
        if(a[j]<a[i]):
            (a[j],a[i])=(a[i],a[j])#swap
print(a)
'''
Logic-
Set the key equal to the first unsorted value.
Compare the key and the sorted elements.
Move the key to the required position.
'''
Enter fullscreen mode Exit fullscreen mode

Output-

[5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

The code enclosed in block comments ''' is block comments. Whenever working in teams, such documentation is of critical. More about it here.

For those who are new to Data structures and algorithms, please check out this course on dev.to.-Data structure & algorithms Series' Articles

Exercise

We did the alphabetical ordering yesterday(In case you missed it- here) Replace the sorted() method used in it with insertion sort. Answer here


Sample question
1) Capital to Small Write a program to turn capital letters into small case and small case to capital case in a list.

a=[]
for i in range(0,4):
    a.append(ord(input("Please enter a letter ")))
for i in range (0,len(a)):
 if(65<=a[i]<=65+26): #65=A
    print(chr(a[i]-65+97))
    #65=A, 97=a 
 elif(97<=a[i]<=97+26): #65=A
    print(chr(a[i]+65-97))
 else:
    print("Error. Please enter only characters.")
Enter fullscreen mode Exit fullscreen mode

Output-

Please enter a letter a
Please enter a letter B
Please enter a letter c
Please enter a letter D
A
b
C
d
Enter fullscreen mode Exit fullscreen mode

This is an example of error handling using if-else statements.



✌️So friends that's all for now. 😊 Hope you all are having fun.😎 Please let me know in the comment section below 👇. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me. 😉
Thank you all👍


One more way to ask any doubts is by forking the repo here and sending Pull request of your doubt.


For those who have not yet made account in Dev.to, you can have a free easy sign-up using your mail or GitHub accounts. I would suggest the budding developers to create your GitHub free account right away. You would require to register sooner or later anyways

🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
Next day will begin from Tuesday📅, Monday is reserved for.... MATLAB MONDAYS💥

Follow me for updates on Github. Visit the learning Python repo made for this course.

Top comments (0)