Day 3

Date: 01/08/2020
Time: 12:00 PM - 3:00 PM
Location: Hostos Community College - A-534


Hot/Cold Random Number Game

Yesterday, we wrote a random number game and had the user guess a number. The user was given 10 tries to guess the correct number. The challenge was to print hot when the user was within 5 of the winning number, print warm when within 10 of the winning number and print cold for every other case. At the end of the game, you had to print out the number of tries that the player took, as well as every attempt that they made to guess the winning number. The code for this can be found on our class Github Repository. The code is also below but feel free to clone the repository and try it out for yourself!

import random

winning_number = random.randint(0, 100)
total_guesses = 10
guesses = []
user_won = False
guesses_taken = 0

while guesses_taken < total_guesses and user_won == False:
    user_guess = int(input("Enter your guess: "))
    guesses.append(user_guess)
    guesses_taken += 1
    if user_guess == winning_number:
        user_won = True
        print("Congrats! You won!")
    elif abs(user_guess - winning_number) <= 5:
        print("Hot!")
    elif abs(user_guess - winning_number) <= 10:
        print("Warm!")
    else:
        print("Cold :(")

print("You took", guesses_taken)
print("Your guesses were: ")
print(guesses)

Linear Search Algorithm

It’s time to combine some of the knowledge that we’ve been gaining throughout the boot camp. Let’s say we’re given an unsorted list of elements and we want to find the first occurrence of a specific element. We can do this by using something called a linear search algorithm .

The linear search algorithm is a iterative search method, i.e. it looks at the elements of the list one by one until it reaches either the element that you specify or the end of the list. Remember how we used for loops and the range function to iterate through a list to print out the elements ? we will be doing something similar for this problem. Then if the element at an index is equal to the element that you are searching for, then we simply return that index. If the element doesn’t exist, the function can return None. Luckily, Python will automatically do that for us with the code here. This search algorithm will work for any data type stored.

def search(x, list):
    for i in range(0,len(list)):
        if list[i] == x:
            return i

Let’s try it with a list of ints and strings.

my_int_list = [5, 10, 28, 32, 90, -1, 3]
print(search(-1, my_int_list)) # prints 5

my_string_list = ["Python", "Is", "pretty", "awesome"]
print(search("awesome", my_string_list)) # prints 3

Finding minimum and maximum

Given an unsorted array, we are tasked with finding the maximum element. We can modify the linear search algorithm to find the maximum element of an array. In this case, we can not stop iterating until we’ve reached the end of the list because we do not know where the maximum will occur.

First we set the first known maximum element to be the very first element of the array. This is our starting assumption. Then we iterate through the list, and as we find a value that is bigger than our current maximum, we will replace the value of the variable max. After the loop is done, we’ve finished iterating through the array. At this point we can return our maximum.

Here is the algorithm implemented:

def find_max(list):
    max = list[0]

    for i in range(0,len(list)):
        if list[i] > max:
            max = list[i]

    return max

In this problem we are returning the element and not the index itself. Knowing this allows us to simply the for loop slightly, as we are not concerned with the indices.

def find_max(list):
    max = list[0]

    for element in list
        if element > max:
            max = element

    return max

We can do the same thing to find the minimum by flipping the comparison sign. First we assume that the minimum is the first element of the list. Then we can begin to iterate through the array just like before. If we encounter an element that is less than our current minimum, we replace the value of the variable min.

def find_min(list):
    min = list[0]

    for element in list
        if element < min:
            min = element

    return min