0% found this document useful (0 votes)
14 views19 pages

Py PDF2

Uploaded by

himanshukhare450
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Py PDF2

Uploaded by

himanshukhare450
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Future Value Calculator

# Program by Rajdeep Mishra

def calculate_future_value(principal, rate, years):

future_value = principal * (1 + rate/100) ** years

return round(future_value, 2)

print("Future Value Calculator by Rajdeep Mishra")

amt = 10000

rate = 3.5

years = 7

result = calculate_future_value(amt, rate, years)

print(f"Future value of ${amt} at {rate}% interest after {years} years: ${result}")

# Output:

# Future Value Calculator by Rajdeep Mishra

# Future value of $10000 at 3.5% interest after 7 years: $12722.79

2. Distance Between Points:

# Program by Rajdeep Mishra

import math

def calculate_distance(x1, y1, x2, y2):

return round(math.sqrt((x2 - x1)**2 + (y2 - y1)**2), 5)

print("Distance Calculator by Rajdeep Mishra")

# Example 1
x1, y1 = 3, 4

x2, y2 = 7, 7

print(f"Distance between points ({x1},{y1}) and ({x2},{y2}): {calculate_distance(x1,y1,x2,y2)}")

# Example 2

x1, y1 = 3, 4

x2, y2 = 4, 3

print(f"Distance between points ({x1},{y1}) and ({x2},{y2}): {calculate_distance(x1,y1,x2,y2)}")

# Output:

# Distance Calculator by Rajdeep Mishra

# Distance between points (3,4) and (7,7): 5.0

# Distance between points (3,4) and (4,3): 1.41421

3. Sum Double

# Program by Rajdeep Mishra

def sum_double(a, b):

return (a + b) * 2 if a == b else a + b

print("Sum Double Calculator by Rajdeep Mishra")

print(f"sum_double(1,2) → {sum_double(1,2)}")

print(f"sum_double(3,2) → {sum_double(3,2)}")

print(f"sum_double(2,2) → {sum_double(2,2)}")

# Output:

# Sum Double Calculator by Rajdeep Mishra

# sum_double(1,2) → 3

# sum_double(3,2) → 5

# sum_double(2,2) → 8
4. Parrot Trouble

# Program by Rajdeep Mishra

def parrot_trouble(talking, hour):

return talking and (hour < 7 or hour > 20)

print("Parrot Trouble Checker by Rajdeep Mishra")

print(f"parrot_trouble(True, 6) → {parrot_trouble(True, 6)}")

print(f"parrot_trouble(True, 7) → {parrot_trouble(True, 7)}")

print(f"parrot_trouble(False, 6) → {parrot_trouble(False, 6)}")

# Output:

# Parrot Trouble Checker by Rajdeep Mishra

# parrot_trouble(True, 6) → True

# parrot_trouble(True, 7) → False

# parrot_trouble(False, 6) → False

5. Positive Negative

# Program by Rajdeep Mishra

def pos_neg(a, b, negative):

if negative:

return a < 0 and b < 0

return (a < 0 and b > 0) or (a > 0 and b < 0)

print("Positive Negative Checker by Rajdeep Mishra")

print(f"pos_neg(1,-1,False) → {pos_neg(1,-1,False)}")
print(f"pos_neg(-1,1,False) → {pos_neg(-1,1,False)}")

print(f"pos_neg(-4,-5,True) → {pos_neg(-4,-5,True)}")

# Output:

# Positive Negative Checker by Rajdeep Mishra

# pos_neg(1,-1,False) → True

# pos_neg(-1,1,False) → True

# pos_neg(-4,-5,True) → True

6. Near Hundred

# Program by Rajdeep Mishra

def near_hundred(n):

return abs(100 - n) <= 10 or abs(200 - n) <= 10

print("Near Hundred Checker by Rajdeep Mishra")

print(f"near_hundred(93) → {near_hundred(93)}")

print(f"near_hundred(90) → {near_hundred(90)}")

print(f"near_hundred(89) → {near_hundred(89)}")

# Output:

# Near Hundred Checker by Rajdeep Mishra

# near_hundred(93) → True

# near_hundred(90) → True

# near_hundred(89) → False

7. Difference 21

# Program by Rajdeep Mishra


def diff21(n):

if n > 21:

return abs(n - 21) * 2

return abs(n - 21)

print("Difference 21 Calculator by Rajdeep Mishra")

print(f"diff21(19) → {diff21(19)}")

print(f"diff21(10) → {diff21(10)}")

print(f"diff21(21) → {diff21(21)}")

# Output:

# Difference 21 Calculator by Rajdeep Mishra

# diff21(19) → 2

# diff21(10) → 11

# diff21(21) → 0

8. Basic Calculator

# Program by Rajdeep Mishra

# Roll No.: 2300971520150

def calculator():

print("Basic Calculator")

print("Developed by Rajdeep Mishra (2300971520150)")

print("----------------------------------------")

num1 = float(input("Enter first number: "))

operator = input("Enter operator (+, -, *, /): ")

num2 = float(input("Enter second number: "))


if operator == '+':

result = num1 + num2

elif operator == '-':

result = num1 - num2

elif operator == '*':

result = num1 * num2

elif operator == '/':

if num2 != 0:

result = num1 / num2

else:

return "Error: Division by zero!"

else:

return "Invalid operator!"

return f"Result: {result}"

print(calculator())

# Sample Output:

# Basic Calculator

# Developed by Rajdeep Mishra (2300971520150)

# ----------------------------------------

# Enter first number: 10

# Enter operator (+, -, *, /): +

# Enter second number: 5

# Result: 15.0

9. Discount Calculator

# Program by Rajdeep Mishra


# Roll No.: 2300971520150

def calculate_discount():

print("Discount Calculator")

print("Developed by Rajdeep Mishra (2300971520150)")

print("----------------------------------------")

total_amount = float(input("Enter the total purchase amount: $"))

if total_amount > 100:

discount = total_amount * 0.10 # 10% discount

discount_percentage = "10%"

else:

discount = total_amount * 0.05 # 5% discount

discount_percentage = "5%"

final_amount = total_amount - discount

print(f"\nOriginal Amount: ${total_amount}")

print(f"Discount Applied: {discount_percentage}")

print(f"Discount Amount: ${discount:.2f}")

print(f"Final Amount: ${final_amount:.2f}")

calculate_discount()

# Sample Output:

# Discount Calculator

# Developed by Rajdeep Mishra (2300971520150)

# ----------------------------------------

# Enter the total purchase amount: $150

#
# Original Amount: $150.0

# Discount Applied: 10%

# Discount Amount: $15.00

# Final Amount: $135.00

10. Grade Calculator

# Program by Rajdeep Mishra

# Roll No.: 2300971520150

def calculate_grade():

print("Grade Calculator")

print("Developed by Rajdeep Mishra (2300971520150)")

print("----------------------------------------")

score = float(input("Enter the student's score: "))

if score >= 90:

grade = 'A'

elif score >= 80:

grade = 'B'

elif score >= 70:

grade = 'C'

elif score >= 60:

grade = 'D'

else:

grade = 'F'

print(f"\nScore: {score}")
print(f"Grade: {grade}")

calculate_grade()

# Sample Output:

# Grade Calculator

# Developed by Rajdeep Mishra (2300971520150)

# ----------------------------------------

# Enter the student's score: 85

# Score: 85.0

# Grade: B

11. Century and Leap Year Checker

# Program by Rajdeep Mishra, Roll No: 2300971520150

def is_century_leap_year(year):

is_century = year % 100 == 0

is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

return is_century, is_leap

print("Century and Leap Year Checker by Rajdeep Mishra, Roll No: 2300971520150")

year = 2000

century, leap = is_century_leap_year(year)

print(f"Year {year} is century year: {century}, leap year: {leap}")

# Output:

# Century and Leap Year Checker by Rajdeep Mishra, Roll No: 2300971520150

# Year 2000 is century year: True, leap year: True


12. Interest Calculator

print("Program by Rajdeep Mishra (Roll No: 2300971520150)")

def calculate_interest(gender, age):

if gender.lower() == 'female':

if 1 <= age <= 58:

return 8.2

elif 59 <= age <= 100:

return 9.2

elif gender.lower() == 'male':

if 1 <= age <= 58:

return 8.4

elif 59 <= age <= 100:

return 10.5

return "Invalid input"

# Test cases

print("\nTest Case 1:")

print(f"Female, age 25: {calculate_interest('Female', 25)}%")

print("\nTest Case 2:")

print(f"Male, age 65: {calculate_interest('Male', 65)}%")

13. Month Name Printer

print("Program by Rajdeep Mishra (Roll No: 2300971520150)")

def get_month_name(month):
months = {

1: "January", 2: "February", 3: "March", 4: "April",

5: "May", 6: "June", 7: "July", 8: "August",

9: "September", 10: "October", 11: "November", 12: "December"

return months.get(month, "Invalid month")

# Test cases

print("\nTest Case 1:")

print(f"Month 12: {get_month_name(12)}")

print("\nTest Case 2:")

print(f"Month 15: {get_month_name(15)}")

14. Sum of Digits

print("Program by Rajdeep Mishra (Roll No: 2300971520150)")

def sum_of_digits(number):

return sum(int(digit) for digit in str(number))

# Test cases

print("\nTest Case 1:")

print(f"Sum of digits in 1234: {sum_of_digits(1234)}")

print("\nTest Case 2:")

print(f"Sum of digits in 5678: {sum_of_digits(5678)}")

15. Floyd's Triangle

print("Program by Rajdeep Mishra (Roll No: 2300971520150)")


def print_floyd_triangle(n):

for i in range(1, n + 1):

print("* " * i)

# Test case

print("\nFloyd's Triangle with n=3:")

print_floyd_triangle(3)

16. Palindrome Checker

print("Program by Rajdeep Mishra (Roll No: 2300971520150)")

def is_palindrome(number):

num_str = str(number)

return num_str == num_str[::-1]

17. Count occurrence of an item in the list:

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to count occurrence of an item in list\n")

def count_occurrence(lst, x):

return lst.count(x)

# Test cases

lst1 = [15, 6, 7, 10, 12, 20, 10, 28, 10]

x1 = 10
lst2 = [8, 6, 8, 10, 8, 20, 10, 8, 8]

x2 = 16

print(f"Input List 1: {lst1}")

print(f"Number to find: {x1}")

print(f"Output: {count_occurrence(lst1, x1)}")

print(f"\nInput List 2: {lst2}")

print(f"Number to find: {x2}")

print(f"Output: {count_occurrence(lst2, x2)}")

18. Find second largest number

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to find second largest number in a list\n")

def find_second_largest(lst):

if len(lst) < 2:

return None

unique_sorted = sorted(set(lst), reverse=True)

return unique_sorted[1] if len(unique_sorted) >= 2 else None

# Test case

numbers = [12, 35, 1, 10, 34, 1, 35]

print(f"Input List: {numbers}")

print(f"Second largest number: {find_second_largest(numbers)}")

19. Remove Multiple Elements


print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to remove multiple elements from list\n")

def remove_elements(lst, elements_to_remove):

return [x for x in lst if x not in elements_to_remove]

# Test case

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

elements_to_remove = [3, 5, 7]

print(f"Original List: {original_list}")

print(f"Elements to remove: {elements_to_remove}")

print(f"Result: {remove_elements(original_list, elements_to_remove)}")

20. Print duplicates

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to print duplicates from a list\n")

def find_duplicates(lst):

seen = set()

duplicates = set()

for num in lst:

if num in seen:

duplicates.add(num)

seen.add(num)

return list(duplicates)
# Test cases

list1 = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]

