Skip to content

Commit 9f66fe6

Browse files
thesagarsehgalantmarakis
authored andcommitted
some optimizations in knowledge.py (aimacode#1034)
1 parent 269786c commit 9f66fe6

3 files changed

Lines changed: 5 additions & 12 deletions

File tree

knowledge.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
# ______________________________________________________________________________
1313

1414

15-
def current_best_learning(examples, h, examples_so_far=None):
15+
def current_best_learning(examples, h, examples_so_far=[]):
1616
""" [Figure 19.2]
1717
The hypothesis is a list of dictionaries, with each dictionary representing
1818
a disjunction."""
1919
if not examples:
2020
return h
2121

22-
examples_so_far = examples_so_far or []
2322
e = examples[0]
2423
if is_consistent(e, h):
2524
return current_best_learning(examples[1:], h, examples_so_far + [e])

knowledge_current_best.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@
654654
"name": "python",
655655
"nbconvert_exporter": "python",
656656
"pygments_lexer": "ipython3",
657-
"version": "3.6.5"
657+
"version": "3.6.7"
658658
}
659659
},
660660
"nbformat": 4,

tests/test_knowledge.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,21 @@ def test_current_best_learning():
5959
examples = restaurant
6060
hypothesis = [{'Alt': 'Yes'}]
6161
h = current_best_learning(examples, hypothesis)
62-
values = []
63-
for e in examples:
64-
values.append(guess_value(e, h))
62+
values = [guess_value(e, h) for e in examples]
6563

6664
assert values == [True, False, True, True, False, True, False, True, False, False, False, True]
6765

6866
examples = animals_umbrellas
6967
initial_h = [{'Species': 'Cat'}]
7068
h = current_best_learning(examples, initial_h)
71-
values = []
72-
for e in examples:
73-
values.append(guess_value(e, h))
69+
values = [guess_value(e, h) for e in examples]
7470

7571
assert values == [True, True, True, False, False, False, True]
7672

7773
examples = party
7874
initial_h = [{'Pizza': 'Yes'}]
7975
h = current_best_learning(examples, initial_h)
80-
values = []
81-
for e in examples:
82-
values.append(guess_value(e, h))
76+
values = [guess_value(e, h) for e in examples]
8377

8478
assert values == [True, True, False]
8579

0 commit comments

Comments
 (0)