Reputation: 35
Everyone,
the task is as follows: to write a program that prints the max and min number of a Python console input.
For example, we enter (the "5" above is "n" => the count of the numbers to be entered):
5
10
30
504
0
60
The Output should say:
Max number: 504
Min number: 0
Below is the solution - could someone please explain to me the definition of max_number = -sys.maxsize and min_number = sys.maxsize (why are both variables' values actually the opposite to their actual ones) along with the if conditional statement, how should the iterations go so that the output is correct?
import sys
n = int(input())
max_number = -sys.maxsize
min_number = sys.maxsize
for i in range(n):
current_number = int(input())
if current_number > max_number:
max_number = current_number
if current_number < min_number:
min_number = current_number
print(f'Max number: {max_number}')
print(f'Min number: {min_number}')
Thanks!
Upvotes: 1
Views: 11669
Reputation: 4580
user_input = input("Enter the numbers separated by a space ").split()
int_list = list(map(int, user_input))
print ("Max number:",max(int_list), "\nMin number:",min(int_list))
# Output
Enter the numbers separated by a space 5 10 30 504 0 60
Max number: 504
Min number: 0
Explanation to your Question:
import sys
max_number = -sys.maxsize
min_number = sys.maxsize
print(max_number, max_number)
# Output
-9223372036854775807 9223372036854775807
As you can see sys.maxmsize
(9223372036854775807) is the greatest integer and sys.maxmsize
(-9223372036854775807) is the smallest integer.
For first If condition
you are considering smallest integer for comparison so that even smallest value will be stored as max value for first iteration and it gets updated as it find out greater value than that.
In your example, 5 would be consider as max_number
in first iteration of for loop
why? first iteration of for loop
if 5 > -9223372036854775807 # max_number = -9223372036854775807, current_number = 5 hence condition is true i.e. 5 > -9223372036854775807
max_number = 5 # max_number updated with 5
# max_number is 5 now
second iteration of for loop
if 10 > max_number # max_number = 5, current_number = 10 hence 10 > 5 condition is true
max_number = 10 # max_number updated with 10
# max_number is 10 now
and so on...at the end 504 will remain max_number
For second If condition
situation is exactly opposite
first iteration of for loop
if 5 < 9223372036854775807 # min_number = 9223372036854775807, current_number = 5 hence condition is true i.e. 5 < 9223372036854775807
min_number = 5 # min_number has become 5 now
# min_number is 5 now
.
. # min_number we remain 5 for 2nd, 3rd and 4th iterations because 10, 30 and 504 are greater than 5 hence failed to satisfy if condition
.
fifth iteration of for loop
if 0 < min_number # min_number = 5, current_number=0
min_number = 0 # min_number has become 0 now
# min_number is 0 now
At the end 0 will remain min_number
Upvotes: 1
Reputation: 4475
For max_number, each iteration in the for loop will check if the newly entered number is larger then the max_number value. If so, this new value will become the max_number value. After all iterations are processed, the variable max_numbers holds the value of the largest number. To make sure the value entered in the first iteration always wins compared to the max_number current value, you initialize max_number with the lowest possible value (-sys.maxsize). This way any entered number will always be larger and thus will become the new value of max_number.
begin: max_number = -sys.max_number
IN=10 -> max_number = 10
IN=30 -> max_number = 30
IN=504 -> max_number = 504
IN=0 -> max_number = 504
IN=60 -> max_number = 504
The same logic is used for min_number but in the other direction: initialise it with the highest possible value so the value of the first iteration will always be smaller and win the comparison.
Upvotes: 0
Reputation: 184
You can do something like this
n = int(input())
numbers = []
for i in range(n):
numbers.append(int(input()))
print(f'Max number: {max(numbers)}')
print(f'Min number: {min(numbers)}')
Upvotes: 1
Reputation: 1542
https://docs.python.org/3/library/sys.html
"sys.maxsize An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform."
So you are initialising the largest and smallest possible integers so the comparison will be able to compare if numbers entered are smaller or larger than the possible maximum or minimum.
>>> -sys.maxsize
-9223372036854775807
>>> sys.maxsize
9223372036854775807
>>>
You want them to be the opposite for the initial loop, else you have nothing to compare against. So anything should be larger than the theoretical minimum and vice versa.
Upvotes: 0
Reputation: 550
sys.maxmsize
is the greatest integer a variable can take.
Thus, the program first initialise the max_number
to the smaller number possible. By doing, it is sure that it will not miss a number in the list.
Then, it goes inside the inputs and each time it sees a number greater than the actual max_number
it updates the max_number
.
Does this help you?
For more precision on sys.maxsize
look at the docs:
https://docs.python.org/3/library/sys.html
(search for maxsize
)
Upvotes: 0