0% found this document useful (0 votes)
22 views8 pages

Lci2022028 Lab

The document discusses three programming problems - printing a 2D array in spiral form, finding the next permutation of a number with the same digits, and finding the starting and ending positions of a target number in a sorted array. For each problem, the question, an example, and the Python solution are provided.

Uploaded by

lci2022028
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)
22 views8 pages

Lci2022028 Lab

The document discusses three programming problems - printing a 2D array in spiral form, finding the next permutation of a number with the same digits, and finding the starting and ending positions of a target number in a sorted array. For each problem, the question, an example, and the Python solution are provided.

Uploaded by

lci2022028
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/ 8

Lab Assignment

Adavanced
Programming Language
(April 16, 2024)

NAME : KASHISH GARG


ROLL NO: LCI2022028
Problem 1: Given a 2D array, print it in spiral form.
Example -
Input: {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16 }}
Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

SOLUTION:

def spiral_print(matrix):
if not matrix:
return

top = 0
bottom = len(matrix) - 1
left = 0
right = len(matrix[0]) - 1
direction = 0

while top <= bottom and left <= right:


if direction == 0:
for i in range(left, right + 1):
print(matrix[top][i], end=" ")
top += 1
elif direction == 1:
for i in range(top, bottom + 1):
print(matrix[i][right], end=" ")
right -= 1
elif direction == 2:
for i in range(right, left - 1, -1):
print(matrix[bottom][i], end=" ")
bottom -= 1
elif direction == 3:
for i in range(bottom, top - 1, -1):
print(matrix[i][left], end=" ")
left += 1
direction = (direction + 1) % 4

# Example usage:
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
spiral_print(matrix)

OUTPUT:
Problem 2: Given a number n, find the smallest number that has same set
of digits as n and is greater than n.
If n is the greatest possible number with its set of digits, then print “not
possible”.
Example –
Input: n = "218765"
Output: "251678"
Input: n = "1234"
Output: "1243"
Input: n = "4321"
Output: "Not Possible"
Input: n = "534976"
Output: "536479"

SOLUTION:

def next_permutation(n):
# Convert the number to a list of digits
digits = list(n)
# Find the first digit from the right which is smaller than the digit next
to it
i = len(digits) - 2
while i >= 0 and digits[i] >= digits[i + 1]:
i -= 1

# If no such digit is found, it means n is the greatest possible number


with its set of digits
if i == -1:
return "Not Possible"

# Find the smallest digit on the right side of digits[i] that is greater
than digits[i]
j = len(digits) - 1
while digits[j] <= digits[i]:
j -= 1

# Swap digits[i] with digits[j]


digits[i], digits[j] = digits[j], digits[i]

# Sort the digits to the right of digits[i] in ascending order


digits[i + 1:] = sorted(digits[i + 1:])

# Convert the list of digits back to a string and return


return "".join(digits)

# Example usage:
inputs = ["218765", "1234", "4321", "534976"]
for n in inputs:
print("Input:", n)
print("Output:", next_permutation(n))

OUTPUT-
3. Given an array of integers nums sorted in non-decreasing order, find
the starting and ending
position of a given target value.
If target is not found in the array, return [-1, -1].
Example -
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Input: nums = [], target = 0
Output: [-1,-1]

SOLUTION -

def search_range(nums, target):


# Function to find the leftmost (starting) index of the target
def find_start(nums, target):
start = 0
end = len(nums) - 1
while start <= end:
mid = (start + end) // 2
if nums[mid] < target:
start = mid + 1
else:
end = mid - 1
return start

# Function to find the rightmost (ending) index of the target


def find_end(nums, target):
start = 0
end = len(nums) - 1
while start <= end:
mid = (start + end) // 2
if nums[mid] <= target:
start = mid + 1
else:
end = mid - 1
return end

# Find the starting and ending positions


start = find_start(nums, target)
end = find_end(nums, target)

# If target is not found, return [-1, -1]


if start > end:
return [-1, -1]
else:
return [start, end]

# Example usage:
nums = [5, 7, 7, 8, 8, 10]
targets = [8, 6, 0]
for target in targets:
print("Input: nums =", nums, "target =", target)
print("Output:", search_range(nums, target))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy