Session 20

PP 28, 29

Exercise 28 was short and simple. To solve it, I: 1) Put the three items into a list, 2) Used sort() on the list to order it from smallest to largest, and 3) Used pop() on the list to return the largest item (via the last index). Exercise 29 was the completion of the tic-tac-toe game and was actually solved in the last session.

There are still seven Practice Python exercises left, but I have decided to move on to something more challenging for now and save the last few exercises for days when I only have the energy for short, simple exercises. They will be good to have in my back-pocket as “warm-ups” or “refreshers”.

Session 19

PP 26, 27

These exercises were the second and third in the “tic-tac-toe” series; by the end of the third, I had a complete working game! This was actually not too challenging. The secret was keeping the data flow between the various functions organized. Related to that, I’m still trying to learn how best to name variables. I want to keep the names short for ease of use, but then they tend to be less descriptive. Also, I’m still learning when it’s OK (if ever) to reuse variable names inside and outside of functions. I think I will probably learn this via experience (especially when I start to write longer, more complex programs).

Below, I have just included the code from the draw_board() function, as the complete code was pretty long to post here; but if anyone is interested in it, I can post it below in the comments!

Session 18

PP 23, 24, 25

Exercise 23 and 24 weren’t too difficult, so I don’t have too much to say about them, but wow did I have trouble with exercise 25! It took me three tries over three days to finally to solve it completely and correctly.

The first approach I came up with used a very complicated set of nested if statements to look at the previous one, two or three guesses to formulate the next guess. The logic became way to complicated for me to follow and while it could guess correctly some of the time, it also had a tendency to run itself in an infinite loop, which I couldn’t fix. Also, I know that using a bunch of nested if statements is bad practice, so I wanted to come up with a better approach.

The second solution involved using the “high” and “low” guess lists that I used in the final solution, but I ran into syntax error involving the lists that I couldn’t fix, even after troubleshooting it extensively.

Finally, I threw out solution two (which wasn’t really a solution, since it didn’t work) and re-wrote it from scratch. What I ended up with was much more concise than if I had kept the original structure, and… it worked!

You can see that solution below. Note that I had the computer start with a random guess, to make the problem a bit more complicated to solve. I also used print statements all over to see that it was working correctly:

If you have any comments on the solution, please let me know!

Session 17

PP 22

This exercise used dictionaries and applied string operations in ways I had not seen them used before; split() just became a whole lot more useful after this exercise! I have also gotten better at using file paths by intentionally looking for new ways to obtain them each time. These Practice Python exercises have been a lot of fun, but I am really looking forward to trying something more challenging after I finish them. Below is what I wrote for PP22 (the ‘extra’ challenge):

Session 16

PP 21

Well, these posts have definitely slowed down a bit since I started my new job (lots of new stuff to learn) but I plan to pick up the pace now that I’m getting settled in. Exercise 21 was an easy one, just writing some text to a file. However, this exercise did help me learn how to obtain and manipulate a file path with argv, as in the define_path() function below. I found this really useful.

Session 15

PP 20

This was a great exercise! The first part, searching a list, was extremely simple. However, implementing a binary search was more complicated than I had initially thought! The difficult part for me was realizing that I had to consider the list values and the list indexes separately. Once I figured that part out I was able to write a function that I could call recursively to find the numbers IN the list. The problem at this point, was that any numbers outside the list would cause an infinite series of recursive calls. After working in circles for a while I took a break, and when came back, the answer was obvious.

All I had to do was add guardrails around the maximum and minimum values in the list. Since the list is already ordered, I assume this doesn’t add much processing time and is a reasonable and necessary step to take. After that, my solution worked perfectly!

Here is result:

Session 14

PP 16, 17, 18, 19

Exercises 16 and 18 weren’t too bad, but I definitely found 17 and 19 to be challenging. The python-related tasks in these exercises (using requests and BeautifulSoup to scrape a webpage) weren’t too bad, but I found working with the HTML tags and handling exceptions to be difficult. I sort of limped my way through these two exercises, so I plan to come back and re-try them when I’ve had some more practice.

In terms of the exceptions I mentioned above: Unicode seems to cause frequent problems for me. I have learned to use .encode(‘utf-8’) to solve some of these problems, but so far it has been an imperfect solution.

I’m looking forward to the next few exercises, which seem a bit simpler, and will plan to revisit these 2x on a weekend when I have more time to dive into the basics of HTML and CSS.

