fork(1) download
  1. from __future__ import division
  2. import re
  3. from collections import namedtuple
  4. from math import floor,ceil,log10
  5. from sys import stderr,stdin
  6.  
  7.  
  8. def gen_test():
  9. '''Generate tests'''
  10. num_pattern = re.compile('\s*(\d+)')
  11. range_pattern = re.compile('(\(\s*\d+\s*,\s*\d+\s*\))')
  12. res_pattern = re.compile('-> (true|false)')
  13. test = namedtuple('test', ['num', 'min', 'max', 'res'])
  14. for line in stdin:
  15. if not line.strip(): continue
  16. num = float(num_pattern.match(line).group(1))
  17. R = eval(range_pattern.search(line).group(1))
  18. res = eval(res_pattern.search(line).group(1).capitalize())
  19. yield test(num,R[0],R[1],res)
  20.  
  21.  
  22. ## Actuall function that carries the algorithm ##
  23. #------------------------------------------------
  24.  
  25. def check(num,min,max):
  26. # num*10^n is between min and max
  27. #-------------------------------
  28. x = num*10**(ceil(log10(min/num)))
  29. if x>=min and x<=max:
  30. return True
  31.  
  32. # if num is prefix substring of min
  33. #-------------------------------
  34. magnitude = 10**(floor(log10(min)) - floor(log10(num)))
  35. if 0 <= (min-num*magnitude)/magnitude < 1:
  36. return True
  37.  
  38. # if num is prefix substring of max
  39. #-------------------------------
  40. magnitude = 10**(floor(log10(max)) - floor(log10(num)))
  41. if 0 <= (max-num*magnitude)/magnitude < 1:
  42. return True
  43.  
  44. return False
  45.  
  46. #------------------------------------------------
  47.  
  48.  
  49. for test in gen_test():
  50. res = check(test.num,test.min,test.max)
  51. print('')
  52. print(test)
  53. print('check function result: {}'.format(res))
  54. if res==test.res:
  55. print('Pass')
  56. else:
  57. print('!!!!! FAIL !!!!!')
  58.  
Success #stdin #stdout 0.1s 10960KB
stdin
 1 is in (  5,   9) -> false
 6 is in (  5,   9) -> true
 1 is in (  5,  11) -> true  (as 10 and 11 are in the range)
 1 is in (  5, 200) -> true  (as 100.. is in the range)
11 is in (  5,  12) -> true
13 is in (  5,  12) -> false 
13 is in (  5,  22) -> true
13 is in (  5, 200) -> true  (as 130 is in the range)
 2 is in (100, 300) -> true  (as 200 is in the range)
13 is in (1200, 1299) -> false
12 is in (135, 140) -> false
13 is in (135, 140) -> true
14 is in (135, 140) -> true
15 is in (135, 140) -> false
101 is in (10101, 10999) -> true
10100 is in (10101, 10999) -> false
10999 is in (10101, 10999) -> true
109991 is in (10101, 10999) -> false
8 is in (7, 12) -> true
10102 is in (10103, 10999) -> false
10101 is in (101030,109900) -> false
 1 is in (  5,   9) -> 1 * pow(10,0) -> same                 -> false
 6 is in (  5,   9)                                          -> true
 1 is in (  5,  11) -> 1 * pow(10,1)  -> 10 is in (5,11)     -> true
 1 is in (  5, 200) -> 1 * pow(10,2)  -> 100 is in (5, 200)  -> true
11 is in (  5,  12)                                          -> true
13 is in (  5,  12) -> 13 * pow(10,0) -> same                -> false 
13 is in (  5,  22)                                          -> true
13 is in (  5, 200)                                          -> true
 2 is in (100, 300) -> 2 * pow(10,2) -> 200 is in (100,300)  -> true
 4 is in (100, 300) -> 4 * pow(10,2)  -> 400 is in (100,300) -> false
