80+ Python Coding Challenges For Beginners
80+ Python Coding Challenges For Beginners
for Beginners
By
Katie Millie
Copyright notice
Copyright © 2024 Katie Millie. All rights reserved.
Table of Contents
INTRODUCTION
Chapter 1
Welcome to the World of Python!
Chapter 2
Setting Up Your Python Environment
Chapter 3
Basic Python Syntax: Variables, Data Types, Operators, and Expressions
Chapter 4
Chapter 6
Putting Your Skills to the Test: Level 1 Challenges (Basic Concepts)
Conclusion
80+ Python Coding Challenges for Beginners
Coding challenges are a great way to improve your problem-
solving skills and solidify your understanding of
programming concepts. Below are 80+ Python coding
challenges designed specifically for beginners to help you
practice and enhance your Python skills.
1. Sum of Two Numbers
Develop a Python script that prompts the user to input two
numbers and displays their total.
```python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)
```
2. Product of Two Numbers
Write a Python program that takes two numbers as input
and prints their product.
```python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
product = num1 * num2
print("Product:", product)
```
3. Area of a Rectangle
Write a Python program that calculates and prints the area
of a rectangle given its length and width.
```python
length = float(input("Enter length of rectangle: "))
width = float(input("Enter width of rectangle: "))
area = length * width
print("Area of rectangle:", area)
```
4. Area of a Circle
Write a Python program that calculates and prints the area
of a circle given its radius.
```python
import math
radius = float(input("Enter radius of circle: "))
area = math.pi * (radius ** 2)
print("Area of circle:", area)
```
5. Celsius to Fahrenheit Conversion
Write a Python program that converts Celsius to Fahrenheit.
```python
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
```
6. Fahrenheit to Celsius Conversion
Write a Python program that converts Fahrenheit to Celsius.
```python
fahrenheit = float(input("Enter temperature in Fahrenheit:
"))
celsius = (fahrenheit - 32) * 5/9
print("Temperature in Celsius:", celsius)
```
7. Swap Two Numbers
Create a Python script to exchange the values stored in two
variables.
```python
num1 = 10
num2 = 20
# Swap logic
temp = num1
num1 = num2
num2 = temp
print("After swapping:")
print("num1:", num1)
print("num2:", num2)
```
8. Check Even or Odd
Write a Python program that checks if a given number is
even or odd.
```python
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
```
9. Check Prime Number
Develop a Python script to calculate the factorial of a
provided number.
```python
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
```
10. Factorial of a Number
Write a Python program to find the factorial of a given
number.
```python
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial:", factorial)
```
11. Fibonacci Series
Write a Python program to generate the Fibonacci series up
to a specified number of terms.
```python
num_terms = int(input("Enter number of terms: "))
a, b = 0, 1
count = 0
while count < num_terms:
print(a)
nth = a + b
a=b
b = nth
count += 1
```
12. Reverse a String
Write a Python program to reverse a given string.
```python
string = input("Enter a string: ")
reversed_string = string[::-1]
print("Reversed string:", reversed_string)
```
13. Check Palindrome
Write a Python program that checks if a given string is a
palindrome.
```python
string = input("Enter a string: ")
if string == string[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
```
14. Count Vowels
Write a Python program that counts the number of vowels in
a given string.
```python
string = input("Enter a string: ")
vowels = 'aeiouAEIOU'
count = 0
for char in string:
if char in vowels:
count += 1
print("Number of vowels:", count)
```
15. Count Words in a String
Write a Python program that counts the number of words in
a given string.
```python
string = input("Enter a string: ")
words = len(string.split())
print("Number of words:", words)
```
16. Check Leap Year
Create a Python script to determine whether a provided
year is a leap year or not.
```python
year = int(input("Enter a year: "))
If the condition (year % 4 == 0 and year % 100 != 0) or
(year % 400 == 0) is satisfied, then output "Leap Year".
else:
print("Not Leap Year")
```
17. Generate Multiplication Table
Write a Python program that generates the multiplication
table for a given number.
```python
num = int(input("Enter a number: "))
for i in range(1, 11):
Output the multiplication of `num` and `i` as `num`
times `i`.
```
18. Check Armstrong Number
Create a Python script to determine whether a provided
number is an Armstrong number or not.
```python
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print("Armstrong Number")
else:
print("Not Armstrong Number")
```
19. Print Pattern
Write a Python program to print a specific pattern.
```python
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print()
```
20. Find Largest Among Three Numbers
Write a Python program that finds the largest among three
numbers.
```python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter
```
second number: "))
num3 = int(input("Enter third number: "))
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
print("Largest number:", largest)
```
21. Check Positive, Negative, or Zero
Develop a Python script to determine whether a provided
number is positive, negative, or zero.
```python
num = float(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
```
22. Find Sum of Natural Numbers
Write a Python program to find the sum of natural numbers
up to a given number.
```python
num = int(input("Enter a number: "))
sum = 0
for i in range(1, num + 1):
sum += i
print("Sum of natural numbers:", sum)
```
23. Check Perfect Number
Create a Python script to determine whether a provided
number is a perfect number or not.
```python
num = int(input("Enter a number: "))
sum = 0
for i in range(1, num):
if num % i == 0:
sum += i
if sum == num:
print("Perfect Number")
else:
print("Not Perfect Number")
```
24. Check Strong Number
Develop a Python script to ascertain whether a provided
number is a strong number or not.
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += factorial(digit)
temp //= 10
if sum == num:
print("Strong Number")
else:
print("Not Strong Number")
```
25. Check Disarium Number
Create a Python script to verify whether a given number is a
Disarium number.
```python
num = int(input("Enter a number: "))
sum = 0
length = len(str(num))
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** length
length -= 1
temp //= 10
if sum == num:
print("Disarium Number")
else:
print("Not Disarium Number")
```
26. Check Harshad Number
Develop a Python script to determine whether a provided
number is a Harshad number.
```python
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit
temp //= 10
if num % sum == 0:
print("Harshad Number")
else:
print("Not Harshad Number")
```
27. Check Pronic Number
Create a Python script to determine whether a given
number is a Pronic number.
```python
num = int(input("Enter a number: "))
flag = False
for i in range(1, num):
If the product of `i` and `(i + 1)` equals `num`, then...
flag = True
break
if flag:
print("Pronic Number")
else:
print("Not Pronic Number")
```
28. Find GCD (Greatest Common Divisor)
Write a Python program to find the GCD of two numbers
using the Euclidean algorithm.
```python
def gcd(a, b):
while b:
Assign the values of `b` and `a % b` to `a` and `b`,
respectively.
return a
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("GCD:", gcd(num1, num2))
```
29. Find LCM (Least Common Multiple)
Write a Python program to find the LCM of two numbers
using the formula LCM = (a * b) / GCD(a, b).
```python
def lcm(a, b):
return (a * b) // gcd(a, b)
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("LCM:", lcm(num1, num2))
```
30. Find Factorial Using Recursion
Create a Python script to calculate the factorial of a
provided number using recursion.
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
print("Factorial:", factorial(num))
```
31. Find Power of a Number
Develop a Python script to determine the power of a number
using recursion.
```python
def power(base, exp):
if exp == 0:
return 1
else:
return base * power(base, exp-1)
base = int(input("Enter base: "))
exp = int(input("Enter exponent: "))
print("Result:", power(base, exp))
```
32. Find Sum of Digits Using Recursion
Create a Python script to calculate the sum of digits of a
provided number using recursion.
```python
def sum_of_digits(n):
if n == 0:
return 0
else:
return n % 10 + sum_of_digits(n // 10)
num = int(input("Enter a number: "))
print("Sum of digits:", sum_of_digits(num))
```
33. Find Fibonacci Series Using Recursion
Write a Python program to generate the Fibonacci series up
to a specified number of terms using recursion.
```python
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
num_terms = int(input("Enter number of terms: "))
print("Fibonacci Series:")
for i in range(num_terms):
print(fibonacci(i), end=' ')
```
34. Find Armstrong Numbers in an Interval
Write a Python program to find Armstrong numbers within a
given interval.
```python
start = int(input("Enter start of interval: "))
end = int(input("Enter end of interval: "))
for num in range(start, end + 1):
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
```
35. Find Perfect Numbers in an Interval
Write a Python program to find perfect numbers within a
given interval.
```python
start = int(input("Enter start of interval: "))
end = int(input("Enter end of interval: "))
for num in range(start, end + 1):
sum = 0
for i in range(1, num):
if num % i == 0:
sum += i
if sum == num:
print(num)
```
36. Find Strong Numbers in an Interval
Write a Python program to find strong numbers within a
given interval.
```python
def factorial(n):
if n ==
```python
0:
return 1
else:
return n * factorial(n-1)
def is_strong_number(num):
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += factorial(digit)
temp //= 10
return sum == num
start = int(input("Enter start of interval: "))
end = int(input("Enter end of interval: "))
print("Strong Numbers in the interval:")
for i in range(start, end + 1):
if is_strong_number(i):
print(i)
```
37. Find Disarium Numbers in an Interval
Write a Python program to find Disarium numbers within a
given interval.
```python
def is_disarium_number(num):
sum = 0
length = len(str(num))
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** length
length -= 1
temp //= 10
return sum == num
start = int(input("Enter start of interval: "))
end = int(input("Enter end of interval: "))
print("Disarium Numbers in the interval:")
for i in range(start, end + 1):
if is_disarium_number(i):
print(i)
```
38. Find Harshad Numbers in an Interval
Write a Python program to find Harshad numbers within a
given interval.
```python
def is_harshad_number(num):
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit
temp //= 10
return num % sum == 0
start = int(input("Enter start of interval: "))
end = int(input("Enter end of interval: "))
print("Harshad Numbers in the interval:")
for i in range(start, end + 1):
if is_harshad_number(i):
print(i)
```
39. Find Pronic Numbers in an Interval
Write a Python program to find Pronic numbers within a
given interval.
```python
def is_pronic_number(num):
for i in range(num):
If the product of `i` and its consecutive number equals
`num`, then...
return True
return False
start = int(input("Enter start of interval: "))
end = int(input("Enter end of interval: "))
print("Pronic Numbers in the interval:")
for i in range(start, end + 1):
if is_pronic_number(i):
print(i)
```
40. Check Palindrome Number
Write a Python program that checks if a given number is a
palindrome.
```python
num = int(input("Enter a number: "))
temp = num
reverse = 0
while temp > 0:
digit = temp % 10
Update the variable "reverse" by adding the digit and
multiplying the result by 10.
temp //= 10
if num == reverse:
print("Palindrome Number")
else:
print("Not Palindrome Number")
```
41. Print Pattern - Triangle
Write a Python program to print a triangle pattern.
```python
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
print(' ' * (rows - i) + '*' * i)
```
42. Print Pattern - Diamond
Write a Python program to print a diamond pattern.
```python
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
Output a combination of spaces and asterisks, where the
number of spaces is determined by `(rows - i)` and the
number of asterisks is determined by `(2 * i - 1)`.
for i in range(rows - 1, 0, -1):
Display a pattern consisting of spaces and asterisks, with
the number of spaces determined by `(rows - i)` and the
number of asterisks determined by `(2 * i - 1)`.
```
43. Check Armstrong Number (n-digits)
Write a Python program that checks if a given n-digit
number is an Armstrong number.
```python
num = int(input("Enter a number: "))
num_digits = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** num_digits
temp //= 10
if num == sum:
print("Armstrong Number")
else:
print("Not Armstrong Number")
```
44. Find Sum of Natural Numbers Using Recursion
Write a Python program to find the sum of natural numbers
up to a given number using recursion.
```python
def sum_of_natural_numbers(n):
if n == 1:
return 1
else:
return n + sum_of_natural_numbers(n - 1)
num = int(input("Enter a number: "))
print("Sum of natural numbers up to", num, ":",
sum_of_natural_numbers(num))
```
45. Find Factors of a Number
Create a Python script to determine the factors of a
provided number.
```python
num = int(input("Enter a number: "))
print("Factors of", num, ":", end=' ')
for i in range(1, num + 1):
if num % i == 0:
print(i, end=' ')
```
46. Check Perfect Number (n-digits)
Write a Python program that checks if a given n-digit
number is a perfect number.
```python
def is_perfect_number(num):
sum = 0
for i in range(1, num):
if num % i == 0:
sum += i
return sum == num
num = int(input("Enter a number: "))
if is_perfect_number(num):
print("Perfect Number")
else:
print("Not Perfect Number")
```
47. Check Prime Number (n-digits)
Write a Python program that checks if a given n-digit
number is prime or not.
```python
def is_prime_number(num):
if num > 1:
for i in range(2, num):
if num % i == 0:
return False
return True
return False
num = int(input("Enter a number: "))
if is_prime_number(num):
print("Prime Number")
else:
print("Not Prime Number")
```
48. Find Prime Factors of a Number
Develop a Python script to identify the prime factors of a
provided number.
```python
def prime_factors(num):
factors = []
while num % 2 == 0:
factors.append(2)
num //= 2
for i in range(3, int(num ** 0.5) + 1, 2):
while num % i == 0:
factors.append(i)
num //= i
if num > 2:
factors.append(num)
return factors
num = int(input("Enter a number: "))
print("Prime factors of", num, ":", prime_factors(num))
```
49. Find Reverse of a Number
Write a Python program to find the reverse of a given
number.
```python
num = int(input("Enter a number: "))
reverse = 0
while num > 0:
digit = num % 10
Assign the product of "reverse" and 10 plus "digit" to
"reverse".
num //= 10
print("Reverse:", reverse)
```
50. Check Disjoint Sets
Write a Python program to check if two given sets are
disjoint or not.
```python
set1 = {1, 2, 3}
set2 = {4, 5, 6}
if len(set1.intersection(set2)) == 0:
print("Disjoint sets")
else:
print("Not disjoint sets")
```
51. Check Sublist
Write a Python program to check if a given list is a subset of
another list.
```python
list1 = [1, 2, 3, 4, 5]
list2 = [2, 3]
if set(list2).issubset(set(list1)):
print("List2 is a subset of List1")
else:
print("List2 is not a subset of List1")
```
52. Check Superlist
Write a Python program to check if a given list is a superset
of another list.
```python
list1 = [1, 2, 3, 4, 5]
list2 = [2, 3]
if set(list1).issuperset(set(list2)):
print("List1 is a superset of List2")
else:
print("List1 is not a superset of List2")
```
53. Remove Duplicates from a List
Develop a Python script to eliminate duplicate elements
from a list.
```python
list1 = [1, 2, 3, 2, 4, 5, 1]
unique_list = list(set(list1))
print("List with duplicates removed:", unique_list)
```
54. Merge Two Dictionaries
Create a Python script to combine two dictionaries.
```python
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = {**dict1, **dict2}
print("Merged dictionary:", merged_dict)
```
55. Check Anagram
Write a Python program to check if two given strings are
anagrams or not.
```python
str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
if sorted(str1) == sorted(str2):
print("Anagrams")
else:
print("Not Anagrams")
```
56. Count Characters in a String
Write a Python program to count the occurrences of each
character in a given string.
```python
string = input("Enter a string: ")
char_count = {}
for char in string:
char_count[char] = char_count.get(char, 0) + 1
print("Character count:", char_count)
```
57. Reverse Words in a String
Write a Python program to reverse the order of words in a
given string.
```python
string = input("Enter a string: ")
reversed_string = ' '.join(reversed(string.split()))
print("Reversed words:", reversed_string)
```
58. Find Longest Word in a String
Write a Python program to find the longest word in a given
string.
```python
string = input("Enter a string: ")
longest_word = max(string.split(), key=len)
print("Longest word:", longest_word)
```
59. Convert List to String
Write a Python program to convert a list of characters into a
string.
```python
char_list = ['H', 'e', 'l', 'l', 'o']
string = ''.join(char_list)
print("String:", string)
```
60. Shuffle a List
Write a Python program to shuffle a given list.
```python
import random
list1 = [1, 2, 3, 4, 5]
random.shuffle(list1)
print("Shuffled list:", list1)
```
61. Find Missing Number
Write a Python program to find the missing number in a
given list of numbers from 1 to n.
```python
def find_missing_number(nums):
n = len(nums) + 1
The expected sum is calculated as `n` multiplied by `n +
1`, then divided by 2.
actual_sum = sum(nums)
return expected_sum - actual_sum
nums = [1, 2, 4, 5, 6]
print("Missing number:", find_missing_number(nums))
```
62. Find Duplicate Numbers
Write a Python program to find the duplicate numbers in a
given list.
```python
def find_duplicate_numbers(nums):
duplicates = set()
seen = set()
for num in nums:
if num in seen:
duplicates.add(num)
else:
seen.add(num)
return list(duplicates)
nums = [1, 2, 3, 2, 4, 5, 4]
print("Duplicate numbers:", find_duplicate_numbers(nums))
```
63. Merge Two Sorted Lists
Write a Python program to merge two sorted lists into a
single sorted list.
```python
def merge_sorted_lists(list1, list2):
merged_list = []
i=j=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
merged_list.extend(list1[i:])
merged_list.extend(list2[j:])
return merged_list
list1 = [1, 3, 5, 7]
list2 = [2, 4, 6, 8]
print("Merged sorted list:", merge_sorted_lists(list1, list2))
```
64. Find Common Elements
Write a Python program to find the common elements
between two lists.
```python
def find_common_elements(list1, list2):
return list(set(list1) & set(list2))
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
print("Common elements:", find_common_elements(list1,
list2))
```
65. Remove All Occurrences
Write a Python program to remove all occurrences of a
specified element from a given list.
```python
def remove_all_occurrences(nums, target):
return [num for num in nums if num != target]
nums = [1, 2, 3, 2, 4, 5, 2]
target = 2
print("List after removal:", remove_all_occurrences(nums,
target))
```
66. Remove Duplicate Characters
Write a Python program to remove all duplicate characters
from a string.
```python
def remove_duplicate_characters(string):
unique_chars = []
for char in string:
if char not in unique_chars:
unique_chars.append(char)
return ''.join(unique_chars)
string = "hello"
print("String after removal of duplicates:",
remove_duplicate_characters(string))
```
67. Check Pangram
Write a Python program to check if a given string is a
pangram or not.
```python
import string
def is_pangram(sentence):
alphabet = set(string.ascii_lowercase)
return set(sentence.lower()) >= alphabet
sentence = "The quick brown fox jumps over the lazy dog"
if is_pangram(sentence):
print("Pangram")
```python
else:
print("Not a Pangram")
```
68. Count Words Frequency
Write a Python program to count the frequency of words in a
given sentence.
```python
def count_word_frequency(sentence):
word_freq = {}
words = sentence.split()
for word in words:
word_freq[word] = word_freq.get(word, 0) + 1
return word_freq
sentence = "This is a test sentence to test word frequency"
print("Word frequency:", count_word_frequency(sentence))
```
69. Find Maximum Occurring Character
Develop a Python script to determine the character that
occurs most frequently in a provided string.
```python
def max_occuring_character(string):
char_count = {}
for char in string:
char_count[char] = char_count.get(char, 0) + 1
max_count = max(char_count.values())
max_chars = [char for char, count in char_count.items() if
count == max_count]
return max_chars
string = "hello world"
print("Maximum occurring character(s):",
max_occuring_character(string))
```
70. Find Intersection of Two Arrays
Write a Python program to find the intersection of two
arrays.
```python
def find_intersection(arr1, arr2):
return list(set(arr1) & set(arr2))
arr1 = [1, 2, 3, 4, 5]
arr2 = [4, 5, 6, 7, 8]
print("Intersection:", find_intersection(arr1, arr2))
```
71. Find Union of Two Arrays
Create a Python script to compute the union of two arrays.
```python
def find_union(arr1, arr2):
return list(set(arr1) | set(arr2))
arr1 = [1, 2, 3, 4, 5]
arr2 = [4, 5, 6, 7, 8]
print("Union:", find_union(arr1, arr2))
```
72. Find Symmetric Difference of Two Arrays
Write a Python program to find the symmetric difference of
two arrays.
```python
def find_symmetric_difference(arr1, arr2):
return list(set(arr1) ^ set(arr2))
arr1 = [1, 2, 3, 4, 5]
arr2 = [4, 5, 6, 7, 8]
print("Symmetric Difference:",
find_symmetric_difference(arr1, arr2))
```
73. Find Missing Element in a Sorted Array
Develop a Python script to identify the absent element in a
sorted array of consecutive numbers.
```python
def find_missing_element(arr):
n = len(arr)
total = (n + 1) * (n + 2) // 2
arr_sum = sum(arr)
return total - arr_sum
arr = [1, 2, 3, 5, 6, 7, 8,10]
print("Missing element:", find_missing_element(arr))
```
74. Find Single Element in a Sorted Array
Write a Python program to find the single element in a
sorted array where every element appears twice except for
one.
```python
def find_single_element(arr):
Define variables "low" and "high" with initial values of 0
and the length of the array minus 1, respectively.
while low < high:
The midpoint equals the low value plus half the
difference between the high and low values.
if mid % 2 == 0:
if arr[mid] == arr[mid + 1]:
low = mid + 2
else:
high = mid
else:
if arr[mid] == arr[mid - 1]:
low = mid + 1
else:
high = mid
return arr[low]
arr = [1, 1, 2, 2, 3, 3, 4, 4, 5]
print("Single element:", find_single_element(arr))
```
75. Rotate Array Left
Write a Python program to rotate an array to the left by a
given number of steps.
```python
def rotate_array_left(arr, steps):
n = len(arr)
steps = steps % n
return arr[steps:] + arr[:steps]
arr = [1, 2, 3, 4, 5]
steps = 2
print("Rotated array:", rotate_array_left(arr, steps))
```
76. Rotate Array Right
Write a Python program to rotate an array to the right by a
given number of steps.
```python
def rotate_array_right(arr, steps):
n = len(arr)
steps = steps % n
return arr[-steps:] + arr[:-steps]
arr = [1, 2, 3, 4, 5]
steps = 2
print("Rotated array:", rotate_array_right(arr, steps))
```
77. Find Maximum Sum Subarray
Write a Python program to find the contiguous subarray with
the largest sum from a given array.
```python
def max_subarray_sum(arr):
max_sum = float('-inf')
current_sum = 0
for num in arr:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4,5]
print("Maximum sum of subarray:", max_subarray_sum(arr))
```
78. Find Missing Number in Arithmetic Progression
Write a Python program to find the missing number in an
arithmetic progression of numbers.
```python
def find_missing_in_ap(arr):
n = len(arr) + 1
total = n * (arr[0] + arr[-1]) // 2
actual_sum = sum(arr)
return total - actual_sum
arr = [1, 3, 5, 7, 9, 13]
print("Missing number in AP:", find_missing_in_ap(arr))
```
79. Find Peak Element
Write a Python program to find a peak element in an array. A
peak element is an element that is larger than the elements
adjacent to it.
```python
def find_peak_element(arr):
Set the low value to 0 and the high value to the length of
the array minus 1.
while low < high:
Calculate the midpoint by adding half of the difference
between the high and low values to the low value.
if arr[mid] > arr[mid + 1]:
high = mid
else:
low = mid + 1
return arr[low]
arr = [1, 3, 20, 4, 1, 0]
print("Peak element:", find_peak_element(arr))
```
80. Find Majority Element
Write a Python program to find the majority element in an
array. The majority element is the element that occurs in
more than half of the total occurrences in a set.
```python
def find_majority_element(arr):
count = 0
candidate = None
for num in arr:
if count == 0:
candidate = num
count += (1 if num == candidate else -1)
return candidate
arr = [3, 3, 4, 2, 4, 4, 2, 4, 4,2,2]
print("Majority element:", find_majority_element(arr))
```
These Python coding challenges cover a wide range of
topics and are suitable for beginners to improve their
programming skills. Each challenge provides an opportunity
to practice problem-solving and algorithmic thinking,
essential skills for any programmer.
Appendix