-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_last.py
58 lines (48 loc) · 1.1 KB
/
first_last.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
# Given a sorted array, A, with possibly duplicated elements,
# find the indices of the first and last occurrences of a
# target element, x. Return -1 if the target is not found.
def first_last(l, target):
res = list()
for i in range(len(l)):
if l[i] == target:
if len(res) == 0:
res.append(i)
elif len(res) == 1:
res.append(i)
else:
res = res[:-1]
res.append(i)
if len(res) == 0:
print([-1,-1])
elif len(res) == 1:
print(res*2)
else:
print(res)
r = []
t = 2
first_last(r,t)
#ugly solution - first
def first_last(l, target):
first_found = False
second_found = False
res = list()
for i in range(len(l)):
if l[i] == target:
if first_found == False:
first_found = True
res.append(i)
elif second_found == False:
second_found = True
res.append(i)
elif second_found:
res.append(i)
if first_found == False:
print([-1,-1])
else:
if len(res) > 2:
print(res[0], res[-1])
else:
print(res)
r = [2,2,3,4,5,2]
t = 2
first_last(r,t)