Development Exercises – L1

Created by Ramses Alexander Coraspe Valdez

Created on February 18, 2020

In [0]:
import random
import math

1. Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2? If the number is a multiple of 4, print out a different message. Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num tell that to the user. If not, print a different appropriate message.

In [5]:
in1= input("supply the number to check:");
in2= input("supply the number to divide:");
num = int(in1)
check = int(in2)

if (num % 4) == 0:
    print(num, "multiple of 4")
elif (num % 2) == 0:
    print(num, "even number")
else:
    print(num, "odd number")

if (num % check) == 0:
    print(num, "divides by", check)
else:
    print(num, "does not divide by", check)
supply the number to check:6
supply the number to check:6
6 even number
6 divides by 6

2. Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise) Keep the game going until the user types “exit” Keep track of how many guesses the user has taken, and when the game ends, print this out.

In [6]:
t = 0
g = 0 
r = random.randint(1,9) 

while g != "exit" and g != r:
    g = input("Guess the number between 1 to 9 (Enter 'exit' to exit): ")

    if g == "exit": break
    
    t += 1
    g = int(g)
    
    if g < r:
        print("Low")
    elif g > r:
        print("High")
    else:
        print("Right")
        print(t, "attempts")
input()
Guess the number between 1 to 9 (Enter 'exit' to exit): 6
High
Guess the number between 1 to 9 (Enter 'exit' to exit): 5
High
Guess the number between 1 to 9 (Enter 'exit' to exit): exit
exit
Out[6]:
'exit'

3. Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates. Write two different functions to do this - one using a loop and constructing a list, and another using sets.

In [7]:
#loop
def f1(l):
  y = []
  for i in l:
    if i not in y:
      y.append(i)
  return y

#sets
def f2(l):
    return list(set(l))

l = ['a',1,'b',0,'b',1,'e','r','t','p','q']
print(l)
print(f1(l))
print(f2(l))
['a', 1, 'b', 0, 'b', 1, 'e', 'r', 't', 'p', 'q']
['a', 1, 'b', 0, 'e', 'r', 't', 'p', 'q']
[0, 1, 'r', 'b', 't', 'a', 'e', 'q', 'p']

4. Write a function that computes the standard deviation for a set of numbers coming from a list. Do not use any math module, compute the algorithm

In [8]:
def s_dev(d):
    m = sum(d)/len(d)
    v = sum([((i - m) ** 2) for i in d]) / len(d)
    return  v ** 0.5
    
data = [1880,9914,5528,4723,2391,4551,8623,9827,3084,618,4176,3621,5736,7045,7208,2281,7243,5924,8083,3234,6329,9100,6648,5793,118,6587,1602,2671,7934,9599,7470,3952,
        4655,5911,4960,4094,7628,2197,7539,9677,5980,8893,3996,8506,2394,3878,3570,5102,2592,4216,6094,6428,8218,6442,22,1239,1329,1890,5971,5005,3017,6068,3793,8516,
        1704,8612,1799,7570,8573,2596,3261,5036,3084,8389,3417,5529,3176,8680,4820,9513,6956,9925,8161,6512,4462,4993,6835,7589,3744,8090,9415,7266,7304,8924,6049,1400,
        974,5351,2888,2072,1968,2228,643,4688,8559,9520,1281,6468,7903,1188,8281,250,3227,9948,3093,6592,6108,389,404,675,9485,8663,2220,8341,7233,7143,4691,8515,
        7685,7557,7505,8193,5845,6418,6567,4749,2025,2056,3283,9230,4739,4247,256,7381,8409,3754,7497,9650,2157,6929,7029,2241,8421,5755,3265,4580,6164,827,8763,5487,
        6815,1710,3179,8233,456,6374,8124,9118,2660,1950,6585,7101,8697,3922,8850,9136,9292,8734,7146,1968,2688,4698,4299,258,9309,6497,8184,8162,6389,2567,9396,5664,
        7143,7457,4676,6819,6042,4392,1769,1691,2078,2499,4807,2259,1428,5162,1951,1337]

print("{0:0.1f}".format(s_dev(data)))
2762.7

5. Write a function that receives as parameters how many Fibonnaci numbers to generate and then generates them. Take this opportunity to think about how you can use functions. Make sure to ask the user to enter the number of numbers in the sequence to generate.(Hint: The Fibonnaci sequence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …)

In [9]:
def fibo():
    initial = 1
    how = int(input("How many?"))    
    if how == 0:
        fl = []
    elif how == 1:
        fl = [1]
    elif how == 2:
        fl = [1,1]
    elif how > 2:
        fl = [1,1]
        while initial < (how - 1):
            fl.append(fl[initial] + fl[initial-1])
            initial += 1
    return fl

print(fibo())
input()
How many?6
[1, 1, 2, 3, 5, 8]
5
Out[9]:
'5'

6. Write a function that evaluates if a given list satisfy Fibonacci sequence returning true or false if the list satisfy the criteria

In [16]:
def isPS(i): 
    s = int(math.sqrt(i)) 
    return s*s == i 
  
def isFibo(n):       
    return isPS(5*n*n + 4) or isPS(5*n*n - 4)

def checkListFibo(l):
    flag = True;
    for i in l:
        if(isFibo(i)==False):
            flag = False;
            break;
    print("Satisfy: " , flag);

