Python Coding Challenges
Python Coding Challenges
1.
Write a Python program that removes all duplicate elements from an array and
returns a new array.
Sample Output:
Original array: 1 3 5 1 3 7 9
After removing duplicate elements from the said array: 1 3 5 7 9
Original array: 2 4 2 6 4 8
After removing duplicate elements from the said array: 2 4 6 8
def remove_duplicates(arr):
# Create an empty set to store unique elements
unique_elements = set()
return new_array
# Example usage
arr1 = [1, 3, 5, 1, 3, 7, 9]
print("Original array:", ' '.join(str(x) for x in arr1))
new_arr1 = remove_duplicates(arr1)
print("After removing duplicate elements from the said array:", ' '.join(str(x) for x in
new_arr1))
arr2 = [2, 4, 2, 6, 4, 8]
print("Original array:", ' '.join(str(x) for x in arr2))
new_arr2 = remove_duplicates(arr2)
print("After removing duplicate elements from the said array:", ' '.join(str(x) for x in
new_arr2))
2.
Write a Python program to find the missing number in a given array of numbers
between 10 and 20.
Sample Output:
Original array: 10 11 12 13 14 16 17 18 19 20
Missing number in the said array (10-20): 15
Original array: 10 11 12 13 14 15 16 17 18 19
Missing number in the said array (10-20): 20
def find_missing_number(numbers):
# Create a set containing all numbers between 10 and 20
full_set = set(range(10, 21))
3.
We are making n stone piles! The first pile has n stones. If n is even, then all piles
have an even number of stones. If n is odd, all piles have an odd number of
stones. Each pile must more stones than the previous pile but as few as possible.
Write a Python program to find the number of stones in each pile.
Input: 2
Output:
[2, 4]
Input: 10
Output:
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
Input: 3
Output:
[3, 5, 7]
Input: 17
Output:
[17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
def find_stone_piles(n):
piles = [] # List to store the number of stones in each pile
return piles
# Example usage:
n = 10
result = find_stone_piles(n)
print(result)
4.
Write a Python program to find the positions of all uppercase vowels (not
counting Y) in even indices of a given string.
Input: w3rEsOUrcE
Output:
[6]
Input: AEIOUYW
Output:
[0, 2, 4]
def find_uppercase_vowel_positions(string):
vowels = ['A', 'E', 'I', 'O', 'U']
positions = []
# Iterate over the characters in the string, considering only even indices
for i in range(0, len(string), 2):
# Check if the character is an uppercase vowel (excluding 'Y')
if string[i] in vowels:
positions.append(i) # Add the index to the positions list
return positions
5.
Write a Python program to sort the numbers in a given list by the sum of their
digits.
Input: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Output:
[10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19]
Input: [23, 2, 9, 34, 8, 9, 10, 74]
Output:
[10, 2, 23, 34, 8, 9, 9, 74]
def sum_of_digits(num):
# Function to calculate the sum of digits in a number
return sum(int(digit) for digit in str(num))
def sort_by_digit_sum(numbers):
# Sorts the numbers in the list based on the sum of their digits
return sorted(numbers, key=sum_of_digits)
# Example usage:
input_list = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
output_list = sort_by_digit_sum(input_list)
print(output_list)