Functions
Functions
Assignment #: 7
Topic : Functions
Level 1: Easy
1. Write a function check_even_odd that takes a number as input and
Input 5 6
Code:
def check_even_odd(num):
if num % 2 == 0:
return "even"
else:
return "odd"
Output:
2. Write a function find_max that takes three numbers as input and returns
Input 3, 7, 5 10, 2, 10
Output 7 10
Code:
def find_max(num1,num2,num3):
if num1 > num2 and num3:
return num1
elif num2 > num1 and num3:
return num2
else:
return num3
print(find_max(num1,num2,num3))
Output:
3. Write a function calculate_area that takes the radius of a circle as input and
Input 5 10
Code:
def radius_of_circle(r):
return 3.14 * r * r
r = int(input("enter r : "))
print(radius_of_circle(r))
Output:
4. Write a function celsius_to_fahrenheit that takes a temperature in Celsius
Input 0 100
Code:
def celcius_to_farenheit(celcius):
return (9/5)*celcius + 32
celcius = int(input("enter celcius : "))
print(celcius_to_farenheit(celcius))
Output:
leap year.
Code:
def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return "leap year"
else:
return "not a leap year"
else:
return "leap year"
else:
return "not a leap year"
Output:
6. Write a function factorial that takes a number as input and returns its factorial.
Input 5 3
Output 120 6
Code:
def factorial(num):
for i in range(1,num):
num = num * i
return num
Output:
a palindrome.
Code:
def is_palindrome(str):
if str == str[::-1]:
return "palindrome"
else:
return "not a palindrome"
Output:
Output 6 15
Code:
def sum_of_digits(number):
if number < 0:
number = -number
sum_digits = 0
while number > 0:
digit = number % 10
sum_digits += digit
number = number // 10
return sum_digits
Output:
9. Write a function string_length that takes a string as input and returns its length.
Output 5 6
Code:
def string_length(str):
return len(str)
Output:
10. Write a function is_prime that takes a number as input and checks if it
is prime.
Input 11 12
Code:
def is_prime(number):
if number <= 1:
return "not prime"
if number <= 3:
return "prime"
if number % 2 == 0 or number % 3 == 0:
return "not prime"
i = 5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
return "not prime"
i += 6
return "prime"
Output:
11. Write a function count_vowels that takes a string as input and returns
Output 2 5
Code:
def count_vowels(str):
vowels = "aeiouAEIOU"
count = 0
for char in str:
if char in vowels:
count += 1
return count
Output:
12. Write a function find_gcd that takes two numbers as input and returns
Output 3 6
Code:
def find_gcd(a, b):
while b:
a, b = b, a % b
return a
Output:
Problem Statement: Given an integer array nums of length n, you want to create
an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for
Constraints
n == nums.length
Output 3 6
14. Shuffle the Array
form [x1,x2,...,xn,y1,y2,...,yn].
Constraints
nums.length == 2n
Output 3 6
runningSum[i] = sum(nums[0]…nums[i]).
Constraints
Level 2: Medium
1. A communication system converts data into binary number format for
Input 25
Output 11001
Code:
def decimal_to_binary_builtin(n):
return bin(n).replace("0b", "")
Output
2. Write a function to find whether the blood donor is eligible or not for donating
blood. The rules laid down are as follows. a. Age should be above 18 years but not
Code:
def is_eligible_to_donate(age, weight):
if age > 18 and age <= 55 and weight > 45:
return "Eligible"
return "Not Eligible"
feature to calculate the power of a number. Write a function power that takes two
numbers, base and exponent, as input and returns the result of base raised to the
power of exponent.
Output 8 0
4. You are developing a statistics module that needs a function to find the
Code:
def find_max_and_min(numbers):
numbers.sort()
return numbers[0],numbers[-1]
Output:
5. You are developing a bank account management system where customers can
balance and a list of transactions (positive for deposits and negative for
50]
Code:
def calculate_balance(initial_balance, transactions):
final_balance = initial_balance
for i in transactions:
final_balance += i
return final_balance
Output:
calculate_total that takes a list of item prices and a discount percentage, then
discount_percentage = 10 discount_percentage = 5
Output 540 213.75
Code:
def calculate_total(item_prices, discount_percentage):
total_price = sum(item_prices)
discount_amount = total_price * (discount_percentage / 100)
total_price_after_discount = total_price - discount_amount
return total_price_after_discount
item_prices = []
for i in range(0,3):
item_prices.insert(i,int(input("enter price : ")))
discount_percentage = int(input("ebter discount : "))
print(calculate_total(item_prices, discount_percentage))
Output:
the number of hours worked, and returns a performance rating (Excellent, Good,
Average, Poor).
Code:
def evaluate_performance(projects_completed, hours_worked):
perfomance = projects_completed / hours_worked
if perfomance >= 2:
return "Excellent"
elif perfomance >= 1.5:
return "Good"
elif perfomance >= 1:
return "Average"
else:
return "Poor"
Output:
8. You are developing a travel expense calculator for business trips. Write a
function calculate_expenses that takes the number of days of the trip, daily
Code:
def expence_calculator(days,daily_allowance,other_expenses):
return days*daily_allowance+other_expenses
Output:
9. You are developing a weather data analysis tool. Write a function
Input temperatures = [30, 32, 31, 29, temperatures = [25, 27, 26, 24,
Code:
def calculate_average_temperature(daily_temperatures):
total_temperature = sum(daily_temperatures)
average_temperature = total_temperature / 7
return average_temperature
temperatures = []
for i in range(0,7):
temperatures.insert(i,int(input("enter temperature : ")))
print(calculate_average_temperature(temperatures))
Output:
10. You are developing an online quiz grading system. Write a function grade_quiz
that takes a list of student scores (out of 20) and returns the average score and
Input scores = [15, 18, 12, 10, 17] scores = [8, 11, 10, 13, 9]
Code:
def grade_quiz(student_scores):
total_score = sum(student_scores)
average_score = total_score / 5
scores = []
for i in range(0,5):
scores.insert(i,int(input("marks : ")))
print(grade_quiz(scores))
output:
11. Sign of Product of an array
· 1 if x is positive.
· -1 if x is negative.
· 0 if x is equal to 0.
You are given an integer array nums. Let product be the product of all values in the
array nums.
Return signFunc(product).
Constraints:
4,3,2,1]
Output 1 0 -1
smallest index i of nums such that i mod 10 == nums[i], or -1 if such index does
Constraints:
Output 0 2
Explanation: Explanation:
nums[i], so we return the smallest 2 is the only index which has i mod
index 0. 10 == nums[i].
Level 3: Hard
1. XYZ Technologies is in the process of increment the salary of the employees.
This increment is done based on their salary and their performance appraisal
rating.
If the appraisal rating is between 1 and 4, the increment is 10% of the salary.
If the appraisal rating is between 4.1 and 7, the increment is 25% of the
salary.
If the appraisal rating is between 7.1 and 10, the increment is 30% of the salary.
Help them to do this, by writing a program that displays the incremented salary.
Note: If either the salary is 0 or negative (or) if the appraisal rating is not in the
rating 4.3
Code:
def salary_increament(salary,appraisal_rating):
if appraisal_rating >=1 and appraisal_rating <=4:
return salary*0.1+salary
elif 4.1 <= appraisal_rating and appraisal_rating >= 7:
return salary*0.25+salary
elif 7.1 <= appraisal_rating and appraisal_rating >= 10:
return salary*0.3+salary
Output:
2. XYZ college wants to recognize the department which has succeeded in getting
the maximum number of placements for this academic year. The departments
that have participated in the recruitment drive are CS, EC, ME. Help the college to
find the department getting maximum placements. Check for all the possible
Note: If any input is negative, the output should be "Input is invalid". If all
the department has got the highest placement". (Use separate function to check
CS:90 CS:55
EC:45 EC:85
ME:70 ME:85
Code:
def find_max_placements(department_placements):
max_department = None
max_placements = -1
print(find_max_placements(placements))
Output:
"pen"])
representation.
Forty Five"