Categories:Viewed: 4 - Published at: a few seconds ago

Write a python decorator function to find how much time user given function takes to execute

def timed(fn):
    from time import perf_counter
    from functools import wraps

    def inner(*args, **kwargs):
        start = perf_counter()
        result = fn(*args, **kwargs)
        end = perf_counter()
        elapsed = end - start

        args_ = [str(a) for a in args]
        kwargs_ = ['{0}={1}'.format(k, v) for k, v in kwargs.items()]
        all_args = args_ + kwargs_
        args_str = ','.join(all_args) # now it is comma delimited

        print(f'{fn.__name__}({args_str}) took {elapsed} seconds')

        return result
    # inner = wraps(fn)(inner)
    return inner

Write a python program to add and print two user defined list using map

input_string = input("Enter a list element separated by space ")
list1  = input_string.split()
input_string = input("Enter a list element separated by space ")
list2  = input_string.split()
list1 = [int(i) for i in list1] 
list2 = [int(i) for i in list2] 
result = map(lambda x, y: x + y, list1, list2) 

Write a python function to convert list of strings to list of integers

def stringlist_to_intlist(sList): 
  return(list(map(int, sList)))

Write a python function to map multiple lists using zip

def map_values(*args):
  return set(zip(*args))

Write a generator function in python to generate infinite square of numbers using yield

def nextSquare(): 
    i = 1;  
    # An Infinite loop to generate squares  
    while True: 
        yield i*i                 
        i += 1

Write a python generator function for generating Fibonacci Numbers

def fib(limit): 
    # Initialize first two Fibonacci Numbers  
    a, b = 0, 1  
    # One by one yield next Fibonacci Number 
    while a < limit: 
        yield a 
        a, b = b, a + b

Write a python program which takes user input tuple and prints length of each tuple element

userInput = input("Enter a tuple:")
x = map(lambda x:len(x), tuple(x.strip() for x in userInput.split(',')))

Write a python function using list comprehension to find even numbers in a list

def find_evennumbers(input_list):
  list_using_comp = [var for var in input_list if var % 2 == 0] 
  return list_using_comp

Write a python function to return dictionary of two lists using zip

def dict_using_comp(list1, list2):
  dict_using_comp = {key:value for (key, value) in zip(list1, list2)} 
  return dict_using_comp

Write a function to get list of profanity words from Google profanity URL

def profanitytextfile():
    url = ""
    html = urlopen(url).read()
    soup = BeautifulSoup(html, features="html.parser")

    textlist = []
    table = soup.find('table')
    trs = table.find_all('tr')
    for tr in trs:
        tds = tr.find_all('td')
        for td in tds:
    return textlist

Write a python program to find the biggest character in a string

bigChar = lambda word: reduce(lambda x,y: x if ord(x) > ord(y) else y, word)

Write a python function to sort list using heapq

def heapsort(iterable):
    from heapq import heappush, heappop
    h = []
    for value in iterable:
        heappush(h, value)
    return [heappop(h) for i in range(len(h))]

Write a python function to return first n items of the iterable as a list

def take(n, iterable):    
    import itertools
    return list(itertools.islice(iterable, n))

Write a python function to prepend a single value in front of an iterator

def prepend(value, iterator):    
    import itertools
    return itertools.chain([value], iterator)

Write a python function to return an iterator over the last n items

def tail(n, iterable):    
    from collections import deque
    return iter(deque(iterable, maxlen=n))

Write a python function to advance the iterator n-steps ahead

def consume(iterator, n=None):
    import itertools
    from collections import deque
    "Advance the iterator n-steps ahead. If n is None, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        deque(iterator, maxlen=0)
        # advance to the empty slice starting at position n
        next(itertools.islice(iterator, n, n), None)