t429.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. def helper(x,y,expect):
  2. l = [0]*6
  3. if expect < 0: # x < y
  4. l[0] = (x < y) == True
  5. l[1] = (x <= y) == True
  6. l[2] = (x > y) == False
  7. l[3] = (x >= y) == False
  8. l[4] = (x == y) == False
  9. l[5] = (x != y) == True
  10. if isinstance(x,(int,float,long,str)) or isinstance(y,(int,float,long,str)):
  11. l.append((x is y)==False)
  12. l.append((x is not y)==True)
  13. elif expect == 0: # x == y
  14. l[0] = (x < y) == False
  15. l[1] = (x <= y) == True
  16. l[2] = (x > y) == False
  17. l[3] = (x >= y) == True
  18. l[4] = (x == y) == True
  19. l[5] = (x != y) == False
  20. if isinstance(x,(int,float,long,str)) or isinstance(y,(int,float,long,str)):
  21. l.append((x is y)==True)
  22. l.append((x is not y)==False)
  23. elif expect > 0: # x > y
  24. l[0] = (x < y) == False
  25. l[1] = (x <= y) == False
  26. l[2] = (x > y) == True
  27. l[3] = (x >= y) == True
  28. l[4] = (x == y) == False
  29. l[5] = (x != y) == True
  30. if isinstance(x,(int,float,long,str)) or isinstance(y,(int,float,long,str)):
  31. l.append((x is y)==False)
  32. l.append((x is not y)==True)
  33. if not isinstance(x,(int,float,long,str)) and not isinstance(y,(int,float,long,str)):
  34. l.append((x is y)==False)
  35. l.append((x is not y)==True)
  36. if all(l):
  37. print True
  38. else:
  39. print False,x,y,l
  40. print "\nINTEGERS"
  41. helper(1,2,-1)
  42. helper(1,1,0)
  43. helper(2,1,1)
  44. helper(-2,-1,-1)
  45. helper(-2,-2,0)
  46. helper(-1,-2,1)
  47. helper(-1,1,-1)
  48. helper(1,-1,1)
  49. print "\nLONG INTEGERS"
  50. helper(1L,2L,-1)
  51. helper(2L,1L,1)
  52. helper(-1L,1L,-1)
  53. helper(1L,-1L,1)
  54. print "\nFLOATING POINT"
  55. helper(1.0,2.0,-1)
  56. helper(1.0,1.0,0)
  57. helper(2.0,1.0,1)
  58. helper(-2.0,-1.0,-1)
  59. helper(-2.0,-2.0,0)
  60. helper(-1.0,-2.0,1)
  61. helper(-1.0,1.0,-1)
  62. helper(1.0,-1.0,1)
  63. print "\nLISTS"
  64. helper([],[1],-1)
  65. helper([1,2],[1,2],0)
  66. helper([1,2,3],[1,2],1)
  67. helper([1,2],[2,1],-1)
  68. helper([1,2,3],[1,2,1,5],1)
  69. print "\nTUPLES"
  70. helper(tuple(),(1,),-1)
  71. helper((1,2),(1,2),0)
  72. helper((1,2,3),(1,2),1)
  73. helper((1,2),(2,1),-1)
  74. helper((1,2,3),(1,2,1,5),1)
  75. print "\nSTRINGS"
  76. helper('','a',-1)
  77. helper('a','a',0)
  78. helper('ab','a',1)
  79. helper('ABCD','abcd',-1)
  80. helper('ABCD','ABCD',0)
  81. helper('aBCD','Abcd',1)
  82. class A:
  83. def __init__(self,x): self.x = x
  84. def __cmp__(self,other): return self.x
  85. print "\nUSER-DEFINED OBJECTS"
  86. helper(A(-1),A(1),-1)
  87. helper(A(0),A(0),0)
  88. helper(A(1),A(-1),1)