Monday, January 11, 2016

Day 2 - The Coding Begins

Today was a lot of orientation, a good chunk of lecture, and a review of the prework material: basic elements of python like data types, string functions, conditional statements, and so on.

It was all review since I've been working with these things for a while.  Regardless, there are always things to learn.

1. Push yourself

Meeting the minimum specs today would have been easy.  On one exercise, I finished early and helped a neighbor. Later, though, I made challenges for myself.  One exercise was to build a simple questionnaire to learn more about other students.  It was to run in the command line and ask 'yes' or 'no' questions.  I built it from the start to be case agnostic and to accept 'y' or 'yes' as equivalent answers (or 'n' or 'no').    The questionnaire also accepted short sentences for some of the questions and returned an answer based on wether user input contained a certain word or phrase.  One of the questions used a while loop with a counter.  An answer recognized as correct was registered, and the loop broken.  Otherwise, 3 wrong answers would break the while loop and move on.  For a half-hour project, it was ok.  (Although I went back later that night and cleaned it up a bit and fixed a weird bug in the while loop.)

The sample is here, with code and functional in-browser implementation courtesy of trinket.io. A useable version is embedded below:

(I'm writing all this down in part to remind myself, later, of how far I've come.  I know this stuff is trivial, but one of my projects while here to work on consistently pushing myself to do more and better than what's required.)

I learned, too, by comparing notes with my neighbor and seeing that she had made a list of questions that were accessed in order whereas I had simply created a script that was executed from top to bottom with a series of conditional statements.

2.  Keep going until it looks 'right'

Python, I hear, is notable for having many alternative ways of getting something accomplished.  Not all of them are equal in terms of speed, elegance, understandability, or "python-ness".  Learning when something 'looks right' is another long term goal.  There was a long series of tests of basic skills as part of the work.  One test (completed by setting a variable r = to some statement) was

# use the 'in' operator to see if 3 is in the array
a = [1,2,3,4]
assert r == True

My first answer was

if 3 in a:
    r = True

That works, and it's easy to understand, but it takes two lines. Defining r didn't sit right, either, since it seems that r should evaluate to True. My second effort:

r = 3 in a

which satisfies both the single line aesthetics and that the value of r is determined by evaluating an expression.

Another challenge asked

#get all the keys of hash a and set it to r

a = {'a':1,'b':2}
assert r == ['a','b']

My solution was

r = [x for x in a]

I've really struggled with this syntax in using an iterator to generate a list.  Trivial, again, but I pushed myself to do it the compact way.

Tomorrow, more things.  And now to sleep.

No comments:

Post a Comment