-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_operations
More file actions
29 lines (25 loc) · 1 KB
/
array_operations
File metadata and controls
29 lines (25 loc) · 1 KB
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
Remove all exclamation marks from the end of sentence.
Examples
remove("Hi!") === "Hi"
remove("Hi!!!") === "Hi"
remove("!Hi") === "!Hi"
remove("!Hi!") === "!Hi"
remove("Hi! Hi!") === "Hi! Hi"
remove("Hi") === "Hi"
def remove(s):
if s[-1]=='!':
i = 1
while s[-i]=='!':
i+=1
i = i-1
return s[:-i]
else:
return s
Your task is to find the first element of an array that is not consecutive.
E.g. If we have an array [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that's the first non-consecutive number.
If the whole array is consecutive then return null or Nothing.
The array will always have at least 2 elements1 and all elements will be numbers. The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too!
def first_non_consecutive(arr):
for i in range(int(len(arr)-1)):
if abs(arr[i]-arr[i+1])>1:
return arr[i+1]