Month: January 2017

Naive Fibonacci Algorithm

Dynamic Programming – Recursion + Memoization

Dynamic Programming – careful brute force, break it into sub problems, solve the sub problems and reuse the solution. Richard Belman invented Dynamic Programming as a means to explain mathematical research he was conducting.

Naive recursive algorithm as above, exponential time, running time as recurrance:

T(n) = T(n-1) + T(n-2) + O(1)
T(n) >= 2T(n-2)
which is of the order of 2 to the power of n/2

Memoized DP algorithm: if we need to compute fib(n), if n in memo, return memo[n], else compute fib(n) and store it. memo is initially an empty dictionary. Then fib(k) only recurses the first time it’s called for all k. Memoized calls cost constant time. Number of non-memoized calls is n, non recursive work per call is constant. Therefore running time is linear The best algorithm for calculating Fibonacci numbers is time proportional to log(n).

Princeton Algorithm Courses

One of the problems I had with setting up the jar file algs4.jar was that the CLASSPATH variable needs to include the directory where your Java programs .class file is. You can include ‘.’ in CLASSPATH to indicate that the current directory should be included. Then I followed the instructions on the book site. and modified my .bash_profile as follows.

Recursive Algorithms

A method can call itself. There are three important rules of thumb in developing recursive programs:

The recursion has a base case – always includes a conditional statements as the first statement in the program that has a return.

Recursive calls must address subprograms that are smaller in some sense, so that recursive calls converge to the base case.

Recursive calls should not address subprograms that overlap.

Greatest Common Divisor

Euclid’s Algorithm – compute the greatest common divisor of two nonnegative integers p and q as follows: If q is 0, the answer is p. If not, divide p by q and take the remainder r. The answer is the greatest common divisor of q and r.

Java Recursive Factorial Method

3 key principles of good code include being reusable, maintainable and easy to change. There are numerous ways to uphold and ensure these principles.

Steps to Writing Error-free Code

Understand the difference between syntax errors and logic errors. Syntax involves the ‘spelling and grammar’ of programming, these are generally picked up by compilers. Check your variable names, brackets and punctuation. Autocomplete in modern IDEs helps reduce syntax errors.

Syntax errors that are more like semantic errors are harder to spot and will not be picked up by a compiler, for example when you type the wrong variable name by mistake, you could then end up with the wrong answer. It is helpful to use meaningful names for your variables and functions.

Logic errors often lead to unexpected results, and are seldom caught by a compiler. They can come about when statements are executed in the wrong order, incorrect boolean expression and decision points, wrong data types, etc. To eliminate logic errors, it helps to write out pseudocode before you start coding to help get the sequence right. Check that against the specification, are you doing what you’re supposed to be doing?

Document code clearly, it helps keep code easy to read and understand, especially when it comes to debugging. It is easier to spot bugs in well organised code. When you come back to your code, it will be easier to know how it works. Using a coding convention helps get the balance of white space right. This helps with code readibility.

Initialise variables, consider the boundary cases of data types chosen. Avoid mutable variables and consider the use of null values carefully. A method on call on null will result in a runtime exception. Consider array indices and use flexible data structures if required.

Write modular code that can be tested in isolation so that the source of errors can be identified more easily. This is improved with abstraction and encapsulation which together enables unit testing.

Error avoidance by checking preconditions/requirements are met on the client side, only execute the code if the precondition is true, isResourceAvailable(), if returns true then proceed. But this isn’t always reliable, the client can’t always be trusted.

Exceptions – defensive programming

Defensive programming assumes that the client can’t be trusted and checks argument values. Exceptions, used correctly help software be robust and recover from errors at run-time. Run-time errors are often related to the environment or resources, URL changed, network down, file deleted, printer switched off… These cannot be anticipated, but can be recovered from by handling the exceptions appropriately. It should not fail silently, outputting a message or code may not be seen or understood. Thowing an exception means that classes can be more cohesive and leave other classes to fix problems that are in their domain, e.g. leave I/O to classes concerned with I/O. Otherwise you bind functionality to technology, which may soon be out of date.

An Exception is a software signal which can be thrown or raised and also caught and handled, either in the same method or higher up. There are loosely three categories, Checked Exceptions (have to be caught or specify method may throw them, Unchecked Exceptions (often a bug in the program, not obliged to catch them) and Runtime Exceptions. Checked in this context means that the compiler checks to see that you catch or throw these exceptions, e.g. FileNotFoundException.

The root password for the kippo honeypot is 123456, change this by editing the /kippo /data/userdb.txt file and restarting kippo.

Kippo defaults to listen for SSH connections on port 2222. Using iptables, direct all connections to port 22 to kippo which is listening on port 2222, then direct set the default port for SSH to a different port.