Advertisement
MrPolywhirl

StepRange.py

Oct 8th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import operator
  4.  
  5. def step_range(start, end, step, step_func, compare_func):
  6.     while compare_func(start, end):
  7.         yield start
  8.         start = step_func(start, step)
  9.  
  10. def main():
  11.     print [x for x in step_range(1, 64, 2, operator.mul, operator.le)]
  12.     print [x for x in step_range(64, 0, 2, operator.div, operator.gt)]
  13.     print [x for x in step_range(0, 64, 8, operator.add, operator.le)]
  14.     print [x for x in step_range(64, 0, 8, operator.sub, operator.ge)]
  15.    
  16. if __name__ == '__main__':
  17.     main()
  18.  
  19. #####################################################################
  20. # Output                                                            #
  21. # ----------------------------------                                #
  22. # [1, 2, 4, 8, 16, 32, 64]                                          #
  23. # [64, 32, 16, 8, 4, 2, 1]                                          #
  24. # [0, 8, 16, 24, 32, 40, 48, 56, 64]                                #
  25. # [64, 56, 48, 40, 32, 24, 16, 8, 0]                                #
  26. #####################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement