<jupyter_start><jupyter_text>Task 1  Question 1.1Write a Python Program to implement your own myreduce() function which works exactly like Python's built-in function reduce()<jupyter_code># Actual reduce working 
from functools import reduce

def add(a, b):
    return a + b

reduce(add, [9,8,7,6])
reduce(lambda x, y : x if x>y else y, [1,2,3,4,5])
# Defining my own reduce function

def myreduce(function, sequence):
    result = sequence[0]
    for i in range(len(sequence)-1):
        result = function(result, sequence[i+1])
    return result
myreduce(add, [9,8,7,6])
myreduce(lambda x, y : x if x > y else y, [1,2,3,4,5])<jupyter_output><empty_output><jupyter_text>Question 1.2Write a Python program to implement your own myfilter() function which works exactly like Python's built-in function filter()<jupyter_code># Actual filter working
import math

def square_or_not(number):
    # Gets square root of number
    root = math.sqrt(number)
    # adding in case of floating operation's precision issue.
    estimate = int(root + 0.5) ** 2
    if estimate == number:
        return True
    return False
    

list(filter(square_or_not, [2,4,35,25]))
def myfilter(function, sequence):
    filtered_list = []
    for element in sequence:
        if function(element):
            filtered_list.append(element)
    return filtered_list
myfilter(square_or_not, [2,4,35,25])<jupyter_output><empty_output><jupyter_text>Question 2Implement List comprehensions to produce the following lists. Write List comprehensions to produce the following Lists['A', 'C', 'A', 'D', 'G', 'I', ’L’, ‘ D’]['x', 'xx', 'xxx', 'xxxx', 'y', 'yy', 'yyy', 'yyyy', 'z', 'zz', 'zzz', 'zzzz'] ['x', 'y', 'z', 'xx', 'yy', 'zz', 'xx', 'yy', 'zz', 'xxxx', 'yyyy', 'zzzz'][[2], [3], [4], [3], [4], [5], [4], [5], [6]][[2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7], [5, 6, 7, 8]][(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3), (3, 3)]<jupyter_code>a = [x for x in "ACADGILD"]
b = [x*i for x in "xyz" for i in range(1,5)]
c = [x*i for i in range(1,5) for x in "xyz"]
d = [[x+i] for x in range(2,5) for i in range(3)]
e = [[x + i for i in range(4)] for x in range(2,6)]
f = [(i,j) for j in range(1,4) for i in range(1,4)]
k = [x for x in [a,b,c,d,e,f]]
for i in k:
    print(i)<jupyter_output>['A', 'C', 'A', 'D', 'G', 'I', 'L', 'D']
['x', 'xx', 'xxx', 'xxxx', 'y', 'yy', 'yyy', 'yyyy', 'z', 'zz', 'zzz', 'zzzz']
['x', 'y', 'z', 'xx', 'yy', 'zz', 'xxx', 'yyy', 'zzz', 'xxxx', 'yyyy', 'zzzz']
[[2], [3], [4], [3], [4], [5], [4], [5], [6]]
[[2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7], [5, 6, 7, 8]]
[(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3), (3, 3)]<jupyter_text>Question 3Implement a function longestWord() that takes a list of words and returns the longest one.<jupyter_code>def longestWord(list_of_words):
    len_dict = dict()
    for word in list_of_words:
        len_dict[word]= len(word)
        
    return max(len_dict, key=len_dict.get)
longestWord(["a","sdhj"])<jupyter_output><empty_output><jupyter_text>Task 2  Question 1.1Write a Python Program(with class concepts) to find the area of the triangle using the below formula.area = (s*(s-a)*(s-b)*(s-c)) ** 0.5Function to take the length of the sides of triangle from user should be defined in the parent class and function to calculate the area should be defined in subclass.<jupyter_code>class Triangle:
    def __init__(self,a, b, c):
        self.a = a
        self.b = b
        self.c = c
        self.s = (a+b+c) / 2
    
    def area(self):
        s = int(self.s)
        a, b, c = self.a, self.b, self.c
        area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
        print(f"Area of the triangle : {area}")
        
t = Triangle(3,4,5)
t.area()<jupyter_output>Area of the triangle : 6.0<jupyter_text>Question 1.2Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n.<jupyter_code>def filter_long_words(list_of_words, n):
    filtered_list = [word for word in list_of_words if len(word) > n]
    return filtered_list
filter_long_words(["sdds","dsugcdsusa","ndsj"],5)<jupyter_output><empty_output><jupyter_text>Question 2.1Write a Python program using function concept that maps list of words into a list of integersrepresenting the lengths of the corresponding words​.Hint: ​If a list [ ab,cde,erty] is passed on to the python function output should come as [2,3,4] Here 2,3 and 4 are the lengths of the words in the list.<jupyter_code>def map_(words):
    return list(map(lambda x :len(x), words))

map_(["ab","cde","erty"])<jupyter_output><empty_output><jupyter_text>Question 2.2Write a Python function which takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.<jupyter_code>def vowel_or_not(char):
    if char.lower() in "aeiou":
        return True
    return False
vowel_or_not("a")
vowel_or_not("v")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17422240292742427, 'fraction_numerical': 0.03455986989225452, 'mean_word_length': 4.141065830721003, 'pattern_counts': {'":': 1, '<': 31, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '579252', 'license': 'CC0-1.0', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1854, 'n_words': 621, 'path': 'Assignments/Session 2 - Assignment 1.ipynb', 'repo_name': 'guptha23/Machine-Learning', 'size': '4919'}