You are on page 1of 3

Republic of the Philippines

North Eastern Mindanao State University


formerly Surigao del Sur State University
Rosario, Tandag City, Surigao del Sur 8300
Telefax No. 086-214-4221
www.nemsu.edu.ph

Name: Atrio Alameda


Course and year: (2CSB) CS 211 - Algorithm and Complexity
Instructor: Grace Love Tidalgo

Instruction:
Create at least 3 python program that uses diff. types of algorithm (Choose between Brute Force Algorithm,
Recursive Algorithm, Randomized Algorithm, Sorting Algorithm, Sorting Algorithm, Searching Algorithm, and Hashing
Algorithm.)
Kindly label what type of algorithm did you choose.

Randomized Algorithm

Sample code #1 Captcha


import random

def checkCaptcha(captcha, user_captcha):

if captcha == user_captcha:

return True

return False

def generateCaptcha(n):

chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

captcha = ""

while (n):

captcha += chrs[random.randint(1, 1000) % 62]

n -= 1

return captcha

captcha = generateCaptcha(9)

print(captcha)
print("Enter above CAPTCHA:")

usr_captcha = input()

if (checkCaptcha(captcha, usr_captcha)):

print("CAPTCHA Matched")

else:

print("CAPTCHA Not Matched")

Output:
91Ql0cvkS

Enter above CAPTCHA:

91Ql0cvkS

CAPTCHA Matched

Output:
a5aUSXzFX

Enter above CAPTCHA:

a3gsce3td

CAPTCHA Not Matched

Searching Algorithm: Linear Search

Sample code #2 Position Check


def LinearSearch(input_list: list, element: 'String'):

list_len = len(input_list)

for i in range(list_len):

if input_list[i] == element:

return i

return -1

myList = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"]

print("Given list is:", myList)

position = LinearSearch(myList, 'Yellow')

print("Element 'Yellow' is at position:", position)


Output:
Given list is: ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']

Element 'Yellow' is at position: 2

Sorting Algorithm

Sample code #3 Sorting Numbers


num = int(input("How many numbers you want to enter?:"))

list1 = [int(input("enter number:")) for x in range(num)]

print("unsorted list",list1)

for i in range(len(list1)-1):

m_ind = i

for j in range(i+1,len(list1)):

if list1[j] < list1[m_ind]:

m_ind = j

if list1[i] != list1[m_ind]:

list1[i],list1[m_ind] = list1[m_ind],list1[i]

print(list1)

Output:
How many numbers you want to enter?:10
enter number:7
enter number:4
enter number:0
enter number:1
enter number:8
enter number:9
enter number:5
enter number:3
enter number:2
enter number:6
unsorted list [7, 4, 0, 1, 8, 9, 5, 3, 2, 6]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

You might also like