Winter Break Roundup

My winter plans proceeded as planned, but with a different theme.  On the programming front I did not use any of the books listed in the last post.  Rather I started working through Learning Python 3 the Hard Way by Zed Shaw and Think Java: How to Think Like a Computer Scientist by Allen Downey and Chris Mayfield.

Portions of the book are available for free at the link above.

The entire book is available for free at the link above.

The reason for choosing these texts over the ones I had checked out previously (Core Java Fundamentals Volume I by Cay S. Horstmann and Learning Python by Mark Lutz) was that I am a true novice.  Not having programmed before.  I did not want to learn a language so much as start to understand the thought processes behind the logic of computer science and develop problem solving skills in this domain, and more generally.

The best attribute of these two books is how they force you to find problems on your own.  This means you will make a lot of mistakes, but those mistakes reinforce your learning significantly.  Additionally, you find a way, teach yourself, how to find the answers to problems, which is an invaluable skill that is hard to teach through text.  Struggling is one of the best ways, and both of these books give you just enough clues that you do not have to flail desperately for answers, but they are not handed to you either.

I found working with two languages simultaneously, again with no prior experience, to be immensely helpful.  Mostly because this forced me to learn concepts (such as variables, loops, conditionals, functions) as abstract rather than particular instantiations.  When I think of these concepts I think of relationships rather than a specific line of code.  This processes does lead to some frustration when debugging while "thinking" in Python instead of Java or vice-versa, but the syntactical errors are solved quickly, especially upon noticing the most frequent mistakes made.  The value is that this thought process can be easily translated to other languages and arenas.

An area where this developmental skill has proved particularly fruitful has been in Math.  Over the winter break I was concurrently taking the mooc trigonometry class through PC's MOER system to prepare for calculus.  Mathematical thinking and programming thinking complement one another beautifully.  I found each mutually reinforcing conceptually and practically.  One of the best practical applications is to code formulas and theorems.  Here is a small example of coding the slope formula for Java and Python respectively.
\[m = \frac{\Delta y}{ \Delta x }\]
import java.util.Scanner;

public class Slope {

  // method from the Mindful Programmer
  static private String convertDecimalToFraction(double m){
      if (m < 0){
          return "-" + convertDecimalToFraction(-m);
      }
      double tolerance = 1.0E-6;
      double h1=1; double h2=0;
      double k1=0; double k2=1;
      double b = m;
      do {
          double a = Math.floor(b);
          double aux = h1; h1 = a*h1+h2; h2 = aux;
          aux = k1; k1 = a*k1+k2; k2 = aux;
          b = 1/(b-a);
      } while (Math.abs(m-h1/k1) > m*tolerance);

      return h1+"/"+k1;
  }

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    double x1, x2, y1, y2, dx, dy, m;

    System.out.println("Enter X coordinantes (Press Enter after each):");
    x1 = in.nextDouble();
    y1 = in.nextDouble();

    System.out.println("Enter X coordinantes (Press Enter after each):");
    x2 = in.nextDouble();
    y2 = in.nextDouble();

    dy = y2 - y1;
    dx = x2 - x1;
    m = dy / dx;

    System.out.println("Slope in decimal form: " + m);
    System.out.println("Slope as a fraction: " + convertDecimalToFraction(m));

  }
}


from fractions import Fraction

x1, y1 = [int(x) for x in input("Enter the coordinantes of the first point: ").split()]
x2, y2 = [int(x) for x in input("Enter the coordinantes of the second point: ").split()]

dy = y2 - y1
dx = x2 - x1

m = dy / dx

print("Slope in decimal form: ", m)
print("Slope as a fraction: ", Fraction(m).limit_denominator(100))


As you may have noticed, Python appears to handle math much more elegantly than Java.

Comments

Popular Posts