FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/algorithms/stack/is_sorted.py at master · pk210700/algorithms · GitHub
pk210700
algorithms
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms
/
algorithms
/
stack
/
is_sorted.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (27 loc) · 866 Bytes
Breadcrumbs
algorithms
/
algorithms
/
stack
/
is_sorted.py
Copy path
File metadata and controls
30 lines (27 loc) · 866 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
28
29
30
"""
Given a stack, a function is_sorted accepts a stack as a parameter and returns
true if the elements in the stack occur in ascending increasing order from
bottom, and false otherwise. That is, the smallest element should be at bottom
For example:
bottom [6, 3, 5, 1, 2, 4] top
The function should return false
bottom [1, 2, 3, 4, 5, 6] top
The function should return true
"""
def
is_sorted
(
stack
):
storage_stack
=
[]
for
i
in
range
(
len
(
stack
)):
if
len
(
stack
)
==
0
:
break
first_val
=
stack
.
pop
()
if
len
(
stack
)
==
0
:
break
second_val
=
stack
.
pop
()
if
first_val
<
second_val
:
return
False
storage_stack
.
append
(
first_val
)
stack
.
append
(
second_val
)
# Backup stack
for
i
in
range
(
len
(
storage_stack
)):
stack
.
append
(
storage_stack
.
pop
())
return
True
Back
|
FazBrowse Home
|
New Git URL