0% found this document useful (0 votes)
2 views

B58_Coding Assignment 07

The document contains six coding assignments related to various algorithms and problem-solving techniques in Python. It includes implementations for finding the next permutation, counting subarrays with a given sum, checking string rotations, validating IP addresses, decoding keylogger inputs, and calculating theater ticket costs with discounts. Each section provides example inputs and outputs to illustrate the functionality of the code.

Uploaded by

regularuse0001
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)
2 views

B58_Coding Assignment 07

The document contains six coding assignments related to various algorithms and problem-solving techniques in Python. It includes implementations for finding the next permutation, counting subarrays with a given sum, checking string rotations, validating IP addresses, decoding keylogger inputs, and calculating theater ticket costs with discounts. Each section provides example inputs and outputs to illustrate the functionality of the code.

Uploaded by

regularuse0001
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/ 4

Name:Omkar Santosh Nikam

PRN:2223001000
Roll No:B58

Coding Assignment 7 : (NLP)


1. Next Permutaon Find the next lexicographically greater permutaon of numbers.
Example:
●​ Input: [1,2,3]
●​ Output: [1,3,2]
def next_permutation(nums):
i=len(nums)-2
while i >= 0 and nums[i] >= nums[i + 1]:
i -= 1

if i>=0:
j = len(nums)-1
while nums[j]<= nums[i]:
j -= 1
nums[i], nums[j] = nums[j], nums[i]
nums[i + 1:] = reversed(nums[i + 1:])
return nums
print(next_permutation([1, 2, 3])) # Output: [1, 3, 2]

2)Subarray Sum Equals K Find the number of connuous subarrays that sum up to k.
Example:
●​ Input: nums = [1,1,1], k = 2
●​ Output: 2
def subarraySum(nums,k):
count, current_sum=0,0
prefix_sum={0: 1}

for num in nums:


current_sum+=num
count+=prefix_sum.get(current_sum - k, 0)
prefix_sum[current_sum] = prefix_sum.get(current_sum, 0) + 1

return count
print(subarraySum([1, 1, 1], 2)) # Output: 2

3. Check If One String is a Rotaon of Another.


Example:
●​ Input: "waterbole", "erbolewat"
●​ Output: True

def is_rotation(s1, s2):


return len(s1) == len(s2) and s2 in (s1+s1)

print(is_rotation("waterbottle","erbottlewat")) # Output: True


4)Validate IP Address Check if a given string is a valid IPv4 or IPv6 address.
●​ Example: Input: "192.168.0.1"
●​ Output: "IPv4"
def is_ipv4(IP):
parts = IP.split(".")
return len(parts) == 4 and all(p.isdigit() and 0 <= int(p) <= 255 for p in parts)

def is_ipv6(IP):
parts = IP.split(":")
if len(parts)>8 or len(parts) < 2:
return False
if IP.count("::") > 1:
return False
if "::" in IP:
parts = [p for p in parts if p]
return all(0<len(p) <= 4 and all(c in "0123456789abcdefABCDEF" for c in p) for p in parts)

def validate_ip(IP):
if is_ipv4(IP):
return "IPv4"
elif is_ipv6(IP):
return "IPv6"
return "Invalid"

print(validate_ip("192.168.0.1")) # Output: "IPv4"


print(validate_ip("fe80::1ff:fe23:4567:890a")) # Output: "IPv6"
print(validate_ip("999.999.999.999")) # Output: "Invalid"

5)Keylogger Decoder Problem Statement:


A keylogger records the keystrokes of a user, capturing numerical inputs corresponding to leers (e.g.,
1 for 'a', 2 for 'b', ..., 26 for 'z').
Given a numeric string, generate all possible leer combinaons that the sequence of numbers could
represent.
●​ Input Format:
•​ A string of digits represenng the keylogger input.
●​ Output Format:
•​ All possible leer combinaons, each on a new line, sorted lexicographically.
●​ Sample Input: 1234
●​ Sample Output: abcd awd lcd
●​ Explanaon:
• The digit sequence can be split into leers as follows:
o 1 -> 'a', 2 -> 'b', 3 -> 'c', 4 -> 'd' => "abcd" o 1 -> 'a', 23 -> 'w', 4 -> 'd' => "awd" o 12 -> 'l', 3
-> 'c', 4 -> 'd' => "lcd"
def decode_keylogger(s):
from itertools import permutations

def backtrack(index, path):


if index == len(s):
result.append("".join(path))
return

if s[index] != '0':
backtrack(index + 1, path + [chr(int(s[index]) + 96)])

if index + 1 < len(s) and '10' <= s[index:index+2] <= '26':


backtrack(index + 2, path + [chr(int(s[index:index+2]) + 96)])

result = []
backtrack(0, [])
return sorted(result)

print("\n".join(decode_keylogger("1234")))

# Expected Output:
# abcd
# awd
# lcd

6 )Theater Ticket Pricing Problem Statement:


In a theater, there's a discount scheme where a 10% discount is applied if more than 20 ckets are
booked, and an addional 2% discount is applied if a special coupon is used. Tickets are priced at Rs.75
for 'k' class and Rs.150 for 'q' class. Refreshments can be added for an extra Rs.50 per cket. Calculate
the total cost based on user inputs.
•Input Format:
●​ Number of tickets (integer).
●​ Refreshments required? (char 'y' or 'n').
●​ Coupon code available? (char 'y' or 'n').
●​ Class type ('k' or 'q').
•Output Format:
●​ Total ticket cost rounded to two decimal places.
•Sample Input:
●​ Enter the number of ckets: 35
●​ Do you want refreshment? (y/n): y
●​ Do you have a coupon code? (y/n): y
●​ Enter the class type (k/q): K
•Sample Output:
●​ Total cket cost: Rs.4065.25
•Explanaon:
●​ Base cost = 35 ckets * Rs.75 = Rs.2625
●​ 10% bulk booking discount = Rs.262.50
●​ 2% coupon discount on remaining = Rs.(2625 - 262.50) * 0.02 = Rs.47.25
●​ Refreshments = 35 ckets * Rs.50 = Rs.1750

def calculate_ticket_cost(num_tickets, refreshments, coupon, class_type):

ticket_price=75 if class_type.lower()=='k' else 150


base_cost=num_tickets*ticket_price

if num_tickets>20:
base_cost*=0.90

if coupon.lower()=='y':
base_cost *= 0.98

if refreshments.lower()=='y':
base_cost += num_tickets * 50
print(f"Total ticket cost: Rs.{base_cost:.2f}")
# Sample Input
num_tickets = int(input("Enter the number of tickets: "))#35
refreshments = input("Do you want refreshment? (y/n): ")#y
coupon = input("Do you have a coupon code? (y/n): ")#y
class_type = input("Enter the class type (k/q): ")#k

# Calculate cost
calculate_ticket_cost(num_tickets, refreshments, coupon, class_type)#4065.25

You might also like