Professional Documents
Culture Documents
M - 2 Lab - Manual
M - 2 Lab - Manual
Given a string ‘S’, find all possible permutations of the string S in lexicographic sorted
Sample Input:
HACK
Expected Output: AC AH AK CA CH CK HA HC HK KA KC KH
Code:
This line imports the permutations function from the itertools module. itertools is a
module in Python's standard library that provides various functions for efficient looping and
combination of iterables.
def generate_permutations(S):
This line defines a function named generate_permutations that takes a string S as input.
This function will generate and return all possible permutations of size 2 from the string S.
perms = []
This line initializes an empty list named perms to store the permutations that will be
generated.
This line starts a loop that iterates over all permutations of size 2 of the input string S. It uses
the permutations function from itertools to generate these permutations.
perms.append(''.join(perm))
This line joins the characters in the current permutation perm into a string and appends it to
the perms list. It uses join to concatenate the characters together.
This line sorts the list perms lexicographically in ascending order. This is done to ensure that
the permutations are in the desired order.
return perms
This line returns the list perms, which contains all the permutations of size 2 from the input
string S.
# Sample Input
S = "HACK"
This line initializes the input string S with the value "HACK"
permutations = generate_permutations(S)
This line calls the generate_permutations function with the input string S and stores the
returned list of permutations in the variable permutations.
print(" ".join(permutations))
This line prints the permutations generated as a single string, separated by spaces. It uses
the join method to concatenate the elements of the permutations list into a single string
before printing.
2.
Code:
import math
This line imports the math module, which provides mathematical functions and constants
like sine, cosine, pi, etc., needed for calculations involving angles and trigonometry.
def calculate_side_length(r):
This line defines a function named calculate_side_length that takes one parameter r,
which represents the length from the center of the pentagon to a vertex.
# Formula to calculate side length of the pentagon
return 2 * r * math.sin(math.pi / 5)
def calculate_pentagon_area(r):
This line defines a function named calculate_pentagon_area that takes one parameter
r, which represents the length from the center of the pentagon to a vertex.
return area
Code:
This line imports the datetime class from the datetime module. The datetime module in
Python provides classes for manipulating dates and times.
def find_day_of_week(date_string):
This line defines a function named find_day_of_week that takes a date string as input. The
date string is expected to be in the format 'DD MM YYYY'.
This line converts the input date string into a datetime object. It uses the strptime()
method of the datetime class, which parses a string representing a date and time according
to the given format ('%d %m %Y' represents day, month, and year in the format expected).
day_of_week = date_obj.weekday()
This line retrieves the day of the week from the datetime object date_obj. The
weekday() method returns an integer representing the day of the week, where Monday is 0
and Sunday is 6.
This line defines a list named days containing the names of the days of the week. This list
will be used to map the integer returned by weekday() to the corresponding day name.
return days[day_of_week]
This line returns the day of the week corresponding to the integer obtained from weekday()
using the days list.
# Sample Input
This line defines the sample input date string "08 05 2015".
day_of_week = find_day_of_week(date_string)
This line calls the find_day_of_week function with the sample input date string and stores
the returned day of the week in the variable day_of_week.
This line prints the result, indicating the day of the week for the given date string. In this
case, it would print "The day on 08 05 2015 was Wednesday".
4. Arun is working in an office which is N blocks away from his house. He wants to
minimize the time it takes him to go from his house to the office. He can either take the
office cab or he can walk to the office. Arun's velocity is V1 m/s when he is walking. The
cab moves with velocity V2 m/s but whenever he calls for the cab, it always starts from
the office, covers N blocks, collects Arun and goes back to the office. The cab crosses a
total distance of N meters when going from office to Arun's house and vice versa, whereas
Arun covers a distance of (2–√∗N)(2∗N) while walking. Help Arun to find whether he
Input Format:
A single line containing three integer numbers N, V1, and V2 separated by a space.
Example-1:
Input:
5 10 15
Output:
Cab
Code:
import math
distance_walked = (2 - (2 ** 0.5)) * N
time_walked = distance_walked / V1
This line calculates the time taken for Arun to walk to the office by dividing the distance
walked by Arun's walking velocity V1.
return time_walked
This line returns the calculated time taken for Arun to walk to the office.
This line defines a function named time_for_cab that takes two parameters N and V2. N
represents the number of blocks away from Arun's house to the office, and V2 represents the
velocity of the cab
total_distance_cab = 2 * N
This line calculates the total distance covered by the cab, which includes the distance from
the office to Arun's house, picking Arun up, traveling back to the office, and returning to
Arun's house.
time_cab = total_distance_cab / V2
This line calculates the time taken for the cab by dividing the total distance covered by the
cab by the velocity of the cab V2.
return time_cab
This line returns the calculated time taken for the cab.
This line takes input from the user in the format of N V1 V2, where N is the number of
blocks, V1 is Arun's walking velocity, and V2 is the velocity of the cab. It then splits the input
string into a list of strings using the split() method and converts each string into an
integer using the map() function.
These lines call the functions time_to_walk and time_for_cab to calculate the time taken
for Arun to walk and for the cab to reach Arun, respectively.
print("Cab")
else:
print("Walk")
This if statement compares the time taken by the cab and the time taken by walking. If the
time taken by the cab is less than the time taken by walking, it prints "Cab", indicating Arun
should take the cab. Otherwise, it prints "Walk", indicating Arun should walk.