0% found this document useful (0 votes)
25 views

Programming 1

The document discusses linear search algorithms. It explains that linear search sequentially searches through a list to find a target element. It provides examples of linear search code in Python using a for loop to iterate through a list and check if each element matches a search term. It also discusses creating a linear search procedure to modularize the linear search code.

Uploaded by

Dorcas Nyaboke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Programming 1

The document discusses linear search algorithms. It explains that linear search sequentially searches through a list to find a target element. It provides examples of linear search code in Python using a for loop to iterate through a list and check if each element matches a search term. It also discusses creating a linear search procedure to modularize the linear search code.

Uploaded by

Dorcas Nyaboke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

4.

1 Amend a program
Look at the program code
The program has three sections:
 The list of atoms
 Procedure definitions
 The main program
Make a list of atoms
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
The next part of the program is called ‘procedure definations’. This part of the
program is empty.
The final part of the program is called the main program. It has the code that
displays the menu.
It also has the code that makes the menu options work.
The code below appends a new atom name to the list of atoms.
if choice=="A":
name=input("enter the name of an atom to add:")
atoms.append(name)
print(name, "has been added to the list")
The code below removes an element from the list. F
if choice=="B":
name=input("enter the name of an atom to remove:")
atoms.remove(name)
print(name, "has been removed to the list")
The command remove, deletes an element from the list. The computer looks
through the list, if it finds an item that matches the name you gave, it removes it
from the list.
Main program
choice=""
while choice !="X":
print("=============")
print("ATOM FINDER")
print("=============")
print("\n")
print("A: Append an atom to the list")
print("B: Remove an atom to the list")
print("C: Print the list")
print("X: Exit the program")
print("\n")
choice=input("choose an option")
Header and Body
A python if structure is made out of a header and body. The header has the
commands that control the if structure. The body has the commands that are
controlled by the header.
The header: starts the if structure. It has:
 The word if
 a logical test
 A colon(two dots)
Example:
if choice=="A":
The body: of the if structure has all the other commands that belong to the
structure. The commands in the body are indented to show that they belong
inside the if structure. The commands will be carried out if the test in the header
is True.
Example:
name=input("enter the name of an atom to add:")
atoms.append(name)
print(name, "has been added to the list")
When will the computer carry out these commands?
Loop Structure
A loop also has a header and body structure.
 The header: of the loop has the command which controls the loop. It sets
the exit condition of the loop. It might be for a loop or a while loop.
 The body: of the loop has the commands which belong inside the loop.
They are indented. These commands will be repeated. The header controls
how many times the commands are repeated.
Double indentation
In this program there is a while loop. Inside the while loop there are if structures.
 If one structure is inside another, this is called a nested structure.
 A nested structure is shown using double indentation.
Activity, Extra challenge, Test (Page 93)
4.2 Make and use procedures

Procedures
A module is a ready made block of code, with a name, that you can use in your
program.
If you put the name of the module into your program, all the commands in the
module will be carried out. That makes your programs shorter and easier to write.
A procedure is a type of module that you can make for you self.
How to use procedures
a) Define the procedure-Give the procedure a name. Store commands inside the
procedure.
b) Call the procedure- Put the name of the procedure into your program. All the
stored commands will be carried out.
How a procedure looks
Header- Structure
- the word def, which stands for ‘define the procedure’.
-a name for the procedure.
-open and close brackets
-a colon
Body
Has all the commands that are stored in the procedure.
The procedure has three commands , they are all print commands.
Example
def recipeMenu():
print('Welcome to all the recipes')
print('------------------------------------')
print('1. Search for a recipe')
print('2. Add a recipe')
print('3. Quit')
recipeMenu()
choice = input('What option do you want? ')

Use a procedure in the atom list program


You will define and call a procedure called add_atom. It will store all the
commands to add an atom to list.
Define the procedure
def add_atom():
name=input("enter the name of an atom to add:")
atoms.append(name)
print(name,"has been added to the list")
Call the procedure
if choice=="A":
name=input("enter the name of an atom to add:")
atoms.append(name)
print(name,"has been added to the list")

Advantages of using procedure


Using procedures and other modules in your programming is called modular
programming.
Advantages;
 Storing code in procedures make your main programs shorter and easier to
read.
 Code stored in a procedure can be reused, saving time and effort.
 The work of writing program can be shared among a team.
 Knowing how to use procedures make you a better programmer.
Built-in functions
Python has some modules that have been written for you. They are called built-in
functions. Examples: input(), int(), print().
Extra challenge, Test (Page 97)

4.3 Linear search


Linear search is a method of finding elements within a list. It is also called a
sequential search. It is the simplest searching algorithm because it searches the
desired element in a sequential manner.
When you do search , the item you look for is called a search term.
A new relational operator
It’s the relational operator: in.
The relational operator in compares a single value and a list. If the value is in the
list, then the test is true. If the value is not in the list, then the value is False.

Example 1:
mylist=["A","B","C"]
letter=input("enter a letter")
if the letter in mylist:
print("the letter is in the list")
Example 2:
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
name=input("enter a name")
if name in atoms:
print(name, "is in the list of atoms")
Example 2: Using if …else
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
name=input("enter a name")
if name in atoms:
print(name, "is in the list of atoms")
else:
print(name,"is not in the list of atoms")
Two ways of doing search
 The linear search
 The binary search
Traverse the list
In unit 3 we learned how to traverse a list using a for loop. Traversing a list means
visiting every item in the list.
Action Python code
Find the stop value for the for loop. Stop=len(atoms)
(The stop value is the same as the
length of the list)
Use a for loop to count through the list For I in range(stop):
one item at a time
Print the list item Print(atoms[i])
If you put all the commands together, the computer will traverse the list.
The Program
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
stop=len(atoms)
for i in range(stop):
print(atoms[i])
The program will print out the name of every atom.

Linear search program


Instead of printing out the items, the computer will check each item, It will check
if it matches the search term.
The program
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
name=input("enter a name")
stop=len(atoms)
for i in range(stop):
if atoms[i]==name:
print("you have found the search term")
Summary
 A linear search is a way of finding a value in a list.
 It uses a for loop to count through the list.
 It compares each value in the list with the search term.
 If there is an exact match, it displays a message saying the item is found.

Activity, Extra challenge, Test Page 103


4.4 Linear search procedure
Making a procedure to do the linear search

Header of the procedure

def linsearch():

Body of the procedure


def linsearch():
name=input("enter a name")
stop=len(atoms)
for i in range(stop):
if atoms[i]==name:
print(name, "is in the list")

Call the procedure


To call the procedure , you must include the name of the procedure in your code.
A new program command return.
You can put the return command into a procedure. When the computer sees the
return command it will stop the procedure and ‘return’ to the main program.
If the computer finds the search term, it will display the message ‘the atom is in
the list.’ Then it will return to the main program. It will never show the final
message.
I the computer does not find the search term in the list, it will finish the loop and
go to the final message. It will display the message, ‘atom is not in the list’.
def linsearch():
name=input("enter a name")
stop=len(atoms)
for i in range(stop):
if atoms[i]==name:
print(name, "is in the list")
return
print(name, "is not in the list")
Activity, Extra challenge, Test Page 105

You might also like