# -*- coding: utf-8 -*-
"""
Created on Thu Jan  6 20:10:46 2022

@author: TANISH
"""


""" Creating Custom Functions """

# In Python, we can create our own functions
# to do a specified task / activity

# Eg.
# Write a function manually to calculate mean / variance...

# Syntax:
# def <function name> :
#     <block of codes>


# Eg.
# Create a function which calculates the PDF of X
# where X ~ exp(lamda)


from math import exp

def dexp(x,lamda):
    return((lamda * exp(- lamda * x)))

dexp(0,0.55)


# Eg.
# Write a function to calculate mean of the obs
# The obs will be given by the user

# Take only 5 obs

def mean():
    obs = list() # obs = []
    
    for i in range(1,6): # [1,6) = [1,5]
        print("Enter obs no.",i,": ")
        obs.append(float(input()))
    
    total = 0

    for j in obs:
        total += j
    
    return(total / len(obs))


mean()

# len() : provides the length / no. of elements


obs = [22,6,2,12,50]

def mean(x):
    
    total = 0
    
    for i in x:
        total += i
        
    return(total / len(x))


# Use of "lambda" in creating anonymous function

# Syntax:
# <var name> = lambda <parameters> : <expression>

# Calling the function
# Syntax:
# <var name>(parameters)


exp_PDF = lambda lm,x : round((lm * exp(- lm * x)),4)
exp_PDF(0.15,0.0145)


""" Exercise """

# Q1.
# Write a function named exp_mode(l), which takes
# the parameter value for an exponential distribution
# as an input and gives the Mode for the same

# Take x in [0,10] with a step of 0.01

# Hint:
import numpy as np
np.arange(0,1,0.1)

# Answer :
    
import numpy as np
from math import exp

l = float(input("Enter the value of lambda : "))

def exp_mode(l):
    
    exp_PDF = []
    exp_x = []
    
    for i in np.arange(0,10 + 0.01,0.01):
        exp_PDF.append(l * exp(- l * i))
        exp_x.append(i)
    
    print("Mode for exp Distribution is : ", exp_x[np.argmax(exp_PDF)],
          " For PDF = ",max(exp_PDF))
    
    

# Q2.
# Write a function len_words() that takes a 
# list of words and an integer n and returns the 
# list of words that are shorter than n.

word_list = ["Asian", "Age", "Business Line",
             "Business","Standard"," Economic", 
             "Times", "Financial","Express", 
             "Financial", "Times", 
             "Financial Chronicle", 
             "Hindu" "Hindustan Times",
             "HindustanTimes", "Indian Express"]    

# Answer :
    
def len_words(word_list,n):
    
    word_list = list(word_list)
    n = int(n)
    output = []
    
    for i in range(0,len(word_list)):
        if((len(word_list[i]) < n)):
            output.append(word_list[i])

    if ((len(output)) == 0):
        print("Empty List = []")
    else:
        print("The required list is : ")
        return(output)


len_words(word_list,10)


# Q3.
# Write a function that takes a list
# and gives output as a dictionary where the
# values are the frequency and key as the band 
# as follows,

# Band 1 : [0 - 100]
# Band 2 : [101 - 200]
# Band 3 : [201 - 500]
# Band 4 : [501 >= ]

# Answer :
    
data = [12,131,34,566,7655,78,23,54,765]


def list_to_dict(x):
    
    freq_table = {}
    freq = [0,0,0,0]
    
    for i in x :
        if (i <= 100):
            freq[0] += 1
        elif (i <= 200):
            freq[1] += 1
        elif (i <= 500):
            freq[2] += 1
        else:
            freq[3] += 1
            
    for j in range(0,3 + 1):
        freq_table["Band-" + str(j + 1)] = freq[j]

    print(freq_table)


list_to_dict(data)