Session 13

CS50X Lesson 1 // PP 9, 10, 11, 12, 13, 14, 15

Recently, things have been pretty busy at work (I accepted a new rotation) so I have been getting a little behind on these posts. However, I have made some pretty good progress on these PP exercises, so I wanted to check in. Also, I expect that I will start posting more frequently once I get past the review material (LPTHW, PP, etc…) and move onto some more interesting / challenging stuff.

First, I should report that I watched the first lecture video for CS50X, which is an admission requirement for Amazon Technical Academy, a program which re-trains non-technical employees as Software Development Engineers (SDEs). The lecture was very engaging and interesting, much more-so than I expected. It’s pretty cool that I can now explain how numbers work in binary after just a 30-second explanation! However, it does seem that the course is much more focused on concepts than just practical coding skills. Ultimately, my goal is to learn software architecture, hardware, algorithms, and all of the computer science concepts, but with limited time I need to focus on practical knowledge that can be applied to my work or personal projects, otherwise I fear that I will pick up lots of concepts, and have no practical way to apply them. So, just to clarify, my goal for this blog and course of study is to:

  1. Learn Python’s syntax
  2. Practice that syntax until I am proficient with it
  3. Choose a specialization area (data science, web applications, etc…) and learn the basic of that area
  4. Choose a project in that area
  5. Learn all of the deeper concepts behind how the tools / libraries I use for the project work (algorithms, math, CS principles, etc…)
  6. Choose a new specialization area and project
  7. Repeat until I have learned a bunch of specializations and CS concepts (and mastered Python’s syntax and libraries)

With this approach I hope to learn some of the fundamental computer science concepts via projects and fill in the gaps in my knowledge later. For example, I would rather reverse-engineer a recommendation engine than read a bunch of books on how recommendation engines work . If I get stuck, I will read those books to get unstuck, but I want my learning to be driven by practical application, not accumulation of esoteric knowledge.

Because this is the approach that I have chosen – I don’t think CS50X really ties in with my current learning plan. I definitely plan to take the course eventually to fill in the gaps in my knowledge (or if I am admitted to ATA), but for now, I think Lesson 1 is where I will stop.

Now, in terms of the PP lessons. They covered default arguments to functions:

def get_input(prompt=”Please provide your input “)

They also covered set() which is an un-ordered list without duplicates, reversed() and reverse(). The first will copy a list in reverse, the second will reverse the original list (I think…). I have had some issues with methods like reverse() returning None when I don’t expect it. I need to play around with this some more to make sure I fully understand how it works. It seems to relate to Python’s memory management model, which I found an excellent explanation on (that I only partially understood) here: https://jeffknupp.com/blog/2013/02/14/drastically-improve-your-python-understanding-pythons-execution-model/.

Whew, that was a long one!

Session 12

Practice Python (PP) Exercise 1, 2, 3, 4, 5, 6, 7, 8

These exercises are great for building confidence with some of the basic tools taught in LPTHW. They are very simple, but can often be solved multiple ways. Usually, when I do these exercises, I try to:

  1. Solve the problem by any means necessary
  2. Solve the problem in alternate ways
  3. Build functions around my solutions
  4. Build classes around my solutions

Many of these exercises are pretty basic and don’t require functions / classes, but it’s good practice!

Also, one of these exercises (#6) showed me how to iterate over or slice a string. Until this exercise, I hadn’t even realized that was possible. Definitely a useful trick.

Finally, Jeff Knupp’s blog https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/ has proven to be really helpful for some of the more complicated topics (for me) like classes. I definitely recommend checking it out.

Session 11

LPTHW – Lesson 40, 41, 42

These lessons covered classes, objects, and is-a / has-a relationships. Lesson 40 covered the syntax required to define a class:

class Song(object):

def __init__(self, lyrics):

self.lyrics = lyrics

def sing_me_a_song(self):

for line in self.lyrics:

print line

Lesson 41 added the .strip() and .count() functions, which strip white-space from a string and count the instances of an item, respectively.

Lesson 42 touched on the different types of relationships that classes can have with each-other. It also mentioned the super keyword in relation to inheriting from a parent class.

There are only 10 lessons left in LPTHW, and I am getting a little bored just reviewing basic concepts. From here on out, I will mix these last few lessons with practice problems from Practice Python and Code Wars, code reviews, notes on the official Python documentation, analysis of some useful Stack Overflow answers and other useful stuff I come across.