-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberOfItems.py
112 lines (84 loc) · 2.73 KB
/
numberOfItems.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'numberOfItems' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER_ARRAY startIndices
# 3. INTEGER_ARRAY endIndices
#
# We need 2 loops?
# Outer loop for start and end indices - n
# Inner loop for within strings
def numberOfItems(s, startIndices, endIndices):
res = []
stack = []
for starti, endi in zip(startIndices, endIndices):
cut_string = s[starti-1:endi]
local_count = 0
for i in cut_string:
if i == "|":
if stack == []:
stack.append(0)
# if first | include in stack
else:
local_count += sum(stack)
stack = [0] # reset
# if second count stars
elif i == "*":
if stack == []:
continue
else:
stack.append(1)
print(i, stack, local_count)
res.append(local_count)
return res
# def numberOfItems(s, startIndices, endIndices):
# item_count = [] # Will hold the list of counts for each item start-end pair
# for starti, endi in zip(startIndices, endIndices):
# cut_string = s[starti-1:endi]
# print(cut_string)
# star_count = 0
# star_pipe_count = 0
# pipe_count = 0
# for i in cut_string:
# if i == "*":
# star_count += 1
# elif i == "|":
# if (pipe_count > 0) and (pipe_count % 2 == 0):
# star_pipe_count += star_count
# star_count = 0
# pipe_count += 1
# else:
# pipe_count = 1
# star_count = 0
# print(star_count, star_pipe_count, pipe_count)
# if (pipe_count > 0) and (pipe_count % 2 == 0):
# item_count.append(star_pipe_count)
# else:
# item_count.append(0)
# return item_count
if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
startIndices_count = int(input().strip())
startIndices = []
for _ in range(startIndices_count):
startIndices_item = int(input().strip())
startIndices.append(startIndices_item)
endIndices_count = int(input().strip())
endIndices = []
for _ in range(endIndices_count):
endIndices_item = int(input().strip())
endIndices.append(endIndices_item)
result = numberOfItems(s, startIndices, endIndices)
print('\n'.join(map(str, result)))
print('\n')
z
fptr.close()