|(Python Output Screenshot in Terminal)|
The Code 7 Project - Problem 1
"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).
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 file. You 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:
- Programming practice: I used Python to program code for problem 1 of 7, which is described more thoroughly at this website, under "1) Sums": blog.smartbear.com/programming/7-silly-programming-challenges-to-do-for-fun
- Here's the GitHub link where I saved my data file (problem1.csv) and my Python code (problem1.py): github.com/hchiam/code7 (If you want to try out the code, you have to save both files into the same folder on your computer. And have Python installed.)
- The original blog post describing this project: hchiam.blogspot.ca/2016/06/code-7-project-github-blog.html
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:
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.py. Type "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:
git add problem1.py
git commit -m “problem 1”
git remote add origin https://github.com/hchiam/code7.git
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 commit -m "Starting with the actual code"
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: