3.17 - 3.18 hacks
these are the hacks for lesson 9
def collatz(i):
while i > 1:
print(i, end=' ')
if (i % 2):
# i is odd
i = 3*i + 1
else:
# i is even
i = i//2
print(1, end='')
while i != 1:
if i % 2 > 0:
i =((3 * i) + 1)
list_.append(i)
else:
i = (i / 2)
list_.append(i)
return list_
print('Please enter a number: ', end='')
while True:
try:
i = int(input())
list_ = [i]
break
except ValueError:
print('Invaid selection, try again: ', end='')
col = collatz(i)
i = int(input('Enter i: '))
print('Sequence: ', end='')
print('Number of iterations:', len(col) - 1)
collatz(i)
def bubblesort(numList):
indexingLen = len(numList) -1
sorted = False
while not sorted:
sorted = True
for i in range(0, indexingLen):
if numList[i] > numList[i+1]:
sorted = False
numList[i], numList[i+1] = numList[i+1], numList[i]
return numList
print(bubblesort([4,5,2,6,1,0,3]))
# As you can see, this code works but is very inefficient.
# this is a list/array sorting function that sorts by
# looking at the left indice and comparing to the right indice and repeating it.
# This doesn't matter with small lists like this, but if the list legnth were to be
# in the millions, then it would be a big problem.
sortList = [4,5,2,6,1,0,3]
sortList.sort()
print(sortList)
The second example is much more efficient at sorting because it saves us time and uses a better algorithm. The first one uses the bubble sort algorithm which is very slow with large datasets. The second one uses the in-built python sort() function which makes use of the Tim Sort algorithm. This function also saved more memory compared to the first one.
Algorithm efficiency: I would say that algorithm efficiency is more than just how many steps it takes for a computer to solve a problem; less steps means more efficient. Algorithm efficiency is more about measuring the amount of resources it takes for a computer to solve a problem. Things like memory usage, power usage, and time complexity
Here is a way that I automated my day. I used the schedule library from python to help me out.
import schedule as se
import time
def wakeUp():
print("Wake up for school!")
def eatBreakfast():
print("don't forget to eat breakfast")
def finHw():
print("it's time to finish your homework")
se.every().day.at("06:00").do(wakeUp)
se.every().day.at("07:00").do(eatBreakfast)
se.every().day.at("15:00").do(finHw)