Solutions To 4 Popular Coding Challenges For Python Beginners.

If you are interested in getting a tech job or at least a freelance tech job, you’d have to at some point take some of the popular coding challenges before you will be granted an oral interview.

In this tutorial, we will be discussing 4 popular coding challenges for Python beginners.

If you are a member on this website and you have gone through our members exclusive Python beginners tutorial, then you must have come across these 4 popular coding challenges we are about to discuss.

So if you were not able to solve them on your own, here is a chance to see what you did wrong or right. These challenges focus on loops, if statements, functions, and list comprehension:

Without wasting your time, we will just delve into the challenges and their respective solutions:

Challenge 1: Find Even Numbers

QUESTION:

Write a Python function that takes a list of numbers as input and returns a new list containing only the even numbers from the original list.

ANSWER:

def find_even_numbers(numbers):
    even_numbers = []
    for number in numbers:
        if number % 2 == 0:
            even_numbers.append(number)
    return even_numbers

Am not sure anyone that has gone through the members exclusive beginner Python tutorial on this website would find it hard to comprehend the answer above.

However if you still find anything confusing, you can always leave your question on the comment section. Or better still, arrange for a special consultation.

Challenge 2: Palindrome Checker

QUESTION:

Write a Python function that checks if a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.

ANSWER:

def is_palindrome(string):
    #remove all punctuations
    clean_string = ''.join(s.lower() for s in string if s.isalnum())
    #check for palindrome
    return clean_string == clean_string[::-1]

#Now let's call and test the function
is_palindrome("Was it a car or a cat I saw?")

First we remove all punctuation in the string by filtering only alphanumeric characters out using .isalnum method.

Then we compare the cleaned string with it’s reverse, the result gives us a boolean(Telling us if the string is a palindrome or not.

Again the solution is easy pizzy! It returns a boolean output. Remember you should be testing these codes on your end to confirm its usability.

Let me give you a few examples of palindromes: Level, Radar, Noon, Deified, Refer, Taco cat, “A man, a plan, a canal, Panama!”, “Madam, in Eden I’m Adam.”, “Was it a car or a cat I saw?”, “Never odd or even.”

You can google more palindromes and try them out on your newly found is_palindrome function.

Challenge 3: FizzBuzz

QUESTION:

Write a Python program that prints the numbers from 1 to 100. But for multiples of 3, print “Fizz” instead of the number, and for the multiples of 5, print “Buzz.” For numbers that are multiples of both 3 and 5, print “FizzBuzz.”

ANSWER:

for i in range(1, 101):
    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)

The FizzBuzz challenge is of the most popular coding challenges you will ever encounter in programming beginners coding challenges. Again try the code on your end and enjoy the fun.

Challenge 4: List Comprehension

QUESTION:

Given a list of numbers, write a Python program that creates a new list containing only the squares of the numbers that are even.

ANSWER:

def square_even_numbers(numbers):
    squared_evens = [num ** 2 for num in numbers if num % 2 == 0]
    return squared_evens

Sure you will definitely understand the code above if you have read the members exclusive tutorial I referenced earlier. As you can see, list comprehension makes your job easier. But be careful, there are times you are not supposed to use it.

I hope you enjoyed those popular coding challenges discussed here. Expect more coding challenges as you progress in your learning in TechSwitch.

Remember! these challenges brings out the best in you as a programmer, so do not fear or detest them.

Depending on your level of membership, you will have access to more advanced challenges like data structure and algorithm challenges.

Make sure you renew and upgrade your membership when necessary to get the best out of TechSwitch. Remember, there is always something to learn here.

Happy coding techie!

Leave a Reply