B58_Coding Assignment 07
B58_Coding Assignment 07
PRN:2223001000
Roll No:B58
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}
return count
print(subarraySum([1, 1, 1], 2)) # Output: 2
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"
if s[index] != '0':
backtrack(index + 1, path + [chr(int(s[index]) + 96)])
result = []
backtrack(0, [])
return sorted(result)
print("\n".join(decode_keylogger("1234")))
# Expected Output:
# abcd
# awd
# lcd
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