Skip to content

Latest commit

 

History

History
140 lines (87 loc) · 2.05 KB

File metadata and controls

140 lines (87 loc) · 2.05 KB

Easy Interview Tasks

Task Template

Add task statement here

Example 1: add here

Example 2: add here

Solution

# Python code here
Task 1

Write a program to calculate factorial of a number recieved via console. Apply validation for invalid input and display appropreate message.

Example 1: Console input: 5, Output: 120

Example 2: Console input: "test", Output: Invalid number

Solution

def fact(n):
    if n<0:
        return f"Invalid value"
    elif n == 0:
        return 1
    return n * fact(n - 1)


try:
    n = int(input("Enter a number: "))
except Exception as e:
    print(f"Invalid value")
else:
    print(fact(n))
Task 2

Write a program to remove vovels from givan string

Example 1: input = "Life is beautiful", output = "Lf s btfl"

Example 2: input = "World Health Organisation" output = "Wrld Hlth rgnstn"

Solution

input = "World Health Organisation"

output = ''.join([char for char in input if char.lower() not in "aeiou"])

print(output)
Task 3

From a given list, fetch single frequency elements.

Example 1: input: [1, 2, 2, 3, 3, 4, 4] output: [1]

Solution

l = [1, 2, 2, 3, 3, 4, 4]

freq = {}

for i in l:
    freq[i] = freq[i] + 1 if i in freq else 1

single_freq = [k for k in freq if freq[k] == 1]

print(single_freq)
Task 4

Guess the output of the following code:

def func(l=[]):
    l.append(10)
    print(l)


l = [20]
func(l)
l.append(30)
func()


def func_2(l=None):
    l = []
    l.append(10)   
    print(l)
    

l = [20]
func_2(l)
l.append(30)
func_2(l)

Solution

[20, 10]
[10]
[10]
[10]