Python Questions
Python Questions
Objectives
After this workshop, you should be able to:
solve simple problems by recursive functions
implement and understand the difference between depth-first and breadth-first search
MARKS: Marks for this workshop will be determined by correctness of implementation as well as meaningful
attempt to solve the conceptual tasks as specified in the instructions.
Part C: Palindromes
Write a recursive function is pal(string) to check if a string is palindrome. A palindrome string is a string
that reads the same form either direction. For example:
>>> is_pal ( ' aa ' )
True
>>> is_pal ( ' aabb ' )
False
>>> is_pal ( ' aba ' )
True
1
>>> import graphs
>>> g = graphs.ex_tree
>>> graphs.print_grid_traversal(g, 10, [0, 1, 2, 12])
000---001---002 ***---*** ***---*** ***---***---***
| | | | |
***---***---003---***---***---***---***---***---*** ***
| | | | | |
***---***---***---***---*** *** *** *** *** ***
| | | | |
*** *** ***---***---***---*** ***---*** ***---***
| | | |
*** ***---*** *** ***---***---***---***---***---***
>>> graphs.print_grid_traversal(g, 10, bfs_traversal(g, 0))
000---001---002 008---013 019---026 033---038---042
| | | | |
007---004---003---005---009---014---020---027---034 045
| | | | | |
015---010---006---011---017 021 028 035 039 048
| | | | |
022 016 012---018---024---031 036---040 043---046
| | | |
029 023---030 025 032---037---041---044---047---049
>>> graphs.print_grid_traversal(g, 10, dfs_traversal(g, 0))
000---001---002 046---047 044---045 039---040---041
| | | | |
049---048---003---025---026---027---029---033---035 042
| | | | | |
022---018---004---016---017 028 030 034 036 043
| | | | |
023 019 005---006---008---015 031---032 037---038
| | | |
024 020---021 007 009---010---011---012---013---014
Do you recognise the dfs and bfs patterns in the printed traversal order? Try to predict what will happen if
you start the traversal in a different vertex than 0. Test your prediction by visualising the traversals with the
above function.
2
>>> print_grid_traversal(g, 10, dfs_traversal(g, 0, {9, 39}))
000---001---002 ***---*** ***---*** ***---***---***
| | | | |
***---***---003---025---026---027---029---033---035 ***
| | | | | |
022---018---004---016---017 028 030 034 036 ***
| | | | |
023 019 005---006---008---015 031---032 037---038
| | | |
024 020---021 007 009---010---011---012---013---014
>>> print_grid_traversal(g, 10, dfs_path(g, 0, {9, 39}))
000---001---002 ***---*** ***---*** ***---***---***
| | | | |
***---***---003---004---005---006---007---008---009 ***
| | | | | |
***---***---***---***---*** *** *** *** 010 ***
| | | | |
*** *** ***---***---***---*** ***---*** 011---012
| | | |
*** ***---*** *** ***---***---***---***---***---***
Can you find inputs (by varying the input graph, the starting vertex and/or the goal vertices) such that the bfs
path is shorter than the dfs path (although the corresponding dfs traversal is shorter than the bfs traversal)?
Discuss your findings in a short text in the doc string at the end of your module file. Include
visualisations of your discovered example inputs by copying the corresponding output of the
print grid traversal function.