acclivity

pyInterviewTask

Apr 4th, 2022 (edited)
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. myarray = []
  2. taruqueries = []
  3. sushqueries = []
  4. fin = open("TaskData.txt")
  5. for x, line in enumerate(fin):
  6.     line = line.strip()
  7.     linesplit = line.split()
  8.     linedata = []
  9.     for ns in linesplit:
  10.         try:
  11.             n = int(ns)
  12.         except:
  13.             break
  14.         linedata.append(n)
  15.     if x == 0:
  16.         arraylen = linedata[0]
  17.         tarustart = arraylen + 1
  18.     elif x < tarustart:
  19.         myarray.append(linedata[0])
  20.     elif x == tarustart:
  21.         numqueries = linedata[0]
  22.         sushstart = x + numqueries + 2
  23.     elif x == tarustart + 1:
  24.         lenquery = linedata[0]
  25.     elif x < sushstart:
  26.         taruqueries.append(linedata)
  27.     elif x == sushstart:
  28.         numqueries = linedata[0]
  29.         enddata = x + numqueries + 2
  30.     elif x == sushstart + 1:
  31.         lenquery = linedata[0]
  32.     elif x < enddata:
  33.         sushqueries.append(linedata)
  34.  
  35. print("Array: ", *myarray)
  36. print("Taru queries: ", *taruqueries)
  37. print("Sushanta queries: ", *sushqueries)
  38.  
  39.  
  40. n = 0
  41. for l, r, k in taruqueries:
  42.     # add k to elements l thru r (1-based)
  43.     for x in range(l-1, r):
  44.         myarray[x] += k
  45.     n += 1
  46.     print(f"Array after Taru query #{n}:", *myarray)
  47.  
  48.  
  49. myarray = myarray[::-1]         # Sushanta reverses the array
  50.  
  51. n = 0
  52. for l, r, k in sushqueries:
  53.     # add k to elements l thru r (1-based)
  54.     for x in range(l-1, r):
  55.         myarray[x] -= k         # Sushanta subtracts k values from array
  56.     n += 1
  57.     print(f"Array after Sushanta query #{n}:", *myarray)
  58.    
  59. # Contents of input file:-
  60. # 5         Length of array
  61. # 1         Array element #1
  62. # 2
  63. # 3
  64. # 4
  65. # 5         Array element #5
  66. # 2         Taru number of queries
  67. # 3         Taru length of query
  68. # 1 3 4     Taru query 1
  69. # 2 5 -1    Taru query 2
  70. # 2         Sushanta number of queries
  71. # 3         Sushanta length of query
  72. # 1 2 3     Sushanta l,r,k 1st query
  73. # 2 5 -2
  74.  
  75.  
  76. # Output:-
  77. # Array:  1 2 3 4 5
  78. # Taru queries:  [1, 3, 4] [2, 5, -1]
  79. # Sushanta queries:  [1, 2, 3] [2, 5, -2]
  80. # Array after Taru query #1: 5 6 7 4 5
  81. # Array after Taru query #2: 5 5 6 3 4
  82. # Array after Sushanta query #1: 1 0 6 5 5
  83. # Array after Sushanta query #2: 1 2 8 7 7
  84. #
  85. # Process finished with exit code 0
Add Comment
Please, Sign In to add comment