Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AI Progamming Exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michael Mutote
AI Progamming Exercises
Commits
fd8d08c1
Commit
fd8d08c1
authored
1 year ago
by
Michael Mutote
Browse files
Options
Downloads
Patches
Plain Diff
22202956
ex 4.9 q 4 had to be made into backtracking
parent
8caf06a5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
4_9_Exercises/Question 4.py
+41
-9
41 additions, 9 deletions
4_9_Exercises/Question 4.py
Search_Algorithms/solution testing.py
+10
-9
10 additions, 9 deletions
Search_Algorithms/solution testing.py
with
51 additions
and
18 deletions
4_9_Exercises/Question 4.py
+
41
−
9
View file @
fd8d08c1
def
np
(
x
,
y
,
trace
=
None
):
import
time
def
np
(
x
,
y
):
trace
=
[[(
0
,
0
)]]
final
=
[]
while
trace
:
new_pos
=
[]
current
=
trace
.
pop
(
-
1
)
last_position
=
current
[
-
1
]
if
last_position
==
(
x
,
y
):
final
.
append
(
current
)
else
:
new_pos
.
append
((
last_position
[
0
]
+
1
,
last_position
[
1
]))
new_pos
.
append
((
last_position
[
0
],
last_position
[
1
]
+
1
))
new_pos
.
append
((
last_position
[
0
]
-
1
,
last_position
[
1
]))
new_pos
.
append
((
last_position
[
0
],
last_position
[
1
]
-
1
))
for
pos
in
new_pos
:
if
(
pos
not
in
current
)
and
((
0
<=
pos
[
0
]
<=
x
)
and
(
0
<=
pos
[
1
]
<=
y
)):
trace
.
append
(
current
+
[
pos
])
return
final
def
np2
(
x
,
y
,
trace
=
None
):
# Old version was done by recursion and is functional
if
trace
is
None
:
trace
=
[(
0
,
0
)]
current
=
trace
[
-
1
]
...
...
@@ -7,13 +31,9 @@ def np(x, y, trace=None):
if
current
[
0
]
>
x
or
current
[
1
]
>
y
or
current
[
0
]
<
0
or
current
[
1
]
<
0
:
return
0
if
(
current
[
0
],
current
[
1
])
==
(
x
,
y
)
and
len
(
trace
)
>
1
:
# print(current[0], x, current[1], y, " this is in success")
# print(trace)
return
1
if
x
+
y
<
2
:
return
1
# if (x, y) in new_cache:
# return new_cache[(x, y)]
x_1
,
y_1
=
(
current
[
0
],
current
[
1
]
+
1
)
if
current
[
1
]
<
y
else
(
current
[
0
],
current
[
1
])
x_2
,
y_2
=
(
current
[
0
]
+
1
,
current
[
1
])
if
current
[
0
]
<
x
else
(
current
[
0
],
current
[
1
])
x_3
,
y_3
=
current
[
0
]
-
1
,
current
[
1
]
...
...
@@ -24,7 +44,19 @@ def np(x, y, trace=None):
trace_3
=
trace
+
[(
x_3
,
y_3
)]
trace_4
=
trace
+
[(
x_4
,
y_4
)]
return
np
(
x
,
y
,
trace_1
)
+
\
np
(
x
,
y
,
trace_2
)
+
\
np
(
x
,
y
,
trace_3
)
+
\
np
(
x
,
y
,
trace_4
)
return
np2
(
x
,
y
,
trace_1
)
+
\
np2
(
x
,
y
,
trace_2
)
+
\
np2
(
x
,
y
,
trace_3
)
+
\
np2
(
x
,
y
,
trace_4
)
# for fun, I timed the two to see which is faster. both almost the same
# start_time = time.time()
# np2(3, 7)
# end_time = time.time()
# print(end_time - start_time)
# start_time = time.time()
# np(3, 7)
# end_time = time.time()
# print(end_time - start_time)
This diff is collapsed.
Click to expand it.
Search_Algorithms/solution testing.py
+
10
−
9
View file @
fd8d08c1
...
...
@@ -30,15 +30,16 @@ for rows in sln:
print
(
rows
)
# N Queens
board
=
((
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
))
board
=
((
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
))
sln
=
(
Search_Algorithms
.
BreadthFirstSearch
(
board
,
Sucessors
.
queens_successor
,
is_goal
.
is_goal_queens
)[
-
1
])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment