Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- myarray = []
- taruqueries = []
- sushqueries = []
- fin = open("TaskData.txt")
- for x, line in enumerate(fin):
- line = line.strip()
- linesplit = line.split()
- linedata = []
- for ns in linesplit:
- try:
- n = int(ns)
- except:
- break
- linedata.append(n)
- if x == 0:
- arraylen = linedata[0]
- tarustart = arraylen + 1
- elif x < tarustart:
- myarray.append(linedata[0])
- elif x == tarustart:
- numqueries = linedata[0]
- sushstart = x + numqueries + 2
- elif x == tarustart + 1:
- lenquery = linedata[0]
- elif x < sushstart:
- taruqueries.append(linedata)
- elif x == sushstart:
- numqueries = linedata[0]
- enddata = x + numqueries + 2
- elif x == sushstart + 1:
- lenquery = linedata[0]
- elif x < enddata:
- sushqueries.append(linedata)
- print("Array: ", *myarray)
- print("Taru queries: ", *taruqueries)
- print("Sushanta queries: ", *sushqueries)
- n = 0
- for l, r, k in taruqueries:
- # add k to elements l thru r (1-based)
- for x in range(l-1, r):
- myarray[x] += k
- n += 1
- print(f"Array after Taru query #{n}:", *myarray)
- myarray = myarray[::-1] # Sushanta reverses the array
- n = 0
- for l, r, k in sushqueries:
- # add k to elements l thru r (1-based)
- for x in range(l-1, r):
- myarray[x] -= k # Sushanta subtracts k values from array
- n += 1
- print(f"Array after Sushanta query #{n}:", *myarray)
- # Contents of input file:-
- # 5 Length of array
- # 1 Array element #1
- # 2
- # 3
- # 4
- # 5 Array element #5
- # 2 Taru number of queries
- # 3 Taru length of query
- # 1 3 4 Taru query 1
- # 2 5 -1 Taru query 2
- # 2 Sushanta number of queries
- # 3 Sushanta length of query
- # 1 2 3 Sushanta l,r,k 1st query
- # 2 5 -2
- # Output:-
- # Array: 1 2 3 4 5
- # Taru queries: [1, 3, 4] [2, 5, -1]
- # Sushanta queries: [1, 2, 3] [2, 5, -2]
- # Array after Taru query #1: 5 6 7 4 5
- # Array after Taru query #2: 5 5 6 3 4
- # Array after Sushanta query #1: 1 0 6 5 5
- # Array after Sushanta query #2: 1 2 8 7 7
- #
- # Process finished with exit code 0
Add Comment
Please, Sign In to add comment