Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Python Coding Challenges

The document contains 5 coding challenges involving Python programming tasks such as removing duplicate elements from an array, finding a missing number in a given range, calculating the number of stones in piles based on an input number, finding positions of uppercase vowels in even indices of a string, and sorting numbers in a list by the sum of their digits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Python Coding Challenges

The document contains 5 coding challenges involving Python programming tasks such as removing duplicate elements from an array, finding a missing number in a given range, calculating the number of stones in piles based on an input number, finding positions of uppercase vowels in even indices of a string, and sorting numbers in a list by the sum of their digits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

IB CS Python coding challenges

Try to complete as many of the challenges below as possible.


Look carefully at the problem, the sample input and sample output to get a clear idea of
what is expected. Submit a document containing your annotated code.
If you want any more challenges, have a look at this site:
https://www.w3resource.com/python-exercises/

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()

# Iterate over the elements in the array


for element in arr:
unique_elements.add(element) # Add each element to the set

# Create a new array from the unique elements set


new_array = list(unique_elements)

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))

# Convert the input list into a set for efficient comparison


input_set = set(numbers)

# Find the missing number by taking the set difference


missing_number = full_set - input_set

# Return the missing number as an integer


return missing_number.pop()
# Sample usage
input_array = [10, 11, 12, 13, 14, 16, 17, 18, 19, 20]
print("Original array:", " ".join(map(str, input_array)))

# Call the function to find the missing number


missing_num = find_missing_number(input_array)

# Print the result


print("Missing number in the said array (10-20):", missing_num)

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

# Determine the parity of n


is_even = n % 2 == 0

# Iterate from 0 to n (exclusive) in steps of 2


for i in range(0, n, 2):
# Calculate the number of stones in the current pile
stones = n + i

# Adjust the number of stones based on the parity of n


if is_even:
stones += 2
else:
stones += 1

# Append the number of stones to the list of piles


piles.append(stones)

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

# Test the function


input_string = input("Enter a string: ")
result = find_uppercase_vowel_positions(input_string)
print("Output:")
print(result)

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)

You might also like