I have been working as QA for 14 years, with 8 of those years as a manager for some security's companies. It is time to re-invent myself. Hod has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

My Journey into Python from Scratch - Taking the Battleship Shortcut

04.19.2012
| 5842 views |
  • submit to reddit

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 then the last one which was the Python RegEx part. During this time I got many visitors and upvotes, I received many comments and emails with advice and nice words, and eventually the leading technology blogging site DZone has selected me to be part of their group of unique and 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 slowly and the reasons for that are two major factors: I am a father for two young children and now in the Winter/Spring time they are often sick.  I also have a full time job doing my QA Manager position. It is very hard to learn when you have such huge gaps between each time you try to study, in addition to the late time at night, which is the only time available to me.

So I did some brainstorming (Me and Myself) and decided 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 haev decided to create a project.  This project will be hard enough to solve in a 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, my knowledge of Python will be way more advanced than my original step-by-step plan.  And lastly, it will be more fun than studying 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 a 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 Destroye
  • Four ships of 1 cell each - also called Submarine

3.  Each Ship may be horizontally or vertically
4.  No ships can touch one 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 the number of cells in the same row that belong to a ship(s).
  • Bottom - represents the number of cells in the same column that belong 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 piece of 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 a Cruiser or Battleship
  • If it is half rounded 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 are already thinking about the code and even may have a strong lead to the answer. I know it will take me more time than 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 get through this task. In the next post, I will share with you how I am going to attack the problem, what the obstacles will be, and I will try to design the code in human words.

Stay Tuned!


Published at DZone with permission of its author, Hod Benbinyamin. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)