This Python program reads two arrays of sizes m and n, then appends the first element of the second array to the end of the first array.
It includes three methods:
- read_array: to read array elements
- append_first_of_second: to append the first element of the second array
- print_arrays: to display both arrays
def read_array(name: str, count: int) -> list[int]:
print(f"Enter elements for array {name}:")
return [int(input(f"{name}[{i}]: ")) for i in range(count)]
def append_first_of_second(arr1: list[int], arr2: list[int]) -> list[int]:
if arr2:
arr1.append(arr2[0])
return arr1
def print_arrays(arr1: list[int], arr2: list[int]):
print(f"First array: {arr1}")
print(f"Second array: {arr2}")
# Read sizes
m = int(input("Number of elements in first array (m): "))
n = int(input("Number of elements in second array (n): "))
# Read arrays
array1 = read_array("A", m)
array2 = read_array("B", n)
# Append first element of second array
array1 = append_first_of_second(array1, array2)
# Final output
print_arrays(array1, array2)
A[0]: 10
A[1]: 20
B[0]: 99
B[1]: 88
First array: [10, 20, 99]
Second array: [99, 88]