Programming assignment - Week 5
Programming assignment - Week 5
Solutions
Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity.
Programming assignment - 1
Create a Python program that performs a binary search on a sorted list of integers using only loops. The program should prompt the user to input a sorted list of integers and a target number to search for. The program should then search for the target number in the list using the binary search algorithm and print the index of the target if found. If the target is not found, the program should print -1.
Input Format:
The first line of input consists of a space-separated sorted list of integers.
The second line of input consists of a single integer, representing the target number.
Output Format:
The output consists of the index of the target number in the list if found. If the target number is not found, the output should be -1.
Example:
Input:
10 20 30 40 50
30
Output:
2
Code:
def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 if __name__ == "__main__": arr = list(map(int, input().split())) target = int(input()) result = binary_search(arr, target) print(result, end="")
Programming assignment - 2
Create a Python program that merges two sorted lists into one sorted list. The program should prompt the user to input two sorted lists of integers. The program should then merge these two lists into one sorted list and print the result.
Input Format:
The first line of input consists of a space-separated sorted list of integers.
The second line of input consists of another space-separated sorted list of integers.
Output Format:
- The output consists of a single sorted list, which is the result of merging the two input lists.
Example:
Input:
1 3 5 7
2 4 6 8
Output:
1 2 3 4 5 6 7 8
Code:
def merge_sorted_lists(list1, list2):
merged_list = []
i, j = 0, 0
while i < len(list1) and j < len(list2):
if list1[i] < list2[j]:
merged_list.append(list1[i])
i += 1
else:
merged_list.append(list2[j])
j += 1
while i < len(list1):
merged_list.append(list1[i])
i += 1
while j < len(list2):
merged_list.append(list2[j])
j += 1
return merged_list
if __name__ == "__main__":
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))
result = merge_sorted_lists(list1, list2)
for e in result:
print(e, end=" ")
Programming assignment - 3
Create a Python program that finds the k-th largest and k-th smallest elements in a list of integers. The program should prompt the user to input a list of integers and a value `k`. The program should then find and print: 1 if the k-th largest and k-th smallest elements are the same and are at the middle of the sorted list. -1 if the k-th largest and k-th smallest elements are the same but are not in the middle of the sorted list. 0 if the k-th largest and k-th smallest elements are different.
Input Format:
The first line of input consists of a space-separated list of integers.
The second line of input consists of a single integer k.
Output Format:
The output consists of a single integer 1, -1, or 0 based on the conditions specified.
Example:
Input:
3 8 7 5 9 1
2
Output: 0
Code:
l = input().split(' ')
l = [int(num) for num in l]
k = int(input())
l.sort()
if l[k-1] == l[-k]:
if k == len(l)//2+1:
print('1', end='')
else:
print('-1', end='')
else:
print('0', end='')
Comments
Post a Comment