|
| 1 | +""" |
| 2 | +>>> hash(42) |
| 3 | +42 |
| 4 | +""" |
| 5 | +# The same in 2.6 and 3.1 -- but not in 2.7 |
| 6 | +# >>> hash("Hello, world!") |
| 7 | +# -943387004357456228 |
| 8 | + |
| 9 | +def test_1(): |
| 10 | + n = 1000 |
| 11 | + nums = [0]*n |
| 12 | + nums.append(1) |
| 13 | + nums.insert(0,1) |
| 14 | + |
| 15 | +def dump_linked_list(L): |
| 16 | + res = [] |
| 17 | + while L is not None: |
| 18 | + res.append(L.value) |
| 19 | + L = L.next |
| 20 | + return res |
| 21 | + |
| 22 | +def test_loop_asymptotics(): |
| 23 | + seq = range(10) |
| 24 | + |
| 25 | + s = 0 |
| 26 | + for x in seq: |
| 27 | + s += x |
| 28 | + |
| 29 | + assert(s == sum(seq)) |
| 30 | + |
| 31 | + squares = [x**2 for x in seq] |
| 32 | + |
| 33 | + s = 0 |
| 34 | + for x in seq: |
| 35 | + for y in seq: |
| 36 | + s += x*y |
| 37 | + for z in seq: |
| 38 | + for w in seq: |
| 39 | + s += x-w |
| 40 | + |
| 41 | + seq1 = range(10) |
| 42 | + seq2 = range(5) |
| 43 | + |
| 44 | + s = 0 |
| 45 | + for x in seq1: |
| 46 | + for y in seq2: |
| 47 | + s += x*y |
| 48 | + |
| 49 | + seq1 = [[0, 1], [2], [3, 4, 5]] |
| 50 | + s = 0 |
| 51 | + for seq2 in seq1: |
| 52 | + for x in seq2: |
| 53 | + s += x |
| 54 | + |
| 55 | + seq = range(10) |
| 56 | + s = 0 |
| 57 | + n = len(seq) |
| 58 | + for i in range(n-1): |
| 59 | + for j in range(i+1, n): |
| 60 | + s += seq[i] * seq[j] |
| 61 | + |
| 62 | +def test_timeit(): |
| 63 | + # Has been tested -- ignored here because timing details vary, and the |
| 64 | + # tests are slow |
| 65 | + IGNORE = """ |
| 66 | + >>> timeit.timeit("x = 2 + 2") |
| 67 | + 0.034976959228515625 |
| 68 | + >>> timeit.timeit("x = sum(range(10))") |
| 69 | + 0.92387008666992188 |
| 70 | + """ |
| 71 | + |
| 72 | +def test_linked_list(): |
| 73 | + """ |
| 74 | + >>> Node = test_linked_list() |
| 75 | + >>> L = Node("a", Node("b", Node("c", Node("d")))) |
| 76 | + >>> L.next.next.value |
| 77 | + 'c' |
| 78 | + >>> b = L.next |
| 79 | + >>> c = b.next |
| 80 | + >>> b.next = Node("x", c) |
| 81 | + >>> dump_linked_list(L) |
| 82 | + ['a', 'b', 'x', 'c', 'd'] |
| 83 | + """ |
| 84 | + class Node: |
| 85 | + def __init__(self, value, next=None): |
| 86 | + self.value = value |
| 87 | + self.next = next |
| 88 | + return Node |
| 89 | + |
| 90 | +def test_listing_2_1(): |
| 91 | + """ |
| 92 | + >>> N = test_listing_2_1() |
| 93 | + >>> a, b, c, d, e, f, g, h = range(8) |
| 94 | + >>> b in N[a] # Neighborhood membership |
| 95 | + True |
| 96 | + >>> len(N[f]) # Degree |
| 97 | + 3 |
| 98 | + """ |
| 99 | + |
| 100 | + a, b, c, d, e, f, g, h = range(8) |
| 101 | + N = [ |
| 102 | + [b, c, d, e, f], # a |
| 103 | + [c, e], # b |
| 104 | + [d], # c |
| 105 | + [e], # d |
| 106 | + [f], # e |
| 107 | + [c, g, h], # f |
| 108 | + [f, h], # g |
| 109 | + [f, g] # h |
| 110 | + ] |
| 111 | + |
| 112 | + return N |
| 113 | + |
| 114 | +def test_listing_2_2(): |
| 115 | + """ |
| 116 | + >>> N = test_listing_2_2() |
| 117 | + >>> a, b, c, d, e, f, g, h = range(8) |
| 118 | + >>> b in N[a] # Neighborhood membership |
| 119 | + True |
| 120 | + >>> len(N[f]) # Degree |
| 121 | + 3 |
| 122 | + """ |
| 123 | + |
| 124 | + a, b, c, d, e, f, g, h = range(8) |
| 125 | + N = [ |
| 126 | + {b, c, d, e, f}, # a |
| 127 | + {c, e}, # b |
| 128 | + {d}, # c |
| 129 | + {e}, # d |
| 130 | + {f}, # e |
| 131 | + {c, g, h}, # f |
| 132 | + {f, h}, # g |
| 133 | + {f, g} # h |
| 134 | + ] |
| 135 | + |
| 136 | + return N |
| 137 | + |
| 138 | +def test_listing_2_3(): |
| 139 | + """ |
| 140 | + >>> N = test_listing_2_3() |
| 141 | + >>> a, b, c, d, e, f, g, h = range(8) |
| 142 | + >>> b in N[a] # Neighborhood membership |
| 143 | + True |
| 144 | + >>> len(N[f]) # Degree |
| 145 | + 3 |
| 146 | + >>> N[a][b] # Edge weight for (a, b) |
| 147 | + 2 |
| 148 | + """ |
| 149 | + |
| 150 | + a, b, c, d, e, f, g, h = range(8) |
| 151 | + N = [ |
| 152 | + {b:2, c:1, d:3, e:9, f:4}, # a |
| 153 | + {c:4, e:3}, # b |
| 154 | + {d:8}, # c |
| 155 | + {e:7}, # d |
| 156 | + {f:5}, # e |
| 157 | + {c:2, g:2, h:2}, # f |
| 158 | + {f:1, h:6}, # g |
| 159 | + {f:9, g:8} # h |
| 160 | + ] |
| 161 | + |
| 162 | + return N |
| 163 | + |
| 164 | +def test_listing_2_4(): |
| 165 | + """ |
| 166 | + >>> N = test_listing_2_4() |
| 167 | + >>> 'b' in N['a'] # Neighborhood membership |
| 168 | + True |
| 169 | + >>> len(N['f']) # Degree |
| 170 | + 3 |
| 171 | + """ |
| 172 | + |
| 173 | + N = { |
| 174 | + 'a': set('bcdef'), |
| 175 | + 'b': set('ce'), |
| 176 | + 'c': set('d'), |
| 177 | + 'd': set('e'), |
| 178 | + 'e': set('f'), |
| 179 | + 'f': set('cgh'), |
| 180 | + 'g': set('fh'), |
| 181 | + 'h': set('fg') |
| 182 | + } |
| 183 | + |
| 184 | + return N |
| 185 | + |
| 186 | +def test_listing_2_5(): |
| 187 | + """ |
| 188 | + >>> N = test_listing_2_5() |
| 189 | + >>> a, b, c, d, e, f, g, h = range(8) |
| 190 | + >>> N[a][b] # Neighborhood membership |
| 191 | + 1 |
| 192 | + >>> sum(N[f]) # Degree |
| 193 | + 3 |
| 194 | + """ |
| 195 | + |
| 196 | + a, b, c, d, e, f, g, h = range(8) |
| 197 | + |
| 198 | + # a b c d e f g h |
| 199 | + |
| 200 | + N = [[0,1,1,1,1,1,0,0], # a |
| 201 | + [0,0,1,0,1,0,0,0], # b |
| 202 | + [0,0,0,1,0,0,0,0], # c |
| 203 | + [0,0,0,0,1,0,0,0], # d |
| 204 | + [0,0,0,0,0,1,0,0], # e |
| 205 | + [0,0,1,0,0,0,1,1], # f |
| 206 | + [0,0,0,0,0,1,0,1], # g |
| 207 | + [0,0,0,0,0,1,1,0]] # h |
| 208 | + |
| 209 | + return N |
| 210 | + |
| 211 | +def test_listing_2_6(): |
| 212 | + """ |
| 213 | + >>> W = test_listing_2_6() |
| 214 | + >>> a, b, c, d, e, f, g, h = range(8) |
| 215 | + >>> inf = float('inf') |
| 216 | + >>> W[a][b] < inf # Neighborhood membership |
| 217 | + True |
| 218 | + >>> W[c][e] < inf # Neighborhood membership |
| 219 | + False |
| 220 | + >>> sum(1 for w in W[a] if w < inf) - 1 # Degree |
| 221 | + 5 |
| 222 | + """ |
| 223 | + |
| 224 | + a, b, c, d, e, f, g, h = range(8) |
| 225 | + _ = float('inf') |
| 226 | + |
| 227 | + # a b c d e f g h |
| 228 | + |
| 229 | + W = [[0,2,1,3,9,4,_,_], # a |
| 230 | + [_,0,4,_,3,_,_,_], # b |
| 231 | + [_,_,0,8,_,_,_,_], # c |
| 232 | + [_,_,_,0,7,_,_,_], # d |
| 233 | + [_,_,_,_,0,5,_,_], # e |
| 234 | + [_,_,2,_,_,0,2,2], # f |
| 235 | + [_,_,_,_,_,1,0,6], # g |
| 236 | + [_,_,_,_,_,9,8,0]] # h |
| 237 | + |
| 238 | + return W |
| 239 | + |
| 240 | +def test_list_tree(): |
| 241 | + """ |
| 242 | + >>> T = [["a", "b"], ["c"], ["d", ["e", "f"]]] |
| 243 | + >>> T[0][1] |
| 244 | + 'b' |
| 245 | + >>> T[2][1][0] |
| 246 | + 'e' |
| 247 | + """ |
| 248 | + |
| 249 | +def test_listing_2_7(): |
| 250 | + """ |
| 251 | + >>> Tree = test_listing_2_7() |
| 252 | + >>> t = Tree(Tree("a", "b"), Tree("c", "d")) |
| 253 | + >>> t.right.left |
| 254 | + 'c' |
| 255 | + """ |
| 256 | + class Tree: |
| 257 | + def __init__(self, left, right=None): |
| 258 | + self.left = left |
| 259 | + self.right = right |
| 260 | + return Tree |
| 261 | + |
| 262 | + |
| 263 | +def test_listing_2_8(): |
| 264 | + """ |
| 265 | + >>> Tree = test_listing_2_8() |
| 266 | + >>> t = Tree(Tree("a", Tree("b", Tree("c", Tree("d"))))) |
| 267 | + >>> t.kids.next.next.val |
| 268 | + 'c' |
| 269 | + """ |
| 270 | + class Tree: |
| 271 | + def __init__(self, kids, next=None): |
| 272 | + self.kids = self.val = kids |
| 273 | + self.next = next |
| 274 | + return Tree |
| 275 | + |
| 276 | + |
| 277 | +def test_bunch(): |
| 278 | + """ |
| 279 | + >>> Bunch = test_bunch() |
| 280 | + >>> x = Bunch(name="Jayne Cobb", position="Public Relations") |
| 281 | + >>> x.name |
| 282 | + 'Jayne Cobb' |
| 283 | + >>> T = Bunch |
| 284 | + >>> t = T(left=T(left="a", right="b"), right=T(left="c")) |
| 285 | + >>> t.left |
| 286 | + {'right': 'b', 'left': 'a'} |
| 287 | + >>> t.left.right |
| 288 | + 'b' |
| 289 | + >>> t['left']['right'] |
| 290 | + 'b' |
| 291 | + >>> "left" in t.right |
| 292 | + True |
| 293 | + >>> "right" in t.right |
| 294 | + False |
| 295 | + """ |
| 296 | + class Bunch(dict): |
| 297 | + def __init__(self, *args, **kwds): |
| 298 | + super(Bunch, self).__init__(*args, **kwds) |
| 299 | + self.__dict__ = self |
| 300 | + return Bunch |
| 301 | + |
| 302 | +def test_hidden_squares(): |
| 303 | + """ |
| 304 | + >>> from random import randrange, seed |
| 305 | + >>> seed(529) |
| 306 | + >>> L = [randrange(10000) for i in range(1000)] |
| 307 | + >>> 42 in L |
| 308 | + False |
| 309 | + >>> S = set(L) |
| 310 | + >>> 42 in S |
| 311 | + False |
| 312 | + >>> input = ["x", "y", "z"] |
| 313 | + >>> s = "" |
| 314 | + >>> for chunk in input: |
| 315 | + ... s += chunk |
| 316 | + ... |
| 317 | + >>> chunks = [] |
| 318 | + >>> for chunk in input: |
| 319 | + ... chunks.append(chunk) |
| 320 | + ... |
| 321 | + >>> s = ''.join(chunks) |
| 322 | + >>> s = ''.join(input) |
| 323 | + >>> lists = [[42] for i in range(100)] |
| 324 | + >>> res = [] |
| 325 | + >>> for lst in lists: |
| 326 | + ... res.extend(lst) |
| 327 | + >>> res = sum(lists, []) |
| 328 | + """ |
| 329 | + |
| 330 | +def test_floats_and_decimals(): |
| 331 | + """ |
| 332 | + >>> sum(0.1 for i in range(10)) == 1.0 |
| 333 | + False |
| 334 | + >>> def almost_equal(x, y, places=7): |
| 335 | + ... return round(abs(x-y), places) == 0 |
| 336 | + >>> almost_equal(sum(0.1 for i in range(10)), 1.0) |
| 337 | + True |
| 338 | + >>> from decimal import * |
| 339 | + >>> sum(Decimal("0.1") for i in range(10)) == Decimal("1.0") |
| 340 | + True |
| 341 | + >>> from math import sqrt |
| 342 | + >>> x = 8762348761.13 |
| 343 | + >>> sqrt(x + 1) - sqrt(x) |
| 344 | + 5.341455107554793e-06 |
| 345 | + >>> 1.0/(sqrt(x + 1) + sqrt(x)) |
| 346 | + 5.3414570026237696e-06 |
| 347 | + """ |
| 348 | + |
0 commit comments