13 is in (135, 140) -> 135 - 130                             -> true
14 is in (135, 139) -> 135 - 140                             -> false
10100 is in (10101, 10999) -> false
1,   (5,   9), -> false },
6,   (5,   9), -> true },
1,   (5,  11), -> true }, // as 10 and 11 are in the range
1,   (5, 200), -> true }, // as e.g. 12 and 135 are in the range
11,   (5,  12), -> true },
13,   (5,  12), -> false },
13,   (5,  22), -> true },
13,   (5, 200), -> true }, // as 130 is in the range
2, (100, 300), -> true }, // as 200 is in the range
13, (135, 140), -> true }, // Ben Voigt
13, (136, 138), -> true }, // MSalters
stdout
test(num=1.0, min=5, max=9, res=False)
check function result: False
Pass

test(num=6.0, min=5, max=9, res=True)
check function result: True
Pass

test(num=1.0, min=5, max=11, res=True)
check function result: True
Pass

test(num=1.0, min=5, max=200, res=True)
check function result: True
Pass

test(num=11.0, min=5, max=12, res=True)
check function result: True
Pass

test(num=13.0, min=5, max=12, res=False)
check function result: False
Pass

test(num=13.0, min=5, max=22, res=True)
check function result: True
Pass

test(num=13.0, min=5, max=200, res=True)
check function result: True
Pass

test(num=2.0, min=100, max=300, res=True)
check function result: True
Pass

test(num=13.0, min=1200, max=1299, res=False)
check function result: False
Pass

test(num=12.0, min=135, max=140, res=False)
check function result: False
Pass

test(num=13.0, min=135, max=140, res=True)
check function result: True
Pass

test(num=14.0, min=135, max=140, res=True)
check function result: True
Pass

test(num=15.0, min=135, max=140, res=False)
check function result: False
Pass

test(num=101.0, min=10101, max=10999, res=True)
check function result: True
Pass

test(num=10100.0, min=10101, max=10999, res=False)
check function result: False
Pass

test(num=10999.0, min=10101, max=10999, res=True)
check function result: True
Pass

test(num=109991.0, min=10101, max=10999, res=False)
check function result: False
Pass

test(num=8.0, min=7, max=12, res=True)
check function result: True
Pass

test(num=10102.0, min=10103, max=10999, res=False)
check function result: False
Pass

test(num=10101.0, min=101030, max=109900, res=False)
check function result: False
Pass

test(num=1.0, min=5, max=9, res=False)
check function result: False
Pass

test(num=6.0, min=5, max=9, res=True)
check function result: True
Pass

test(num=1.0, min=5, max=11, res=True)
check function result: True
Pass

test(num=1.0, min=5, max=200, res=True)
check function result: True
Pass

test(num=11.0, min=5, max=12, res=True)
check function result: True
Pass

test(num=13.0, min=5, max=12, res=False)
check function result: False
Pass

test(num=13.0, min=5, max=22, res=True)
check function result: True
Pass

test(num=13.0, min=5, max=200, res=True)
check function result: True
Pass

test(num=2.0, min=100, max=300, res=True)
check function result: True
Pass

test(num=4.0, min=100, max=300, res=False)
check function result: False
Pass

test(num=13.0, min=135, max=140, res=True)
check function result: True
Pass

test(num=14.0, min=135, max=139, res=False)
check function result: False
Pass

test(num=10100.0, min=10101, max=10999, res=False)
check function result: False
Pass

test(num=1.0, min=5, max=9, res=False)
check function result: False
Pass

test(num=6.0, min=5, max=9, res=True)
check function result: True
Pass

test(num=1.0, min=5, max=11, res=True)
check function result: True
Pass

test(num=1.0, min=5, max=200, res=True)
check function result: True
Pass

test(num=11.0, min=5, max=12, res=True)
check function result: True
Pass

test(num=13.0, min=5, max=12, res=False)
check function result: False
Pass

test(num=13.0, min=5, max=22, res=True)
check function result: True
Pass

test(num=13.0, min=5, max=200, res=True)
check function result: True
Pass

test(num=2.0, min=100, max=300, res=True)
check function result: True
Pass

test(num=13.0, min=135, max=140, res=True)
check function result: True
Pass

test(num=13.0, min=136, max=138, res=True)
check function result: True
Pass