0% found this document useful (0 votes)
6 views7 pages

Daa Skill Week 3

The document contains code implementations for five different programming problems: 'Sherlock and The Beast', 'Priyanka and Toys', 'Largest Permutation', 'Mark and Toys', and 'Jim and the Orders'. Each problem includes a function that processes inputs to produce the desired outputs, demonstrating various algorithmic techniques such as sorting, swapping, and counting. The examples provided illustrate how to use each function with sample inputs.

Uploaded by

beautyinyou3
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)
6 views7 pages

Daa Skill Week 3

The document contains code implementations for five different programming problems: 'Sherlock and The Beast', 'Priyanka and Toys', 'Largest Permutation', 'Mark and Toys', and 'Jim and the Orders'. Each problem includes a function that processes inputs to produce the desired outputs, demonstrating various algorithmic techniques such as sorting, swapping, and counting. The examples provided illustrate how to use each function with sample inputs.

Uploaded by

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

DAA SKILL WEEK- 3 2300033657 G.

Sri lekha

1.Sherlock and The Beast :

Code:

def decentNumber(n):

# Start with the largest number of 5's possible, which must be divisible by 3

fives = n

while fives % 3 != 0:

fives -= 5

if fives < 0:

print("-1")

else:

threes = n - fives

print("5" * fives + "3" * threes)

# Read input

t = int(input().strip())

for _ in range(t):

n = int(input().strip())

decentNumber(n)
2. Priyanka and Toys

Code:

def toys(w):

# Step 1: Sort the list of weights

w.sort()

# Step 2: Initialize the number of containers

containers = 0

# Step 3: Use a pointer to track the starting point of the current container

i=0

n = len(w)

while i < n:

# This weight is the minimum weight in the current container

min_weight = w[i]

# We start a new container

containers += 1

# Step 4: Continue adding items to the current container

# until we find an item that cannot be added

while i < n and w[i] <= min_weight + 4:

i += 1

return containers

# Example usage:
if __name__ == '__main__':

n = int(input().strip())

w = list(map(int, input().rstrip().split()))

result = toys(w)

print(result)

3. Largest Permutation

Code:

def largestPermutation(k, arr):

n = len(arr)

# Create a dictionary to store the index of each element

index_map = {value: idx for idx, value in enumerate(arr)}

for i in range(n):

# If no swaps left, break early

if k <= 0:

break

# The value that should ideally be at this position for the largest permutation

ideal_value = n - i

# If the ideal value is already in place, continue


if arr[i] == ideal_value:

continue

# Get the current index of the ideal value

ideal_index = index_map[ideal_value]

# Swap the current element with the ideal value element

arr[i], arr[ideal_index] = arr[ideal_index], arr[i]

# Update the indexes in the index_map

index_map[arr[ideal_index]] = ideal_index

index_map[arr[i]] = i

# Decrement the swap count

k -= 1

return arr

# Example usage:

if __name__ == '__main__':

n, k = map(int, input().strip().split())

arr = list(map(int, input().strip().split()))

result = largestPermutation(k, arr)

print(' '.join(map(str, result)))


4. Mark and Toys

Code:

def maximumToys(prices, k):

# Step 1: Sort the list of toy prices

prices.sort()

# Step 2: Initialize the number of toys and the current total cost

num_toys = 0

total_cost = 0

# Step 3: Iterate through the sorted prices and count the number of toys

for price in prices:

if total_cost + price <= k:

total_cost += price

num_toys += 1

else:

break

return num_toys

# Example usage:

if __name__ == '__main__':

n, k = map(int, input().strip().split())

prices = list(map(int, input().strip().split()))

result = maximumToys(prices, k)
print(result)

5. Jim and the Orders

Code:

def jimOrders(orders):

# Calculate the serve times and pair them with customer numbers

serve_times = [(i + 1, order[0] + order[1]) for i, order in enumerate(orders)]

# Sort the serve times, primarily by serve time and secondarily by customer number

serve_times.sort(key=lambda x: (x[1], x[0]))

# Extract and return the customer numbers in the order of sorted serve times

return [customer[0] for customer in serve_times]

# Example usage:

if __name__ == '__main__':

import sys

input = sys.stdin.read

data = input().strip().split()

n = int(data[0])

orders = []

for i in range(n):

orders.append((int(data[2*i+1]), int(data[2*i+2])))
result = jimOrders(orders)

print(' '.join(map(str, result)))

You might also like