FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms-python/array/missing_ranges.py at master · izanbf1803/algorithms-python · GitHub
izanbf1803
/
algorithms-python
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms-python
/
array
/
missing_ranges.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (24 loc) · 684 Bytes
Breadcrumbs
algorithms-python
/
array
/
missing_ranges.py
Copy path
File metadata and controls
27 lines (24 loc) · 684 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
## find missing ranges between low and high in the given array.
# ex) [3, 5] lo=1 hi=10 => answer: [1->2, 4, 6->10]
def
missing_ranges
(
nums
,
lo
,
hi
):
res
=
[]
start
=
lo
for
num
in
nums
:
if
num
<
start
:
continue
if
num
==
start
:
start
+=
1
continue
res
.
append
(
get_range
(
start
,
num
-
1
))
start
=
num
+
1
if
start
<=
hi
:
res
.
append
(
get_range
(
start
,
hi
))
return
res
def
get_range
(
n1
,
n2
):
if
n1
==
n2
:
return
str
(
n1
)
else
:
return
str
(
n1
)
+
"->"
+
str
(
n2
)
nums
=
[
3
,
5
,
10
,
11
,
12
,
15
,
19
]
print
(
"original:"
,
nums
)
print
(
"missing range: "
,
missing_ranges
(
nums
,
0
,
20
))
Back
|
FazBrowse Home
|
New Git URL