Monday, March 31, 2014

Sorting and Efficiency

Hello pythoneers! Have you ever wondered what you would do if you dropped a deck of cards? Well, if you're like me you would probably pick them up and remake the stack. BUT, what if you had obsessive-compulsive disorder and needed to sort every card in the deck to keep them in order? Then you would probably want to know the fastest way to sort your cards! If you think you fit the criteria for this compulsion, then this sounds like a slog entry for you!
You see, in 148 recently we've been learning about sorting algorithms and their efficiencies. The ones we focused on were bubble sort, insertion sort, selection sort, quicksort and mergesort. You might recall seeing a few of these from 108 and not knowing why they would be useful, but now I understand! Here's where sorting could improve your well-being: Let's say we have a huge list of data, like the index at the back of a textbook. Then, wouldn't it be insanely hard and time-consuming to search for something if the list wasn't sorted? You could save yourself loads of time by sorting this list first and then searching for what you want. Now, depending on how your list is sorted - ex: reversed, sorted - some sorting algorithms may be a better choice than others. For instance, in a best-case and a worst-case scenario selection sort would run at an efficiency of n^2, making it not that great of an algorithm (sorry selection sort). Mergesort however has an efficiency of nlogn for both cases!! wow! Lab #10 proved to be super helpful for getting to know my sorting algorithms. In that lab we compared these algorithms' runtimes by making graphs and by using different types of lists of various sizes. What my partner and I noticed is that bubble sort was pretty bad in every situation, while others proved to be quicker. The coolest thing about it was learning about the built-in sort that python uses. When you ask python to sort a list for you, it will choose the most efficient one according to the list you give it!!
Also, I was on Piazza the night before the second test and noticed that someone posted a link to a list of best/average/worst-case complexities for sorting algorithms. I'm guessing you want to see it so here you go!!!! http://bigocheatsheet.com/ You're welcome :)

Friday, February 28, 2014

Recursion

Between week four and week seven, CSC148 focused on recursion. Beforehand, I had no experience with recursive functions and only heard simple descriptions of it in 108 and 165. Back in week four, I read the assigned reading to get a basic understanding of what was to come in the following weeks. It gave a helpful definition of the term, saying that it means "“defining something in terms of itself” usually at some smaller scale, perhaps multiple times, to achieve your objective." We can deduce that a recursive function calls itself within its body until it reaches its base case to achieve the correct result. It's basically when an active function uses itself over and over again to return an end.

For instance, in one of its simplest applications, recursion can be used on a nested list to return information about it, such as the amount of items within it, the sum of integers within it, its depth, among the infinite number of other possibilities. My first real challenge with recursion was during step 5 of A1, where you had to move all the cheese from the first stool to the last in a minimal amount of moves. Although it took a really long time to figure out, I was very satisfied with the fact that I was able to accomplish it.  The tree emphasized during week six was a key example of recursive structures. It presented the use of recursion and object-oriented programming* together.

I believe that the advantage of recursion is that it allows us to write fast and optimized code, whereas writing for an assignment/exercise without recursion may lead to a longer runtime. Plus, we can even use recursion for abstract data types, which proved to be useful in week seven's lab.
Recursion is definitely more challenging that object-oriented programming. While OOP was just about writing simple classes and their methods, recursion makes us thinking deeper about how we want to achieve results and if there are faster ways to do it. Although it's taking longer for it to sink in, its practicality is motivating me to keep trying.

*Object Oriented Programming

Thursday, February 13, 2014

Week 6

(This is a tree)
Hello fellow pythonista! Week 6 was full of challenges and fun, while our heads ached from A1.This week's slides focused once more on recursion. As the weeks go by, I start to worry more about writing recursive code on the midterm, but I'll leave those thoughts for reading week. Usually I would look at the slides and panic but this week's weren't as frightening! We talked mostly about terminology of recursive structures. For instance, Heap showed us this example called a tree which contain nodes, with one parent, that can be in a sequence by following paths. There are other terms including  height, being the maximum path length of the tree, and leaves which are nodes without children, but I wont name them all. Following this explanation of trees, we went through tree traversal! We learned about pre-order, in-order, and post-order traversal, which I'll have to study a bit. So far they look like ways of sorting values in a tree, regarding their height and location. The remaining parts of the Wednesday's lecture focused on methods that could be implemented on a Tree class, such as __repr__, sum, and height.
This week's lab was super easy! It required us to understand built-in methods such as all, any, filter, etc., but didn't require us to use them at all; we only had to break them down. Thus, we finished early and I gave myself a pat on the back. One extra thing it familiarized me with was test cases. Every time I saw that we had to make test cases, I cringed, but I feel like I can do them now.
One huge topic among 148 students this week was A1. I spent hours doing it, and I'm proud for finishing it as well as I could, but getting there was a long progress. I spent a lot of time looking at the handout for step 5, and though I couldn't get the best number of moves for all number, I think I got them close enough to get a good amount of credit. Anyways, that's all I have to say for this week.
See y'all after reading week!

Wednesday, February 5, 2014

Week 5

