t489.py 998 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import random
  2. def makeset(lst):
  3. result = {}
  4. for a in lst:
  5. if not result.has_key(a):
  6. result[a] = []
  7. result[a].append(True)
  8. return result
  9. def sorttest(lst1):
  10. lst2 = lst1[:]
  11. lst2.sort()
  12. assert len(lst1) == len(lst2)
  13. assert makeset(lst1) == makeset(lst2)
  14. position = {}
  15. i = 0
  16. err = False
  17. for a in lst1:
  18. if not position.has_key(a):
  19. position[a] = []
  20. position[a].append(i)
  21. i += 1
  22. for i in range(len(lst2)-1):
  23. a, b = lst2[i], lst2[i+1]
  24. if not a <= b:
  25. print "resulting list is not sorted"
  26. err = True
  27. if a == b:
  28. if not position[a][0] < position[b][-1]:
  29. print "not stable"
  30. err = True
  31. if err:
  32. print lst1
  33. print lst2
  34. for v in range(137):
  35. up = 1 + int(v * random.random() * 2.7)
  36. lst1 = [random.randrange(0, up) for i in range(v)]
  37. sorttest(lst1)
  38. print "everything's fine"