Skip to content

Commit afed867

Browse files
committed
Refine verification of cycle finder
1 parent 93306b4 commit afed867

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

source-code/design-patters/strategy_design_pattern.ipynb

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@
786786
},
787787
{
788788
"cell_type": "code",
789-
"execution_count": 31,
789+
"execution_count": 56,
790790
"id": "e7368cf0",
791791
"metadata": {},
792792
"outputs": [],
@@ -796,17 +796,17 @@
796796
},
797797
{
798798
"cell_type": "code",
799-
"execution_count": 32,
799+
"execution_count": 57,
800800
"id": "91426fd9-654b-48e7-a77d-0c78ff248c9e",
801801
"metadata": {},
802802
"outputs": [
803803
{
804804
"data": {
805805
"text/plain": [
806-
"[0, 0, 1, 0, 0, 1, 1, 0, 1, 1]"
806+
"[1, 0, 1, 0, 1, 0, 1, 0, 1, 1]"
807807
]
808808
},
809-
"execution_count": 32,
809+
"execution_count": 57,
810810
"metadata": {},
811811
"output_type": "execute_result"
812812
}
@@ -817,7 +817,7 @@
817817
},
818818
{
819819
"cell_type": "code",
820-
"execution_count": 33,
820+
"execution_count": 58,
821821
"id": "acd88973-5f07-4565-a4fd-ca4fc732b425",
822822
"metadata": {},
823823
"outputs": [],
@@ -827,7 +827,7 @@
827827
},
828828
{
829829
"cell_type": "code",
830-
"execution_count": 34,
830+
"execution_count": 59,
831831
"id": "8be1959e-e0fb-4daa-8ccc-0ab5d083e8a3",
832832
"metadata": {},
833833
"outputs": [],
@@ -837,7 +837,7 @@
837837
},
838838
{
839839
"cell_type": "code",
840-
"execution_count": 35,
840+
"execution_count": 60,
841841
"id": "3d62e21f-e93c-4870-9c56-6610cfaeb5b9",
842842
"metadata": {},
843843
"outputs": [],
@@ -847,7 +847,7 @@
847847
},
848848
{
849849
"cell_type": "code",
850-
"execution_count": 36,
850+
"execution_count": 61,
851851
"id": "128a85b0-2626-431a-adde-137f3738eb8f",
852852
"metadata": {},
853853
"outputs": [
@@ -857,7 +857,7 @@
857857
"(1, 10)"
858858
]
859859
},
860-
"execution_count": 36,
860+
"execution_count": 61,
861861
"metadata": {},
862862
"output_type": "execute_result"
863863
}
@@ -868,28 +868,28 @@
868868
},
869869
{
870870
"cell_type": "code",
871-
"execution_count": 37,
871+
"execution_count": 62,
872872
"id": "92d7262c-88b3-46c9-87b9-813144df30da",
873873
"metadata": {},
874874
"outputs": [
875875
{
876876
"data": {
877877
"text/plain": [
878-
"[[0, 0, 1, 0, 0, 1, 1, 0, 1, 1],\n",
879-
" [0, 1, 1, 0, 1, 1, 0, 1, 1, 0],\n",
880-
" [1, 1, 0, 1, 1, 0, 1, 1, 0, 0],\n",
881-
" [1, 0, 1, 1, 0, 1, 1, 0, 0, 1],\n",
882-
" [0, 1, 1, 0, 1, 1, 0, 0, 1, 1],\n",
883-
" [1, 1, 0, 1, 1, 0, 0, 1, 1, 0],\n",
884-
" [1, 0, 1, 1, 0, 0, 1, 1, 0, 1],\n",
885-
" [0, 1, 1, 0, 0, 1, 1, 0, 1, 1],\n",
886-
" [1, 1, 0, 0, 1, 1, 0, 1, 1, 0],\n",
887-
" [1, 0, 0, 1, 1, 0, 1, 1, 0, 1],\n",
888-
" [0, 0, 1, 1, 0, 1, 1, 0, 1, 1],\n",
889-
" [0, 1, 1, 0, 1, 1, 0, 1, 1, 0]]"
878+
"[[1, 0, 1, 0, 1, 0, 1, 0, 1, 1],\n",
879+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],\n",
880+
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
881+
" [1, 0, 0, 1, 1, 1, 1, 1, 1, 1],\n",
882+
" [0, 0, 1, 1, 0, 0, 0, 0, 0, 0],\n",
883+
" [1, 1, 1, 0, 0, 1, 1, 1, 1, 1],\n",
884+
" [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],\n",
885+
" [1, 1, 1, 1, 1, 0, 0, 1, 1, 1],\n",
886+
" [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],\n",
887+
" [1, 1, 1, 1, 1, 1, 1, 0, 0, 1],\n",
888+
" [0, 0, 0, 0, 0, 0, 0, 0, 1, 1],\n",
889+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0]]"
890890
]
891891
},
892-
"execution_count": 37,
892+
"execution_count": 62,
893893
"metadata": {},
894894
"output_type": "execute_result"
895895
}
@@ -903,26 +903,28 @@
903903
"id": "3da8dbca-2170-47c6-8de1-aadf6ad6a857",
904904
"metadata": {},
905905
"source": [
906-
"Just to make sure we define a function that verifies the result. The generations prior to the start index should not occur in the history, the one at the start index should be equal to the last generation in the history."
906+
"Just to make sure we define a function that verifies the result. The generations prior to the start index should not occur in the history, the one at the start index should be equal to the last generation in the history. Finally, none of the other generations should occur more than once."
907907
]
908908
},
909909
{
910910
"cell_type": "code",
911-
"execution_count": 38,
911+
"execution_count": 65,
912912
"id": "7a60eae7-3ee5-4c2f-a334-a7629fd5b5dd",
913913
"metadata": {},
914914
"outputs": [],
915915
"source": [
916916
"def verify_cycle_strategy(strategy, result):\n",
917917
" start_idx, cycle_length = result\n",
918-
" for idx in range(start_idx):\n",
919-
" assert strategy._history[idx] not in strategy._history[start_idx:]\n",
920-
" assert strategy._history[start_idx] == strategy._history[-1]"
918+
" for idx, automaton in enumerate(strategy._history[:-1]):\n",
919+
" if idx == start_idx:\n",
920+
" assert automaton == strategy._history[-1]\n",
921+
" else:\n",
922+
" assert automaton not in strategy._history[idx + 1:] "
921923
]
922924
},
923925
{
924926
"cell_type": "code",
925-
"execution_count": 39,
927+
"execution_count": 66,
926928
"id": "72297084-e9a7-494e-81c7-3b936b60471b",
927929
"metadata": {},
928930
"outputs": [],
@@ -940,17 +942,17 @@
940942
},
941943
{
942944
"cell_type": "code",
943-
"execution_count": 40,
945+
"execution_count": 67,
944946
"id": "f46f2070-1a3a-40b9-a718-22ee084cff44",
945947
"metadata": {},
946948
"outputs": [
947949
{
948950
"data": {
949951
"text/plain": [
950-
"[<matplotlib.lines.Line2D at 0x7f6fd6a90400>]"
952+
"[<matplotlib.lines.Line2D at 0x7f6fe42ee800>]"
951953
]
952954
},
953-
"execution_count": 40,
955+
"execution_count": 67,
954956
"metadata": {},
955957
"output_type": "execute_result"
956958
},

0 commit comments

Comments
 (0)