Regular classes resumed this week, and for this I was thankful. I missed Prof. Heap's excellent lectures. Besides teaching the theory, I feel like I learn more good coding techniques from watching him code examples than from reading. When I actually go to write code for the course, a large amount of the conventions that I use that I think are efficient, interesting, or clever at all, are copying things that I've seen Prof. Heap do (list comprehensions, ternary if, tuple packing, and generator comprehensions are all good examples). However, it felt somewhat hollow without the exercises to accompany it. This may not make me popular with my peers, or with myself on in a lackadaisical mood, but I think there should be more exercises. If there's time in a week to do a lab, surely there's time to do an exercise. They're great for learning, I like doing them, and they're one of the only productive things I can do on the bus. In their absence I've taken to books, audiobooks, and radio shows, and good heavens are Canadian radio shows boring. I listened to an entire half hour segment getting various opinions (including that of the head of the American society) on doorknobs. Yeah.
MapReduce was cool, as was property. I can see that MapReduce has a lot of power, and I love that it is recursive in nature. It inspires me to know that real world software companies are using the recursive techniques that 148 drills in to us repeatedly.
The lab was not my favourite. It didn't involve solving any problem, or building any program from the ground up. However, it did deepen my understanding of the various sorts, and I'm confident that I know how they work. Also, I did this lab on my laptop in PyCharm instead of Wing. PyCharm has a lot more static analysis, which helps me catch errors before running my code. I also like the autocomplete feature, because I'm super bad at typing under pressure. Especially when someone is watching me, like my partner or the TA. I swear I get ten times worse. And the shift key on the keyboard in the lab is not sensitive enough. So many of my brackets come out as 9s and 0s, it's ridonk.
I made my own sorting algorithm this week! Here's the code:
def rand_sort(L):
'''
Shuffle the list L until it is sorted.
'''
J = L[:]
i = -1
while True:
i = i + 1
if i == len(J) - 1:
return J
if J[i] > J[i+1]:
shuffle(J)
i = -1
Basically, it checks if the list is sorted, and if it's not, it shuffles the list, and repeats the process. Occasionally, it's very efficient. One time, it sorted an 11 item list in 60 seconds. Another time, it took 45 minutes. For very small inputs (3, 4, 5, 6 elements), it's pretty much just as good as the other sorting algorithms (at least BubbleSort), because there aren't very many permutations. But that decreases rapidly as input sizze increases. For a 5 element list, there's 120 elements. For an 11 element list, there's around 40 million. I put a print(J) command in one version, so I could watch the output as it goes. It's kind of funny, it's so obvious to a human observer when some parts are right, especially when the first element is right. I could perhaps make a more efficient version that only shuffles the portion of the list that isn't sorted. 'More efficient' is of course relative, rand_sort will never be as good as TimSort. Except sometimes it is. Randomly.
And no random number generator is truly random, so it could fall into a pattern of trying the same orders over and over again, without reaching a sort. In fact this is probably a good way to see how random.shuffle works, by reading the printed output.
The bloody print command! I learned on Python 2.7, and there, the print command worked in the following fashion (which is far more intuitive, I believe):
print 'hello world'
Even easier, one could simply:
print foo(bar)
might have worked, allowing Python credit for some fairly clever automatic interpretation of words that are not assigned as variables. Now, one must wrap round brackets around whatever it is that is to be printed, adding additional layers to what is already likely a phalanx of nested brackets, especially where multiple function calls:
pring(list(str(len(foo(bar)))))
something that would be nearly impossible without a good IDE. Thank goodness for PyCharm.
My first foray into the field of computer science was entirely self-directed, through an online tutorial called CodeCademy. The point is, I didn't spend a lot of time programming before 148, and certainly even less of that time was spent writing print commands. Yet still, without fail, EVERY TIME I go to print anything, usually for debugging purposes, I default to Python 2 syntax. Why!? I know they say that whatever you learn first seems more intuitive to you, simply by virtue of precedence. But I didn't even do that much Python 2! Surely that is testimony to the greater intuitive clarity of the former. ISN'T IT!?
Other than that, not a big week. Somewhere around this time I started coming early to the Wednesday lectures. I really like coming early and sitting in the front row, I feel like it's a better lecture experience. It's more exciting and I pay more attention. If only I could wake up earlier. #commuterproblems
No comments:
Post a Comment