Home > Programming > Learning Python and Ruby – Fibonacci

Learning Python and Ruby – Fibonacci

Oh, the classic Fibonacci.

Just in case: the Fibonacci series is made up of the numbers which are the sum of the two preceding numbers, starting with 0 and 1. The first Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

JavaScript

// Returns the Fibonacci number at position index. Zero-based.
function getFibonacciNumberByIndex(index) {
    if (index < 0) {
        return null;
    }
    else if (index == 0) {
        return 0;
    }
    else if (index == 1) {
        return 1;
    }
    else {
        var i = 1;
        var prev = 0;
        var cur = 1;
        while (i < index) {
            var temp = prev;
            prev = cur;
            cur += temp;
            i++;
        }

        return cur;
    }
};

/*
Returns the list of all the Fibonacci numbers
up to and including the index argument.
*/
function getFibonacciList(index) {
    if (index < 0) {
        return [];
    }
    else if (index == 0) {
        return [0];
    }
    else if (index == 1) {
        return [0, 1];
    }
    else {
        var list = [0, 1];
        var i = 2;
        while (i <= index) {
            list.push(list[i - 2] + list[i - 1]);
            i++;
        }

        return list;
    }
};

Python

# Returns the Fibonacci number at position index. Zero-based.
def getFibonacciNumberByIndex(index):
    if index < 0:
        return None
    elif index == 0:
        return 0
    elif index == 1:
        return 1
    else:
        i = 1
        prev = 0
        cur = 1
        while i < index:
            temp = prev
            prev = cur
            cur += temp
            i = i + 1

        return cur

"""
Returns the list of all the Fibonacci numbers
up to and including the index argument.
"""
def getFibonacciList(index):
    if index < 0:
        return []
    elif index == 0:
        return [0]
    elif index == 1:
        return [0, 1]
    else:
        list = [0, 1]
        i = 2
        while i <= index:
            list += [list[i-2] + list[i-1]]
            i = i + 1

        return list

Ruby

# Returns the Fibonacci number at position index. Zero-based.
def getFibonacciNumberByIndex index
    if index < 0
        nil
    elsif index == 0
        0
    elsif index == 1
        1
    else
	i = 1
	prev = 0
	cur = 1
	while i < index
	    temp = prev
	    prev = cur
	    cur += temp
	    i = i + 1
	end

	cur
    end
end

=begin
Returns the list of all the Fibonacci numbers
up to and including the index argument.
=end
def getFibonacciList index
    if index < 0
        []
    elsif index == 0
        [0]
    elsif index == 1
        [0, 1]
    else
	list = [0, 1]
	i = 2
	while i <= index
	    list += [list[i-2] + list[i-1]]
	    i = i + 1
	end

	list
    end
end

Right there, I have to admit that Python code looks clean and elegant. I’m sure I will get used to indentation-scopes pretty quickly. JavaScript looks cluttered, but more┬áprofessional. I’m currently having mixed feelings about Ruby, mostly because of the end that mar the elegant feel it would otherwise have.

Aside from some list operations, I learned about the null equivalents: None in Python, and nil in Ruby. I also learned about comments: // and /* … */ in JavaScript, # and “”” … “”” in Python, and # and =begin … =end in Ruby. The multi-line comments in Ruby look terribly weird, more similar to how you define regions in c#. Also, and I did it by design to show consistency, there are docstring in Python – the first line following a definition can (and should) be a multi-line comment describing what it’s supposed to do.

Categories: Programming Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.