l= [1, 1, 2, 3, 8, 62]
checkListFibo(l)
Satisfy:  False

7. Write a password generator function in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password.

In [18]:
def passwordGenerator(l):    
    chars = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"
    return "".join(random.sample(chars,l)) 
       
print(passwordGenerator(8))   
print(passwordGenerator(10))
print(passwordGenerator(15))
zeQ8UHqZ
tjuDleVXYd
Vn)4x$TtJL5i*wm

8. Write a module containing different function that computes:

  1. Sample mean
  2. Sample standard deviation
  3. Median
  4. A function that returns the n-quartil
  5. A function that returns the n-percentil
In [39]:
def calculate_sd(d):
    m = calculate_mean(d)
    v = sum([((i - m) ** 2) for i in d]) / len(d)
    return  v ** 0.5

def calculate_mean(n):
    return sum(n)/len(n)

def calculate_median(d):            
    d.sort()
    n= len(d)     
    if n % 2 == 0: 
        m1 = d[n//2] 
        m2 = d[n//2 - 1] 
        m = (m1 + m2)/2
    else: 
        m = d[n//2] 
    return str(m)

def calculate_quartile(d,q):    
    d.sort()
    if(q==1):        
        lm = int( round( ( len(d) + 1 ) / 4.0 ) - 1 )
        lq = d[lm]
        return lq;
    if(q==2):
        return calculate_median(d)
    if(q==3):        
        try:
            hm = ( len( d ) - 1 ) * 0.75
            uq = d[ hm ]
            return uq
        except TypeError:               
            ceil = int( math.ceil( hm ) )
            floor = int( math.floor( hm ) )
            uq = ( d[ ceil ] + d[ floor ] ) / 2
            return uq

def calculate_percentile(d, p, key=lambda x:x):
    d.sort()

    k = (len(d)-1) * p
    f = math.floor(k)
    c = math.ceil(k)
    if f == c:
        return key(d[int(k)])
    pp0 = key(d[int(f)]) * (c-k)    
    pp1 = key(d[int(c)]) * (k-f)
    return pp0+pp1            

                
data = [1880,9914,5528,4723,2391,4551,8623,9827,3084,618,4176,3621,5736,7045,7208,2281,7243,5924,8083,3234,6329,9100,6648,5793,118,6587,1602,2671,7934,9599,7470,3952,
        4655,5911,4960,4094,7628,2197,7539,9677,5980,8893,3996,8506,2394,3878,3570,5102,2592,4216,6094,6428,8218,6442,22,1239,1329,1890,5971,5005,3017,6068,3793,8516,
        1704,8612,1799,7570,8573,2596,3261,5036,3084,8389,3417,5529,3176,8680,4820,9513,6956,9925,8161,6512,4462,4993,6835,7589,3744,8090,9415,7266,7304,8924,6049,1400,
        974,5351,2888,2072,1968,2228,643,4688,8559,9520,1281,6468,7903,1188,8281,250,3227,9948,3093,6592,6108,389,404,675,9485,8663,2220,8341,7233,7143,4691,8515,
        7685,7557,7505,8193,5845,6418,6567,4749,2025,2056,3283,9230,4739,4247,256,7381,8409,3754,7497,9650,2157,6929,7029,2241,8421,5755,3265,4580,6164,827,8763,5487,
        6815,1710,3179,8233,456,6374,8124,9118,2660,1950,6585,7101,8697,3922,8850,9136,9292,8734,7146,1968,2688,4698,4299,258,9309,6497,8184,8162,6389,2567,9396,5664,
        7143,7457,4676,6819,6042,4392,1769,1691,2078,2499,4807,2259,1428,5162,1951,1337]

print("Mean: ", calculate_mean(data))
print("SD: ", calculate_sd(data))
print("Quartile 1: ", calculate_quartile(data, 1))
print("Median: ", calculate_median(data))
print("Quartile 3: ", calculate_quartile(data, 3))
print("Percentile 10% : ",calculate_percentile(data, 0.10))
print("Percentile 25% : ", calculate_percentile(data, 0.25))
print("Percentile 50% : ",calculate_percentile(data, 0.50))
print("Percentile 75% : ",calculate_percentile(data, 0.75))
print("Percentile 100% : ",calculate_percentile(data, 1))
Mean:  5317.408653846154
SD:  2762.7098547722812
Quartile 1:  2688
Median:  5700.0
Quartile 3:  7579.5
Percentile 10% :  1664.3000000000002
Percentile 25% :  2838.0
Percentile 50% :  5700.0
Percentile 75% :  7574.75
Percentile 100% :  9948

9. Write a function that converts a decimal number into a Roman format

In [46]:
def convertTo_Roman(n): 
    
    romanos = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"] 
    decimales = [1, 4, 5, 9, 10, 40, 50, 90,  100, 400, 500, 900, 1000] 

    itera = 12
    r=[];
    while n: 
        d = n // decimales[itera] 
        n %= decimales[itera] 
        while d:
            r.append(romanos[itera])             
            d -= 1
        itera -= 1
    return r;

print("2585 : ", "".join(convertTo_Roman(2585)));
print("2020 : ", "".join(convertTo_Roman(2020)));
2585 :  MMDLXXXV
2020 :  MMXX