[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.

Overview

--There is an algorithm in the afternoon exam of the Fundamental Information Technology Engineer Examination. I can't understand even if I solve the past questions ... I would like to actually write the algorithm in Python to deepen my understanding.

--Last time, I wrote the algorithm of Maximum array value. --This time, I will write from the algorithm of ** linear search **.

Linear search

algorithm

--Extracts the elements of the array in order from the beginning to the end and compares them with the specified value.

code

#SeqSearch function that finds the value specified by a linear search
def SeqSearch(A,Length,X):
    Pos = -1
    i = 0
    #Iterative processing
    while i < Length and Pos == -1: #Means "not reached the end of the array" and "not found"
        print("Pos=",Pos,"i=",i,"A[i]=",A[i]) #Results on the way
        #Branch processing
        if A[i] == X:
            Pos = i
        i += 1
    return Pos

print("Execution result:",SeqSearch([22,55,66,11,44,77,33],7,77))

Execution result

Pos= -1 i= 0 A[i]= 22
Pos= -1 i= 1 A[i]= 55
Pos= -1 i= 2 A[i]= 66
Pos= -1 i= 3 A[i]= 11
Pos= -1 i= 4 A[i]= 44
Pos= -1 i= 5 A[i]= 77
Execution result: 5

Summary

--Linear search is a little complicated --Next time, let's write an algorithm for ** binary search **

reference

--I quoted or referred to Chapter 3 04 Linear Search in this book. Information processing textbook, book that can solve algorithm problems of the Fundamental Information Technology Engineer Examination, 2nd edition

Recommended Posts

[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python.
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
python Basic sorting algorithm summary (Basic Information Technology Engineer Examination)
I tried it with Wolfram Alpha and google, referring to "[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python."
A story about downloading the past question PDF of the Fundamental Information Technology Engineer Examination in Python at once
Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python Sample Question Explanation
A memo that I wrote a quicksort in Python
I wrote a class in Python3 and Java
Linear search in Python
Fundamental Information Technology Engineer Examination Implemented Python sample questions without using external libraries
I wrote python in Japanese
Algorithm in Python (binary search)
Algorithm in Python (breadth-first search, bfs)
Write a binary search in Python
Write A * (A-star) algorithm in Python
Algorithm in Python (depth-first search, dfs)
I wrote Fizz Buzz in Python
Write a depth-first search in Python
I wrote the queue in Python
Implementing a simple algorithm in Python 2
Run a simple algorithm in Python
I wrote the stack in Python
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
I wrote a code to convert quaternions to z-y-x Euler angles in Python
Algorithm learned with Python 9th: Linear search
I made a payroll program in Python!
Algorithm in Python (ABC 146 C Binary Search
Search the maze with the python A * algorithm
Write a simple greedy algorithm in Python
[Python] I forcibly wrote a short Perlin noise generation function in Numpy.
I created a password tool in Python.
I wrote FizzBuzz in python using a support vector machine (library LIVSVM).
I wrote a graph like R glmnet in Python for sparse modeling in Lasso
Note that I understand the least squares algorithm. And I wrote it in Python.
I searched for the skills needed to become a web engineer in Python
In Python, I made a LINE Bot that sends pollen information from location information.
I want to create a window in Python
I tried playing a typing game in Python
I wrote "Introduction to Effect Verification" in Python
[Memo] I tried a pivot table in Python
I wrote a design pattern in kotlin Prototype
I tried adding a Python3 module in C
I wrote a Japanese parser in Japanese using pyparsing.
I made a Caesar cryptographic program in Python.
I implemented breadth-first search in python (queue, drawing self-made)
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Basic information Write the 2018 fall algorithm problem in Python
I made a prime number generation program in Python
I wrote a design pattern in kotlin Builder edition
I want to write in Python! (2) Let's write a test
I wrote a design pattern in kotlin Singleton edition
I wrote a design pattern in kotlin Adapter edition
I tried to implement a pseudo pachislot in Python
I wrote a design pattern in kotlin, Iterator edition
I want to randomly sample a file in Python
I want to work with a robot in python.
I tried to implement GA (genetic algorithm) in Python