Hola pythonista! It's week 5! Now that this week's lectures are over, let's take a walk down memory lane to see what we've learned.
So this week we dove deeper into recursion as well as it's importance in A1. Honestly, Monday's lecture could have been really helpful, but I didn't read the A1 handout until Tuesday so I wasn't sure what he was talking about. Looking back at the lecture slide, it looks like I'll be able to use it to my advantage when I'm actually working on A1. During Wednesday's lecture, Prof Heap went over namespace and rec_max, another example of recursion. I think I'll have to read more about what we've learned this week because I don't feel comfortable enough with it yet.
This week's lab focused on tracing and recursion. Overall, the tasks were pretty easy. My partner and I took down the tracing challenge like we've been doing it our wholes lives. The last two parts needed us to create recursive code. At first we didn't know what to do, and slowly enough we had a chunk of terrible code. But, our TA gave us a hint and made us realize that we only had one mistake that wouldn't let us return anything! (Yay for TAs) For the second part, we used an example from class to guide us and we finished it in two minutes. I definitely feel a lot better with recursion now because I had no idea what I was doing beforehand.

Sunday, February 2, 2014

Week 4

Hello there you fabulous pythonista! This week Danny Heap gave lectures about recursion. As stated by one of the week's readings, "Recursion means “defining something in terms of itself” usually at some smaller scale, perhaps multiple times, to achieve your objective." Back in the first few weeks of my 148 adventure, recursion made no sense. It's still tricky to this day, but I now understand in which context I would create a recursive function. A cool/helpful example of recursion that we observed in class is the nested_depth function, where it returns an int according to the depth of nested lists in a list. Ex: list = [1, [2, 3, [4], 5], 6, 7]. If say nested_depth(list), it will return 2.
Week 4's lab was pretty swaggie. It helped my partner and I to design classes and to create complex methods. Although it was pretty easy and we were going considerably fast, our TA wouldn't explain how to use inheritance. So we sat there and surfed the web to find an explanation, but we never found a solution. Thus I need to learn how to do that really soon before it pops up in an exercise/assignment.
At first glance, exercise 2 was scary. It was full of exceptions and I had no idea what to do, so I laid out what the final product would look like and called it a day. The next afternoon I looked at it and I was pleasantly surprised to find out that I did half of it right. With the help of readings I was able to prevail over it's threat and dominate the exercise. I might be blowing this out of proportion though, since it was fairly simple.

Thursday, January 23, 2014

Object-Oriented Programming

The first three weeks of the course have explored the subject of object-oriented programming. We have been learning about classes, which is what we ended with in CSC108. Personally, the class system is way more enjoyable than learning basic programming skills. Although it was tedious to learn the basics, especially with a decent amount of knowledge prior to taking the course, I'd say that 108 showed me how to create efficient code with enough style to prevent confusion.
Object-oriented programming focuses on the creation and programming of objects. These objects, such as Dog, can be created with the class system, where you can assign to it different attributes inside the __init__ method, like name, type, etc. Within these classes, methods can be implemented to manipulate its attributes. These methods are typical functions, but they are accessible by typing object.method(x) inside the shell. For example, if we have an object Phone, we can write a method Call(self, person) to call a person, producing a string of a conversation. To further understand the meaning of object-oriented programming, a reading from the first week provided a straightforward description: "In procedural programming the focus is on writing functions or procedures which operate on data. In object-oriented programming the focus is on the creation of objects which contain both data and functionality together." Exercise One tied in perfectly with the quote above. The exercise gave us the task to write a Dog object and a Toy object, and showed how their unique methods allow these objects to interact. I prefer object-oriented programming because it feels like I'm actually manipulating something substantial or valuable. While, in procedural programming, it felt like I was inputting arbitrary values into functions, which were often confusing and scattered all over a python file, to see what they would produce. Now that I know the basics of Python programming, I think I'm ready to tackle bigger, more rewarding tasks.

*Notable readings: Object-oriented Python

Week 1-3

To begin my slogging journey of CSC148, here's a recap of the lectures/exercises/labs of the first three weeks (for future reference):
  • The first week's lectures covered the design of new classes. For instance, we analyzed a situation (class Point) to figure out what to call our object, and what methods we needed to implement within the class to manipulate the object. Exercise One from that week, the one with class Dog and class Toy, familiarized me with classes; including how to use the __init__ method to create attributes and how to call other classes' methods.
  • The second week's lectures covered the creation of abstract data types (ADTs). A clever example from the Monday lecture described a Stack ADT as a stack of rocks; where you may add to the stack by adding rocks to the top (push), and you can only remove the rock at the top of the Stack (pop). The week ended with comprehensions and the ternary if, which I will need to look at a bit more to understand them because I honestly didn't pay enough attention in class.
  • The third week's lectures started with the creation of subclasses with the format: class NewClass(OldClass):. Then prof Heap explained how to raise your own exceptions. Finally, he went over how to use the __eq__ method to see if stack objects have the same items in the same order, and the __repr__ method. The __repr__ method was new for me but looked like a fairly simple way to represent a class. The lab for week three helped me understand how to create ADTs such as class Queue, where items come and go at a first come first served basis, rather than the way Stack pushes and pops items.
To conclude my first three weeks of CSC148, I'd say that I'm pleased with my progress. The exercises and labs are actually aiding me a lot and teaching me how to apply the knowledge acquired during the lectures. I'm feeling confident in my abilities with object-oriented programming and I'm excited to see what we learn next! Feel free to comment, you beautiful pythonista/pythoneer.

*Note to self: work on comprehensions, ternary if, exceptions, __repr__ method.