Find the maximum Python

I'm still a beginner, so I'd be happy if you could give me some advice. Now, let's get into the main subject. Here, we will create an algorithm that uses python to find the maximum value from n numbers. I thought about the algorithm myself as much as possible. Surprisingly, it is hard to make an algorithm to find the maximum value. .. .. Algorithm is difficult! !! !!

Below is the algorithm I made (sorry for the chords!)

max.py


num = int(input("How many pieces do you want to substitute?"))
i = 0
value_list = [] #Prepare an array to store numbers
while i < num:
    value = int(input("Substitute a number"))
    value_list.append(value) #Add values to the prepared list
    i += 1

max_value = 0 #Initialize maximum value to 0
i = 0
while i < num:
    if value_list[i] >= max_value: #Compare the stored numbers in order
        max_value = value_list[i]
    i += 1

print(max_value) #Output maximum value

I will explain the flow

num = int(input("How many pieces do you want to substitute?"))
i = 0
value_list = [] #Prepare an array to store numbers
while i < num:
    value = int(input("Substitute a number"))
    value_list.append(value) #Add values to the prepared list
    i += 1

Here, enter n (the number of numerical values to be compared). I used a list to work with numbers all together. You can now store n values. append () is the default method in python. Now you can add numbers to the array from now on.

max_value = 0 #Initialize maximum value to 0
i = 0
while i < num:
    if value_list[i] >= max_value: #Compare the stored numbers in order
        max_value = value_list[i]
    i += 1

Here, the numerical values stored in the list are taken out in order and compared, but the maximum value is initialized to 0 first. Then, if a value larger than the maximum value candidate is found by comparing in order, that value is reset to the maximum value candidate. And finally, the maximum value is output and the end.

How was it? Surprisingly, the algorithm for finding the maximum value was difficult for programming beginners. I think the engineers are really amazing. I respect you.

Recommended Posts

Find the maximum Python
Find the maximum python (improved)
Find the difference in Python
[Python] Find the second smallest value.
Find the Levenshtein Distance with python
Solve the maximum subarray problem in Python
Find the mood value with python (Rike Koi)
the zen of Python
Find the divisor of the value entered in python
List find in Python
Find the solution of the nth-order equation in python
[Python] Split the date
Find the geometric mean of n! Using Python
[Introduction to Algorithm] Find the shortest path [Python3]
[Python] Find the transposed matrix in a comprehension
Find the shortest path with the Python Dijkstra's algorithm
python xlwings: Find the cell in the last row
Find the cumulative distribution function by sorting (Python version)
Find out the location of Python class definition files.
Find the part that is 575 from Wikipedia in Python
Find the Hermitian matrix and its eigenvalues in Python
[Python] Find out about pip
python3 Measure the processing speed.
Towards the retirement of Python2
Download the file in Python
Compare the Python array-like guys
About the Python module venv
About the ease of Python
About the enumerate function (python)
[Python] Adjusting the color bar
Find permutations / combinations in Python
Find the factorial prime factorization
[Python] Get the previous month
[Python 2/3] Parse the format string
Call the API with python3.
About the features of Python
Find the optimal cooking order
Let's find pi in Python
[Python] Check the installed libraries
I downloaded the python source
The Power of Pandas: Python
I tried to find the entropy of the image with python
Find out the apparent width of a string in python
[Python] Heron's formula functionalization and calculation of the maximum area
Write a python program to find the editing distance [python] [Levenshtein distance]
Python --Find out number of groups in the regex expression
Find the index of the maximum value (minimum value) of a multidimensional array
Find the diameter of the graph by breadth-first search (Python memory)
Find the eigenvalues of a real symmetric matrix in Python
Find the definition of the value of errno
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Leave the troublesome processing to Python
[Python] Check the current directory, move the directory
Extract the xz file with python
The story of Python and the story of NaN
Getting the arXiv API in Python
Check the behavior when assigning Python
Extract the maximum value with pandas.
[Python] The stumbling block of import
Python
First Python 3 ~ The beginning of repetition ~