Warning

# 01 Intro and Solvability

We log on, discuss the rules, answer first questions, create first simple Python program.

Reading about Python and programming essentials from the Programming Essentials course. Tutorials help with setting Python, discuss how to upload homework correctly, and add some class examples and some most frequent Python modules.

After solving the logging issues we will go into Objective Python. We will program the NPuzzle class that would represent the classical n-1 puzzle. Necessary basis will be explained at the first lecture. Python is well on-line documented/discussed, in case of problems, google is your friend.

## Quiz for bonus points: During lab OR uploaded on BRUTE within the day

• Solvable and unsolvable puzzle states
• 0.5 points
• submit your solution to BRUTE lab01quiz by February 14, midnight
• format: text file, photo of your solution on paper, pdf - what is convenient for you
• solution will be discussed on the next lab
• quiz assignment: [Students with their family name from A to L (included) should draw solvable 4-1 puzzles configurations The other half, students with a family name from M to Z have to draw unsolvable 4-1 puzzles configurations.]

## NPuzzle class

Besides the constructor __init__() we will need a few basic methods, namely reset(), visualise(), and read_tile(). You can create some auxiliary methods/function as it suits to you.

class NPuzzle:
'''
sliding puzzle class of a general size
https://en.wikipedia.org/wiki/15_puzzle
'''
def __init__(self, size):
'''
create the list of symbols, typically from 1 to 8 or 15. Empty tile
is represented by None
:param size: the board will be size x size,
size=3 - 8-puzzle; 4 - 15 puzzle
'''

def reset(self):
'''
initialize the board by a random shuffle of symbols
:return: None
'''

def visualise(self):
'''
just print itself to the standard output
:return: None
'''

'''
returns a symbol on row, col position
:param row: index of the row
:param column: index of the column
:return: value of the tile - int 1 to size^2-1, None for empty tile
The function raises IndexError exception if outside the board
'''

## Bonus Programming task - solvability n-1 puzzle

Is the given configuration/state solvable at all? You can use also the code from npuzzle.py and compare it to your solution of npuzzle.py.

The task is to implement a method is_solvable(env), which decides whether a given env (NPuzzle instance) is solvable or not. Implement the function in a module named solvability_check.py and upload it to BRUTE. A correct solution is worth 2 points.

Example structure of (solvability_check.py):

import npuzzle

def is_solvable(env):
'''
True or False?
'''

if __name__=="__main__": # testing suite
env = npuzzle.NPuzzle(3) # instance of NPuzzle class
env.reset()              # random shuffle
env.visualise()          # just to show the board
# just check
print(is_solvable(env))  # should output True or False

Basic communication with the NPuzzle object is through the function env.read_tile(row, col), which returns the value of the tile; None for empty or an exception is generated (IndexError) in case the index is out of range. An example is provided in npuzzle.py

When done, upload to the BRUTE

## Mandatory Assignment: searching in a maze

This assignment is not meant to be started on the first week, as it will be related to the second and third lecture and labs, but if you can do the bonus N-puzzle assignment fast enough and if you are used to Python, you can start working on the first mandatory assignment.

The full description of the assignment is here Search (1st assignment). In a maze environment, you will program the algorithm to find the shortest path. Do not forget that the cost of transition between different positions does not have to be the same. 