DEV Community

Cover image for 5 coding challenges for beginners (with solutions in Python)
rachel
rachel

Posted on • Edited on

5 coding challenges for beginners (with solutions in Python)

Here are some fun beginner-friendly coding problems that I came across recently. I hope that this article can help you improve your understanding and motivate you to learn.

1. Number of integers in a string

Given a string, return the number of integers present. For example, the string "My favourite number is 17 and 30" has 2 integers.

Tips:

  • Break the string down into individual words.
  • Use Python's built in functions to check for the type of each word.

Solution:

def num_of_integers(str):

    count = 0 # initialise count

    str = str.split() # split the string into list of words

    for i in range(len(str)):
        if(str[i].isdigit()): #check if the word is an integer
            count += 1 # increment count

    return count

print(num_of_integers("My favourite number is 17 and 30"))
Enter fullscreen mode Exit fullscreen mode

2. Palindrome checker
Write a function that checks is a given word is a palindrome(a word that is read the same forward and backward, for example, "bob", "abba", "radar"), assuming that input will be in lowercase letters.

Solution:

# Method 1: Iterative solution
def palindrome(str):
    for i in range(int(len(str) / 2)):
        if (str[i] == str[len(str)-1]):
            return True
    return False

# Method 2: Using Python's built in function
def palindrome(str):
    return str == str[::-1] # compares the string and its reverse
Enter fullscreen mode Exit fullscreen mode

3. Zip two lists

Write a custom zip function that replicates Python's built in zip() function.

Python Zip

Solution:

def custom_zip(list1, list2):
    result = []
    for i in range(len(list1)):
        result.append((list1[i], list2[i]))
    return result

print(custom_zip([0, 1, 2, 3],[5, 6, 7, 8]))

Enter fullscreen mode Exit fullscreen mode

4. Print a staircase
Given a positive integer n, print a staircase of height and width of n using "#" and spaces. An example is shown below:

n = 4
#
##
###
####
Enter fullscreen mode Exit fullscreen mode

Tips:

  • Use for loops
  • Think of how many "#" and white spaces you want in each row

Solution:

def staircase(n):
    for i in range(n):
        for x in range(i+1):
            print("#", end = "") 
        for y in range(n-i-1):
            print(" ", end = "")
        print()

staircase(4)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • For the first row, you want 1 "#" and (n - 1) white spaces. For the second row, you want 2 "#" and (n - 2) white spaces and so on...
  • The nested for loop is used for displaying the "#" and white spaces for each line.
  • Once each line is completed, it will go to the outer loop then print the next line.

5. Recursive Pascal's triangle

Given a row and column number, write a recursive function that returns the corresponding value of the specified row and column in the Pascal's triangle as shown below:

Pascal's triangle

Tips:

  • The values are formed by adding together the two numbers just above to the left and right of each position in the triangle.
  • There are a few base cases to consider.
  • Number of columns should not be larger than number of rows.

Solution:

def pascal_triangle(row, col):
    if (row >= col):
        if (col == 0 or row == 0 or row == col):
            return 1
        else:
            return pascal_triangle(row-1, col-1) + pascal_triangle(row-1, col) 
    else:
        return 0

print(pascal_triangle(3,0))
print(pascal_triangle(3,1))
print(pascal_triangle(3,2))
print(pascal_triangle(3,3))
Enter fullscreen mode Exit fullscreen mode

Hope you had fun trying out these challenges! Feel free to share your solutions and any comments regarding the post.

Top comments (0)