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

Write a regex pattern in python to print all adverbs and their positions in user input text

import re
text = input("Enter a string: ")
for m in re.finditer(r"\w+ly", text):
    print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))

Write a python function to return random selection from itertools.permutations(iterable, r)

def random_permutation(iterable, r=None):
    import random
    pool = tuple(iterable)
    r = len(pool) if r is None else r
    return tuple(random.sample(pool, r))

Write a python function to random select from itertools.combinations(iterable, r)

def random_combination(iterable, r):
    import random
    pool = tuple(iterable)
    n = len(pool)
    indices = sorted(random.sample(range(n), r))
    return tuple(pool[i] for i in indices)

Write a python function to perform random selection from itertools.combinationswithreplacement(iterable, r)

def random_combination_with_replacement(iterable, r):
    import random
    pool = tuple(iterable)
    n = len(pool)
    indices = sorted(random.choices(range(n), k=r))
    return tuple(pool[i] for i in indices)

Write a python function to locate the leftmost value exactly equal to x

def index(a, x):
    from bisect import bisect_left    
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

Write a python function to locate the rightmost value less than x

def find_lt(a, x):
    from bisect import bisect_left  
    i = bisect_left(a, x)
    if i:
        return a[i-1]
    raise ValueError

Write a python function to find rightmost value less than or equal to x

def find_le(a, x):
    from bisect import bisect_right  
    i = bisect_right(a, x)
    if i:
        return a[i-1]
    raise ValueError

Write a python function to find leftmost value greater than x

def find_gt(a, x):
    from bisect import bisect_right 
    i = bisect_right(a, x)
    if i != len(a):
        return a[i]
    raise ValueError

Write a python function to find leftmost item greater than or equal to x

def find_ge(a, x):
    from bisect import bisect_left 
    i = bisect_left(a, x)
    if i != len(a):
        return a[i]
    raise ValueError

Write a python function to map a numeric lookup using bisect

def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
    from bisect import bisect
    i = bisect(breakpoints, score)
    return grades[i]