print(f"Input: {list1}")

print(f"Output: {find_duplicates(list1)}")

21. Uncommon elements in lists of list

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to find uncommon elements in lists of list\n")

def find_uncommon(list1, list2):

return [item for item in list1 + list2 if (item in list1) ^ (item in list2)]

# Test case

list1 = [[1, 2], [3, 4], [5, 6]]

list2 = [[3, 4], [5, 7], [1, 2]]

print(f"List 1: {list1}")

print(f"List 2: {list2}")

print(f"Uncommon elements: {find_uncommon (list1, list2)}")

22. Extract elements with Frequency greater than K

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to extract elements with frequency greater than K\n")


def extract_elements(lst, K):

freq_dict = {}

for num in lst:

if num in freq_dict:

freq_dict[num] += 1

else:

freq_dict[num] = 1

return [num for num, freq in freq_dict.items() if freq > K]

# Test cases

test_list1 = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8]

K1 = 3

test_list2 = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6]

K2 = 2

print(f"Input List 1: {test_list1}, K = {K1}")

print(f"Output: {extract_elements(test_list1, K1)}")

print(f"\nInput List 2: {test_list2}, K = {K2}")

print(f"Output: {extract_elements(test_list2, K2)}")

23. Check if list contains three consecutive common numbers

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to check if list contains three consecutive common numbers\n")

def check_consecutive(lst):

for i in range(len(lst) - 2):


if lst[i] == lst[i + 1] == lst[i + 2]:

return lst[i]

return None

# Test cases

list1 = [4, 5, 5, 5, 3, 8]

list2 = [1, 1, 1, 64, 23, 64, 22, 22, 22]

print(f"Input List 1: {list1}")

print(f"Output: {check_consecutive(list1)}")

print(f"\nInput List 2: {list2}")

print(f"Output: {check_consecutive(list2)}")

24. Find the Strongest Neighbour

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to find the Strongest Neighbour\n")

def find_strongest_neighbour(lst):

return [max(lst[i], lst[i + 1]) for i in range(len(lst) - 1)]

# Test cases

arr1 = [1, 2, 2, 3, 4, 5 ]

arr2 = [5, 5]

print(f"Input Array 1: {arr1}")

print(f"Output: {find_strongest_neighbour(arr1)}")
print(f"\nInput Array 2: {arr2}")

print(f"Output: {find_strongest_neighbour(arr2)}")

25. Print all Possible Combinations from the three Digits

print("Name: Rajdeep Mishra")

print("Roll No: 2300971520150")

print("\nProgram to print all Possible Combinations from the three Digits\n")

import itertools

def print_combinations(digits):

for p in itertools.permutations(digits):

print(' '.join(map(str, p)))

# Test cases

digits1 = [1, 2, 3]

digits2 = [0, 9, 5]

print(f"Input Digits 1: {digits1}")

print_combinations(digits1)

print(f"\nInput Digits 2: {digits2}")

print_combinations(digits2)

26. Retain records with N occurrences of K

print("Name: Rajdeep Mishra")


print("Roll No: 2300971520150")

print("\nProgram to retain records with N occurrences of K\n")

def retain_records(lst, K, N):

return [tup for tup in lst if tup.count(K) == N]

# Test cases

test_list1 = [(4, 5, 5, 4), (5, 4, 3)]

K1 = 5

N1 = 2

test_list2 = [(4, 5, 5, 4), (5, 4, 3)]

K2 = 5

N2 = 3

print(f"Input List 1: {test_list1}, K = {K1}, N = {N1}")

print(f"Output: {retain_records(test_list1, K1, N1)}")

print(f"\nInput List 2: {test_list 2}, K = {K2}, N = {N2}")

print(f"Output: {retain_records(test_list2, K2, N2)}")

You might also like