Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1.

Given a string ‘S’, find all possible permutations of the string S in lexicographic sorted

order. Each Permutation size is “2” or “3”.

Sample Input:

HACK

Expected Output: AC AH AK CA CH CK HA HC HK KA KC KH

Code:

from itertools import permutations

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.

for perm in permutations(S, 2):

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.

perms.sort() # Sort the permutations lexicographically

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"

# Generate and print permutations

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.

# Calculate side length of the pentagon


s = calculate_side_length(r)
Inside the calculate_pentagon_area function, this line calls the
calculate_side_length function to calculate the side length of the pentagon using the
given r.

# Calculate area using the formula


area = math.sqrt(5 * (5 + 2 * math.sqrt(5))) * s * s / 4

return area

Finally, this line returns the calculated area of the pentagon.

# Example: Length from center of pentagon to vertex


r = 8

# Calculate and print the area of the pentagon


area = calculate_pentagon_area(r)
print("Area of the pentagon:", area)
3. Given X as a date. Write a program to find what the day is on that date.

Sample Input: 08 05 2015

Expected Output: Wednesday

Code:

from datetime import datetime

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'.

# Convert the date string to a datetime object

date_obj = datetime.strptime(date_string, '%d %m %Y')

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).

# Get the day of the week (0 = Monday, 1 = Tuesday, ..., 6 = Sunday)

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.

# Define a list of days of the week

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

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 the corresponding day of the week

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

date_string = "08 05 2015"

This line defines the sample input date string "08 05 2015".

# Find and print the day of the week

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.

print("The day on", date_string, "was", 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

should walk or take a cab to minimize the time.

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

def time_to_walk(N, V1):


This line defines a function named time_to_walk that takes two parameters N and V1. N
represents the number of blocks away from Arun's house to the office, and V1 represents
Arun's walking velocity.

# Calculate the distance Arun walks

distance_walked = (2 - (2 ** 0.5)) * N

# Calculate the time taken for walking

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.

def time_for_cab(N, V2):

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

# Calculate the total distance covered by 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.

# Calculate the time taken for the cab

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.

# Input Format: N, V1, V2 separated by a space


N, V1, V2 = map(int, input().split())

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.

# Calculate time taken for walking and for the cab

time_walked = time_to_walk(N, V1)

time_cab = time_for_cab(N, V2)

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.

# Compare the time taken by walking and by the cab

if time_cab < time_walked:

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.

You might also like