Challenge 1: FizzBuzz

FizzBuzz is a classic coding challenge that serves as an excellent warm-up exercise. The task is simple: write a program that prints the numbers from 1 to n, but for multiples of three, print “Fizz” instead of the number, and for multiples of five, print “Buzz”. For numbers which are multiples of both three and five, print “FizzBuzz”.

def fizzbuzz(n):
    for i in range(1, n+1):
        if i % 3 == 0 and i % 5 == 0:
            print("FizzBuzz")
        elif i % 3 == 0:
            print("Fizz")
        elif i % 5 == 0:
            print("Buzz")
        else:
            print(i)

fizzbuzz(15)

Challenge 2: Palindrome Checker

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. In this challenge, your task is to write a function that checks whether a given string is a palindrome.

def is_palindrome(s):
    s = s.lower()  # Convert to lowercase for case-insensitivity
    return s == s[::-1]

print(is_palindrome("radar"))  # True
print(is_palindrome("hello"))  # False

Challenge 3: Fibonacci Sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. Your challenge is to write a function that generates the Fibonacci sequence up to a specified limit.

def fibonacci(n):
    fib_sequence = [0, 1]
    while fib_sequence[-1] + fib_sequence[-2] <= n:
        fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
    return fib_sequence

print(fibonacci(100))  # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Challenge 4: Sum of Multiples

Write a function that computes the sum of all the multiples of 3 or 5 below a given number n. For example, if n is 10, the sum of multiples of 3 or 5 below 10 is 3 + 5 + 6 + 9 = 23.

def sum_of_multiples(n):
    total = 0
    for i in range(n):
        if i % 3 == 0 or i % 5 == 0:
            total += i
    return total

print(sum_of_multiples(10))  # 23

These challenges are not only great for honing your programming skills but also for building problem-solving intuition. As you tackle each challenge, take the time to understand the underlying concepts and explore different approaches to solving them. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *