0% found this document useful (0 votes)
3 views3 pages

Assignment 5 (B58)

The document contains Python code snippets for various algorithms, including finding all subsequences of a string, determining a winner in a game, calculating the maximum subarray sum, finding the longest subarray with a sum less than or equal to K, checking if one string is a rotation of another, and determining the starting position for a circular route based on fuel and distance. Each function is accompanied by user input prompts and example outputs. The code demonstrates fundamental programming concepts and problem-solving techniques.

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)
3 views3 pages

Assignment 5 (B58)

The document contains Python code snippets for various algorithms, including finding all subsequences of a string, determining a winner in a game, calculating the maximum subarray sum, finding the longest subarray with a sum less than or equal to K, checking if one string is a rotation of another, and determining the starting position for a circular route based on fuel and distance. Each function is accompanied by user input prompts and example outputs. The code demonstrates fundamental programming concepts and problem-solving techniques.

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/ 3

Name: OMKAR SANTOSH NIKAM

PRN: 2223001000

Class: TY B

Roll_no: 58

def subsequence(s, index=0, current="", result=None):


if result is None:
result = []

if index == len(s):
result.append(current)
return result
subsequence(s, index + 1, current, result)
subsequence(s, index + 1, current + s[index], result)

return result

s = input("Enter a string: ")


print("All subsequences:", subsequence(s))

Enter a string: abc


All subsequences: ['', 'c', 'b', 'bc', 'a', 'ac', 'ab', 'abc']

def winner(n, k):


friends = list(range(1, n + 1))
index = 0

while len(friends) > 1:


index = (index + k - 1) % len(friends)
friends.pop(index)

return friends[0]

n = int(input("Enter number of friends: "))


k = int(input("Enter step count: "))
print("The winner is:", winner(n, k))

Enter number of friends: 5


Enter step count: 2
The winner is: 3

def array(nums):
max_sum = float('-inf')
current_sum = 0

for num in nums:


current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum

nums = list(map(int, input("Enter space-separated integers:


").split()))

print("Maximum Subarray Sum:", array(nums))

Enter space-separated integers: -2 1 -3 4 -1 2 1 -5 4


Maximum Subarray Sum: 6

def subarray(nums, K):


left = 0
current_sum = 0
max_length = 0

for right in range(len(nums)):


current_sum += nums[right]

while current_sum > K:


current_sum -= nums[left]
left += 1

max_length = max(max_length, right - left + 1)

return max_length

nums = list(map(int, input("Enter space-separated integers:


").split()))
K = int(input("Enter K: "))

print("Longest Subarray Length:", subarray(nums, K))

Enter space-separated integers: 3 1 2 7 4 2 1


Enter K: 8
Longest Subarray Length: 3

def rotate(s1, s2):


if len(s1) != len(s2):
return False
return s2 in (s1 + s1)

s1 = input("Enter first string: ")


s2 = input("Enter second string: ")

print(rotate(s1, s2))

Enter first string: abcde


Enter second string: cdeab
True
def StartingPosition(Fuel, Dist):
total_fuel, total_dist = sum(Fuel), sum(Dist)

if total_fuel < total_dist:


return -1

start_position = 0
fuel_balance = 0

for i in range(len(Fuel)):
fuel_balance += Fuel[i] - Dist[i]

if fuel_balance < 0:
start_position = i + 1
fuel_balance = 0

return start_position

Fuel = list(map(int, input("Enter space-separated fuel values:


").split()))
Dist = list(map(int, input("Enter space-separated distance values:
").split()))

print("Starting Position:", StartingPosition(Fuel, Dist))

Enter space-separated fuel values: 6 3 7 4


Enter space-separated distance values: 5 6 2 5
Starting Position: 2

You might also like