fork download
  1. '''
  2. Your code will run inside a Python 2.7.13 sandbox. All tests will be run by calling the solution() function.
  3.  
  4. Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.
  5.  
  6. Input/output operations are not allowed.
  7.  
  8. Your solution must be under 32000 characters in length including new lines and and other non-printing characters.
  9.  
  10. When you went undercover in Commander Lambda's organization, you set up a coded messaging system with Bunny Headquarters to allow them to send you important mission updates. Now that you're here and promoted to Henchman, you need to make sure you can receive those messages - but since you need to sneak them past Commander Lambda's spies, it won't be easy!
  11.  
  12. Bunny HQ has secretly taken control of two of the galaxy's more obscure numbers stations, and will use them to broadcast lists of numbers. They've given you a numerical key, and their messages will be encrypted within the first sequence of numbers that adds up to that key within any given list of numbers.
  13.  
  14. Given a non-empty list of positive integers l and a target positive integer t, write a function solution(l, t) which verifies if there is at least one consecutive sequence of positive integers within the list l (i.e. a contiguous sub-list) that can be summed up to the given target positive integer t (the key) and returns the lexicographically smallest list containing the smallest start and end indexes where this sequence can be found, or returns the array [-1, -1] in the case that there is no such sequence (to throw off Lambda's spies, not all number broadcasts will contain a coded message).
  15.  
  16. For example, given the broadcast list l as [4, 3, 5, 7, 8] and the key t as 12, the function solution(l, t) would return the list [0, 2] because the list l contains the sub-list [4, 3, 5] starting at index 0 and ending at index 2, for which 4 + 3 + 5 = 12, even though there is a shorter sequence that happens later in the list (5 + 7). On the other hand, given the list l as [1, 2, 3, 4] and the key t as 15, the function solution(l, t) would return [-1, -1] because there is no sub-list of list l that can be summed up to the given target value t = 15.
  17.  
  18. To help you identify the coded broadcasts, Bunny HQ has agreed to the following standards:
  19.  
  20. - Each list l will contain at least 1 element but never more than 100.
  21. - Each element of l will be between 1 and 100.
  22. - t will be a positive integer, not exceeding 250.
  23. - The first element of the list l has index 0.
  24. - For the list returned by solution(l, t), the start index must be equal or smaller than the end index.
  25.  
  26. Remember, to throw off Lambda's spies, Bunny HQ might include more than one contiguous sublist of a number broadcast that can be summed up to the key. You know that the message will always be hidden in the first sublist that sums up to the key, so solution(l, t) should only return that sublist.
  27.  
  28. Languages
  29. =========
  30.  
  31. To provide a Python solution, edit solution.py
  32. To provide a Java solution, edit Solution.java
  33.  
  34. Test cases
  35. ==========
  36. Your code should pass the following test cases.
  37. Note that it may also be run against hidden test cases not shown here.
  38.  
  39. -- Python cases --
  40. Input:
  41. solution.solution([1, 2, 3, 4], 15)
  42. Output:
  43. -1,-1
  44.  
  45. Input:
  46. solution.solution([4, 3, 10, 2, 8], 12)
  47. Output:
  48. 2,3
  49.  
  50. -- Java cases --
  51. Input:
  52. Solution.solution({1, 2, 3, 4}, 15)
  53. Output:
  54. -1,-1
  55.  
  56. Input:
  57. Solution.solution({4, 3, 10, 2, 8}, 12)
  58. Output:
  59. 2,3
  60.  
  61. Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.
  62. '''
  63. def solution(l, t):
  64. length = len(l)
  65. i = 0
  66. while i < length:
  67. summ = 0
  68. while summ < t:
  69. for j in range(i, length, 1):
  70. summ += l[j]
  71. #print (summ)
  72. if summ == t:
  73. return i, j
  74.  
  75.  
  76. i += 1
  77. return -1, -1
Success #stdin #stdout 0.01s 7184KB
stdin
Standard input is empty
stdout
Standard output is empty