Jovian
⭐️
Sign In

Write a Python Program to implement your own myreduce() function which works exactly like Python's built-in function reduce()

In [16]:
product=1
x=[1,2,3,4]
def myreduce(product):
    for i in x:
        product *= i
    return product
    
print(myreduce(product))

    
24

Write a Python program to implement your own myfilter() function which works exactly like Python's built-in function filter()

In [18]:
result = []
def myfilter(result):
    for x in range(-10,10):
        if x > 0:
            result.append(x)
    return result
    
print(myfilter(result)) 

[1, 2, 3, 4, 5, 6, 7, 8, 9]

'''Implement 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)]'''

In [26]:
s= [i for i in 'ACADGILD']
print(s)

input_list = ['x','y','z']
result = [ item*num for item in input_list for num in range(1,4)  ]
print("['x','y','z'] => " +   str(result))

input_list = ['x','y','z']
result = [ item*num for num in range(1,4) for item in input_list  ]
print("['x','y','z'] => " +   str(result))

input_list = [2,3,4]
result = [ [item+num] for item in input_list for num in range(0,3)]
print("[2,3,4] =>" +  str(result))

input_list = [2,3,4,5]
result = [ [item+num for item in input_list] for num in range(0,4) ]
print("[2,3,4,5] =>" +  str(result))

input_list=[1,2,3]
result = [ (b,a) for a in input_list for b in input_list]
print("[1,2,3] =>" +  str(result))



     
['A', 'C', 'A', 'D', 'G', 'I', 'L', 'D'] ['x','y','z'] => ['x', 'xx', 'xxx', 'y', 'yy', 'yyy', 'z', 'zz', 'zzz'] ['x','y','z'] => ['x', 'y', 'z', 'xx', 'yy', 'zz', 'xxx', 'yyy', 'zzz'] [2,3,4] =>[[2], [3], [4], [3], [4], [5], [4], [5], [6]] [2,3,4,5] =>[[2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7], [5, 6, 7, 8]] [1,2,3] =>[(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3), (3, 3)]

Implement a function longestWord() that takes a list of words and returns the longest one

In [39]:
def LargestWord(words_list):  
    word_len = []  
    for n in words_list:  
        word_len.append((len(n), n))
    word_len.sort() 
    return word_len[-1][1]
print(LargestWord(["hasta ", "lavista", "baby"])) 
lavista

'''Write 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.5

Function 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.

'''

In [75]:
a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
The area of the triangle is 14.70

Write 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.

In [100]:
def filter_long_words(words, n):
    return [word for word in words if len(word) > n]


if __name__ == "__main__":
    print (filter_long_words(['a', 'avg', 'abcde', 'zxcw', 'b'], 3))
['abcde', 'zxcw']

'''Write a Python program using function concept that maps list of words into a list of integers representing 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. '''

In [55]:
convert =[]
def strlen(convert):
    for i in ['ab','cde','erty']:
        convert.append(len(i))
    return convert                
    
print(strlen(convert))       

[2, 3, 4]

Write a Python function which takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise

In [104]:
def isVowel(char):
    '''
    char: a single letter of any case

    returns: True if char is a vowel and False otherwise.
    '''
    if char == 'a' or char =='e' or char== 'i' or char=='o' or char=='u' or char=='A' or char=='E' or char=='I' or char=='O' or char =='U':
        return True
    else:
        return False
print(isVowel('z'))
            
        
        
False
In [105]:
import jovian
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [ ]: