Developing Algorithms
- Determine the outcome of algorithms
- Determine the outcome of similar algorithms
- Editing Algorithms
- Developing Algorithms
- Homework
- IP CONVERTER
Algorithms can be written in different ways and still accomplish the same tasks. Algorithms that look similar often yield differnet outputs. To solve the same problem, many different algorithms can be used.
Therefore, algorithms are very important for programmers, and today we're going to explore how to determine the outcome of algorithms, how to deteremine the output of similar algorithms, how to edit existing algorithms, and how to develop our own algorithms.
Consider the following algorithm.
def mystery(num, num2):
if (num % num2 == 0):
print("True")
else:
print("False")
mystery(20, 4)
- What does the algorithm do? Please explain in words.
This algorithm has two inputs. if num is divisible by num 2 it will print true.
- What if I put in 30 as num and 4 as num2. What would be the output?
false.
What is the output of this algorithm?
temp = 95
if (temp >= 90):
print("it is too hot outside")
else:
if (temp >= 65):
print("I will go outside")
else:
print("it is too cold outside")
What is the output of this algorithm? it looks similar but the output is different! there is no else statement so it will print two of the statements (ex: 95 is also greater than 65)
temp = 95
if (temp >= 90):
print("it is too hot outside")
if (temp >= 65):
print("i will go outside")
if (temp < 65):
print("it is too cold outside")
temp = 67
if (temp >= 90):
print("it is too hot outside")
if (temp >= 65) and (temp < 90):
print("i will go outside")
if (temp < 65):
print("it is too cold outside")
To develop algorithms, we first need to understand what the question is asking. Then, think about how you would approach it as a human and then try to find what pattern you went through to arrive at the answer. Apply this to code, and there you have it! An algorithm!
Let's say you wanted to sum up the first five integers. How would you do this in real life? Your thought process would probably be:
- The sum of the first integer is 1.
- Then, increase that integer by 1. I now add 2 to my existing sum (1). My new sum is 3.
- Repeat until I add 5 to my sum. The resulting sum is 15.
Now let's translate this into code.
sum = 0 # start with a sum of 0
for i in range (1, 6): # you will repeat the process five times for integers 1-5
sum = sum + i # add the number to your sum
print(sum) # print the result
Task: Write an algorithm in python that sums up the first 5 odd integers. You can use the following code as a starter.
sum = 0
counter = 1
for i in range (0,5):
sum = sum + counter
counter = counter + 2
print(sum)
Homework
Create an algorithm that will start with any positive integer n and display the full sequence of numbers that result from the Collatz Conjecture. The COllatz Conjecture is as follows:
- start with any positive integer
- if the number is even, divide by 2
- if the number is odd, multiply by 3 and add 1
- repeat steps 2 and 3 until you reach 1
Example: if the starting number was 6, the output would be 6, 3, 10, 5, 16, 8, 4, 2, 1
n = 25
while n != 1:
if (n % 2 == 0):
n = int(n / 2)
else:
n = (n * 3) + 1
print(n)
IP = 192.168.0.1
subnet = 255.255.255.0
def ipconverter(IP, subnet):
if IP, subnet >= 1:
ipconverter(IP / 2)
ipconverter (subnet / 2)
subnet + IP = n
def decconverter(n):
return int (n, 2)
I was completely lost on this and I didn't really understand the error. I tried to do the adding and the division/ multiplication that the binary and decimal usually entail but it didn't work.
def sqrt(N):
vb = N
vble = (vb+1) // 2
while vble < vb:
vb = vble
vble = (vb + N //vb) //2
return vb
from math import sqrt as sq
test_cases = [0,1,4,85248289,22297284,18939904,91107025,69122596,9721924,37810201,1893294144,8722812816,644398225]
answers = [int(sq(x)) for x in test_cases]
def checkValid():
for i in range(len(test_cases)):
if sqrt(test_cases[i]) == answers[i]:
print("Check number {} passed".format(i+1))
else:
print("Check number {} failed".format(i+1))
checkValid()