Skip to main content

Command Palette

Search for a command to run...

#100DaysOfCode - Day 8

Functions with Prime Numbers and Caesar Cipher

Published
3 min readView as Markdown
#100DaysOfCode - Day 8
V

Titles: Test Consultant | QA Analyst | Test Analyst | Contract Manager | Test Consultant | Special Waste Chemist | Depot Manager | Operations Manager

Traits: Organised | Innovative | Professional | Relish a challenge

Experience: Consultant in Public Transport Smart Ticketing Smartcard Agile or Waterfall Processes Web and Mobile Application testing Operational management experience in chemical handling and traffic management.

I am a Disposable Manager to the casual observer, but scratch the surface and there is a method in my madness.

Bring new skills to others.
Sacrifice your reputation for that better team result.

Don't chase glory,
Everyone should work with you,
Not for you.

Walk the talk, deliver results as a team, and make sure every employee is proud of the team.

I can be reached by the following: Telephone: 07853 101959 Email: vchealy@gmail.com

Today's lessons were based around the use of functions, adding parameters and using the arguments. Starting with a simple add in a couple of parameters that log to console messages.

# use_parameter.py
def say_hi_to(name, location):
    print(f'Hi there {name}')
    print(f' I hear you are from {location}')

say_hi_to('John', 'England')
say_hi_to('Paul', 'Wales')
say_hi_to('George', 'Scotland')
say_hi_to(location='Glasgow', name='Jimmy')

Following onto a bit of calculation of arguments within a function.

# area_calculator.py
'''
    The code is to determine the number of tins of paint required to 
    cover a specific wall size.
    Constants: 
        The paint tin as a fixed amount of coverage coverage        
'''
from math import ceil

def coverage_of_wall(height, width):
    one_tin_coverage = 5 # metres square
    number_of_tins = (height * width)/one_tin_coverage
    tins = ceil(number_of_tins)

    # Output
    print(f'For a wall of height {height}mtr and width of {width}mtr.')
    if tins >1:
        print(f'You need {tins} tins of paint for the job. \n')
    else:
        print(f'You need {tins} tin of paint for the job. \n')

coverage_of_wall(5,5)
coverage_of_wall(5,4)
coverage_of_wall(4,3)
coverage_of_wall(1,2)

Nice simple start to the lessons, going over the basics of getting parameters into a function and working on the arguments. Next up was adding some of the previous lessons to build up the skills.

Prime Number The next task was to build a prime number checker, this is a process I already had right in my head.
Is the number a prime number?
I just read that the excerise was to write a prime number checker.
Then got the head down to work on it without listening to the lesson.
Got it! I wrote the code with an user input to test, then converted it into a function that accepts a parameter check_number.
Finally, as an addition to the lesson, I thought I would build in a little more from the previous lessons with adding in a random number list generator, and iterate over that list.

from os import system
from random import randint

def main_function():
    system("cls")
    # Generate a list of 10 integers between 1 - 499
    random_list = [randint(1, 500) for i in range(10)]
    # Check each value in the list
    for i in random_list:
        prime_checker(i)

def prime_checker(check_number):
    print("***__Prime Number Checker__***")
    # check_number = int(input('Type in your number \n'))
    for i in range(2, check_number - 1):
        if check_number % i == 0:
            return print(f"Nope \n {check_number} is not prime \n")

    return print(f"The number {check_number} is a Prime Number \n")

if __name__ == "__main__":
    main_function()

As you will see in the code, the generator is in a separate function that calls the checker function. I thought that was a nice way to add in this days lessons to the final code. A lot nicer than just calling the checker function with individual integer parameters.

The major task was to write up Caesar's Cipher. This is something I done in the past. Shifting the alphabet along by a set number so the letter 'a' was encoded to another letter in the alphabet etc. I follow through the lessons, but apart from adding in a negated while loop, I didn't think much of it work through again.
Here is a snippet of the code.

def cypher(start_text, off_set, cypher_direction):
    end_text = ""
    if cypher_direction == "decode":
        off_set *= -1
    for i in start_text:
        if i in alphabet:
            position = alphabet.index(i)
            new_position = position + off_set
            end_text += alphabet[new_position]
        else:
            end_text += i
    print(f"Here's the {cypher_direction}d result: {end_text}")