-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
66 lines (52 loc) · 2.04 KB
/
solution.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
# What's in a name?
# ..Or rather, what's a name in? For us, a particular string is where we are looking for a name.
# Task
# Test whether or not the string contains all of the letters which spell a given name, in order.
# The format
# A function passing two strings, searching for one (the name) within the other.
# ``function nameInStr(str, name){ return true || false }``
# Examples
# nameInStr('Across the rivers', 'chris') --> true
# ^ ^ ^^ ^
# c h ri s
# Contains all of the letters in 'chris', in order.
# nameInStr('Next to a lake', 'chris') --> false
# Contains none of the letters in 'chris'.
# nameInStr('Under a sea', 'chris') --> false
# ^ ^
# r s
# Contains only some of the letters in 'chris'.
# nameInStr('A crew that boards the ship', 'chris') --> false
# cr h s i
# cr h s i
# c h r s i
# ...
# Contains all of the letters in 'chris', but not in order.
# nameInStr('A live son', 'Allison') --> false
# ^ ^^ ^^^
# A li son
# Contains all of the correct letters in 'Allison', in order,
# but not enough of all of them (missing an 'l').
def name_in_str(s, name):
def find_betweens_value(prev, arr, next_el):
r = 0
next_el = next_el[0] if len(next_el) == 1 else max(next_el)
for v in arr:
if prev < v <= next_el:
r = v
return r
s = list(s.replace(' ', ''))
if not all(i in s for i in name):
return False
# indexes of every char in name
indexes = [[i for i, v in enumerate(s) if v == x] for x in name]
# print('--> indexes:', indexes)
r_i = []
# trying to get array of ascending indexes
for i, v in enumerate(indexes):
if len(v) == 1:
r_i.append(v[0])
else:
r_i.append(find_betweens_value(r_i[i - 1], v, indexes[i + 1] if i < len(indexes) - 1 else v))
# if array is ascending sorted, return true
return r_i == sorted(r_i)