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

DSA Assignmet 1

Data structures assignment

Uploaded by

physizzmva
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 views14 pages

DSA Assignmet 1

Data structures assignment

Uploaded by

physizzmva
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/ 14

JAN 9 2025

DATA STRUCTURES AND


ALGORITHMS
ASSIGNMENT 1

M.VIDYADHARI
B231051EC
EC02
1) def calculate_bill():
items = ['Item1', 'Item2', 'Item3', 'Item4', 'Item5']
prices = [80, 45, 36, 100, 25]
quantities = [int(input(f"Enter quantity for {item}: ")) for item in
items]
print(f"{'Item':<10}{'Quantity':<10}{'Price':<10}{'Total':<10}")
total_bill = 0
for item, qty, price in zip(items, quantities, prices):
total = qty * price
print(f"{item:<10}{qty:<10}{price:<10}{total:<10}")
total_bill += total
print(f"Total Bill: {total_bill}")
calculate_bill()

1|Page
2) def display_grade(marks):
grades = {
'A': range(90, 101),
'B': range(80, 90),
'C': range(70, 80),
'D': range(60, 70),
'E': range(50, 60),
'F': range(0, 50),
'S': [100]
}
for grade, range_ in grades.items():
if marks in range_:
return f"Grade: {grade}"
return "Invalid marks."
marks = int(input("Enter marks: "))
print(display_grade(marks))

2|Page
3) def calculator():
op = input("Enter operation (add, subtract, multiply, divide):
").lower()
nums = list(map(float, input("Enter numbers separated by space:
").split()))
if op == 'add':
print(f"Result: {sum(nums)}")
elif op == 'subtract':
result = nums[0] - sum(nums[1:])
print(f"Result: {result}")
elif op == 'multiply':
result = 1
for num in nums:
result *= num
print(f"Result: {result}")

3|Page
elif op == 'divide':
try:
result = nums[0]
for num in nums[1:]:
result /= num
print(f"Result: {result}")
except ZeroDivisionError:
print("Cannot divide by zero.")
else:
print("Invalid operation.")
calculator()

4|Page
4) def print_matrix_with_nested_loops():
matrix = []
print("Enter the values for a 3x3 matrix row by row:")
for i in range(3):
row = []
for j in range(3):
value = int(input(f"Enter value for position ({i+1},{j+1}): "))
row.append(value)
matrix.append(row)
print("\nThe 3x3 Matrix is:")
for row in matrix:
for value in row:
print(value, end=" ")
print()
print_matrix_with_nested_loops()

5|Page
5) def star_pattern():
for i in range(1, 7):
print('*' * i)
star_pattern()

11) import calendar


def print_calendar():
print(calendar.month(2014, 8))
print_calendar()

6|Page
12) def reverse_and_sum():
number = int(input("Enter a 6-digit number: "))
reversed_num = int(str(number)[::-1])
digits_sum = sum(map(int, str(number)))
print(f"Reversed: {reversed_num}, Sum of digits: {digits_sum}")
reverse_and_sum()

SET2
1) import math
def calculate_series_sum(x, n):
series_sum = 0
for i in range(1, n + 1):
power = 2 * i - 1
factorial = math.factorial(2 * i - 2)
term = ((-1) * (i - 1)) * (x * power) / factorial
series_sum += term
return series_sum
try:

7|Page
x = float(input("Enter the value of x: "))
n = int(input("Enter the number of terms in the series (n): "))
if n > 0:
result = calculate_series_sum(x, n)
print(f"The sum of the series with x = {x} and {n} terms is:
{result:.6f}")
else:
print("Please enter a positive integer for n.")
except ValueError:
print("Invalid input. Please enter numerical values.")

2) def generate_pyramid_pattern(n):
for i in range(1, n + 1):
print(" " * (n - i) * 5, end="")
for j in range(1, i + 1):

8|Page
print(f"x^{i}+{j}", end=" "
print()
n=5
generate_pyramid_pattern(n)

3) def print_pyramid_pattern():
total_rows = 6
current_row = 0
for i in range(total_rows):
print(" " * (total_rows - i - 1), end="")
for j in range(i + 1):
print(j, end="")
for j in range(i - 1, -1, -1):
print(j, end="")
print()
print_pyramid_pattern()

9|Page
4) def largest_modified_number(numbers):
modified_numbers = [int(''.join(sorted(str(num), reverse=True))) for
num in numbers]
print(f"Modified numbers: {modified_numbers}")
print(f"Largest modified number: {max(modified_numbers)}")
numbers = [int(input(f"Enter 6-digit number {i+1}: ")) for i in range(3)]
largest_modified_number(numbers)

10 | P a g e
5) import math
def fibonacci_factorial(n):
a, b = 1, 1
print(f"{'Number (n)':<15}{'Factorial (n!)':<15}")
for _ in range(n):
print(f"{a:<15}{math.factorial(a):<15}")
a, b = b, a + b
n = int(input("Enter the number of Fibonacci terms: "))
fibonacci_factorial(n)

6) def analyze_string(s):
vowels = "aeiouAEIOU"
vowel_count = sum(1 for char in s if char in vowels)
non_alpha_count = sum(1 for char in s if not char.isalpha())
first_vowel = next((char for char in s if char in vowels), None)
print(f"Number of vowels: {vowel_count}")
11 | P a g e
print(f"Number of non-alphabetic characters: {non_alpha_count}")
print(f"First vowel: {first_vowel if first_vowel else 'None'}")
s = input("Enter a sentence: ")
analyze_string(s)

7) def remove_consecutive_repeats(s):
result = [s[0]]
for char in s[1:]:
if char != result[-1]:
result.append(char)
print(f"Original string length: {len(s)}")
print(f"Processed string: {''.join(result)}")
print(f"Processed string length: {len(result)}")
s = input("Enter a string: ")
remove_consecutive_repeats(s)

12 | P a g e
13 | P a g e

You might also like