Thamizhiniyan C S
HomeWriteupsResourcesCheatsheets
HackerRank
HackerRank
  • HackerRank
  • Linux Shell
    • Bash
    • Text Processing
      • Cut
      • Head
      • Middle
      • Tail
      • Tr
      • Sort
      • Uniq
      • Paste
    • Arrays In Bash
    • Grep Sed Awk
      • Grep
      • Sed
      • Awk
  • Regex
    • Introduction
    • Character Class
    • Repetitions
    • Grouping and Capturing
    • Backreferences
    • Assertions
    • Applications
  • Python
    • Introduction
    • Basic Data Types
    • Strings
    • Sets
    • Math
    • Itertools
    • Collections
    • Date and Time
    • Errors and Exceptions
    • Classes
    • Built-Ins
    • Python Functionals
    • Regex and Parsing
    • XML
    • Closures and Decorators
    • Numpy
    • Debugging
  • C
    • Introduction
    • Conditionals and Loops
    • Arrays and Strings
Powered by GitBook
On this page
  • Map and Lambda Function
  • Validating Email Address with a Filter
  • Reduce Function

Was this helpful?

  1. Python

Python Functionals

PreviousBuilt-InsNextRegex and Parsing

Last updated 1 year ago

Was this helpful?

Map and Lambda Function

cube = lambda x: pow(x, 3)

def fibonacci(n):
    numbers = []
    
    for i in range(n):
        if i <= 1:
            numbers.append(i)
        else:
            numbers.append(numbers[len(numbers)-1] + numbers[len(numbers)-2])

    return numbers

if __name__ == '__main__':
    n = int(input())
    print(list(map(cube, fibonacci(n))))

Validating Email Address with a Filter

import re

def fun(s):
    # return True if s is a valid email, else return False
    if re.match(r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}$', s):
        return True
    else:
        return False
    

def filter_mail(emails):
    return list(filter(fun, emails))

if __name__ == '__main__':
    n = int(input())
    emails = []
    for _ in range(n):
        emails.append(input())

filtered_emails = filter_mail(emails)
filtered_emails.sort()
print(filtered_emails)

Reduce Function

from fractions import Fraction
from functools import reduce

def product(fracs):
    t = reduce(lambda x, y: x * y, fracs)
    return t.numerator, t.denominator

if __name__ == '__main__':
    fracs = []
    for _ in range(int(input())):
        fracs.append(Fraction(*map(int, input().split())))
    result = product(fracs)
    print(*result)
Solve Programming Questions | HackerRankHackerRank
Logo