Tuesday, April 17, 2012

Python from Scratch- Shortcut

I started to learn Python from Scratch using Google Python class on my own around 3 months ago.
The first lesson was fast and I felt ease with the new language.
After that I continued with my lessons doing the second lesson following the third lesson and last one which was the Python RegEx part.
During this time I got many visitors and upvotes, I received many comments and emails with advices and nice words and eventually the leading technology blogging site dzone has selected me to be part of his unique talented writers

Although I still have the last lesson to do which is called 'Utilities' I found myself in a situation where I know I am progressing too slow and the reason for that are two major factors: I am a father for two young children and now in the Winter/Spring time they are mostly sick, also I have a full time job at work doing my QA Manager position.
It is very hard to learn when you have such huge gaps between each time you try to study adding to it the late time at night which is the only time available to me.

So I did some brainstorming (Me and Myself) and decide that I need to jump forward and change the way I am studding Python, to take the shortcut.
Instead of going step by step which may take forever I will create a project, this project will be hard enough to solve in few hours, it will need a plan, it will require me to study before I write any part of the code and it will make me deal with Python in a way that once I finished the project I will be way advanced then my original step by step plan.
And last it will be more fun than study step by step endlessly

Again, this is due to time limitations I am experiencing and I do not support such an act to anyone else (but me)

So I thought about it over and over and looked for a good project to make me an advanced coder until I found one- a game I like to play while reading the weekend newspaper- Battleships. You can play the online version here.

Battleships rules are:

Position of the battleships:
1.                  The Battleships board is 10 x 10 grid
2.                  There are Ten ships:
§        One ship of 4 consecutive cells- also called Battleship
§        Two ships of 3 consecutive cells each- also called Cruiser
§        Three ships of 2 consecutive cells each- also called Destroyer
§        Four ships of 1 cell each- also called Submarine
3.                  Each Ship may be horizontally or vertically
4.                  No ship touch another, not even diagonally
5.                  The ships are hidden- You need to find their exact location

Initial Data:
6.                  Numbers on the grid:
§        Right- represents number of cells in the same row belongs to a ship(s).
§        Bottom- represents number of cells in the same column belongs to a ship(s).
7.                  You may get a cell already marked as water and not part of a ship
8.                  You may get a cell already marked as part of a ship.
9.                  Following rule 8, an important information is the shape of the marked cell
§        If it is rounded- we know for sure it is a Submarine
§        If it is squared- We know it is part of Cruiser or Battleship
§        If it is half rounder and half Squared- We know it is either the start or the end of Battleship, Cruiser or Destroyer.

My code should get from the newspaper the initial data as seen in the above attached example, analyze the data and eventually output the solution for that puzzle.

This is the solution for the above example-

I am sure many of you, Python coders, already thinking about the code and even may have a strong lead to the answer.
I know it will take me more then others and I am planning to share with you the steps I am taking until the final solution.

I will appreciate tips and leads from you guys to help me going through this task.

Next post I will share with you how I am going to attack the problem, where do I see the obstacles and will try to design the code in human words.

Stay Tune

No comments:

Post a Comment