Algorithms I


Finally, I started studying algorithms – after delaying delving into algorithms for a long time; I ultimately had no choice but to learn it. So pronto I picked up a book – Problem solving with Algorithms and Data structures using Python – and started with gusto. Having little Python skills, I was somewhat worried that my lack of experience was going to make it more difficult for me.

However the book is quite easy to read and I found myself learning faster than I expected – maybe this was due to my improved experience writing interpreted languages, maybe not. All the same thanks be to God who made it so easy. Compared to Java, Python is great for implementing algorithms; it makes life a whole lot easier, Java was such a pain. Python, which sometimes reads like English, makes it easy to write Stacks, Queues and other abstract data structures. Complex algorithms are also easy to write and test in fewer lines.

Alhamdulilah I’ve gotten to study classical topics such as modular exponentiation, greatest common denominator, fractals and the towers of Hanoi. Compared to Java solutions, Python solutions are elegant, simple and light. While I still don’t understand some of the algorithms fully, it’s a start and insha Allah I hope to be confident one day…

One thing: I’ve fallen in love with Python and I hope to learn more of the language as I go along.

4 Comments

·

Leave a Reply

  1. I am a Java programmer of course, but I also recommend beginners to start learning something like python.

    I like your python-java comparison. But you also fail to mention that

    Java solutions are although bulky, but easier to understand than most of the copy-paste code you are doing in python, where you either don’t understand what is going on or have only a partial understanding.

    Java solutions runs way more faster than python solutions.

    Can you show me YOUR java solutions and python solutions and side by side tell me why you think the java solution is a pain? How many algorithms have you tried to solve in Java parry pass python?

    If you are going to make language comparisons, show us your code/implementation/statistics in both languages and tell us FACTUALLY why one is better than the other.

    To be frank, you really want to learn algorithms? pick up C. If you can’t write C, choose a statically typed language, any language without types or loosely typed is a joke.

    And seriously, I would love to see your python implementation of Stacks and Queues for Example, if you have the Java solutions too let’s see them.

    Like

    • Thanks Akintayo; I prefer Python because it’s makes rapid PROTOTYPING easy and better still, you can pick up the language in a few days. I agree that Java solutions are faster but I don’t quite agree that they are easier to understand.
      Personally I am language-agnostic; I don’t think learning C gives an edge in understanding algorithms in any way. Algorithms can be implemented in any language – the programmer has to make the choice but I strongly believe in using the best tools for tasks at hand.
      Here is my implementation:
      class Stack:
      def __init__(self):
      self.items = []

      def isEmpty(self):
      return self.items == []

      def push(self, item):
      self.items.append(item)

      def pop(self):
      return self.items.pop()

      def peek(self):
      return self.items[len(self.items)-1]

      def size(self):
      return len(self.items)

      And here is a stack implementation I found at http://algs4.cs.princeton.edu/13stacks/Stack.java.html. (I’m too lazy/busy to write one myself)
      public class Stack {
      private int N; // size of the stack
      private Node first; // top of stack

      private class Node {
      private Item item;
      private Node next;
      }

      /**
      * Create an empty stack.
      */
      public Stack() {
      first = null;
      N = 0;
      }

      /**
      * Is the stack empty?
      */
      public boolean isEmpty() {
      return first == null;
      }

      /**
      * Return the number of items in the stack.
      */
      public int size() {
      return N;
      }

      /**
      * Add the item to the stack.
      */
      public void push(Item item) {
      Node oldfirst = first;
      first = new Node();
      first.item = item;
      first.next = oldfirst;
      N++;
      }

      /**
      * Delete and return the item most recently added to the stack.
      * Throw an exception if no such item exists because the stack is empty.
      */
      public Item pop() {
      if (isEmpty()) throw new RuntimeException(“Stack underflow”);
      Item item = first.item; // save item to return
      first = first.next; // delete first node
      N–;
      return item; // return the saved item
      }

      /**
      * Return the item most recently added to the stack.
      * Throw an exception if no such item exists because the stack is empty.
      */
      public Item peek() {
      if (isEmpty()) throw new RuntimeException(“Stack underflow”);
      return first.item;
      }
      }

      I believe you’ll agree with me that the python solution is simpler and I actually started learning algorithms in Java.
      I appreciate your views all the same: nice to have met you.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s