[Python] A program that finds the minimum and maximum values without using methods

[Python] How to find the minimum value without using a method

How to find the minimum value without using the min or max methods.

Way of thinking

Repeat the comparison of the two elements.

** ▼ When finding the minimum value ** ・ First element: The initial value is the 0th value. This is the current minimum value.

Replace the smaller one with the current minimum value compared to the second element.

・ Second element: The next element. Move up one by one. It is always compared to the current minimum. 

Find the minimum value

python


def min_val(arr):
  min = 0  #For the first time, set the 0th to the current minimum value (specified by the sequence number)
  for i in range(1, len(arr)):
    if arr[min] > arr[i]:  #If the next number is smaller in comparison, replace the current lowest sequence number.
      min = i
  print(a[min])

python


a = [2,4,5,3,10,1,8,6,3,2]
min_val(a)

#1

## Find the maximum value Just replace the inequality sign in the if statement.

python


def max_val(arr):
  max = 0
  for i in range(1, len(arr)):
    if arr[max] < arr[i]:
      max = i
  print(a[max])

python


a = [2,4,5,3,10,1,8,6,3,2]
max_val(a)

#10

Recommended Posts

[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that finds the most common bird types
[Ev3dev] Create a program that captures the LCD (screen) using python
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
[Python] A program that finds the maximum number of toys that can be purchased with your money
Function to extract the maximum and minimum values ​​in a slice with Go
[Python] A program that calculates the number of updates of the highest and lowest records
[Python] A program that counts the number of valleys
[Python] A program that compares the positions of kangaroos.
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
There is a pattern that the program did not stop when using Python threading
A note that runs an external program in Python and parses the resulting line
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
A program that just presses and releases the Esc key
A story that makes it easy to estimate the living area using Elasticsearch and Python
[Python] A program to find the number of apples and oranges that can be harvested
Try to write a program that abuses the program and sends 100 emails
A program that answers a few questions and predicts the next answer
[Python] A program that rotates the contents of the list to the left
Understand the probabilities and statistics that can be used for progress management with a python program
[Python] I tried to make a simple program that works on the command line using argparse.
[Python] A program that creates stairs with #
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that finds a pair that can be divided by a specified value
A python program that resizes a video and turns it into an image
[Python] Note: A self-made function that finds the area of the normal distribution
Prepare a development environment that is portable and easy to duplicate without polluting the environment with Python embeddable (Windows)
Create a Python image in Django without a dummy image file and test the image upload
Make a Python program a daemon and run it automatically when the OS starts
A Python script that reads a SQL file, executes BigQuery and saves the csv
Publishing and using a program that automatically collects facial images of specified people
I want to exe and distribute a program that resizes images Python3 + pyinstaller
[Python] A program that calculates the difference between the total numbers on the diagonal line.
A nice nimporter that connects nim and python
ffmpeg-Build a python environment and split the video
A program that searches for the same image
Create a web map using Python and GDAL
Checking methods and variables using the library see
Testing methods that return random values in Python
Let's write a Python program and run it
Create a Mac app using py2app and Python3! !!
A discussion of the strengths and weaknesses of Python
A shell program that displays the Fibonacci sequence
A story that was convenient when I tried using the python ip address module
A program that asks for a few kilograms to reach BMI and standard weight [Python]
[Python] Sort apples and pears from pixel values using a support vector machine (SVM)
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
The story of Python without increment and decrement operators.
Try creating a compressed file using Python and zlib
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Creating a graph using the plotly button and slider
Send and receive Gmail via the Gmail API using Python
Implementing a generator using Python> link> yield and next ()> yield
Python program that looks for the same file name
PHP and Python samples that hit the ChatWork API
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
A memo that I touched the Datastore with python
[Python] Mask the image into a circle using Pillow
Extract the maximum value with pandas and change that value