34
Learning Python-Basic course: Day 17, Summary of the week and Insertion sort.
try
catch
statements, and basic exception handling. Advanced exception handling and types of exceptions, user defined exceptions is for later.try
catch
and learnt about nesting them. We also saw one really challenging question related to try
except
here.try
except
. We covered questions like fractal lists, alphabetical order of lists and reversing lists.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.
'''
Output-
[5, 4, 3, 2, 1]
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.
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.")
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
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π
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π₯
Next day will begin from Tuesdayπ , Monday is reserved for.... MATLAB MONDAYSπ₯
34