Sunday, June 26, 2016

The Code 7 Project - Problem 2 - Fibonacci

(Click image to test code in Java online.  No install necessary.)

The Code 7 Project - Problem 2 - Fibonacci

  • Given a number "n", find the "n"-th number in the Fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13, 21, ...).  
  • Full problem description is here.  
  • Description of the Code 7 Project is here

For this problem (and sub-problems), I've used tutorialspoint.com's online interface to compile and execute the code quickly.

There were 5 sub-problems:
2.a) Just do it.
2.b) No loop.  Only constants, no variables.
2.c) No local variables.
2.d) Tail recursion (the function calls itself on the last line in said function).
2.e) No recursion. 
 


Code for all of these can be found under my GitHub repo:  
https://github.com/hchiam/code7

Below are individual links to each sub-problem's solution code, and instructions to view/run the code in your web browser:  


2.a) Just do it.

http://goo.gl/sxNER4
Click on "problem2a.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up. 
Click on "Execute" to run the code.  

2.b) No loop.  Only constants, no variables.

http://goo.gl/HS4xhp
Click on "problem2b.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up. 
Click on "Execute" to run the code.  

2.c) No local variables.

http://goo.gl/diziRi
Click on "problem2c.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up.  
Click on "Execute" to run the code.   

2.d) Tail recursion (the function calls itself on the last line in said function).

http://goo.gl/6ixNZ8
[EDIT:  the above link's code isn't true tail recursion, but it does use recursion.  Go to this link for the example of true tail recursion:  http://goo.gl/ytnRHF]
Click on "problem2d.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up.  
Click on "Execute" to run the code.   

2.e) No recursion.

(Same as 2.b).)



_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

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

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