Tag Archive for Programming

Solving the Even Fibonacci Challenge

The Even Fibonacci Challenge, Problem 2 in the Project Euler Challenge. The modified problem requires adding the even-valued terms in the series which do not exceed a given integer N.

My solution is as follows:

Reading a file in Python

In this article, I will show how to open, read and close a file in Python.

This code sample is for a function that accepts a filename as a parameter, opens the file given by the filename parameter, reads the file line by line ( fd.readline() ), prints the line and finally closes the file. The loop is broken when nothing is read from the file any longer.

An optimisation to the above code (thanks to Dr Jean-Paul Ebejer [x2]) would be:

 

A Binary Search Tree in Python

A Binary Search Tree (BST) is a rooted binary tree where items in the left subtree are less than the value of the root and items in the right subtree are greater than the value of the root. A binary tree is a data structure which consists of either a single node called the root or one where each node in in the binary tree has at most 2 children referred to as the left or right child.

A labeled binary tree of size 9 and height 3, with a root node whose value is 2. The above tree is unbalanced and not sorted. By Derrick Coetzee (Own work) [Public domain], via Wikimedia Commons

Code

The code is split into two classes, one representing an individual node in the BST, and the other the tree with its individual operations. Note that this implementation ignored duplicates.

The Node Class

 

The Tree Class

 

Testing the code

 

The result should appear as follows:

Hope you found this tutorial useful!

Reimporting a Module in Python 3

If you are using a module in a Python Console, and make changes to the Module, you can “re-import” the module as follows:

This will reload the module and make the changes available.

Hope that you’ll find this information useful.

Issue Running Storm Locally with IPv6 enabled

I am currently running Apache Storm locally and in particular I am running the storm-starter project on Windows 8.1. I ran in to the following problem, however, where the connection times out.
A sample output is given hereunder.

 

 

4216 [storm.starter.WordCountTopology.main()] INFO  backtype.storm.utils.StormBo
undedExponentialBackoffRetry – The baseSleepTimeMs [1000] the maxSleepTimeMs [30
000] the maxRetries [5]
4217 [storm.starter.WordCountTopology.main()] INFO  org.apache.storm.curator.fra
mework.imps.CuratorFrameworkImpl – Starting
4217 [storm.starter.WordCountTopology.main()] INFO  org.apache.storm.zookeeper.Z
ooKeeper – Initiating client connection, connectString=localhost:2000 sessionTim
eout=20000 watcher=org.apache.storm.curator.ConnectionState@3489ffb4
4223 [storm.starter.WordCountTopology.main(0:0:0:0:0:0:0:1:2000)] INFO  org.apac
he.storm.zookeeper.ClientCnxn – Opening socket connection to server 0:0:0:0:0:0:
0:1/0:0:0:0:0:0:0:1:2000. Will not attempt to authenticate using SASL (unknown e
rror)
14104 [storm.starter.WordCountTopology.main(0:0:0:0:0:0:0:1:2000)] INFO  org.apa
che.storm.zookeeper.ClientCnxn – Client session timed out, have not heard from s
erver in 10009ms for sessionid 0x0, closing socket connection and attempting rec
onnect
19239 [storm.starter.WordCountTopology.main()] ERROR org.apache.storm.curator.Co
nnectionState – Connection timed out for connection string (localhost:2000) and
timeout (15000) / elapsed (15020)
org.apache.storm.curator.CuratorConnectionLossException: KeeperErrorCode = Conne
ctionLoss
at org.apache.storm.curator.ConnectionState.checkTimeouts(ConnectionStat
e.java:198) [storm-core-0.10.0-SNAPSHOT.jar:0.10.0-SNAPSHOT]
at org.apache.storm.curator.ConnectionState.getZooKeeper(ConnectionState
.java:88) [storm-core-0.10.0-SNAPSHOT.jar:0.10.0-SNAPSHOT]
at org.apache.storm.curator.CuratorZookeeperClient.getZooKeeper(CuratorZ
ookeeperClient.java:115) [storm-core-0.10.0-SNAPSHOT.jar:0.10.0-SNAPSHOT]

Resolving the issue

 

The issue is resolved by adding the following parameter -Djava.net.preferIPv4Stack=true to mvn at the command prompt, as follows:

mvn compile exec:java “-Dstorm.topology=storm.starter.WordCountTopology” “-Djava.net.preferIPv4Stack=true”

Hope you’ll find this post useful!

Timer in WPF

To add a timer in .NET, you need to use the DispatchTimer class (example taken from http://stackoverflow.com/questions/5410430/wpf-timer-like-c-sharp-timer). The following is an example: