Sunday, June 19, 2016

The Code 7 Project - Problem 1 - Sums

(Python Output Screenshot in Terminal)

The Code 7 Project - Problem 1 - Sums

"Goodies" in this blog post: 

  • My solution code in Python (GitHub link & description below).
  • The 3-5 essential commands for GitHub command-line.
  • Other links.

Things I practiced while working through this problem:

  • Coding in Python (function, dictionary, file reading, unittest, ...).
  • Running Python code (as a "script" file, as opposed to "line-by-line" in Python IDLE).
  • GitHub commands in command-line (in terminal for Mac).
  • Googling.

Problem 1 Description:  Read a data file and tally sums, while displaying each total sum as soon as possible, i.e. you can't just do all the sums after reading the whole data fileYou have to output each sum as soon as possible, while reading line-by-line.  In other words, you have to tally sums on-the-fly.  But sums of what?  In the original example problem, the sums to be calculated are of--and I quote--"the number of red-haired students in each grade, in each school, in each city, and in each state", taken from a data file.  One key assumption is that the data file is already sorted, as was the example data in the original problem description.  I re-created that data in a CSV file (problem1.csv), to be read by my Python code (problem1.py).

Links, Links, Links:  Programming Practice —> Github —> Blog:

My Overall Strategy:  "Strategic Google searches".  :)  Sometimes the first results aren't always the best.  I didn't always find what I needed on w3schools or Stack Exchange.  For GitHub command-line, I even found a visual explanation, but I eventually figured out it still didn't meet my needs.  Sometimes I had to rephrase and try different search terms.  Learning Python was less overwhelming with this website (simple, interactive, no Python install needed)learnpython.org
---

Some useful notes for using GitHub (basically Dropbox for code) with command line:

I found these 2 web pages helpful for extracting the essential commands:

  1. readwrite.com/2013/09/30/understanding-github-a-journey-for-beginners-part-1/ 
  2. readwrite.com/2013/10/02/github-for-beginners-part-2/

And here's my summary of the essential commands:

(I already set up my account, login, password, etc. as well as the repo on the GitHub website.) 
  • Navigate to the folder of problem1.pyType "ls" (lowercase L and S) and hit Enter to see what files/folders there are, and type "cd <folder name>" and Enter to go down a folder path.  The first few lines in the picture at the beginning of this blog post show this in action.
  • Type these 5 commands in the command-line, in order (just replace "hchiam" with your username, and "code7" with your GitHub folder name):
git init 
git add problem1.py 
git commit -m “problem 1 
git remote add origin https://github.com/hchiam/code7.git 
git push

And then after that, to update/add files (I committed 2 files at the same time):

 (Notice the similarity of the git commands to the ones just mentioned above.)
  • Navigate to the folder containing the files problem1.py and problem1.csv.
  • Type these 3 commands in the command-line, in order:
git add problem1.py problem1.csv 
git commit -m "Starting with the actual code" 
git push
---

Extra note on how I ran the python file (the simplest way):

  • Open the command-line program.
  • Navigate to the file folder.  In this case, to the folder with problem1.py in it.
  • Type this 1 command:
python problem1.py
  • That's it.

_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics