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 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:

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.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 (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

Friday, June 17, 2016

Code 7 Project + Github + Blog


The "Code 7 Project"*:  
I'll be practicing general programming skills by building 7 simple programs over 7 different weekends (when my other commitments allow it). 

Project breakdown: 
  1. Get a short list of simple programming practice problems.
  2. Program some working code for 1 problem by the end of 1 day.
  3. Commit to Github account.
  4. Post to this blog.

Simple Practice Problems:
From a quick Google search, I got 2 websites:
  1. http://blog.smartbear.com/programming/7-silly-programming-challenges-to-do-for-fun
  2. https://adriann.github.io/programming_problems.html 
The first link should not be overwhelming.  With just 7 seemingly straightforward practice problems, it should be manageable.  For problem # 7 however, I wanted to have something more motivating for me, with more general problem-solving problems, and not just math.  So I also found the second link has many problems that I can use to keep going on as long as I want.  I decided that I want to do these problems, instead of the "Project Euler" problems, for #7.  I may also group code for the problems from this website once I start working on this set, especially in the beginner levels. 

Strategies I Can Think of Right Now:
  1. Set dates in my planner for each of the 7 problems.
  2. Decide ahead of time which programming language to use for each practice problem.
  3. Figure out how to easily get around to using the language (minimal installation besides what I already have installed).
  4. Sort of related to 3:  find websites that can teach me to do that quickly.
  5. Put it down as a to-do with a checklist on Habitica (formerly known as HabitRPG).
From initial these strategies, here are my notes so far:

Programming Practice —> Github —> Blog:

http://blog.smartbear.com/programming/7-silly-programming-challenges-to-do-for-fun/

  1. in Python
  2. in Java
  3. in Python https://opentechschool.github.io/python-beginners/en/simple_drawing.html
  4. in Scratch
  5. in SQL http://www.w3schools.com/sql/default.asp and type it into the online code and just copy and paste the code into a text file
  6. in HTML5 canvas http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_canvas_tut_path add lines and fill and just copy and paste the code into a text file
  7. do not do, and instead go to: the next website: https://adriann.github.io/programming_problems.html (all of them in python) [EDIT:  I'm actually planning to use the websites CheckiO and CodeWars.  I'm already using them on weekdays.  You can see my current levels at http://hchiam.blogspot.ca/p/current-programming-xp.html]
 (I already have Python, Java, and Scratch installed, so I can just create SQL and HTML5 code online on the w3schools websites.)


_________________________
*Disclaimer:  I do not own the phrase "Code 7".  Any appearance of similarity to brands, sayings, phrases, etc. is purely coincidental.  I'm just using it as a convenient, simple name for this project.
_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

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

Wednesday, June 8, 2016

Quick Vector Graphics Lesson

 
A Test Vector Graphic Drawing

Inkscape is actually surprisingly easy to learn.  
It took me less than an hour to do these 3 things:  install, create, & embed.

1) Download & install Inkscape (and also XQuartz if you're using a Mac):  

 inkscape.org/en/download 

2) Learn how to create the donut above:   

(The techniques are easy to pick up on if you have a context to use them in.  Follow the steps but also pause to experiment with the alternate controls to see how they work, like intersect, difference, union, etc. as well as the control and shift buttons in combination/isolation.)
youtube.com/watch?v=s41_-qsmA7s 

3) BONUS:  Learn how to embed the .svg file into a webpage, like I've done above:   

(I've also adjusted the frame size to 300 x 300.)
tech.spoilertv.com/2012/05/embedding-pdf-or-other-documents-into.html



LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

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

Tuesday, December 29, 2015

How to Convert Temperature (F<-->C) Quickly in Your Head

 

These are the "exact" equations for converting between Fahrenheit and Celsius:

C = ( F - 32 ) x 5/9
F = C x 9/5 + 32

https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature#Celsius_.28centigrade.29


But what if you see temperatures on TV, and just want to convert it in your head to get a feel for how hot or cold it is, without unlocking your phone to use a calculator?

 
Easy.  Use equations that are easier to do in your head, but that also give you answers close enough to be useful for our purposes.  As long as you're not doing chemistry calculations requiring mission-critical accuracy, you should be fine.

I finally figured out a way while waiting on the subway:

C = (F-30)/2
F = Cx2+30

These are what I'll call

"Rapid equations":

C = ( F - 30 ) / 2 --> and round
F = C x 2 + 30 --> and round

I'm guessing a few degrees off doesn't make that much difference in how hot/cold it feels for you, so you can round to the nearest 10 degrees.

If you want more accuracy, going into the 1's places or decimals, add the

"refiners":

C = (F-30) /2 + F/2 /10 + 1
F = Cx2 +30 - Cx2 /10 + 2

These can also be written as:

C = (F-30) /2 + F/20 + 1
F = Cx2 +30 - Cx0.2 + 2

If you want a way to remember the "rapid equations", take the first letter of each relevant math operation, and create some

mnemonics (memory aids) :


"farmed cow":
FMDRC, which means:
F --> Minus, Divide, Round --> C
"C = F M D R"
C = (F Minus 30) Divide by 2 and Round to one significant digit.
C = ( F - 30 ) / 2 --> round

"caterpillar fly":
CTPRF, which means:
C --> Times, Plus, Round --> F
" F = C T P R"
F = (C Times 2) Plus 30 and Round to one significant digit.
F = C x 2 + 30 --> round

2 is involved in multiplying/dividing, and
30 is involved in adding/subtracting.

Or you can make up your own way to remember the formulas, but they're pretty simple.  Just two steps.

Practice:

Check if you can do these in your head:

100 C  ~  200 F (or 212 F)
20 C  ~  70 F  (or 68 F)
10 C  ~  50 F
0 C  ~ 30 F (or 32 F)
-10 C  ~  10 F (or 14 F)

Now you can tell your American friends how cold (or hot) this winter felt.

_________________________

Why does this all work?


Algebra and rounding.

It goes back to the original "exact" equations:

C = ( F - 32 ) x 5/9
F = C x 9/5 + 32


These can be re-written as:

C = ( F - 32 ) x 0.55555
F = C x 1.8 + 32


And these can be re-written as:

C = ( F - 32 ) x (1/2 + 1/20 + 1/200 + 1/20000 + ...)
F = C x (2 - 0.2) + 32


Or:

C = ( F - 30 - 2 ) x (1/2 + 1/20 + 1/200 + 1/20000 + ...)
F = C x (2 - 0.2) + 30 + 2


And if we trade off a little accuracy, we can get rid of the less significant multipliers:


C = ( F - 30 - 2 ) x (1/2 + 1/20 + 1/200 + 1/20000 + ...)
F = C x (2 - 0.2) + 30 + 2


And from there you can keep going using the simplifying assumptions of significant digits and eventually you can get to the same place as the "rapid equations".


_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

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

Tuesday, September 1, 2015

How to Quickly Convert Binary To Decimal In Your Head - And Then Go Faster


How to Quickly Convert Binary To Decimal In Your Head:


See this article for the original idea that inspired this post.  This post discusses an extension to that idea to make it even faster. 

If you go to that article at http://www.zenoli.net/2007/03/quickly-convert-binary-to-decimal-in-your-head/ then you'll see that it's a neat trick to let you just simply read from left to right while figuring out the decimal number, and with minimal memorization along the way.  

All you need to know is how to multiply by 2, add 1 or 0, and remember only one number at a time as you read from left to right going along the binary number.  No need to keep track of absolute positions, especially for longer binary numbers.

It makes use of the idea that binary digits are multiples of 2 of their respective lower positions to their right.

To summarize that article, there are two rules:

1) Multiply by 2 in binary --> shift all 1's and 0's to the left (and keep the same pattern).
2) Divide by 2 in binary --> shift all 1's and 0's to the right (and keep the same pattern).

(Although in practice I only need to remember the first rule.)




And Then Go Faster:



A powerful addition of this idea is that you can then make use of "convenient numbers" to quickly deduce other numbers and reduce both memorization and calculation time.

Here are some "convenient numbers":  5, 10, 20.

Let's use 5-digits in binary to show some examples:

Binary to Decimal:
00101 = 5
01010 = 10 = 5 x 2
10100 = 20 = 5 x 2 x 2 or = 10 x 2

Did you notice how the same three digits just shifted left when you multiply by 2?
If you just memorize that 5 = 101 in binary, then you can easily remember that ten = 1010 because ten = 5 x 2, and 1010 = "101 times two".

So for the price of memorizing two things (i.e. that 5 = 101 and this rule of shift left = x2), you get to know at least two other numbers instantly:  10 and 20.

Okay, not so impressive.

But what I said earlier is you can quickly deduce other numbers from these "convenient numbers".

What's 10101 equal to in decimal?

Well, let's see:
10100 = 20, so
10101 = 20 + 1
So then
10101 = 21.  Kaboom.

So when you see the pattern "101" in a binary number, you can intuitively figure out the number almost instantly just by "looking".  You can at least figure out the significant digits rapidly.

Now, consider that you can remember that 2 = 10 in binary, or that 1 x 2 = 2, or that the 2nd digit from the right is 2.  Any of these ways works.  Whatever is faster for you. 

How about we try converting 10110?

Remember that
10100 = 20
so
10110 = 22 because 10100 + 10 = 10110.
(10 = 2) 

Done.

Here are some more examples:

01110 = 
01010 +
00100 =
10 + 4 = 14

01111 =
01010 + 
00101 = 
10 + 5 = 15

A visual way to instantly see this is to imagine the 1's "falling" into their respective positions.

01010 = 10
00101 = 5
01111 = 15

You can probably figure out other numbers, but let's just say it pays to memorize a little to get a lot more speed, just by "looking" at the numbers instead of trying to remember mulitple numbers to add together at the same time.

Some "convenient numbers" to remember are the place values and easy patterns to see that coincide with the decimal system's base of ten, i.e.:

2,4,8,16 (binary place values)
5,10 (simple multiples of base 10)

("1" pattern)
00001 = 1
00010 = 2
00100 = 4      = 2 x 2
01000 = 8      = 4 x 2
10000 = 16    = 8 x 2

("101" pattern)
00101 = 5
01010 = 10    = 5 x 2

Another interesting pattern to think about:

("11" pattern)
00011 = 3
00110 = 6      = 3 x 2
01100 = 12    = 6 x 2

_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

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