Python Exercise Example 67
**Problem:** Input an array, swap the largest element with the first element and the smallest element with the last element, then output the array.
**Analysis:** None.
Program source code:
Example
#!/usr/bin/python
-- coding: UTF-8 --
def
inp
(
numbers
)
:
for
i
in
range
(
6
)
:
numbers
.
append
(
int
(
raw_input
(
'
Enter a number:
\n
'
)
)
)
p
=
0
def
arr_max
(
array
)
:
max
=
0
for
i
in
range
(
1
,
len
(
array
)
1
)
:
p
=
i
if
array
[
p
]
array
[
max
]
:
max
=
p
k
=
max
array
[
0
]
,
array
[
k
]
=
array
[
k
]
,
array
[
0
]
def
arr_min
(
array
)
:
min
=
0
for
i
in
range
(
1
,
len
(
array
)
1
)
:
p
=
i
if
array
[
p
]
<
array
[
min
]
:
min
=
p
l
=
min
array
[
5
]
,
array
[
l
]
=
array
[
l
]
,
array
[
5
]
def
outp
(
numbers
)
:
for
i
in
range
(
len
(
numbers
)
)
:
numbers
[
i
]
if
name
==
'
main
'
:
array
=
[
]
inp
(
array
)
Input 6 numbers and put them into an array
arr_max
(
array
)
Get the largest element and swap it with the first element
arr_min
(
array
)
Get the smallest element and swap it with the last element
'
Computation result:
'
outp
(
array
)
The output of the above example is:
Enter a number:
1
Enter a number:
2
Enter a number:
3
Enter a number:
7
Enter a number:
9
Enter a number:
8
Calculation result:
9
2
3
7
8
1
Python 100 Examples