JS Challenge Attempt Below

Using JavaScript, create an algorithm that takes in an IP address and a subnet mask and computes the network address.


The following are the steps to determine the network that an IP address is in given the subnet mask:

Example: IP address: 192.168.0.1

Subnet mask: 255.255.255.0

  1. Convert the IP address into binary: 192.168.0.1 -> 11000000.10101000.00000000.00000001
  2. Convert the subnet mask into binary: 255.255.255.0 -> 11111111.11111111.11111111.00000000
  3. Do a bitwise AND operation on the binary IP address and subnet mask:
 11000000.10101000.00000000.00000001
+11111111.11111111.11111111.00000000
=11000000.10101000.00000000.00000000
  1. Convert the result back to decimal: 11000000.10101000.00000000.00000000 -> 192.168.0.0

The network address is 192.168.0.0

 

Developing Algorithm Work Below

Consider the following algorithm.

def mystery(num, num2):
    if (num % num2 == 0):
        print("True")
    else:
        print("False")

mystery(20, 4)
True
  1. What does the algorithm do? Please explain in words.
  • The algorithm defines a function and prints True if num is divisible by num2. Otherwise, if there is a remainder, it prints False
  1. What if I put in 30 as num and 4 as num2. What would be the output?
  • False

Determine the outcome of similar algorithms

What is the output of this algorithm?

  • The output is dependent on the temp; if the temp is 90 or more, it is too hot, but if it is 65 or less, it is too cold
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")
it is too hot outside

What is the output of this algorithm? it looks similar but the output is different!

  • This time, if the temp is between 65 and 95, "i will go outside" is printed
temp = 95
if (temp >= 95):
    print("it is too hot outside")
if (temp >= 65):
    print("i will go outside")
if (temp < 65):
    print("it is too cold outside")
it is too hot outside
i will go outside

Editing Algorithms

Task: Please edit the algorithm above to have it yield the same results as the previous algorithm! (no matter what temp you put in)

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) # print to check sum
25

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:

  1. start with any positive integer
  2. if the number is even, divide by 2
  3. if the number is odd, multiply by 3 and add 1
  4. 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

My work is below:

i = int(input('Enter a positive integer: '))
print (i)
while i >= 1:
    if i % 2 == 0:
        i = int(i / 2)
    elif i % 2 != 0:
        i = int(i*3 + 1)
    print (i)
    if i == 1:
        break
6
3
10
5
16
8
4
2
1

Binary Search Challenges and Homework

Problem: Given a specific integer N, return the square root of N (R) if N is a perfect square, otherwise, return the square root of N rounded down to the nearest integer

Input: N (Integer)

Output: R (Integer)

Constraints: Do not use any built-in math operations such as sqrt(x) or x**(0.5), Try complete the problem in logarithmic time.

Hint 1: Maybe you can use Binary Search to try and reduce the number of checks you have to perform?

Hint 2: Is there a mathematical pattern amongst numbers and their square roots that could help you reduce the number of searches or iterations you must execute? Is there some value or rule you can set before applying binary search to narrow the range of possible values?

def sqrt(N):
    # Write your code here (didn't work, so I commented it)
    # minimum = 0
    # maximum = N
   # while minimum < maximum:
       # center = (minimum + maximum) / 2 # the middle value 
       # if center**2 == N:
        #    print(center) # checking if center is sqrt
       # else:
        #    minimum += 1 # advancing loop until a value is the sqrt




    # I tried to use the max/ min idea, but it didn't really work out and gave an error message, so I am using powers
    N**0.5


    
    return None
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()
Check number 1 failed
Check number 2 failed
Check number 3 failed
Check number 4 failed
Check number 5 failed
Check number 6 failed
Check number 7 failed
Check number 8 failed
Check number 9 failed
Check number 10 failed
Check number 11 failed
Check number 12 failed
Check number 13 failed
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()
Check number 1 failed
Check number 2 failed
Check number 3 failed
Check number 4 failed
Check number 5 failed
Check number 6 failed
Check number 7 failed
Check number 8 failed
Check number 9 failed
Check number 10 failed
Check number 11 failed
Check number 12 failed
Check number 13 failed