Subject: Python For Engineers Lab (Course Code: CS1018)
ASSIGNMENT ON OPERATORS
(Assignment No 12 To 19)
Branch: Mechanical
Div: F
Batch: F2
SEM: 1
Year: 2024-25
Name: Antara Wagh
Roll No: 46
PRN NO: 12413565
ASSIGNMENT NO 12
1) Title: Program to find maximum of three numbers.
2) Theory: Taking input from user. Then comparing those numbers with each
other and determining the maximum of the numbers.
3) Algorithm:
1) Read the three numbers as input.
2) Compare the numbers using conditional statements.
3) Display output of the maximum number.
4) Python Source Code:
5) Output:
ASSIGNMENT NO 13
1) Title: Program to check if a year is leap.
2) Theory: A leap year is a year that is divisible by 4, but there are exceptions.
If the year is divisible by 4 and not divisible by 100, it is a leap year. If the
year is divisible by 100, it must also be divisible by 400 to be a leap year.
3) Algorithm:
1) Take input of the year.
2) Check divisibility.
3) Print whether the year is a leap year or not.
4) Python Source Code:
5) Output:
ASSIGNMENT NO 14
1) Title: Program to check if a date is valid.
2) Theory: A valid date must satisfy the following conditions: Day must be
between 1 and the maximum valid day for that month. Month must be
between 1 and 12. Year should be a valid integer.
3) Algorithm:
1) Accept three integers representing the day, month, and year.
2) Check if the month is between 1 and 12.
3) Based on the month, check if the day is valid
4) Print whether the date is valid or not.
4) Python Source Code:
5) Output:
ASSIGNMENT NO 15
1) Title: Program to find the roots of a quadratic equation.
2) Theory: A quadratic equation is an equation of the form: ax2+bx+c=0,
where a, b, and c are constants, and x represents the variable.We will find
the nature of roots and then determine the roots of the equation.
3) Algorithm:
1) Input the values of a, b, c.
2) Calculate the discriminant.
3) Check the discriminant
4) Output the roots.
4) Python source code:
5) Output:
ASSIGNMENT NO 16
1) Title: Given a string. Delete from it all the characters whose indices are
divisible by 3.
2) Theory: The task is to delete all characters from a given string whose
indices are divisible by 3. The string indexing starts at 0, and the problem
asks to remove characters from positions 0, 3, 6, 9, etc.
3) Algorithm:
1) Input the value of n
2) Initialize an empty result string to store the modified string.
3) Output the resulting string after all indices divisible by 3 are removed.
4) Python source code:
5) Output:
ASSIGNMENT NO 17
1) Title: Given a sequence of integer numbers ending with the number 0.
Determine the length of the widest fragment where all the elements are
equal to each other.
2) Theory: The program finds the longest consecutive sequence (fragment) of
the same number in a given list of integers, ending when 0 is entered. It
prints the element that has the longest consecutive fragment and the count
of how many times it repeats consecutively.
3) Algorithm:
1) Input: Accept a sequence of integers from the user, ending when 0 is
entered, and store the values in a list.
2) Processing: Traverse the list to find the element with the longest
consecutive occurrence and track its count.
3) Output: Print the element with the longest consecutive fragment and its
count.
4) Python source code:
5)Output:
ASSIGNMENT NO 18
1) Title: In bowling, the player starts with 10 pins at the far end of a lane. The
object is to knock all the pins down. For this exercise, the number of pins
and balls will vary. Given the number of pins N and then the number of
balls K to be rolled, followed by K pairs of numbers (one for each ball
rolled), determine which pins remain standing after all the balls have been
rolled. The balls are numbered from 1 to N (inclusive) for this situation. The
subsequent number pairs, one for each K represent the start to stop
(inclusive) positions of the pins that were knocked down with each role.
Print a sequence of N characters, where "I" represents a pin left standing
and "." represents a pin knocked down
2) Theory: The number of pins, N, which represents how many pins are at the
start. The number of balls, K, which represent the number of rolls in the
game. For each ball, a pair of integers representing the range of pins that
were knocked down (inclusive).
3) Algorithm:
1) Input the values of n and k.
2) Create an array pins of size N initialized with "I"
3) Read the range of pins knocked down (start, end).
4) Set the corresponding positions in the pins array to "." to mark them as
knocked down.
5) Output the final string of pins after processing all balls.
4) Python source code:
5) Output:
ASSIGNMENT NO 19
1) Title: A timestamp is three numbers: a number of hours, minutes and
seconds. Given two timestamps, calculate how many seconds is between
them. The moment of the first timestamp occurred before the moment of
the second timestamp.
2) Theory: We need to calculate the difference in seconds between two
timestamps. A timestamp consists of three numbers: hours, minutes, and
seconds. To calculate the difference, we need to: Convert both
timestamps into seconds (since the start of the day). Subtract the first
timestamp (in seconds) from the second timestamp (in seconds) to get
the difference in seconds.
3) Algorithm:
1) Read the two timestamps: hours, minutes, and seconds for both.
2) Convert both timestamps into total seconds
3) Calculate the difference between the two timestamps in seconds.
4) Print the result.
4) Python source code:
5) Output: