Advertisement
nprasanna

split the number without any operator

Jun 4th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. '''
  2.  
  3.  
  4. Eg:
  5. 1232 ab+cd -> a:1, b:2, c:3, d:2 -> 12+32 -> 44
  6.  
  7. Input
  8.  
  9. 3413289830 a-bcdefghij
  10. 776 a+bc
  11. 12345 a+bcde
  12. 1232 ab+cd
  13. 90602 a+bcde
  14.  
  15. Constraints:
  16. N is in range [100, 1000000000]
  17. Assume that only valid expressions are provided (no leading zeros)
  18.  
  19. I/p : 1232 ab+cd
  20.  
  21.  
  22. '''
  23.  
  24. import sys
  25.  
  26. test_cases = open(sys.argv[1], 'r')
  27. for line in test_cases.read().splitlines():        
  28.     operation = '+' if '+' in line else '-'
  29.     input = line.split()
  30.     numbers = input[0]
  31.     if int(numbers) > 100 and int(numbers) <  1000000000:
  32.         expression = input[1]
  33.         index = expression.index(operation)
  34.         left=numbers[:index]
  35.         right=numbers[index:]      
  36.        
  37.         if operation == '+':
  38.             print int(left)+int(right)
  39.         else:      
  40.             print int(left)-int(right)
  41.        
  42. test_cases.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement