mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-06 08:45:11 -04:00
Compare commits
725 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c1935a5723 | ||
![]() |
f51e2f289a | ||
![]() |
454f989928 | ||
![]() |
7a42c72a71 | ||
![]() |
8e2ee50f0e | ||
![]() |
4b58e7b308 | ||
![]() |
20771919fb | ||
![]() |
2178f880ee | ||
![]() |
ed6bc75d3e | ||
![]() |
93a3d07928 | ||
![]() |
faa7b70a00 | ||
![]() |
5108f546c6 | ||
![]() |
0264d26b7e | ||
![]() |
fcd4eb423b | ||
![]() |
14404c47c8 | ||
![]() |
bbdd47ffd3 | ||
![]() |
b9318bf6ea | ||
![]() |
bc8ff138e3 | ||
![]() |
86b7f9ce18 | ||
![]() |
2ef7451aba | ||
![]() |
3d8e44eef9 | ||
![]() |
802c4c9d5a | ||
![]() |
39a321a8e0 | ||
![]() |
039bc676f7 | ||
![]() |
eea51a4809 | ||
![]() |
65da6b0c55 | ||
![]() |
7341955f63 | ||
![]() |
ef51d31441 | ||
![]() |
74b313da96 | ||
![]() |
0225dba9ab | ||
![]() |
215115e76b | ||
![]() |
3d2fda8696 | ||
![]() |
567efaaa42 | ||
![]() |
a684d7989e | ||
![]() |
7b32dbb0d7 | ||
![]() |
ac1f70506a | ||
![]() |
ba0780c240 | ||
![]() |
044afb8d21 | ||
![]() |
a0d31643e4 | ||
![]() |
4e895726bd | ||
![]() |
728b441734 | ||
![]() |
a97c06f579 | ||
![]() |
761e86705f | ||
![]() |
bd2fb7fb45 | ||
![]() |
544f5e8f4b | ||
![]() |
f02da2c80e | ||
![]() |
66cf6cc427 | ||
![]() |
f75b102b78 | ||
![]() |
bf35809bb2 | ||
![]() |
e7d19505dc | ||
![]() |
9077029939 | ||
![]() |
e8c7b716af | ||
![]() |
3d8b77bf80 | ||
![]() |
4002609137 | ||
![]() |
09b98e3740 | ||
![]() |
fde384b038 | ||
![]() |
0f85ea546c | ||
![]() |
d574e9627d | ||
![]() |
93a6452f57 | ||
![]() |
cc05ef2ea8 | ||
![]() |
c8fad05719 | ||
![]() |
f117ec52e5 | ||
![]() |
1b5c749a50 | ||
![]() |
92a74485ec | ||
![]() |
d1123f8c75 | ||
![]() |
e0ef50da5b | ||
![]() |
f8dc224ae5 | ||
![]() |
6f312b7631 | ||
![]() |
cb2909d421 | ||
![]() |
a3ce5db3a5 | ||
![]() |
122a535241 | ||
![]() |
27f5ce1c6f | ||
![]() |
ef073d11ca | ||
![]() |
d50ed323fd | ||
![]() |
8bc3090801 | ||
![]() |
f08037aeb8 | ||
![]() |
23c20d4aaa | ||
![]() |
5d56bb3855 | ||
![]() |
cba141be6a | ||
![]() |
75bb0277cd | ||
![]() |
39922339a3 | ||
![]() |
f7dc8af9c4 | ||
![]() |
bb47074bdd | ||
![]() |
94de93780a | ||
![]() |
b61c58b158 | ||
![]() |
dd98393aa4 | ||
![]() |
e5efd5424a | ||
![]() |
83fc152596 | ||
![]() |
b91db08a40 | ||
![]() |
ff3987dffc | ||
![]() |
15d407b4ef | ||
![]() |
a42c13186c | ||
![]() |
5eba7a1a80 | ||
![]() |
f7bc342551 | ||
![]() |
ba1a8755f0 | ||
![]() |
f1a8659193 | ||
![]() |
1044a13ad9 | ||
![]() |
95fd556031 | ||
![]() |
f41011e849 | ||
![]() |
153e85ca03 | ||
![]() |
3606be45c1 | ||
![]() |
faeb83a072 | ||
![]() |
ffe28f3d8e | ||
![]() |
66060486a4 | ||
![]() |
a78436b51b | ||
![]() |
91cd6134b6 | ||
![]() |
b5c71cf3d5 | ||
![]() |
096185a6e8 | ||
![]() |
0e2b6b39b0 | ||
![]() |
9486e25675 | ||
![]() |
b1a9367ee4 | ||
![]() |
eedb6b6863 | ||
![]() |
cd5ac00126 | ||
![]() |
cf170e3d57 | ||
![]() |
3e375366ae | ||
![]() |
5844ecc8bf | ||
![]() |
f2b423db58 | ||
![]() |
b6faf3956b | ||
![]() |
f2091f10d2 | ||
![]() |
ff73b28ae7 | ||
![]() |
79872f08eb | ||
![]() |
e25dd1f601 | ||
![]() |
3b3a40d782 | ||
![]() |
c4e0cdc9ff | ||
![]() |
56af227e6e | ||
![]() |
b52c7e0d03 | ||
![]() |
e63cfe856f | ||
![]() |
855ecff683 | ||
![]() |
e372bbe6f7 | ||
![]() |
8ef56f3b77 | ||
![]() |
fa5bba3881 | ||
![]() |
8857af6e97 | ||
![]() |
35fb490c38 | ||
![]() |
1ac9dbd6d5 | ||
![]() |
ee9384b374 | ||
![]() |
40a4976c5f | ||
![]() |
6f1dd06515 | ||
![]() |
06e19d394b | ||
![]() |
813cd4db01 | ||
![]() |
dbcf75bcac | ||
![]() |
a4fc7156af | ||
![]() |
8da5fea499 | ||
![]() |
7e40ca15da | ||
![]() |
5f44f6b89f | ||
![]() |
af264699b7 | ||
![]() |
db7be11ea0 | ||
![]() |
2c1df477e2 | ||
![]() |
5ab9d7582a | ||
![]() |
c176257c26 | ||
![]() |
f895ac1da8 | ||
![]() |
18c33edfb0 | ||
![]() |
ad2c24a519 | ||
![]() |
afe695c69f | ||
![]() |
71ca605489 | ||
![]() |
3d361330b7 | ||
![]() |
6ed5cb896b | ||
![]() |
27bc931a53 | ||
![]() |
a8e6ec8884 | ||
![]() |
66a2a3ad3a | ||
![]() |
f55ce43de3 | ||
![]() |
a079cef3d2 | ||
![]() |
975b4d7dbd | ||
![]() |
fd439a2d84 | ||
![]() |
87f52134da | ||
![]() |
60c86f0759 | ||
![]() |
7d89916381 | ||
![]() |
e6b5af60e3 | ||
![]() |
fd291fa48b | ||
![]() |
b9b5d8e21c | ||
![]() |
e54535ae2b | ||
![]() |
a37d96b756 | ||
![]() |
a4fe8d519e | ||
![]() |
0b6dbbaa27 | ||
![]() |
250e136166 | ||
![]() |
b14d9fcee2 | ||
![]() |
2d0842019d | ||
![]() |
2fe4c94c7f | ||
![]() |
b81ccd579b | ||
![]() |
2f1db3f84e | ||
![]() |
86a538032a | ||
![]() |
761c1f0670 | ||
![]() |
5c0e0228e6 | ||
![]() |
37a25d4423 | ||
![]() |
c73adbc7b8 | ||
![]() |
7e6f0817be | ||
![]() |
cf27a0741e | ||
![]() |
15135b757e | ||
![]() |
17f53eb593 | ||
![]() |
6dbd139af1 | ||
![]() |
3d8721aaeb | ||
![]() |
9a820d8599 | ||
![]() |
5d9313dd85 | ||
![]() |
3215e25e12 | ||
![]() |
e1e46e59c8 | ||
![]() |
d61684057c | ||
![]() |
e84d63ff68 | ||
![]() |
0aa2a467e1 | ||
![]() |
d407a66536 | ||
![]() |
4ed4ef89c6 | ||
![]() |
56c0e95516 | ||
![]() |
784b8a8698 | ||
![]() |
bce645eb9e | ||
![]() |
6b3923e32c | ||
![]() |
aca9429097 | ||
![]() |
2861501448 | ||
![]() |
240d008647 | ||
![]() |
2f8af300a6 | ||
![]() |
178b3aea34 | ||
![]() |
e65d65d6eb | ||
![]() |
00dada5ff3 | ||
![]() |
95f73c5771 | ||
![]() |
d1e0aa2a52 | ||
![]() |
38f6c1491a | ||
![]() |
dd2462222f | ||
![]() |
0f92fc245c | ||
![]() |
e01b5b5e2a | ||
![]() |
85f1f87c8d | ||
![]() |
ea12ccce74 | ||
![]() |
0389271f6b | ||
![]() |
d8d1887a85 | ||
![]() |
f6e853a5ab | ||
![]() |
544caafaa3 | ||
![]() |
8b3feb691e | ||
![]() |
69d068678b | ||
![]() |
93c18361f3 | ||
![]() |
4a03488022 | ||
![]() |
60b5ec4a88 | ||
![]() |
b16196e810 | ||
![]() |
3024f285e5 | ||
![]() |
8d8b3f7e53 | ||
![]() |
08a29ececf | ||
![]() |
e06d9fbbf6 | ||
![]() |
1c51827e08 | ||
![]() |
907bf738ec | ||
![]() |
35c888571e | ||
![]() |
e333844d4d | ||
![]() |
e80361656e | ||
![]() |
18a5fbd75c | ||
![]() |
a2f5521bd3 | ||
![]() |
0e5c36f426 | ||
![]() |
b7475a8481 | ||
![]() |
cd2e4557dc | ||
![]() |
390316781e | ||
![]() |
7da5b84f60 | ||
![]() |
1821dd41e0 | ||
![]() |
74f6ef2913 | ||
![]() |
d7280726ad | ||
![]() |
18389003c4 | ||
![]() |
86d533c4bf | ||
![]() |
feb21289db | ||
![]() |
c041716806 | ||
![]() |
141a6b5944 | ||
![]() |
e95aa5a731 | ||
![]() |
faf6e7321e | ||
![]() |
fb6507ce38 | ||
![]() |
39262bfbe4 | ||
![]() |
05ffc6e0b7 | ||
![]() |
eb7a7af574 | ||
![]() |
072febeb9c | ||
![]() |
50c188a88c | ||
![]() |
817cbb57a2 | ||
![]() |
177914c7be | ||
![]() |
46b4f11520 | ||
![]() |
75eb7e2dd9 | ||
![]() |
50436468d3 | ||
![]() |
270783268c | ||
![]() |
fab5bed80f | ||
![]() |
e3e5e4bf35 | ||
![]() |
15e4b6068c | ||
![]() |
5c2393dc07 | ||
![]() |
0c05504ae1 | ||
![]() |
e1a2904cfe | ||
![]() |
2fe870650b | ||
![]() |
8e8f934d12 | ||
![]() |
fb8081ae42 | ||
![]() |
419eeee0dc | ||
![]() |
169d2197c9 | ||
![]() |
070071e687 | ||
![]() |
9d1aa92451 | ||
![]() |
919111cd22 | ||
![]() |
4aba894c46 | ||
![]() |
84b35ceaf3 | ||
![]() |
7520042ef9 | ||
![]() |
ca5c15c9b6 | ||
![]() |
7d6d6189e5 | ||
![]() |
5159d766d9 | ||
![]() |
e7284b27e2 | ||
![]() |
a0b0090f8a | ||
![]() |
e0d9624032 | ||
![]() |
57075bd388 | ||
![]() |
0871054dd3 | ||
![]() |
5b67529485 | ||
![]() |
9cfd35035a | ||
![]() |
753ce86ad2 | ||
![]() |
1a9f793b21 | ||
![]() |
51e6497cef | ||
![]() |
a2dce9493e | ||
![]() |
abbad25ed8 | ||
![]() |
3d8bf7d339 | ||
![]() |
b69bc7db19 | ||
![]() |
4475910493 | ||
![]() |
ff1a7e5d51 | ||
![]() |
5b38a549a7 | ||
![]() |
1f23338150 | ||
![]() |
bcc9398a27 | ||
![]() |
02c26f8d8a | ||
![]() |
d357be4e52 | ||
![]() |
2e7f4a9410 | ||
![]() |
7bc4115287 | ||
![]() |
d3143cc5fb | ||
![]() |
908352604b | ||
![]() |
0ba5a05762 | ||
![]() |
96607ab5e2 | ||
![]() |
e7995a9461 | ||
![]() |
c3e12c40a4 | ||
![]() |
384b79adf8 | ||
![]() |
4d2e7857b2 | ||
![]() |
28d28b817e | ||
![]() |
52cba3f861 | ||
![]() |
e1805c9fb5 | ||
![]() |
b068e60212 | ||
![]() |
e5c9084b64 | ||
![]() |
e085d74110 | ||
![]() |
3d5ed34eab | ||
![]() |
0ea70e2122 | ||
![]() |
57ddd32859 | ||
![]() |
2c1c7c0ff7 | ||
![]() |
fb7059d0e6 | ||
![]() |
fd5bbaee8d | ||
![]() |
f567a5a594 | ||
![]() |
7d62415e5f | ||
![]() |
98ece41e3b | ||
![]() |
4a762cf606 | ||
![]() |
50409a0e85 | ||
![]() |
9441428acf | ||
![]() |
5ea9c0bbbf | ||
![]() |
79fc85b28d | ||
![]() |
f5a30a6176 | ||
![]() |
82ac14af5d | ||
![]() |
397402f144 | ||
![]() |
c004a953a9 | ||
![]() |
17ca5877bf | ||
![]() |
31c7cc4654 | ||
![]() |
eb36c2249d | ||
![]() |
654bb8c76f | ||
![]() |
5eaeb66493 | ||
![]() |
5ea3de0fc9 | ||
![]() |
67656b197d | ||
![]() |
4e82aa6060 | ||
![]() |
94db93ff42 | ||
![]() |
296c30779b | ||
![]() |
8509c2f7a1 | ||
![]() |
4ce3ef0daa | ||
![]() |
586803c39b | ||
![]() |
2ac7e9a678 | ||
![]() |
25054eabb1 | ||
![]() |
d6fedcee53 | ||
![]() |
3eb6a7c828 | ||
![]() |
c04d598bfa | ||
![]() |
29b7948275 | ||
![]() |
b53b78285d | ||
![]() |
f7779dc6e3 | ||
![]() |
4caa1fe7f4 | ||
![]() |
07f6564fe6 | ||
![]() |
179bbd568e | ||
![]() |
0588483a19 | ||
![]() |
8f4fc49e7a | ||
![]() |
f595c7b2cf | ||
![]() |
e8882c2da0 | ||
![]() |
cf0a3cd358 | ||
![]() |
041e403d46 | ||
![]() |
f5b741f0c4 | ||
![]() |
e47e3241b8 | ||
![]() |
5a3aaf58e3 | ||
![]() |
d27d42a800 | ||
![]() |
b38403f6de | ||
![]() |
52bf99bd0f | ||
![]() |
1ddc83c1ff | ||
![]() |
e0d4dd2066 | ||
![]() |
4530fcce5c | ||
![]() |
00d8c462bf | ||
![]() |
543e2d7d29 | ||
![]() |
743f091905 | ||
![]() |
eb45cdd938 | ||
![]() |
fb971d112c | ||
![]() |
3f8f299f37 | ||
![]() |
1295041070 | ||
![]() |
969435d08f | ||
![]() |
d4e17ad741 | ||
![]() |
84a9c9fb9d | ||
![]() |
04cd4a4b7b | ||
![]() |
0bc7deb3af | ||
![]() |
0cd3198d05 | ||
![]() |
8c631c5cd0 | ||
![]() |
b467dca561 | ||
![]() |
0963a43bcc | ||
![]() |
649a881647 | ||
![]() |
06f3c4dd1e | ||
![]() |
d0bbfed0e5 | ||
![]() |
5456593be9 | ||
![]() |
a449abf709 | ||
![]() |
2626cec18a | ||
![]() |
398c10877c | ||
![]() |
2396654457 | ||
![]() |
afdff04a69 | ||
![]() |
40c0dd37dd | ||
![]() |
914d1eda87 | ||
![]() |
65615f2f05 | ||
![]() |
48d1683be5 | ||
![]() |
16d09b5b0d | ||
![]() |
78f2ab992e | ||
![]() |
e15c16c55b | ||
![]() |
614f985f1a | ||
![]() |
40034ce8bd | ||
![]() |
84f2a8fe94 | ||
![]() |
eb7bc74843 | ||
![]() |
c2799fec3a | ||
![]() |
a853cb34c5 | ||
![]() |
50878d0b85 | ||
![]() |
a50bbdf518 | ||
![]() |
1202f248cf | ||
![]() |
06b74ecc34 | ||
![]() |
8f9fc12b05 | ||
![]() |
1fd4595e92 | ||
![]() |
a7864edb7f | ||
![]() |
b66625f149 | ||
![]() |
06d7a6633e | ||
![]() |
166afb1ce1 | ||
![]() |
f39587af7f | ||
![]() |
a1b611ac72 | ||
![]() |
9f532c9b01 | ||
![]() |
16a1b8b501 | ||
![]() |
7fdb76fce8 | ||
![]() |
321ef3b254 | ||
![]() |
d94f45a59a | ||
![]() |
8978a8b74c | ||
![]() |
913a26b906 | ||
![]() |
da47a56961 | ||
![]() |
cdfd761d87 | ||
![]() |
f5490128cd | ||
![]() |
22adc6f880 | ||
![]() |
c17c348eed | ||
![]() |
0033b2ee00 | ||
![]() |
0a2c958014 | ||
![]() |
a85ed914d3 | ||
![]() |
1d44d182e2 | ||
![]() |
696c045a16 | ||
![]() |
0ad1ab44be | ||
![]() |
f4e431412e | ||
![]() |
fa7c3cde66 | ||
![]() |
ec9ccd5dfd | ||
![]() |
402b6d3cf8 | ||
![]() |
2e37835477 | ||
![]() |
d982849688 | ||
![]() |
0ae7126e96 | ||
![]() |
fcdf1422ac | ||
![]() |
a9127ff3fc | ||
![]() |
50ba4aaed4 | ||
![]() |
03a836ca53 | ||
![]() |
cd0575f81e | ||
![]() |
e4bfc72334 | ||
![]() |
e522443df1 | ||
![]() |
cf32f0112c | ||
![]() |
45a3a9b050 | ||
![]() |
805e80144d | ||
![]() |
3fbab4d88f | ||
![]() |
e85b88725f | ||
![]() |
818626f205 | ||
![]() |
c87740bc66 | ||
![]() |
737898f55d | ||
![]() |
bed6d221fd | ||
![]() |
72c11a1b44 | ||
![]() |
f374afd0c7 | ||
![]() |
7a44401e4c | ||
![]() |
1d556cfd04 | ||
![]() |
0013f69950 | ||
![]() |
cd9326891f | ||
![]() |
e6f990cab6 | ||
![]() |
158eb74e76 | ||
![]() |
3b218fae01 | ||
![]() |
e96bf43463 | ||
![]() |
153b111e09 | ||
![]() |
896aaaf88f | ||
![]() |
0672ae9a3a | ||
![]() |
79f1970f2e | ||
![]() |
f519830497 | ||
![]() |
efd9f72f84 | ||
![]() |
24b38f0d5b | ||
![]() |
092700f566 | ||
![]() |
35b14ddfde | ||
![]() |
d5089d5b0e | ||
![]() |
93c8e2abf7 | ||
![]() |
f6477288e1 | ||
![]() |
6ed9a5376b | ||
![]() |
cd4d8dea44 | ||
![]() |
e7d855b99e | ||
![]() |
38cbe1c55b | ||
![]() |
0526bb7aee | ||
![]() |
23881f329f | ||
![]() |
f2418e56c1 | ||
![]() |
bc0d591be1 | ||
![]() |
48ce09742e | ||
![]() |
8d6fac8dc9 | ||
![]() |
e428fb1d50 | ||
![]() |
3bbe283cd7 | ||
![]() |
50e749dcc2 | ||
![]() |
16359103a9 | ||
![]() |
0ed8794fda | ||
![]() |
6dc689f3a0 | ||
![]() |
606784af38 | ||
![]() |
1f174755c9 | ||
![]() |
7f7d1bdf24 | ||
![]() |
918a38bc9e | ||
![]() |
df0c4465f2 | ||
![]() |
185bbbc341 | ||
![]() |
ef2a1cf18c | ||
![]() |
47085d5d94 | ||
![]() |
483e7c1805 | ||
![]() |
abc3e2abf8 | ||
![]() |
90ef21f8be | ||
![]() |
24df4c93d7 | ||
![]() |
f0c936fe6c | ||
![]() |
16f0dff34e | ||
![]() |
93b3587bca | ||
![]() |
2d481f78b0 | ||
![]() |
690d8bd706 | ||
![]() |
937183da0c | ||
![]() |
8a30000724 | ||
![]() |
132a5700c4 | ||
![]() |
621f32f4aa | ||
![]() |
006074a1c7 | ||
![]() |
cf5bcd8020 | ||
![]() |
06722c4871 | ||
![]() |
465e840600 | ||
![]() |
c1949996e4 | ||
![]() |
4b22537ee7 | ||
![]() |
c77eb0dd00 | ||
![]() |
f0f0be2375 | ||
![]() |
dcaef97146 | ||
![]() |
bd0a4c9ee3 | ||
![]() |
f32e58cc68 | ||
![]() |
84d1bf598d | ||
![]() |
9e938fa336 | ||
![]() |
c64f3318f8 | ||
![]() |
da0f3fd5d8 | ||
![]() |
bfeb0bf97c | ||
![]() |
cf4a797646 | ||
![]() |
135c6bd58a | ||
![]() |
5ec8587911 | ||
![]() |
ea039f5514 | ||
![]() |
bb72bab679 | ||
![]() |
48720ada4d | ||
![]() |
1caaed58a1 | ||
![]() |
bfcfec83b6 | ||
![]() |
ad4b836800 | ||
![]() |
cdae9ca4f7 | ||
![]() |
8a8884839d | ||
![]() |
2654ba1a75 | ||
![]() |
5a07abce93 | ||
![]() |
769129ecae | ||
![]() |
5c55b81641 | ||
![]() |
5732cfcc61 | ||
![]() |
0f455a0322 | ||
![]() |
f3ee2cdf52 | ||
![]() |
5af08f7498 | ||
![]() |
ea7a3fdfbb | ||
![]() |
8a9868fe7d | ||
![]() |
8e4f1f5f4f | ||
![]() |
ef55c8c8fd | ||
![]() |
6c7eae7715 | ||
![]() |
7403b5f900 | ||
![]() |
7c0583efbd | ||
![]() |
013c5cfb3a | ||
![]() |
35816e90f0 | ||
![]() |
0a13fcc208 | ||
![]() |
405421ec19 | ||
![]() |
c299610a4c | ||
![]() |
223ef93067 | ||
![]() |
90e734fe30 | ||
![]() |
3844a4352f | ||
![]() |
d116ed2935 | ||
![]() |
89c43077b7 | ||
![]() |
59dfa3d95b | ||
![]() |
eb4a362049 | ||
![]() |
a28975720d | ||
![]() |
c5311d2f42 | ||
![]() |
ed16bb8ed4 | ||
![]() |
234b4b1fc8 | ||
![]() |
600dcc25c7 | ||
![]() |
a82a93987b | ||
![]() |
96b0c06106 | ||
![]() |
dfa9c32576 | ||
![]() |
41756cb10c | ||
![]() |
1b969e7db3 | ||
![]() |
978a053fdb | ||
![]() |
f30b2c45ca | ||
![]() |
4263555391 | ||
![]() |
95864fe473 | ||
![]() |
87447ad43d | ||
![]() |
bb3ad8dcdc | ||
![]() |
003f5953ed | ||
![]() |
5fafebba15 | ||
![]() |
dc3ebf3173 | ||
![]() |
5fd154f3fd | ||
![]() |
70b22c1738 | ||
![]() |
cc27c43b91 | ||
![]() |
70b28d5622 | ||
![]() |
87fefbae69 | ||
![]() |
baac84ae3f | ||
![]() |
5581a47a1a | ||
![]() |
1963988230 | ||
![]() |
e303e3c507 | ||
![]() |
0c765993ce | ||
![]() |
bf7d1053df | ||
![]() |
c22225ec08 | ||
![]() |
28f35a0c2b | ||
![]() |
878cac0daf | ||
![]() |
afcc80bcac | ||
![]() |
eebc81ed07 | ||
![]() |
b4416bc8f0 | ||
![]() |
3c5ee16ff0 | ||
![]() |
397584b1de | ||
![]() |
e0eb554dfd | ||
![]() |
67419cf9de | ||
![]() |
09633fd797 | ||
![]() |
1ef64d6e17 | ||
![]() |
af25278fb3 | ||
![]() |
6ac18187a3 | ||
![]() |
2e96a35a0d | ||
![]() |
2fdaad121c | ||
![]() |
a8e71c50db | ||
![]() |
1b6f705e7c | ||
![]() |
fc419aadcc | ||
![]() |
8acbecd2b0 | ||
![]() |
9d671e5685 | ||
![]() |
286316a29f | ||
![]() |
8e9cbfd87f | ||
![]() |
a5b67b61c0 | ||
![]() |
1b336bf09a | ||
![]() |
d99d1d27dc | ||
![]() |
85ecba5ace | ||
![]() |
bb6afce467 | ||
![]() |
47e5ee3918 | ||
![]() |
9b4c8df7f7 | ||
![]() |
0d5e21b867 | ||
![]() |
fa83f7e42a | ||
![]() |
e04656170b | ||
![]() |
fb81535ffb | ||
![]() |
1d476139a8 | ||
![]() |
4d9c04e61d | ||
![]() |
ac67b30284 | ||
![]() |
3cc8c1d5c0 | ||
![]() |
1b5c89e75f | ||
![]() |
47321c03fc | ||
![]() |
ad67d2045f | ||
![]() |
6680aa6414 | ||
![]() |
2f4a9638c0 | ||
![]() |
6b6fe21db3 | ||
![]() |
7097288107 | ||
![]() |
0b79a1a1c6 | ||
![]() |
277b556961 | ||
![]() |
8e0dbb67df | ||
![]() |
47fed55509 | ||
![]() |
aef15cfe84 | ||
![]() |
d2c082d261 | ||
![]() |
8aca11b171 | ||
![]() |
b240b35433 | ||
![]() |
b7f8e031c3 | ||
![]() |
8b4aa469a7 | ||
![]() |
792434e3af | ||
![]() |
20c3036b4d | ||
![]() |
9c4f52e7bc | ||
![]() |
9b8453e57e | ||
![]() |
faa442ccb5 | ||
![]() |
cc8f1099d6 | ||
![]() |
c3cb3f1c1b | ||
![]() |
bc442a0727 | ||
![]() |
56ad845ccc | ||
![]() |
792a269597 | ||
![]() |
62601a8556 | ||
![]() |
4aac7e9d9b | ||
![]() |
f59008eeb4 | ||
![]() |
137ffd8264 | ||
![]() |
813d29653f | ||
![]() |
6f5dfa743d | ||
![]() |
0dd174bf3a | ||
![]() |
864418ef79 | ||
![]() |
9c2c9b557c | ||
![]() |
44b93d8117 | ||
![]() |
fc7949d7e9 | ||
![]() |
5161f9d08a | ||
![]() |
52752fb931 | ||
![]() |
28b84cef65 | ||
![]() |
91825867f6 | ||
![]() |
01703751f1 | ||
![]() |
3fdbc2a605 | ||
![]() |
6afe96fa4d | ||
![]() |
c2ca11f247 | ||
![]() |
a4637a3411 | ||
![]() |
e31b9e4d5f | ||
![]() |
d29e0d82ad | ||
![]() |
4b1fe33f96 | ||
![]() |
ee01cd9446 | ||
![]() |
87a257c09c | ||
![]() |
56d224a80c | ||
![]() |
1f6a54c87a | ||
![]() |
35e2780fea | ||
![]() |
52fdba36de | ||
![]() |
2e3116f207 | ||
![]() |
ff9d598a9c | ||
![]() |
373adef285 | ||
![]() |
ac4d1152c6 | ||
![]() |
c3d315eb4f | ||
![]() |
045e2cc061 | ||
![]() |
3536f80ca0 | ||
![]() |
e1c6e92a2a | ||
![]() |
b698d79bb5 | ||
![]() |
09357abe64 | ||
![]() |
d410174ef5 | ||
![]() |
da6dca2805 | ||
![]() |
84e8d42871 | ||
![]() |
ac0c509a73 | ||
![]() |
fda02ef3c8 | ||
![]() |
0f559fa0d7 | ||
![]() |
899a11e9ad |
486 changed files with 30151 additions and 3810 deletions
39
.gitignore
vendored
39
.gitignore
vendored
|
@ -1,39 +0,0 @@
|
|||
*.py[cod]
|
||||
*~
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Packages
|
||||
*.egg
|
||||
*.egg-info
|
||||
dist
|
||||
build
|
||||
eggs
|
||||
parts
|
||||
bin
|
||||
var
|
||||
sdist
|
||||
develop-eggs
|
||||
.installed.cfg
|
||||
lib
|
||||
lib64
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
.coverage
|
||||
.tox
|
||||
nosetests.xml
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
|
||||
# Mr Developer
|
||||
.mr.developer.cfg
|
||||
.project
|
||||
.pydevproject
|
||||
|
||||
# PyCharm
|
||||
.idea
|
BIN
MY_BOOK/600_stars.png
Normal file
BIN
MY_BOOK/600_stars.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 665 KiB |
Binary file not shown.
41
MY_BOOK/ebook_src/abstract_structures/HashTableClass.py
Executable file
41
MY_BOOK/ebook_src/abstract_structures/HashTableClass.py
Executable file
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class HashTable(object):
|
||||
def __init__(self, slots=10):
|
||||
self.slots = slots
|
||||
self.table = []
|
||||
self.create_table()
|
||||
|
||||
def hash_key(self, value):
|
||||
return hash(value)%self.slots
|
||||
|
||||
def create_table(self):
|
||||
for i in range(self.slots):
|
||||
self.table.append([])
|
||||
|
||||
def add_item(self, value):
|
||||
key = self.hash_key(value)
|
||||
self.table[key].append(value)
|
||||
|
||||
def print_table(self):
|
||||
for key in range(len(self.table)):
|
||||
print "Key is %s, value is %s." %(key, self.table[key])
|
||||
|
||||
def find_item(self, item):
|
||||
pos = self.hash_key(item)
|
||||
if item in self.table[pos]:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
if __name__ == '__main__':
|
||||
dic = HashTable(5)
|
||||
for i in range(1, 40, 2):
|
||||
dic.add_item(i)
|
||||
|
||||
dic.print_table()
|
||||
assert(dic.find_item(20) == False)
|
||||
assert(dic.find_item(21) == True)
|
47
MY_BOOK/ebook_src/abstract_structures/QueueClass.py
Executable file
47
MY_BOOK/ebook_src/abstract_structures/QueueClass.py
Executable file
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python
|
||||
# Time: 5 min
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Queue(object):
|
||||
def __init__(self):
|
||||
self.enq = []
|
||||
self.deq = []
|
||||
|
||||
def enqueue(self, item):
|
||||
return self.enq.append(item)
|
||||
|
||||
def deque(self):
|
||||
if not self.deq:
|
||||
while self.enq:
|
||||
self.deq.append(self.enq.pop())
|
||||
return self.deq.pop()
|
||||
|
||||
def peak(self):
|
||||
if not self.deq:
|
||||
while self.enq:
|
||||
self.deq.append(self.enq.pop())
|
||||
if self.deq:
|
||||
return self.deq[-1]
|
||||
|
||||
def size(self):
|
||||
return len(self.enq) + len(self.deq)
|
||||
|
||||
def isempty(self):
|
||||
return not (self.enq + self.deq)
|
||||
|
||||
if __name__ == '__main__':
|
||||
q = Queue()
|
||||
for i in range(1,11):
|
||||
q.enqueue(i)
|
||||
print 'Size:', q.size()
|
||||
print 'Is empty?', q.isempty()
|
||||
print 'Peak: ', q.peak()
|
||||
print
|
||||
print 'Dequeuing...'
|
||||
for i in range(10):
|
||||
print q.deque()
|
||||
print 'Size:', q.size()
|
||||
print 'Is empty?', q.isempty()
|
||||
print 'Peak: ', q.peak()
|
63
MY_BOOK/ebook_src/abstract_structures/Stack.py
Executable file
63
MY_BOOK/ebook_src/abstract_structures/Stack.py
Executable file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Stack(object):
|
||||
def __init__(self):
|
||||
self.content = []
|
||||
self.min_array = []
|
||||
self.min = float('inf')
|
||||
|
||||
def push(self, value):
|
||||
if value < self.min:
|
||||
self.min = value
|
||||
|
||||
self.content.append(value)
|
||||
self.min_array.append(self.min)
|
||||
|
||||
def pop(self):
|
||||
if self.content:
|
||||
value = self.content.pop()
|
||||
self.min_array.pop()
|
||||
if self.min_array:
|
||||
self.min = self.min_array[-1]
|
||||
return value
|
||||
else:
|
||||
return 'Empty List. '
|
||||
|
||||
def find_min(self):
|
||||
if self.min_array:
|
||||
return self.min_array[-1]
|
||||
else:
|
||||
return 'No min value for empty list.'
|
||||
|
||||
def size(self):
|
||||
return len(self.content)
|
||||
|
||||
def isEmpty(self):
|
||||
return not bool(self.content)
|
||||
|
||||
def peek(self):
|
||||
if self.content:
|
||||
return self.content[-1]
|
||||
else:
|
||||
print('Stack is empty.')
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return '{}'.format(self.content)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
q = Stack()
|
||||
|
||||
for i in range(15,20):
|
||||
q.push(i)
|
||||
for i in range(10,5,-1):
|
||||
q.push(i)
|
||||
|
||||
|
||||
|
||||
for i in range(1, 13):
|
||||
print q.pop(), q.find_min()
|
0
src/abstract_structures/__init__.py → MY_BOOK/ebook_src/abstract_structures/__init__.py
Normal file → Executable file
0
src/abstract_structures/__init__.py → MY_BOOK/ebook_src/abstract_structures/__init__.py
Normal file → Executable file
|
@ -1,13 +1,13 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import heapq
|
||||
|
||||
def find_N_largest_items_seq(seq, N):
|
||||
''' find the N largest items in a sequence '''
|
||||
return heapq.nlargest(N,seq)
|
||||
|
||||
|
||||
def find_N_smallest_items_seq(seq, N):
|
||||
''' find the N smallest items in a sequence '''
|
||||
return heapq.nsmallest(N, seq)
|
||||
|
@ -18,7 +18,7 @@ def find_smallest_items_seq_heap(seq):
|
|||
''' pops the smallest item, O(logN) '''
|
||||
heapq.heapify(seq)
|
||||
return heapq.heappop(seq)
|
||||
|
||||
|
||||
def find_smallest_items_seq(seq):
|
||||
''' if it is only one item, min() is faster '''
|
||||
return min(seq)
|
||||
|
@ -26,7 +26,7 @@ def find_smallest_items_seq(seq):
|
|||
def find_N_smallest_items_seq_sorted(seq, N):
|
||||
''' N ~ len(seq), better sort instead'''
|
||||
return sorted(seq)[:N]
|
||||
|
||||
|
||||
def find_N_largest_items_seq_sorted(seq, N):
|
||||
''' N ~ len(seq), better sort instead'''
|
||||
return sorted(seq)[len(seq)-N:]
|
||||
|
@ -41,7 +41,7 @@ def test_find_N_largest_smallest_items_seq(module_name='this module'):
|
|||
assert(find_N_smallest_items_seq_sorted(seq, N) == [1,2,3])
|
||||
assert(find_smallest_items_seq(seq) == 1)
|
||||
assert(find_smallest_items_seq_heap(seq) == 1)
|
||||
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
class Heapify(object):
|
||||
def __init__(self, data=None):
|
||||
self.data = data or []
|
||||
for i in range(len(data)//2, -1, -1):
|
||||
self.__max_heapify__(i)
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return '{}'.format(self.data)
|
||||
|
||||
|
@ -19,12 +19,12 @@ class Heapify(object):
|
|||
|
||||
def right_child(self, i):
|
||||
return (i << 1) + 2 # +2 instead of +1 because it's 0-indexed.
|
||||
|
||||
|
||||
def __max_heapify__(self, i):
|
||||
largest = i
|
||||
left = self.left_child(i)
|
||||
right = self.right_child(i)
|
||||
n = len(self.data)
|
||||
n = len(self.data)
|
||||
largest = (left < n and self.data[left] > self.data[i]) and left or i
|
||||
largest = (right < n and self.data[right] > self.data[largest]) and right or largest
|
||||
if i is not largest:
|
||||
|
@ -44,7 +44,6 @@ def test_Heapify():
|
|||
l1 = [3, 2, 5, 1, 7, 8, 2]
|
||||
h = Heapify(l1)
|
||||
assert(h.extract_max() == 8)
|
||||
print ("Tests Passed!")
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_Heapify()
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import heapq
|
||||
|
||||
|
@ -19,9 +19,7 @@ def test_merge_sorted_seq(module_name='this module'):
|
|||
seq2 = [2, 3, 4, 5, 6, 7, 9]
|
||||
seq3 = seq1 + seq2
|
||||
assert(merge_sorted_seqseq1, seq2) == sorted(seq3))
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import heapq
|
||||
|
||||
|
@ -9,11 +9,11 @@ class PriorityQueue(object):
|
|||
def __init__(self):
|
||||
self._queue = []
|
||||
self._index = 0 # comparying same priority level
|
||||
|
||||
|
||||
def push(self, item, priority):
|
||||
heapq.heappush(self._queue, (-priority, self._index, item))
|
||||
heapq.heappush(self._queue, (-priority, self._index, item))
|
||||
self._index += 1
|
||||
|
||||
|
||||
def pop(self):
|
||||
return heapq.heappop(self._queue)[-1]
|
||||
|
||||
|
@ -32,7 +32,6 @@ def test_PriorityQueue():
|
|||
q.push(Item('test2'), 4)
|
||||
q.push(Item('test3'), 3)
|
||||
assert(str(q.pop()) == "Item('test2')")
|
||||
print('Tests passed!'.center(20,'*'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
|
@ -1,8 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' implement a function to see whether a linked list is circular.
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Implement a double-linked list, which is very simple, we just need inherits
|
||||
from a Linked List Class and add an attribute for previous.'''
|
||||
|
@ -18,7 +17,6 @@ class dNode(object):
|
|||
|
||||
class dLinkList(LinkedListFIFO):
|
||||
|
||||
# print each node's value, starting from tail
|
||||
def printListInverse(self):
|
||||
node = self.tail
|
||||
while node:
|
||||
|
@ -28,8 +26,6 @@ class dLinkList(LinkedListFIFO):
|
|||
except:
|
||||
break
|
||||
|
||||
# add a node in a position different from head,
|
||||
# ie, in the end of the list
|
||||
def _add(self, value):
|
||||
self.length += 1
|
||||
node = dNode(value)
|
||||
|
@ -38,14 +34,12 @@ class dLinkList(LinkedListFIFO):
|
|||
node.previous = self.tail
|
||||
self.tail = node
|
||||
|
||||
# delete a node in some position
|
||||
def _delete(self, node):
|
||||
self.length -= 1
|
||||
node.previous.pointer = node.pointer
|
||||
if not node.pointer:
|
||||
self.tail = node.previous
|
||||
|
||||
# locate node with some index
|
||||
def _find(self, index):
|
||||
node = self.head
|
||||
i = 0
|
||||
|
@ -66,11 +60,6 @@ class dLinkList(LinkedListFIFO):
|
|||
print('Node with index {} not found'.format(index))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
from collections import Counter
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
''' Find the mth-to-last element of a linked list.
|
||||
One option is having two pointers, separated by m. P1 start at the roots
|
|
@ -1,15 +1,12 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
''' A class for a linked list that has the nodes in a FIFO order (such as a queue)'''
|
||||
|
||||
|
||||
from node import Node
|
||||
|
||||
|
||||
|
||||
class LinkedListFIFO(object):
|
||||
|
||||
def __init__(self):
|
||||
|
@ -17,32 +14,24 @@ class LinkedListFIFO(object):
|
|||
self.length = 0
|
||||
self.tail = None # this is different from ll lifo
|
||||
|
||||
|
||||
# print each node's value, starting from the head
|
||||
def _printList(self):
|
||||
node = self.head
|
||||
while node:
|
||||
print(node.value)
|
||||
node = node.pointer
|
||||
|
||||
# add a node in the first position
|
||||
# read will never be changed again while not empty
|
||||
def _addFirst(self, value):
|
||||
self.length = 1
|
||||
node = Node(value)
|
||||
self.head = node
|
||||
self.tail = node
|
||||
|
||||
# delete a node in the first position, ie
|
||||
# when there is no previous node
|
||||
def _deleteFirst(self):
|
||||
self.length = 0
|
||||
self.head = None
|
||||
self.tail = None
|
||||
print('The list is empty.')
|
||||
|
||||
# add a node in a position different from head,
|
||||
# ie, in the end of the list
|
||||
def _add(self, value):
|
||||
self.length += 1
|
||||
node = Node(value)
|
||||
|
@ -50,15 +39,12 @@ class LinkedListFIFO(object):
|
|||
self.tail.pointer = node
|
||||
self.tail = node
|
||||
|
||||
|
||||
# add nodes in general
|
||||
def addNode(self, value):
|
||||
if not self.head:
|
||||
self._addFirst(value)
|
||||
else:
|
||||
self._add(value)
|
||||
|
||||
# locate node with some index
|
||||
def _find(self, index):
|
||||
prev = None
|
||||
node = self.head
|
||||
|
@ -69,7 +55,6 @@ class LinkedListFIFO(object):
|
|||
i += 1
|
||||
return node, prev, i
|
||||
|
||||
# delete nodes in general
|
||||
def deleteNode(self, index):
|
||||
if not self.head or not self.head.pointer:
|
||||
self._deleteFirst()
|
||||
|
@ -87,9 +72,6 @@ class LinkedListFIFO(object):
|
|||
print('Node with index {} not found'.format(index))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ll = LinkedListFIFO()
|
||||
for i in range(1, 5):
|
|
@ -1,9 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Implement a unordered linked list, i.e. a LIFO linked list (like a stack) '''
|
||||
|
@ -16,15 +13,12 @@ class LinkedListLIFO(object):
|
|||
self.head = None
|
||||
self.length = 0
|
||||
|
||||
|
||||
# print each node's value, starting from the head
|
||||
def _printList(self):
|
||||
node = self.head
|
||||
while node:
|
||||
print(node.value)
|
||||
node = node.pointer
|
||||
|
||||
# delete a node, given the previous node
|
||||
def _delete(self, prev, node):
|
||||
self.length -= 1
|
||||
if not prev:
|
||||
|
@ -32,14 +26,10 @@ class LinkedListLIFO(object):
|
|||
else:
|
||||
prev.pointer = node.pointer
|
||||
|
||||
# add a new node, pointing to the previous node
|
||||
# in the head
|
||||
def _add(self, value):
|
||||
self.length += 1
|
||||
self.head = Node(value, self.head)
|
||||
|
||||
|
||||
# locate node with some index
|
||||
def _find(self, index):
|
||||
prev = None
|
||||
node = self.head
|
||||
|
@ -50,7 +40,6 @@ class LinkedListLIFO(object):
|
|||
i += 1
|
||||
return node, prev, i
|
||||
|
||||
# locate node by value
|
||||
def _find_by_value(self, value):
|
||||
prev = None
|
||||
node = self.head
|
||||
|
@ -63,8 +52,6 @@ class LinkedListLIFO(object):
|
|||
node = node.pointer
|
||||
return node, prev, found
|
||||
|
||||
|
||||
# find and delete a node by index
|
||||
def deleteNode(self, index):
|
||||
node, prev, i = self._find(index)
|
||||
if index == i:
|
||||
|
@ -72,8 +59,6 @@ class LinkedListLIFO(object):
|
|||
else:
|
||||
print('Node with index {} not found'.format(index))
|
||||
|
||||
|
||||
# find and delete a node by value
|
||||
def deleteNodeByValue(self, value):
|
||||
node, prev, found = self._find_by_value(value)
|
||||
if found:
|
|
@ -1,8 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, value=None, pointer=None):
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
''' This function divides a linked list in a value, where everything smaller than this value
|
||||
goes to the front, and everything large goes to the back:'''
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Supposing two linked lists representing numbers, such that in each of their
|
||||
nodes they carry one digit. This function sums the two numbers that these
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
""" A class for an animal shelter with two queues"""
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
''' a class for a double ended queue (also inefficient) '''
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Queue acts as a container for nodes (objects) that are inserted and removed according FIFO'''
|
||||
|
|
@ -1,17 +1,19 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
""" Using our deque class and Python's deque class """
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
import string
|
||||
import collections
|
||||
|
||||
from deque import Deque
|
||||
|
||||
|
||||
STRIP = string.whitespace + string.punctuation + "\"'"
|
||||
|
||||
|
||||
""" Using our deque class and Python's deque class """
|
||||
def palindrome_checker_with_deque(str1):
|
||||
|
||||
d1 = Deque()
|
|
@ -1,10 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
|
||||
''' an example of a queue implemented from 2 stacks '''
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Queue(object):
|
||||
|
@ -13,17 +9,13 @@ class Queue(object):
|
|||
self.in_stack = []
|
||||
self.out_stack = []
|
||||
|
||||
|
||||
# basic methods
|
||||
def _transfer(self):
|
||||
while self.in_stack:
|
||||
self.out_stack.append(self.in_stack.pop())
|
||||
|
||||
|
||||
def enqueue(self, item):
|
||||
return self.in_stack.append(item)
|
||||
|
||||
|
||||
def dequeue(self):
|
||||
if not self.out_stack:
|
||||
self._transfer()
|
||||
|
@ -32,11 +24,9 @@ class Queue(object):
|
|||
else:
|
||||
return "Queue empty!"
|
||||
|
||||
|
||||
def size(self):
|
||||
return len(self.in_stack) + len(self.out_stack)
|
||||
|
||||
|
||||
def peek(self):
|
||||
if not self.out_stack:
|
||||
self._transfer()
|
||||
|
@ -54,9 +44,7 @@ class Queue(object):
|
|||
return "Queue empty!"
|
||||
|
||||
def isEmpty(self):
|
||||
return not ((bool(self.in_stack) or bool(self.out_stack)))
|
||||
|
||||
|
||||
return not (bool(self.in_stack) or bool(self.out_stack))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
'''transform a decimal number to a binary number with a stack '''
|
||||
|
|
@ -1,13 +1,11 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
""" A stack made of linked list"""
|
||||
|
||||
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, value=None, pointer=None):
|
||||
self.value = value
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
''' Uses a stack to reverse a string '''
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
""" define a class for a set of stacks """
|
||||
|
65
MY_BOOK/ebook_src/abstract_structures/stacks/stack.py
Executable file
65
MY_BOOK/ebook_src/abstract_structures/stacks/stack.py
Executable file
|
@ -0,0 +1,65 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# copy of the class ../Stack.py
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Stack(object):
|
||||
def __init__(self):
|
||||
self.content = []
|
||||
self.min_array = []
|
||||
self.min = float('inf')
|
||||
|
||||
def push(self, value):
|
||||
if value < self.min:
|
||||
self.min = value
|
||||
|
||||
self.content.append(value)
|
||||
self.min_array.append(self.min)
|
||||
|
||||
def pop(self):
|
||||
if self.content:
|
||||
value = self.content.pop()
|
||||
self.min_array.pop()
|
||||
if self.min_array:
|
||||
self.min = self.min_array[-1]
|
||||
return value
|
||||
else:
|
||||
return 'Empty List. '
|
||||
|
||||
def find_min(self):
|
||||
if self.min_array:
|
||||
return self.min_array[-1]
|
||||
else:
|
||||
return 'No min value for empty list.'
|
||||
|
||||
def size(self):
|
||||
return len(self.content)
|
||||
|
||||
def isEmpty(self):
|
||||
return not bool(self.content)
|
||||
|
||||
def peek(self):
|
||||
if self.content:
|
||||
return self.content[-1]
|
||||
else:
|
||||
print('Stack is empty.')
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return '{}'.format(self.content)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
q = Stack()
|
||||
|
||||
for i in range(15,20):
|
||||
q.push(i)
|
||||
for i in range(10,5,-1):
|
||||
q.push(i)
|
||||
|
||||
|
||||
|
||||
for i in range(1, 13):
|
||||
print q.pop(), q.find_min()
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
__author__ = "bt3"
|
||||
|
||||
''' A stack with a minimum lookup '''
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/python
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
|
||||
""" Implement the 'towers of hanoi'"""
|
18
src/neat_builtin_examples/sets/bit_operations/bit_array.py → MY_BOOK/ebook_src/bitwise_operations/bit_array.py
Normal file → Executable file
18
src/neat_builtin_examples/sets/bit_operations/bit_array.py → MY_BOOK/ebook_src/bitwise_operations/bit_array.py
Normal file → Executable file
|
@ -1,17 +1,16 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
''' Example of how to use a bit array in python as a "counter" dict:
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Example of how to use a bit array in python as a "counter" dict'''
|
||||
|
||||
def print_dupl_ba(l1):
|
||||
'''
|
||||
>>> l1 = [0, 1, 2, 3, 4, 2, 6, 7, 8, 9]
|
||||
>>> print_dupl_ba(l1)
|
||||
2
|
||||
'''
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def print_dupl_ba(l1):
|
||||
bs = bytearray(10)
|
||||
for i in range(len(l1)):
|
||||
if i == l1[i]:
|
||||
|
@ -22,9 +21,6 @@ def print_dupl_ba(l1):
|
|||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
33
MY_BOOK/ebook_src/bitwise_operations/bitwise.txt
Executable file
33
MY_BOOK/ebook_src/bitwise_operations/bitwise.txt
Executable file
|
@ -0,0 +1,33 @@
|
|||
BIT-WISE
|
||||
----------------------
|
||||
|
||||
1. To find a number:
|
||||
11000101 is 2^0+2^2+2^6+2^7 = 197
|
||||
|
||||
|
||||
2. Left shifting:
|
||||
0010 1011 << 4 ---> 1011 000
|
||||
|
||||
|
||||
3. Right shifting:
|
||||
0010 1011 >> 4 ---> 0000 0010
|
||||
or it can be filled with the copy of the first bit, instead of 0:
|
||||
1011 0010 >> 4 ---> 1111 1011
|
||||
|
||||
|
||||
4. XOR can cancels out:
|
||||
15 ^ 12 ^ 15 = 12
|
||||
|
||||
|
||||
5. 2^x:
|
||||
left-shift 1 by x:
|
||||
0000 0001 << x
|
||||
|
||||
so if x = 2, 2^2 = 4 -> 100
|
||||
|
||||
0000 0001 << 2 ---> 0000 0100
|
||||
|
||||
|
||||
6. Is power of 2?
|
||||
just do x&(x-1).
|
||||
if 0 --> yes!
|
37
MY_BOOK/ebook_src/bitwise_operations/clear_bits.py
Executable file
37
MY_BOOK/ebook_src/bitwise_operations/clear_bits.py
Executable file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Clear a bit in a binary number.
|
||||
Like the reverse of set bit:
|
||||
1) first create a number filled of 1s,
|
||||
with 0 at i (can create 0001000 and ~)
|
||||
2) AND the number so it clears the ith bit
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def clear_bit(num, i):
|
||||
mask = ~ (1 << i) # -0b10001
|
||||
return bin(num & mask)
|
||||
|
||||
|
||||
def clear_all_bits_from_i_to_0(num, i):
|
||||
mask = ~ ( (1 << (i+1)) - 1)
|
||||
return bin(num & mask)
|
||||
|
||||
|
||||
def clear_all_bits_from_most_sig_to_1(num, i):
|
||||
mask = ( 1 << i) -1
|
||||
return bin(num & mask)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
num = int('10010000', 2)
|
||||
print clear_bit(num, 4) # '0b10000000'
|
||||
|
||||
num = int('10010011', 2)
|
||||
print clear_all_bits_from_i_to_0(num, 2) # '0b10010000'
|
||||
|
||||
num = int('1110011', 2)
|
||||
print clear_all_bits_from_most_sig_to_1(num, 2) #'0b11'
|
21
src/neat_builtin_examples/sets/bit_operations/find_bit_len.py → MY_BOOK/ebook_src/bitwise_operations/find_bit_len.py
Normal file → Executable file
21
src/neat_builtin_examples/sets/bit_operations/find_bit_len.py → MY_BOOK/ebook_src/bitwise_operations/find_bit_len.py
Normal file → Executable file
|
@ -1,28 +1,25 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Find how many bits a int has:
|
||||
1) Start with a mask of 1
|
||||
2) Mask with AND
|
||||
3) if result (if true): count += 1
|
||||
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int))
|
||||
|
||||
>>> for i in range(17): print(find_bit_len(i))
|
||||
(obs: to find the int of a bin do int('1001', 2)) and to show in bin
|
||||
do bin(int))
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def find_bit_len(int_num):
|
||||
lenght = 0
|
||||
while int_num:
|
||||
int_num >>= 1
|
||||
lenght += 1
|
||||
return lenght
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
for i in range(17):
|
||||
print(find_bit_len(i))
|
||||
print i.bit_length()
|
|
@ -1,21 +1,22 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Find how many 1s in the binary:
|
||||
1) Start with a mask of 1
|
||||
2) Mask with AND
|
||||
3) if result (if true): count += 1
|
||||
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int))
|
||||
|
||||
>>> find_how_many_1_in_a_binary(9)
|
||||
2
|
||||
|
||||
(obs: to find the int of a bin do int('1001',
|
||||
2)) and to show in bin do bin(int))
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def find_how_many_1_in_a_binary(n):
|
||||
'''
|
||||
>>> find_how_many_1_in_a_binary(9)
|
||||
2
|
||||
'''
|
||||
|
||||
counter = 0
|
||||
while n:
|
||||
if n & 1:
|
||||
|
@ -24,9 +25,6 @@ def find_how_many_1_in_a_binary(n):
|
|||
return counter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
27
MY_BOOK/ebook_src/bitwise_operations/get_bit.py
Executable file
27
MY_BOOK/ebook_src/bitwise_operations/get_bit.py
Executable file
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Get a bit in a binary number:
|
||||
1) Shifts 1 over by i bits
|
||||
2) make an AND with the number
|
||||
3) all the other than the bit at i are clean, now compare to 0
|
||||
4) if the new value is not 0, bit i is 1
|
||||
'''
|
||||
|
||||
|
||||
def get_bit(num, i):
|
||||
mask = 1 << i
|
||||
return num & mask != 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
num = int('0100100', 2)
|
||||
get_bit(num, 0) # 0
|
||||
get_bit(num, 1) # 0
|
||||
get_bit(num, 2) # 1
|
||||
get_bit(num, 3) # 0
|
||||
get_bit(num, 4) # 0
|
||||
get_bit(num, 5) # 1
|
||||
get_bit(num, 6) # 0
|
||||
|
16
src/neat_builtin_examples/sets/bit_operations/get_float_rep_bin.py → MY_BOOK/ebook_src/bitwise_operations/get_float_rep_bin.py
Normal file → Executable file
16
src/neat_builtin_examples/sets/bit_operations/get_float_rep_bin.py → MY_BOOK/ebook_src/bitwise_operations/get_float_rep_bin.py
Normal file → Executable file
|
@ -1,20 +1,22 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary
|
||||
representation. If the Number cannot be represented accurately in binary, with at
|
||||
most 32 chars, print error:
|
||||
|
||||
'''
|
||||
|
||||
def get_float_rep(num):
|
||||
'''
|
||||
>>> get_float_rep(0.72)
|
||||
('Error 2', '.1011100001010001111010111000010')
|
||||
>>> get_float_rep(0.1)
|
||||
('Error 2', '.0001100110011001100110011001100')
|
||||
>>> get_float_rep(0.5)
|
||||
'.1'
|
||||
'''
|
||||
'''
|
||||
|
||||
def get_float_rep(num):
|
||||
if num >= 1 or num <= 0: return 'Error 1'
|
||||
result = '.'
|
||||
while num:
|
||||
|
@ -27,7 +29,7 @@ def get_float_rep(num):
|
|||
result += '0'
|
||||
num = r
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
|
@ -1,33 +1,34 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Given two 32-bit numbers, N and M, and two bit positions, i and j, this
|
||||
method insert M into N such that M starts at bit j and ends at bit i:
|
||||
method insert M into N such that M starts at bit j and ends at bit i:
|
||||
1) clear the bits j thru i in N'
|
||||
2) shift M so that it lines up with bits j thru i
|
||||
3) merge M and N
|
||||
'''
|
||||
|
||||
def insert_small_bin_into_big_bin(M, N, i, j):
|
||||
'''
|
||||
>>> N = 0b10000000000
|
||||
>>> M = 0b10011
|
||||
>>> j = 6
|
||||
>>> i = 2
|
||||
>>> insert_small_bin_into_big_bin(M, N, i, j)
|
||||
'0b10001001100'
|
||||
'''
|
||||
'''
|
||||
|
||||
def insert_small_bin_into_big_bin(M, N, i, j):
|
||||
allOnes = ~0
|
||||
left = allOnes << (j+1) # 1110000
|
||||
right = ( (1 << i) - 1) # 0000111
|
||||
mask = left | right # 1110111
|
||||
N_cleared = N & mask
|
||||
M_shifted = M << i
|
||||
|
||||
|
||||
return bin( N_cleared | M_shifted)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
|
@ -1,27 +1,19 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Give a positive int, print the next smallest and next largest ints with
|
||||
same number of 1 bits.
|
||||
The brute force is:
|
||||
1) find number of 1 bits
|
||||
2) loop above and down until find same, checking for each
|
||||
|
||||
>>> num = 0b1001
|
||||
>>> next = '0b1010'
|
||||
>>> prev = '0b110'
|
||||
>>> print_prev_same_1s(num) == prev
|
||||
True
|
||||
>>> print_next_same_1s(num) == next
|
||||
True
|
||||
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def print_prev_same_1s(num):
|
||||
n1s = find_num_1s(num)
|
||||
n1s = find_num_1s(num)
|
||||
# find prev
|
||||
i = num-1
|
||||
while True:
|
||||
|
@ -31,10 +23,10 @@ def print_prev_same_1s(num):
|
|||
i -= 1
|
||||
if i < 0:
|
||||
return None
|
||||
|
||||
|
||||
def print_next_same_1s(num):
|
||||
n1s = find_num_1s(num)
|
||||
# find next
|
||||
n1s = find_num_1s(num)
|
||||
# find next
|
||||
i = num+1
|
||||
while True:
|
||||
n1s_here = find_num_1s(i)
|
||||
|
@ -43,8 +35,8 @@ def print_next_same_1s(num):
|
|||
i += 1
|
||||
if i < 0:
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def find_num_1s(num):
|
||||
counter = 0
|
||||
|
@ -54,11 +46,14 @@ def find_num_1s(num):
|
|||
num >>= 1
|
||||
return counter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
num = 0b1001
|
||||
n = '0b1010'
|
||||
p = '0b110'
|
||||
print_prev_same_1s(num) == p
|
||||
print_next_same_1s(num) == n
|
||||
|
|
@ -1,18 +1,12 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' This method returns the number of bits that are necessary to change to convert two
|
||||
numbers A and B:
|
||||
1) XOR
|
||||
2) count 1s
|
||||
|
||||
>>> a = int('10010000', 2)
|
||||
>>> b = int('01011010', 2)
|
||||
>>> count_bits_swap(a, b)
|
||||
4
|
||||
>>> count_bits_swap2(a, b)
|
||||
4
|
||||
'''
|
||||
|
||||
def count_bits_swap2(a, b):
|
||||
|
@ -28,8 +22,8 @@ def count_bits_swap2(a, b):
|
|||
def count_bits_swap(a, b):
|
||||
m = a^b
|
||||
return count_1s(m)
|
||||
|
||||
|
||||
|
||||
|
||||
def count_1s(m):
|
||||
count = 0
|
||||
while m:
|
||||
|
@ -37,9 +31,10 @@ def count_1s(m):
|
|||
count +=1
|
||||
m >>= 1
|
||||
return count
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
a = int('10010000', 2)
|
||||
b = int('01011010', 2)
|
||||
print count_bits_swap(a, b) #4
|
||||
print count_bits_swap2(a, b) #4
|
26
MY_BOOK/ebook_src/bitwise_operations/set_bit.py
Executable file
26
MY_BOOK/ebook_src/bitwise_operations/set_bit.py
Executable file
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
''' Set a bit in a binary number:
|
||||
1) Shifts 1 over by i bits
|
||||
2) make an OR with the number, only the value at bit i will change and all the others bit
|
||||
of the mask are zero so will not affect the num
|
||||
'''
|
||||
|
||||
|
||||
def set_bit(num, i):
|
||||
mask = 1 << i
|
||||
return bin( num | mask )
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
num = int('0100100', 2)
|
||||
print set_bit(num, 0) #'0b100101'
|
||||
print set_bit(num, 1) #'0b100110'
|
||||
print set_bit(num, 2) # nothing change '0b100100'
|
||||
print set_bit(num, 3) #'0b101100'
|
||||
print set_bit(num, 4) #'0b110100'
|
||||
print set_bit(num, 5) # nothing change '0b100100'
|
23
MY_BOOK/ebook_src/bitwise_operations/swap_in_place.py
Executable file
23
MY_BOOK/ebook_src/bitwise_operations/swap_in_place.py
Executable file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
'''
|
||||
swapping values in place without extra memory
|
||||
'''
|
||||
|
||||
|
||||
def swap_bit(a, b):
|
||||
'''
|
||||
>>> swap_bit(14, 73)
|
||||
(73, 14)
|
||||
'''
|
||||
a = a^b
|
||||
b = a^b
|
||||
a = a^b
|
||||
return a, b
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
23
src/neat_builtin_examples/sets/bit_operations/swap_odd_even.py → MY_BOOK/ebook_src/bitwise_operations/swap_odd_even.py
Normal file → Executable file
23
src/neat_builtin_examples/sets/bit_operations/swap_odd_even.py → MY_BOOK/ebook_src/bitwise_operations/swap_odd_even.py
Normal file → Executable file
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Swap odd and even bits in a smart way in a binary:
|
||||
1) first for odds, take n and move the odd:
|
||||
|
@ -8,17 +9,18 @@
|
|||
(b) shift by right by 1
|
||||
2) do the same to ints with 01010101
|
||||
3) merge
|
||||
|
||||
>>> num = 0b11011101
|
||||
>>> result = '0b1101110'
|
||||
>>> swap_odd_even(num) == result
|
||||
True
|
||||
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def swap_odd_even(num):
|
||||
'''
|
||||
>>> num = 0b11011101
|
||||
>>> result = '0b1101110'
|
||||
>>> swap_odd_even(num) == result
|
||||
True
|
||||
'''
|
||||
|
||||
mask_odd = 0xAA # 0b10101010
|
||||
mask_even = 0x55 # 0b1010101
|
||||
odd = num & mask_odd
|
||||
|
@ -28,9 +30,6 @@ def swap_odd_even(num):
|
|||
return bin(odd | even)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
18
src/neat_builtin_examples/sets/bit_operations/update_bit.py → MY_BOOK/ebook_src/bitwise_operations/update_bit.py
Normal file → Executable file
18
src/neat_builtin_examples/sets/bit_operations/update_bit.py → MY_BOOK/ebook_src/bitwise_operations/update_bit.py
Normal file → Executable file
|
@ -1,26 +1,22 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' This method merges set bit and clean bit:
|
||||
1) first clear the bit at i using a mask such as 1110111
|
||||
2) then shift the intended value v by i bits
|
||||
3) this will create a number with bit i to v and all other to 0
|
||||
4) finally update the ith bit with or
|
||||
|
||||
>>> num = int('10010000', 2)
|
||||
>>> update_bit(num, 2, 1)
|
||||
'0b10010100'
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def update_bit(num, i, v):
|
||||
mask = ~ (1 << i)
|
||||
return bin( (num & mask) | (v << i) )
|
||||
|
||||
return bin( (num & mask) | (v << i) )
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
num = int('10010000', 2)
|
||||
print update_bit(num, 2, 1) # '0b10010100'
|
||||
|
0
src/abstract_structures/stacks/__init__.py → MY_BOOK/ebook_src/builtin_structures/__init__.py
Normal file → Executable file
0
src/abstract_structures/stacks/__init__.py → MY_BOOK/ebook_src/builtin_structures/__init__.py
Normal file → Executable file
51
MY_BOOK/ebook_src/builtin_structures/anagram.py
Executable file
51
MY_BOOK/ebook_src/builtin_structures/anagram.py
Executable file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def is_anagram(s1, s2):
|
||||
'''
|
||||
>>> is_anagram('cat', 'tac')
|
||||
True
|
||||
>>> is_anagram('cat', 'hat')
|
||||
False
|
||||
'''
|
||||
counter = Counter()
|
||||
for c in s1:
|
||||
counter[c] += 1
|
||||
|
||||
for c in s2:
|
||||
counter[c] -= 1
|
||||
|
||||
for i in counter.values():
|
||||
if i:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
''' verify if words are anagrams by comparing a sum of Unicode code
|
||||
point of the character'''
|
||||
|
||||
def get_unicode_sum(word):
|
||||
s = 0
|
||||
for p in word:
|
||||
s += ord(p)
|
||||
return s
|
||||
|
||||
|
||||
def is_anagram2(word1, word2):
|
||||
'''
|
||||
>>> is_anagram2('cat', 'tac')
|
||||
True
|
||||
>>> is_anagram2('cat', 'hat')
|
||||
False
|
||||
'''
|
||||
return get_unicode_sum(word1) == get_unicode_sum(word2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
9
MY_BOOK/ebook_src/builtin_structures/balance.txt
Executable file
9
MY_BOOK/ebook_src/builtin_structures/balance.txt
Executable file
|
@ -0,0 +1,9 @@
|
|||
__author__ = "bt3"
|
||||
|
||||
|
||||
This is the classic "you have 8 balls/coins, which are the same weight, except for one which is slightly heavier than the others. You also have an old-style balance. What is the fewest number of weighings to find the heavy coin/ball?
|
||||
|
||||
Answer: 2! You need to use every information available:
|
||||
Weight 3 x 3 balls/coins.
|
||||
If they weight the same: weight the 2 balls/coins left outside.
|
||||
Else, measure 2 of the 3 heavier balls/coins.
|
40
MY_BOOK/ebook_src/builtin_structures/balance_symbols.py
Executable file
40
MY_BOOK/ebook_src/builtin_structures/balance_symbols.py
Executable file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
'''
|
||||
Given a N different open and close braces in a string "( { [ } ] )".
|
||||
How do you check whether the string has matching braces.
|
||||
'''
|
||||
|
||||
from collections import Counter
|
||||
def check_if_balance(string):
|
||||
'''
|
||||
>>> check_if_balance('{[[(])}]')
|
||||
True
|
||||
>>> check_if_balance('{[[()}]')
|
||||
False
|
||||
>>> check_if_balance('')
|
||||
True
|
||||
'''
|
||||
table = Counter()
|
||||
for i in string:
|
||||
|
||||
index = str(ord(i))[0]
|
||||
if i in '{[(':
|
||||
table[index] += 1
|
||||
|
||||
elif i in ')}]':
|
||||
table[index] -= 1
|
||||
|
||||
for i in table.values():
|
||||
if i !=-0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
71
MY_BOOK/ebook_src/builtin_structures/check_if_2_numbers_sum_to_k.py
Executable file
71
MY_BOOK/ebook_src/builtin_structures/check_if_2_numbers_sum_to_k.py
Executable file
|
@ -0,0 +1,71 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
"""
|
||||
Given an integer x and an unsorted array of integers, describe an
|
||||
algorithm to determine whether two of the numbers add up to x.
|
||||
|
||||
1. Using hash tables.
|
||||
2. Sorting the array and keeping two pointers in the array, one in
|
||||
the beginning and one in the end. Whenever the sum of the current
|
||||
two integers is less than x, move the first pointer forwards, and
|
||||
whenever the sum is greater than x, move the second pointer
|
||||
backwards. O(nln n).
|
||||
3. Create a BST with x minus each element in the array.
|
||||
Check whether any element of the array appears in the BST.
|
||||
It takes O(nlog n) times two.
|
||||
"""
|
||||
|
||||
from collections import defaultdict, Counter
|
||||
|
||||
def check_sum(array, k):
|
||||
'''
|
||||
>>> check_sum([3, 2, 6, 7, 9, 1], 8)
|
||||
[(6, 2), (1, 7)]
|
||||
>>> check_sum([5, 2, 6, 7, 9, 1], 4)
|
||||
[]
|
||||
>>>
|
||||
'''
|
||||
|
||||
dict = defaultdict()
|
||||
res = []
|
||||
|
||||
for i in array:
|
||||
if k-i in dict:
|
||||
res.append((i, k-i))
|
||||
del dict[k-i]
|
||||
else:
|
||||
dict[i] = 1
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def check_sum2(array, k):
|
||||
'''
|
||||
>>> check_sum2([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 6)
|
||||
set([(3, 3)])
|
||||
>>> check_sum2([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 0)
|
||||
set([])
|
||||
'''
|
||||
|
||||
dict = Counter()
|
||||
res = set()
|
||||
|
||||
for i in array:
|
||||
dict[i] += 1
|
||||
|
||||
for i in array:
|
||||
if dict[k-i] > 0:
|
||||
if i == k-i and dict[k-i] > 1:
|
||||
res.add((i, k-i))
|
||||
dict[k-i] -= 2
|
||||
elif i == k-i:
|
||||
res.add((i, k-i))
|
||||
dict[k-i] -= 1
|
||||
|
||||
return res
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
36
MY_BOOK/ebook_src/builtin_structures/check_if_3_numbers_sum_to_zero.py
Executable file
36
MY_BOOK/ebook_src/builtin_structures/check_if_3_numbers_sum_to_zero.py
Executable file
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Determine if an Array of integers contains 3 numbers that sum to 0 '''
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
def find_3_number(array):
|
||||
'''
|
||||
>>> find_3_number([1,5,56,11,-3,-12])
|
||||
1 11 -12
|
||||
True
|
||||
>>> find_3_number([] )
|
||||
False
|
||||
'''
|
||||
hash_ = defaultdict()
|
||||
for i in array:
|
||||
hash_[i] = 1
|
||||
|
||||
for i, x in enumerate(array):
|
||||
for y in array[i+1:]:
|
||||
if -(x+y) in hash_:
|
||||
print x, y, -(x+y)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
35
MY_BOOK/ebook_src/builtin_structures/check_non_overlapping_intervals.py
Executable file
35
MY_BOOK/ebook_src/builtin_structures/check_non_overlapping_intervals.py
Executable file
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
'''
|
||||
given an array of intervals, return max number of non-overlapping intervals
|
||||
'''
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
def non_overlapping(array):
|
||||
'''
|
||||
>>> non_overlapping([(1,2), (2,5), (1, 6)])
|
||||
[[(1, 2), (2, 5)]]
|
||||
>>> non_overlapping([(1,4), (2,5), (3, 6)])
|
||||
[]
|
||||
'''
|
||||
total = []
|
||||
|
||||
for i, t in enumerate(array):
|
||||
start = t[0]
|
||||
end = t[1]
|
||||
for tt in array[i+1:] :
|
||||
if end <= tt[0]:
|
||||
total.append([(start, end), (tt[0], tt[1])])
|
||||
|
||||
return total
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
64
MY_BOOK/ebook_src/builtin_structures/convert_numerical_bases.py
Executable file
64
MY_BOOK/ebook_src/builtin_structures/convert_numerical_bases.py
Executable file
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' convert an integer to a string in any base'''
|
||||
|
||||
def convert_from_dec_to_any_base(number, base):
|
||||
'''
|
||||
>>> number, base = 9, 2
|
||||
>>> convert_from_dec_to_any_base(number, base)
|
||||
'1001'
|
||||
'''
|
||||
|
||||
convertString = '012345679ABCDEF'
|
||||
|
||||
if number < base:
|
||||
return convertString[number]
|
||||
|
||||
else:
|
||||
return convert_from_dec_to_any_base(number//base, base) + \
|
||||
convertString[number%base]
|
||||
|
||||
|
||||
|
||||
def convert_from_decimal_to_binary(number, base):
|
||||
'''
|
||||
>>> number, base = 9, 2
|
||||
>>> convert_from_decimal_to_binary(number, base)
|
||||
1001
|
||||
'''
|
||||
|
||||
multiplier, result = 1, 0
|
||||
|
||||
while number > 0:
|
||||
result += number%base*multiplier
|
||||
multiplier *= 10
|
||||
number = number//base
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
def convert_from_decimal_larger_bases(number, base):
|
||||
'''
|
||||
>>> number, base = 31, 16
|
||||
>>> convert_from_decimal_larger_bases(number, base)
|
||||
'1F'
|
||||
'''
|
||||
strings = "0123456789ABCDEFGHIJ"
|
||||
result = ""
|
||||
|
||||
while number > 0:
|
||||
digit = number%base
|
||||
result = strings[digit] + result
|
||||
number = number//base
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
67
src/neat_builtin_examples/lists_and_strings/conv_str2int.py → MY_BOOK/ebook_src/builtin_structures/convert_str_2_int.py
Normal file → Executable file
67
src/neat_builtin_examples/lists_and_strings/conv_str2int.py → MY_BOOK/ebook_src/builtin_structures/convert_str_2_int.py
Normal file → Executable file
|
@ -1,9 +1,10 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
''' two routines to convert str to int and int to str:
|
||||
- bc: negative str, zero, exp numbers (ex. 1e5), other bases, NAN
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def conv_int2str(int1):
|
||||
'''
|
||||
>>> conv_str2int('367')
|
||||
367
|
||||
>>> conv_str2int('0')
|
||||
|
@ -12,46 +13,59 @@
|
|||
-10
|
||||
>>> conv_str2int('1e5')
|
||||
100000
|
||||
>>> conv_int2str(0)
|
||||
'0'
|
||||
>>> conv_int2str(1e5)
|
||||
'100000'
|
||||
>>> conv_int2str(367)
|
||||
'367'
|
||||
>>> conv_int2str(-10)
|
||||
'-10'
|
||||
'''
|
||||
'''
|
||||
|
||||
def conv_int2str(int1):
|
||||
aux_dict = {key:value for key in range(10) for value in '0123456789'[key]}
|
||||
if int1 == 0: return '0' # REMEMBER TO HANDLE 0
|
||||
if int1 < 0: # REMEMBER TO HANDLE NEGATIVE
|
||||
|
||||
if int1 == 0:
|
||||
return '0'
|
||||
elif int1 < 0:
|
||||
sign = '-'
|
||||
int1 = int1*(-1)
|
||||
else:
|
||||
sign = ''
|
||||
|
||||
|
||||
aux_ls = []
|
||||
|
||||
while int1 > 0:
|
||||
c = int1%10
|
||||
int1 = int1//10
|
||||
cadd = aux_dict[c]
|
||||
aux_ls.append(cadd)
|
||||
aux_ls.reverse() # REMEMBER TO REVERSE
|
||||
|
||||
aux_ls.reverse()
|
||||
|
||||
return sign + ''.join(aux_ls)
|
||||
|
||||
|
||||
|
||||
|
||||
def conv_str2int(str1):
|
||||
if not str1: return None
|
||||
'''
|
||||
>>> conv_int2str(0)
|
||||
'0'
|
||||
>>> conv_int2str(1e5)
|
||||
'100000'
|
||||
>>> conv_int2str(367)
|
||||
'367'
|
||||
>>> conv_int2str(-10)
|
||||
'-10'
|
||||
'''
|
||||
if not str1:
|
||||
return None
|
||||
|
||||
aux_dict = {key:value for value in range(10) for key in '0123456789'[value]}
|
||||
if str1[0] == '-':
|
||||
|
||||
if str1[0] == '-':
|
||||
sign = -1
|
||||
str1 = str1[1:] # REMEMBER TO CUT THE SIGN FROM THE STR
|
||||
else:
|
||||
str1 = str1[1:]
|
||||
else:
|
||||
sign = 1
|
||||
|
||||
dec, result = 1, 0
|
||||
for i in range(len(str1)-1, -1, -1): # REMEMBER TO FINISH IN -1, NOT 0, AND
|
||||
aux = str1[i] # AND DO THE STEPS -1
|
||||
|
||||
for i in range(len(str1)-1, -1, -1):
|
||||
aux = str1[i]
|
||||
if aux == 'e':
|
||||
exp_num = conv_str2int(str1[i+1:])
|
||||
number = conv_str2int(str1[:i])
|
||||
|
@ -59,8 +73,9 @@ def conv_str2int(str1):
|
|||
break
|
||||
result += aux_dict[aux]*dec
|
||||
dec *= 10
|
||||
|
||||
return result*sign
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
17
src/neat_builtin_examples/dicts/count_unique_words_.py → MY_BOOK/ebook_src/builtin_structures/count_unique_words_On2.py
Normal file → Executable file
17
src/neat_builtin_examples/dicts/count_unique_words_.py → MY_BOOK/ebook_src/builtin_structures/count_unique_words_On2.py
Normal file → Executable file
|
@ -1,21 +1,26 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import collections
|
||||
import string
|
||||
import sys
|
||||
|
||||
|
||||
def count_unique_word():
|
||||
|
||||
words = collections.defaultdict(int)
|
||||
strip = string.whitespace + string.punctuation + string.digits + "\"'"
|
||||
|
||||
strip = string.whitespace + string.punctuation + string.digits + "\"'"
|
||||
|
||||
for filename in sys.argv[1:]:
|
||||
with open(filename) as file:
|
||||
for line in file:
|
||||
for line in file:
|
||||
for word in line.lower().split():
|
||||
word = word.strip(strip)
|
||||
if len(word) > 2:
|
||||
words[word] = +1
|
||||
|
||||
for word in sorted(words):
|
||||
print("'{0}' occurs {1} times.".format(word, words[word]))
|
||||
|
||||
|
49
MY_BOOK/ebook_src/builtin_structures/delete_duplicate_char_str.py
Executable file
49
MY_BOOK/ebook_src/builtin_structures/delete_duplicate_char_str.py
Executable file
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' find and delete all the duplicate characters in a string '''
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def delete_unique(str1):
|
||||
'''
|
||||
>>> delete_unique("Trust no one")
|
||||
'on'
|
||||
>>> delete_unique("Mulder?")
|
||||
''
|
||||
'''
|
||||
|
||||
str_strip = ''.join(str1.split())
|
||||
repeat = Counter()
|
||||
|
||||
for c in str_strip:
|
||||
repeat[c] += 1
|
||||
|
||||
result = ''
|
||||
for c, count in repeat.items():
|
||||
if count > 1:
|
||||
result += c
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def removing_duplicates_seq(str1):
|
||||
'''
|
||||
>>> delete_unique("Trust no one")
|
||||
'on'
|
||||
>>> delete_unique("Mulder?")
|
||||
''
|
||||
'''
|
||||
seq = str1.split()
|
||||
result = set()
|
||||
for item in seq:
|
||||
if item not in result:
|
||||
#yield item
|
||||
result.add(item)
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
60
MY_BOOK/ebook_src/builtin_structures/fibonacci.py
Executable file
60
MY_BOOK/ebook_src/builtin_structures/fibonacci.py
Executable file
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def fib_generator():
|
||||
a, b = 0, 1
|
||||
|
||||
while True:
|
||||
yield b
|
||||
a, b = b, a+b
|
||||
|
||||
|
||||
def fib(n):
|
||||
'''
|
||||
>>> fib(2)
|
||||
1
|
||||
>>> fib(5)
|
||||
5
|
||||
>>> fib(7)
|
||||
13
|
||||
'''
|
||||
if n < 3:
|
||||
return 1
|
||||
|
||||
a, b = 0, 1
|
||||
count = 1
|
||||
|
||||
while count < n:
|
||||
count += 1
|
||||
a, b = b, a+b
|
||||
|
||||
return b
|
||||
|
||||
|
||||
def fib_rec(n):
|
||||
'''
|
||||
>>> fib_rec(2)
|
||||
1
|
||||
>>> fib_rec(5)
|
||||
5
|
||||
>>> fib_rec(7)
|
||||
13
|
||||
'''
|
||||
if n < 3:
|
||||
return 1
|
||||
return fib_rec(n - 1) + fib_rec(n - 2)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
fib = fib_generator()
|
||||
print(next(fib))
|
||||
print(next(fib))
|
||||
print(next(fib))
|
||||
print(next(fib))
|
|
@ -1,8 +1,8 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
def find_0_MxN(m):
|
||||
''' find 0s in a matrix and replace the col and row to 0s:
|
||||
>>> m1 = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]
|
||||
|
@ -13,6 +13,7 @@ def find_0_MxN(m):
|
|||
[[0, 2, 3, 4], [0, 0, 0, 0], [0, 10, 11, 12], [0, 14, 15, 16]]
|
||||
'''
|
||||
index = []
|
||||
|
||||
for row in range(len(m)):
|
||||
for col in range(len(m[0])):
|
||||
if m[row][col] == 0:
|
||||
|
@ -24,9 +25,10 @@ def find_0_MxN(m):
|
|||
m[row][i] = 0
|
||||
for i in range(len(m[0])):
|
||||
m[i][col] = 0
|
||||
|
||||
return m
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
32
MY_BOOK/ebook_src/builtin_structures/find_dice_probabilities.py
Executable file
32
MY_BOOK/ebook_src/builtin_structures/find_dice_probabilities.py
Executable file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
'''
|
||||
given 2 dice, determine number of ways to sum S if all dice are rolled
|
||||
'''
|
||||
|
||||
from collections import Counter, defaultdict
|
||||
|
||||
def find_dice_probabilities(S=5, n_faces=6):
|
||||
if S > 2*n_faces or S < 2:
|
||||
return None
|
||||
|
||||
cdict = Counter()
|
||||
ddict = defaultdict(list)
|
||||
|
||||
for dice1 in range(1, n_faces+1):
|
||||
for dice2 in range(1, n_faces+1):
|
||||
t = [dice1, dice2]
|
||||
cdict[dice1+dice2] += 1
|
||||
ddict[dice1+dice2].append( t)
|
||||
|
||||
return [cdict[S], ddict[S]]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
results = find_dice_probabilities()
|
||||
assert(results[0] == len(results[1]))
|
43
MY_BOOK/ebook_src/builtin_structures/find_edit_distance.py
Executable file
43
MY_BOOK/ebook_src/builtin_structures/find_edit_distance.py
Executable file
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' computes the edit distance between two strings '''
|
||||
|
||||
|
||||
def find_edit_distance(str1, str2):
|
||||
'''
|
||||
>>> s = 'sunday'
|
||||
>>> t = 'saturday'
|
||||
>>> find_edit_distance(s, t)
|
||||
3
|
||||
'''
|
||||
|
||||
m = len(str1)
|
||||
n = len(str2)
|
||||
diff = lambda c1, c2: 0 if c1 == c2 else 1
|
||||
|
||||
E = [[0] * (n + 1) for i in range(m + 1)]
|
||||
|
||||
for i in range(m + 1):
|
||||
E[i][0] = i
|
||||
|
||||
for j in range(1, n + 1):
|
||||
E[0][j] = j
|
||||
|
||||
for i in range(1, m + 1):
|
||||
for j in range(1, n + 1):
|
||||
E[i][j] = min(E[i-1][j] + 1, E[i][j-1] + 1, E[i-1][j-1] + diff(str1[i-1], str2[j-1]))
|
||||
|
||||
return E[m][n]
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
|
||||
|
||||
|
29
MY_BOOK/ebook_src/builtin_structures/find_first_non_repetead_char.py
Executable file
29
MY_BOOK/ebook_src/builtin_structures/find_first_non_repetead_char.py
Executable file
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def find_non_rep_char(s1):
|
||||
'''
|
||||
>>> s1 = 'aabbcceff'
|
||||
>>> find_non_rep_char(s1)
|
||||
e
|
||||
>>> find_non_rep_char('ccc')
|
||||
'''
|
||||
|
||||
aux_dict = Counter()
|
||||
|
||||
for i in s1:
|
||||
aux_dict[i] += 1
|
||||
|
||||
for k, v in aux_dict.items():
|
||||
if v < 2:
|
||||
print k
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
17
src/neat_builtin_examples/numbers/finding_gcd.py → MY_BOOK/ebook_src/builtin_structures/find_gcd.py
Normal file → Executable file
17
src/neat_builtin_examples/numbers/finding_gcd.py → MY_BOOK/ebook_src/builtin_structures/find_gcd.py
Normal file → Executable file
|
@ -1,13 +1,14 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
def finding_gcd(a, b):
|
||||
''' implements the greatest common divider algorithm '''
|
||||
while(b != 0):
|
||||
result = b
|
||||
a, b = b, a % b
|
||||
a, b = b, a % b
|
||||
return result
|
||||
|
||||
|
||||
|
@ -22,7 +23,7 @@ if __name__ == '__main__':
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
36
MY_BOOK/ebook_src/builtin_structures/find_if_substr.py
Executable file
36
MY_BOOK/ebook_src/builtin_structures/find_if_substr.py
Executable file
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def find_substr(s1, s2):
|
||||
|
||||
if len(s1) < len(s2):
|
||||
bs = s2
|
||||
ss = s1
|
||||
else:
|
||||
bs = s1
|
||||
ss = s2
|
||||
|
||||
ps = 0
|
||||
|
||||
for c in bs:
|
||||
|
||||
if ss[ps] == c:
|
||||
ps += 1
|
||||
else:
|
||||
ps = 0
|
||||
|
||||
if ps == len(ss)-1:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
s1 = 'buffy is a vampire slayer'
|
||||
s2 = 'vampire'
|
||||
s3 = 'angel'
|
||||
assert(find_substr(s2, s1) == True)
|
||||
assert(find_substr(s3, s1) == False)
|
32
MY_BOOK/ebook_src/builtin_structures/find_if_unique_char.py
Executable file
32
MY_BOOK/ebook_src/builtin_structures/find_if_unique_char.py
Executable file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
import collections
|
||||
|
||||
def find_if_unique_chars(word):
|
||||
"""
|
||||
>>> find_if_unique_chars('abcde')
|
||||
True
|
||||
>>> find_if_unique_chars('abcae')
|
||||
False
|
||||
"""
|
||||
|
||||
unique = True
|
||||
|
||||
counter = collections.Counter()
|
||||
|
||||
for c in word:
|
||||
if not counter[c]:
|
||||
counter[c] += 1
|
||||
else:
|
||||
unique = False
|
||||
|
||||
return unique
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
42
MY_BOOK/ebook_src/builtin_structures/find_largest_sum.py
Executable file
42
MY_BOOK/ebook_src/builtin_structures/find_largest_sum.py
Executable file
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
'''
|
||||
You are given an array of integers (both positive and negative).
|
||||
Find the contiguous sequence with the largest sum.
|
||||
'''
|
||||
|
||||
|
||||
def find_largest_sum(array):
|
||||
'''
|
||||
>>> find_largest_sum([-1, 2, -3, 5, 3, 1, -16, 7, 1, -13, 1])
|
||||
9
|
||||
>>> find_largest_sum([])
|
||||
0
|
||||
>>> find_largest_sum([1])
|
||||
1
|
||||
'''
|
||||
|
||||
sum_ = 0
|
||||
sum_here = 0
|
||||
|
||||
for i in array:
|
||||
|
||||
sum_here += i
|
||||
|
||||
if sum_here < 0:
|
||||
sum_here = 0
|
||||
|
||||
if sum_ < sum_here:
|
||||
sum_ = sum_here
|
||||
|
||||
return sum_
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
34
MY_BOOK/ebook_src/builtin_structures/find_longest_inc_subseq.py
Executable file
34
MY_BOOK/ebook_src/builtin_structures/find_longest_inc_subseq.py
Executable file
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' find the longest continuous increasing subsequence'''
|
||||
|
||||
|
||||
def find_long_con_inc(seq):
|
||||
'''
|
||||
>>> find_long_con_inc([1, -2, 3, 5, 1, -1, 4, -1, 6])
|
||||
[-2, 3, 5]
|
||||
>>> find_long_con_inc([1, 3, -2, 3, 5, 6])
|
||||
[-2, 3, 5, 6]
|
||||
>>> find_long_con_inc([1, 3, 4, -13, 2, 5, 8, -1, 2,-17])
|
||||
[-13, 2, 5, 8]
|
||||
'''
|
||||
|
||||
res, aux = [], []
|
||||
seq.append(-float('infinity'))
|
||||
|
||||
for i, n in enumerate(seq[:-1]):
|
||||
aux.append(n)
|
||||
if n > seq[i+1]:
|
||||
if len(res) < len(aux):
|
||||
res = aux[:]
|
||||
aux = []
|
||||
|
||||
return res
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
38
MY_BOOK/ebook_src/builtin_structures/find_longest_str_unique_chars.py
Executable file
38
MY_BOOK/ebook_src/builtin_structures/find_longest_str_unique_chars.py
Executable file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
'''
|
||||
given a string, find longest string with unique characters
|
||||
'''
|
||||
|
||||
def find_longest(string):
|
||||
'''
|
||||
>>> find_longest('abfgrhgtrdsandwejfhdasjcbdsjvrejwghireeej')
|
||||
'wejfhdas'
|
||||
>>> find_longest('abcabcabcabcdefabcccc')
|
||||
'defabc'
|
||||
'''
|
||||
maxs = ''
|
||||
result = ''
|
||||
|
||||
for c in string:
|
||||
if c in result:
|
||||
if len(maxs) < len(result):
|
||||
maxs = result
|
||||
result = ''
|
||||
else:
|
||||
result += c
|
||||
|
||||
if result and len(maxs) < len(result):
|
||||
maxs = result
|
||||
|
||||
return maxs
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
33
MY_BOOK/ebook_src/builtin_structures/find_non_repeating_number.py
Executable file
33
MY_BOOK/ebook_src/builtin_structures/find_non_repeating_number.py
Executable file
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def find_unique_number(array):
|
||||
'''
|
||||
>>> find_unique_number([1, 3, 6, 1, 5, 6, 9, 3, 7])
|
||||
[1, 6, 3]
|
||||
>>> find_unique_number([1, 3, 5, 6, 9, 7])
|
||||
[]
|
||||
'''
|
||||
|
||||
table = defaultdict()
|
||||
total = []
|
||||
|
||||
for i in array:
|
||||
if i in table:
|
||||
total.append(i)
|
||||
else:
|
||||
table[i] = 1
|
||||
|
||||
return total
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
42
MY_BOOK/ebook_src/builtin_structures/find_product_without_division.py
Executable file
42
MY_BOOK/ebook_src/builtin_structures/find_product_without_division.py
Executable file
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
'''Given an array of numbers, replace each number with the product of all
|
||||
the numbers in the array except the number itself *without* using division
|
||||
'''
|
||||
|
||||
|
||||
def find_product_without_division(seq):
|
||||
'''
|
||||
>>> seq = [2,3,4]
|
||||
>>> find_product_without_division(seq)
|
||||
[12, 8, 6]
|
||||
'''
|
||||
|
||||
forw = []
|
||||
bacw = []
|
||||
|
||||
for i in range(len(seq)):
|
||||
|
||||
prod_f, prod_b = 1, 1
|
||||
|
||||
for next in range(i+1, len(seq)):
|
||||
prod_f *= seq[next]
|
||||
|
||||
for before in range(0, i):
|
||||
prod_b *= seq[before]
|
||||
|
||||
forw.append(prod_f)
|
||||
bacw.append(prod_b)
|
||||
|
||||
for i in range(len(seq)):
|
||||
seq[i] = forw[i] * bacw[i]
|
||||
|
||||
return seq
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
26
src/neat_builtin_examples/dicts/find_top_N_recurring_words.py → MY_BOOK/ebook_src/builtin_structures/find_top_N_recurring_words.py
Normal file → Executable file
26
src/neat_builtin_examples/dicts/find_top_N_recurring_words.py → MY_BOOK/ebook_src/builtin_structures/find_top_N_recurring_words.py
Normal file → Executable file
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import Counter
|
||||
|
||||
|
@ -8,24 +8,20 @@ def find_top_N_recurring_words(seq, N):
|
|||
''' find the top N recurring words in a file:
|
||||
1) use a hash table to find counts
|
||||
2) sort the list on base of the maximum counts
|
||||
3) return the last N words '''
|
||||
3) return the last N words
|
||||
'''
|
||||
|
||||
dcounter = Counter()
|
||||
for word in seq.split():
|
||||
dcounter[word] += 1
|
||||
dcounter[word] += 1
|
||||
|
||||
return dcounter.most_common(N)
|
||||
|
||||
|
||||
|
||||
def test_find_top_N_recurring_words(module_name='this module'):
|
||||
seq = 'buffy angel monster xander a willow gg buffy the monster super buffy angel'
|
||||
N = 3
|
||||
assert(find_top_N_recurring_words(seq, N) == [('buffy', 3), ('monster', 2), ('angel', 2)])
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_find_top_N_recurring_words()
|
||||
seq = 'buffy angel monster xander a willow gg buffy the monster super buffy angel'
|
||||
N = 3
|
||||
assert(find_top_N_recurring_words(seq, N) == [('buffy', 3), ('monster', 2), ('angel', 2)])
|
||||
|
||||
|
56
MY_BOOK/ebook_src/builtin_structures/find_two_missing_numbers_in_sequence.py
Executable file
56
MY_BOOK/ebook_src/builtin_structures/find_two_missing_numbers_in_sequence.py
Executable file
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
"""
|
||||
Two numbers out of n numbers from 1 to n are missing.
|
||||
The remaining n-2 numbers are in the array but not sorted.
|
||||
Find the missing numbers The sum1 is the sum of all the elements in n.
|
||||
The sum2 is the sum of all the elements in n-2. sum1 - sum2 = num1 + num2 = s.
|
||||
The prod1 is the prod of all the elements in n. The prod2 is the prod of all
|
||||
the elements in n-2. prod1/prod2 = num1*num2 =p.
|
||||
Runtime is O(n), because it scan 3 times. Space is O(1)
|
||||
|
||||
In case of finding one integer, Let the missing number be M. We know that
|
||||
the sum of first N natural numbers is N*(N+1)/2. Traverse through the array
|
||||
once and calculate the sum. This is the sum of first N natural numbers -
|
||||
M or S=N*(N+1)/2 - M. Therefore M = N*(N+1)/2 - S.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
def find_two_missing_numbers(l1):
|
||||
'''
|
||||
>>> l1 = [1, 3, 5]
|
||||
>>> find_two_missing_numbers(l1)
|
||||
(4, 2)
|
||||
'''
|
||||
|
||||
n_min_2 = len(l1)
|
||||
n = n_min_2 + 2
|
||||
sum1, sum2, prod1, prod2 = 0, 0, 1, 1
|
||||
sum2 = sum(l1[:])
|
||||
sum1 = sum(range(1,n+1))
|
||||
s = sum1 - sum2
|
||||
|
||||
for i in range(1, n-1):
|
||||
prod1 = prod1*i
|
||||
prod2 = prod2*l1[i-1]
|
||||
|
||||
prod1 = prod1*n*(n-1)
|
||||
p = prod1/prod2
|
||||
num1 = (s + math.sqrt(s*s - 4*p))/2
|
||||
num2 = (s - math.sqrt(s*s - 4*p))/2
|
||||
|
||||
return int(num1), int(num2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
29
MY_BOOK/ebook_src/builtin_structures/get_float_rep_bin.py
Executable file
29
MY_BOOK/ebook_src/builtin_structures/get_float_rep_bin.py
Executable file
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Given a real number between 0 and 1 (eg: 0.72), this method print the
|
||||
binary representation. If the Number cannot be represented accurately
|
||||
in binary, with at exit most 32 chars, print error:
|
||||
'''
|
||||
|
||||
def get_float_rep(num):
|
||||
if num >= 1 or num <= 0: return 'Error 1'
|
||||
result = '.'
|
||||
while num:
|
||||
if len(result) >= 32: return 'Error 2', result
|
||||
r = num*2
|
||||
if r >= 1:
|
||||
result += '1'
|
||||
num = r - 1
|
||||
else:
|
||||
result += '0'
|
||||
num = r
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print get_float_rep(0.72) #('Error 2', '.1011100001010001111010111000010')
|
||||
print get_float_rep(0.1) # ('Error 2', '.0001100110011001100110011001100')
|
||||
print get_float_rep(0.5) #'.1'
|
63
MY_BOOK/ebook_src/builtin_structures/interserction_two_arrays.py
Executable file
63
MY_BOOK/ebook_src/builtin_structures/interserction_two_arrays.py
Executable file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def intersection_two_arrays_sets(seq1, seq2):
|
||||
'''
|
||||
>>> intersection_two_arrays_sets([1,2,3,5,7,8], [3,5,6])
|
||||
set([3, 5])
|
||||
>>> intersection_two_arrays_sets([1,2,7,8], [3,5,6])
|
||||
set([])
|
||||
'''
|
||||
# O(n)
|
||||
set1 = set(seq1)
|
||||
set2 = set(seq2)
|
||||
|
||||
return set1.intersection(set2) #same as list(set1 & set2)
|
||||
|
||||
|
||||
def intersection_two_arrays_On2(seq1, seq2):
|
||||
'''
|
||||
>>> intersection_two_arrays_On2([1,2,3,5,7,8], [3,5,6])
|
||||
[3, 5]
|
||||
>>> intersection_two_arrays_On2([1,2,7,8], [3,5,6])
|
||||
[]
|
||||
'''
|
||||
|
||||
final = []
|
||||
|
||||
for i in seq1:
|
||||
for j in seq2:
|
||||
if i == j:
|
||||
final.append(i)
|
||||
|
||||
return final
|
||||
|
||||
|
||||
def intersection_two_arrays_On(seq1, seq2):
|
||||
'''
|
||||
>>> intersection_two_arrays_On([1,2,3,5,7,8], [3,5,6])
|
||||
[5, 3]
|
||||
>>> intersection_two_arrays_On([1,2,7,8], [3,5,6])
|
||||
[]
|
||||
'''
|
||||
|
||||
final = []
|
||||
|
||||
while seq1 and seq2:
|
||||
|
||||
if seq1[-1] == seq2[-1]:
|
||||
final.append(seq1.pop())
|
||||
seq2.pop()
|
||||
elif seq1[-1] > seq2[-1]:
|
||||
seq1.pop()
|
||||
else:
|
||||
seq2.pop()
|
||||
|
||||
return final
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
54
MY_BOOK/ebook_src/builtin_structures/max_subarray_stocks.py
Executable file
54
MY_BOOK/ebook_src/builtin_structures/max_subarray_stocks.py
Executable file
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
def beating_stock(array):
|
||||
|
||||
imin = 0
|
||||
|
||||
# first deal is just buying in the next day (1)
|
||||
deal = [array[1] - array[imin], imin, 1]
|
||||
|
||||
for i, d in enumerate(array):
|
||||
|
||||
deal_here = d - array[imin]
|
||||
|
||||
if deal_here > deal[0]:
|
||||
deal = [deal_here, imin, i]
|
||||
|
||||
elif d < array[imin]:
|
||||
imin = i
|
||||
|
||||
return deal[0], array[deal[1]], array[deal[2]]
|
||||
|
||||
|
||||
def beating_stock2(array):
|
||||
|
||||
deal = 0
|
||||
min_value = array[0]
|
||||
|
||||
for i, d in enumerate(array):
|
||||
|
||||
deal_here = d - min_value
|
||||
|
||||
if deal_here > deal:
|
||||
deal = deal_here
|
||||
|
||||
else:
|
||||
if min_value > array[i]:
|
||||
min_value = array[i]
|
||||
|
||||
return deal
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
array = [7, 2, 3, 6, 5, 8, 5, 3, 4]
|
||||
|
||||
deal = beating_stock(array)
|
||||
print("Profit: %d, buying at %d, selling at %d." %(deal[0], deal[1], deal[2]))
|
||||
|
||||
deal = beating_stock2(array)
|
||||
print "Profit: " + str(deal)
|
15
MY_BOOK/ebook_src/builtin_structures/number_of_zeros_factorial.txt
Executable file
15
MY_BOOK/ebook_src/builtin_structures/number_of_zeros_factorial.txt
Executable file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
How to know how many 0 are in 100!
|
||||
|
||||
You look for the primes that multiply to 10, i.e. 2 and 5.
|
||||
|
||||
There are more 5 than 2s so you can count the fives.
|
||||
|
||||
there is 100/5 = 20, so 20 5s. However, there are two 5s in 25, 50, 75 and 100.
|
||||
|
||||
result: 20+4 = 24
|
33
MY_BOOK/ebook_src/builtin_structures/palindrome.py
Executable file
33
MY_BOOK/ebook_src/builtin_structures/palindrome.py
Executable file
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
def is_palindrome(array):
|
||||
'''
|
||||
>>> is_palindrome('subi no onibus')
|
||||
True
|
||||
>>> is_palindrome('helllo there')
|
||||
False
|
||||
>>> is_palindrome('h')
|
||||
True
|
||||
>>> is_palindrome('')
|
||||
True
|
||||
'''
|
||||
array = array.strip(' ')
|
||||
if len(array) < 2:
|
||||
return True
|
||||
|
||||
if array[0] == array[-1]:
|
||||
return is_palindrome(array[1:-1])
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
70
MY_BOOK/ebook_src/builtin_structures/permutations.py
Executable file
70
MY_BOOK/ebook_src/builtin_structures/permutations.py
Executable file
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
def perm(str1):
|
||||
'''
|
||||
>>> perm('123')
|
||||
['123', '132', '231', '213', '312', '321']
|
||||
'''
|
||||
|
||||
if len(str1) < 2:
|
||||
return str1
|
||||
|
||||
res = []
|
||||
for i, c in enumerate(str1):
|
||||
for cc in perm(str1[i+1:] + str1[:i]):
|
||||
res.append(c + cc)
|
||||
return res
|
||||
|
||||
|
||||
def perm2(str1):
|
||||
'''
|
||||
>>> perm2('123')
|
||||
['123', '132', '213', '231', '312', '321']
|
||||
'''
|
||||
from itertools import permutations
|
||||
return [''.join(p) for p in permutations(str1)]
|
||||
|
||||
|
||||
def ispermutation(s1, s2):
|
||||
'''
|
||||
>>> ispermutation('231', '123')
|
||||
True
|
||||
>>> ispermutation('231', '153')
|
||||
False
|
||||
'''
|
||||
|
||||
from collections import Counter
|
||||
aux = Counter()
|
||||
for i in s1:
|
||||
aux[i] += 1
|
||||
for i in s2:
|
||||
aux[i] -= 1
|
||||
for v in aux.values():
|
||||
if v != 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
|
||||
|
||||
def ispermutation2(s1, s2):
|
||||
'''
|
||||
>>> ispermutation2('231', '123')
|
||||
True
|
||||
>>> ispermutation2('231', '153')
|
||||
False
|
||||
'''
|
||||
if sorted(s1) == sorted(s2):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
37
MY_BOOK/ebook_src/builtin_structures/permutations_alphanumeric.py
Executable file
37
MY_BOOK/ebook_src/builtin_structures/permutations_alphanumeric.py
Executable file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Write code to generate all possible case permutations of a given
|
||||
lower-cased string
|
||||
'''
|
||||
|
||||
def alpha_permutation(string):
|
||||
'''
|
||||
>>> alpha_permutation('0ab')
|
||||
['0Ab', '0Ab', '0ab', '0ab', '0Ba', '0Ba', '0ba', '0ba', 'ab0', 'a0b', 'a0b', 'b0a', 'b0a', 'ba0']
|
||||
>>> alpha_permutation('')
|
||||
''
|
||||
'''
|
||||
|
||||
if len(string) < 2:
|
||||
return string
|
||||
|
||||
result = []
|
||||
|
||||
for i, c in enumerate(string):
|
||||
rest = string[i+1:] + string[:i]
|
||||
for cc in alpha_permutation(rest):
|
||||
if cc.isalpha():
|
||||
result.append(c.upper() + cc)
|
||||
result.append(c + cc)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
47
MY_BOOK/ebook_src/builtin_structures/primes.py
Executable file
47
MY_BOOK/ebook_src/builtin_structures/primes.py
Executable file
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
'''
|
||||
find prime factors of a number.
|
||||
'''
|
||||
|
||||
import math
|
||||
import random
|
||||
|
||||
def find_prime_factors(n):
|
||||
'''
|
||||
>>> find_prime_factors(14)
|
||||
[2, 7]
|
||||
>>> find_prime_factors(19)
|
||||
[]
|
||||
'''
|
||||
|
||||
divisors = [d for d in range(2, n//2 + 1) if n % d == 0]
|
||||
primes = [d for d in divisors if is_prime(d)]
|
||||
|
||||
return primes
|
||||
|
||||
|
||||
def is_prime(n):
|
||||
for j in range(2, int(math.sqrt(n))):
|
||||
if (n % j) == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
''' return a n-bit prime '''
|
||||
def generate_prime(number=3):
|
||||
while 1:
|
||||
p = random.randint(pow(2, number-2), pow(2, number-1)-1)
|
||||
p = 2 * p + 1
|
||||
if find_prime_factors(p):
|
||||
return p
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
28
MY_BOOK/ebook_src/builtin_structures/prod_other_ints.py
Executable file
28
MY_BOOK/ebook_src/builtin_structures/prod_other_ints.py
Executable file
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def get_products_of_all_except_at_index(array):
|
||||
'''
|
||||
>>> a = [1, 7, 3, 4]
|
||||
>>> get_products_of_all_except_at_index(a)
|
||||
[84, 12, 28, 21]
|
||||
'''
|
||||
total = 1
|
||||
for n in array:
|
||||
total *= n
|
||||
|
||||
new_array = []
|
||||
for n in array:
|
||||
if n is not 0:
|
||||
item = total/n
|
||||
new_array.append(item)
|
||||
else:
|
||||
new_array.append(n)
|
||||
|
||||
return new_array
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
34
MY_BOOK/ebook_src/builtin_structures/ransom_note.py
Executable file
34
MY_BOOK/ebook_src/builtin_structures/ransom_note.py
Executable file
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def check_if_ransom_note(magazines, note):
|
||||
count = Counter()
|
||||
pm, pn = 0, 0
|
||||
|
||||
while pn < len(note) and pm < len(magazines):
|
||||
char_note = note[pn]
|
||||
if count[char_note]>0:
|
||||
count[char_note] -= 1
|
||||
pn += 1
|
||||
else:
|
||||
char_magazine = magazines[pm]
|
||||
count[char_magazine] += 1
|
||||
pm +=1
|
||||
|
||||
return pn == len(note)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
magazines1 = "avfegthhgrebvkdsvnijnvyijfdmckdsmovkmmfvskumvl;cdkmioswckofjbkreenyukjemjgnmkmvkmnvdkmvkr g gmvdvmldm vldfkmbldkmlvdkm"
|
||||
magazines2 = "adfsfa"
|
||||
note = "you should disobey"
|
||||
|
||||
print(check_if_ransom_note(magazines1, note))
|
||||
print(check_if_ransom_note(magazines2, note))
|
37
MY_BOOK/ebook_src/builtin_structures/reverse_string.py
Executable file
37
MY_BOOK/ebook_src/builtin_structures/reverse_string.py
Executable file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def revert(string):
|
||||
'''
|
||||
>>> s = 'hello'
|
||||
>>> revert(s)
|
||||
'olleh'
|
||||
>>> revert('')
|
||||
''
|
||||
'''
|
||||
return string[::-1]
|
||||
|
||||
|
||||
|
||||
def reverse_string_inplace(s):
|
||||
'''
|
||||
>>> s = 'hello'
|
||||
>>> reverse_string_inplace(s)
|
||||
'olleh'
|
||||
>>> reverse_string_inplace('')
|
||||
''
|
||||
'''
|
||||
if s:
|
||||
s = s[-1] + reverse_string_inplace(s[:-1])
|
||||
return s
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
45
MY_BOOK/ebook_src/builtin_structures/reverse_words.py
Executable file
45
MY_BOOK/ebook_src/builtin_structures/reverse_words.py
Executable file
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
def reversing_words(word):
|
||||
"""
|
||||
>>> reversing_words('buffy is awesome')
|
||||
'awesome is buffy'
|
||||
"""
|
||||
new_word = []
|
||||
|
||||
words = word.split(' ')
|
||||
for word in words[::-1]:
|
||||
new_word.append(word)
|
||||
|
||||
return " ".join(new_word)
|
||||
|
||||
|
||||
def reversing_words2(s):
|
||||
"""
|
||||
>>> reversing_words2('buffy is awesome')
|
||||
'awesome is buffy'
|
||||
"""
|
||||
words = s.split()
|
||||
return ' '.join(reversed(words))
|
||||
|
||||
|
||||
def reversing_words3(s):
|
||||
"""
|
||||
>>> reversing_words('buffy is awesome')
|
||||
'awesome is buffy'
|
||||
"""
|
||||
words = s.split(' ')
|
||||
words.reverse()
|
||||
return ' '.join(words)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
11
src/neat_builtin_examples/lists_and_strings/rotate_NxN.py → MY_BOOK/ebook_src/builtin_structures/rotate_NxN.py
Normal file → Executable file
11
src/neat_builtin_examples/lists_and_strings/rotate_NxN.py → MY_BOOK/ebook_src/builtin_structures/rotate_NxN.py
Normal file → Executable file
|
@ -1,8 +1,7 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def rotate_NxN(m):
|
||||
n = len(m)
|
||||
for layer in range(n//2):
|
||||
|
@ -16,7 +15,7 @@ def rotate_NxN(m):
|
|||
m[last][last-offset] = m[i][last]
|
||||
m[i][last] = top
|
||||
return m
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -25,7 +24,7 @@ def main():
|
|||
assert(rotate_NxN(m) == mr)
|
||||
m2 = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
print(rotate_NxN(m2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -1,9 +1,19 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
'''To use timeit you create a Timer object whose parameters are two Python statements. The first parameter is a Python statement that you want to time; the second parameter is a statement that will run once to set up the test. The timeit module will then time how long it takes to execute the statement some number of times. By default timeit will try to run the statement one million times. When its done it returns the time as a floating point value representing the total number of seconds. However, since it executes the statement a million times you can read the result as the number of microseconds to execute the test one time. You can also pass timeit a named parameter called number that allows you to specify how many times the test statement is executed. The following session shows how long it takes to run each of our test functions 1000 times. '''
|
||||
'''To use timeit you create a Timer object whose parameters are two Python
|
||||
statements. The first parameter is a Python statement that you want to time;
|
||||
the second parameter is a statement that will run once to set up the test.
|
||||
The timeit module will then time how long it takes to execute the statement
|
||||
some number of times. By default timeit will try to run the statement one
|
||||
million times. When its done it returns the time as a floating point value
|
||||
representing the total number of seconds. However, since it executes the
|
||||
statement a million times you can read the result as the number of
|
||||
microseconds to execute the test one time. You can also pass timeit a
|
||||
named parameter called number that allows you to specify how many times the
|
||||
test statement is executed. The following session shows how long it takes to
|
||||
run each of our test functions 1000 times. '''
|
||||
|
||||
|
||||
import timeit
|
||||
|
@ -19,7 +29,7 @@ for i in range(10000,1000001,20000):
|
|||
print("%d,%10.3f,%10.3f" % (i, lst_time, d_time))
|
||||
|
||||
|
||||
""" There rersults are:
|
||||
""" There results are:
|
||||
10000, 0.192, 0.002
|
||||
30000, 0.600, 0.002
|
||||
50000, 1.000, 0.002
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
'''To use timeit you create a Timer object whose parameters are two Python statements. The first parameter is a Python statement that you want to time; the second parameter is a statement that will run once to set up the test. The timeit module will then time how long it takes to execute the statement some number of times. By default timeit will try to run the statement one million times. When its done it returns the time as a floating point value representing the total number of seconds. However, since it executes the statement a million times you can read the result as the number of microseconds to execute the test one time. You can also pass timeit a named parameter called number that allows you to specify how many times the test statement is executed. The following session shows how long it takes to run each of our test functions 1000 times. '''
|
||||
|
24
src/neat_builtin_examples/numbers/search_entry_matrix.py → MY_BOOK/ebook_src/builtin_structures/search_entry_matrix.py
Normal file → Executable file
24
src/neat_builtin_examples/numbers/search_entry_matrix.py → MY_BOOK/ebook_src/builtin_structures/search_entry_matrix.py
Normal file → Executable file
|
@ -1,18 +1,22 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' Search an Entry in a Matrix where the Rows and columns are Sorted
|
||||
In this 2D matrix, every row is increasingly sorted from left to right,
|
||||
and every column is increasingly sorted from top to bottom.
|
||||
The runtime is O(m+n).
|
||||
'''
|
||||
|
||||
def find_elem_matrix_bool(m1, value):
|
||||
''' Search an Entry in a Matrix where the Rows and columns are Sorted
|
||||
In this 2D matrix, every row is increasingly sorted from left to right,
|
||||
and every column is increasingly sorted from top to bottom.
|
||||
The runtime is O(m+n). '''
|
||||
|
||||
found = False
|
||||
row = 0
|
||||
col = len(m1[0]) - 1
|
||||
|
||||
while row < len(m1) and col >= 0:
|
||||
|
||||
if m1[row][col] == value:
|
||||
found = True
|
||||
break
|
||||
|
@ -20,8 +24,9 @@ def find_elem_matrix_bool(m1, value):
|
|||
col-=1
|
||||
else:
|
||||
row+=1
|
||||
|
||||
return found
|
||||
|
||||
|
||||
|
||||
|
||||
def test_find_elem_matrix_bool(module_name='this module'):
|
||||
|
@ -30,9 +35,6 @@ def test_find_elem_matrix_bool(module_name='this module'):
|
|||
assert(find_elem_matrix_bool(m1,3) == False)
|
||||
m2 = [[0]]
|
||||
assert(find_elem_matrix_bool(m2,0) == True)
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
|
@ -1,11 +1,19 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def str_comp(s):
|
||||
''' basic str compression with counts of repeated char'''
|
||||
'''
|
||||
>>> s1 = 'aabcccccaaa'
|
||||
>>> str_comp(s1)
|
||||
'a2b1c5a3'
|
||||
>>> str_comp('')
|
||||
''
|
||||
'''
|
||||
|
||||
count, last = 1, ''
|
||||
list_aux = []
|
||||
for i, c in enumerate(s):
|
||||
|
@ -17,16 +25,10 @@ def str_comp(s):
|
|||
list_aux.append(c)
|
||||
count = 1
|
||||
last = c
|
||||
list_aux.append(str(count))
|
||||
list_aux.append(str(count))
|
||||
return ''.join(list_aux)
|
||||
|
||||
|
||||
def main():
|
||||
s1 = 'aabcccccaaa'
|
||||
s2 = ''
|
||||
print(str_comp(s1)) # 'a2b1c5a3'
|
||||
print(str_comp(s2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
import doctest
|
||||
doctest.testmod()
|
41
MY_BOOK/ebook_src/builtin_structures/sum_two_numbers_as_strings.py
Executable file
41
MY_BOOK/ebook_src/builtin_structures/sum_two_numbers_as_strings.py
Executable file
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
'''
|
||||
find the sum of two integers represented as strings,
|
||||
return the sum as string, i.e "123" and "456" would return "579"
|
||||
'''
|
||||
|
||||
|
||||
def get_number(s):
|
||||
count = 1
|
||||
num = 0
|
||||
p = len(s) -1
|
||||
while p >= 0:
|
||||
num = num + int(s[p])*count
|
||||
count *= 10
|
||||
p -= 1
|
||||
return num
|
||||
|
||||
|
||||
def sum_string(s1, s2):
|
||||
'''
|
||||
>>> sum_string('10', '5')
|
||||
'15'
|
||||
>>> sum_string('0', '1')
|
||||
'1'
|
||||
>>> sum_string('123', '456')
|
||||
'579'
|
||||
'''
|
||||
|
||||
n1 = get_number(s1)
|
||||
n2 = get_number(s2)
|
||||
return str(n2 + n1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from functools import wraps
|
||||
|
@ -20,7 +20,7 @@ def memo(func):
|
|||
def find_fibonacci_seq_rec(n):
|
||||
''' implements the nth fibonacci value in a recursive exponential runtime '''
|
||||
if n < 2: return n
|
||||
return find_fibonacci_seq_rec(n - 1) + find_fibonacci_seq_rec(n - 2)
|
||||
return find_fibonacci_seq_rec(n - 1) + find_fibonacci_seq_rec(n - 2)
|
||||
|
||||
def test_memo():
|
||||
n = 50
|
||||
|
@ -31,9 +31,9 @@ def test_memo():
|
|||
|
||||
if __name__ == '__main__':
|
||||
test_memo()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from itertools import combinations
|
||||
from bisect import bisect
|
||||
|
@ -14,7 +13,7 @@ def naive_longest_inc_subseq(seq):
|
|||
for sub in combinations(seq, length):
|
||||
if list(sub) == sorted(sub):
|
||||
return len(sub)
|
||||
|
||||
|
||||
|
||||
def longest_inc_subseq1(seq):
|
||||
''' an iterative algorithm for the longest increasing subsequence problem '''
|
||||
|
@ -24,8 +23,8 @@ def longest_inc_subseq1(seq):
|
|||
if idx == len(end): end.append(val)
|
||||
else: end[idx] = val
|
||||
return len(end)
|
||||
|
||||
|
||||
|
||||
|
||||
def longest_inc_subseq2(seq):
|
||||
''' another iterative algorithm for the longest increasing subsequence problem '''
|
||||
L = [1] * len(seq)
|
||||
|
@ -51,17 +50,17 @@ def memoized_longest_inc_subseq(seq):
|
|||
@benchmark
|
||||
def test_naive_longest_inc_subseq():
|
||||
print(naive_longest_inc_subseq(s1))
|
||||
|
||||
|
||||
benchmark
|
||||
def test_longest_inc_subseq1():
|
||||
def test_longest_inc_subseq1():
|
||||
print(longest_inc_subseq1(s1))
|
||||
|
||||
@benchmark
|
||||
def test_longest_inc_subseq2():
|
||||
def test_longest_inc_subseq2():
|
||||
print(longest_inc_subseq2(s1))
|
||||
|
||||
|
||||
@benchmark
|
||||
def test_memoized_longest_inc_subseq():
|
||||
def test_memoized_longest_inc_subseq():
|
||||
print(memoized_longest_inc_subseq(s1))
|
||||
|
||||
|
||||
|
@ -73,9 +72,9 @@ if __name__ == '__main__':
|
|||
test_longest_inc_subseq1()
|
||||
test_longest_inc_subseq2()
|
||||
test_memoized_longest_inc_subseq()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
@ -12,12 +14,12 @@ def change_file_ext():
|
|||
print("Usage: change_ext.py filename.old_ext 'new_ext'")
|
||||
sys.exit()
|
||||
|
||||
name = os.path.splitext(sys.argv[1])[0] + "." + sys.argv[2]
|
||||
name = os.path.splitext(sys.argv[1])[0] + "." + sys.argv[2]
|
||||
print (name)
|
||||
|
||||
|
||||
try:
|
||||
shutil.copyfile(sys.argv[1], name)
|
||||
except OSError as err:
|
||||
shutil.copyfile(sys.argv[1], name)
|
||||
except OSError as err:
|
||||
print (err)
|
||||
|
||||
|
26
src/neat_builtin_examples/lists_and_strings/count_unique_words.py → MY_BOOK/ebook_src/manipulating_files/count_unique_words_files.py
Normal file → Executable file
26
src/neat_builtin_examples/lists_and_strings/count_unique_words.py → MY_BOOK/ebook_src/manipulating_files/count_unique_words_files.py
Normal file → Executable file
|
@ -1,24 +1,28 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
import collections
|
||||
import string
|
||||
import sys
|
||||
|
||||
def count_unique_word():
|
||||
''' list every word and number of times in alphabetical order for input files '''
|
||||
words = {} # create an empty dictionary
|
||||
strip = string.whitespace + string.punctuation + string.digits + "\"'"
|
||||
def count_unique_word_file():
|
||||
if len(sys.argv) < 2:
|
||||
print "Usage: python count_unique_word.py NAMEFILE"
|
||||
|
||||
words = collections.defaultdict(int)
|
||||
strip = string.whitespace + string.punctuation + string.digits + "\"'"
|
||||
for filename in sys.argv[1:]:
|
||||
with open(filename) as file:
|
||||
for line in file:
|
||||
for line in file:
|
||||
for word in line.lower().split():
|
||||
word = word.strip(strip)
|
||||
if len(word) > 2:
|
||||
words[word] = words.get(word,0) +1
|
||||
words[word] = +1
|
||||
for word in sorted(words):
|
||||
print("'{0}' occurs {1} times.".format(word, words[word]))
|
||||
|
||||
|
||||
count_unique_word()
|
||||
|
||||
if __name__ == '__main__':
|
||||
count_unique_word_file()
|
15
MY_BOOK/ebook_src/manipulating_files/count_unique_words_frequency.py
Executable file
15
MY_BOOK/ebook_src/manipulating_files/count_unique_words_frequency.py
Executable file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
import collections
|
||||
import sys
|
||||
|
||||
def count_unique_word_freq():
|
||||
return collections.Counter(\
|
||||
sys.stdin.read().lower().split()).most_common(n)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
count_unique_word_freq()
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import sys
|
||||
|
||||
|
@ -13,7 +14,7 @@ def grep_word_from_files():
|
|||
if word in line:
|
||||
print("{0}:{1}:{2:.40}".format(filename, lino, line.rstrip()))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: grep_word_from_files.py word infile1 [infile2...]")
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue