Python Practice Exercises – Part 3: Fibonacci, Domino, and Random Code Generator

Python Practice Exercises – Part 3

In this week’s exercises, we explore pattern generation, recursion-like sequences, mathematical simulations, and random code generation.
These tasks will help you apply logic, iteration, and randomization — essential skills for real-world Python problem-solving.

Try solving each challenge on your own first, then scroll down for the solutions!

1- Domino Stones

Write a program that prints all possible domino stones in the following format:

(0|0)(0|1)(0|2)(0|3)(0|4)(0|5)(0|6)
     (1|1)(1|2)(1|3)(1|4)(1|5)(1|6)
          (2|2)(2|3)(2|4)(2|5)(2|6)
               (3|3)(3|4)(3|5)(3|6)
                    (4|4)(4|5)(4|6)
                         (5|5)(5|6)
                              (6|6)

2- Fibonacci Sequence

Write a program that prints the first 10 numbers of the Fibonacci sequence:

0 1 1 2 3 5 8 13 21 34

The first two numbers can be hardcoded (print(0, 1)), but all subsequent ones must be generated inside a loop.

Bonus: Print all Fibonacci numbers less than 500.

3- The Frog Problem

A very peculiar frog wants to cross a 2.5-meter-wide road.
His first jump covers 1 meter, but with each subsequent jump, he can only jump half the previous distance because he gets tired.

The total distance he covers can be represented as the sum:
1 + 0.5 + 0.25 + 0.125 + ...

Write a program to determine if the frog ever reaches the other side of the road.

4- Web Code Generator

For a publishing project, a web code is required for accessing online content.
Each code must:

  • be 8 characters long,
  • contain only digits (2–9) and lowercase letters (except l, 1, and 0),
  • and generate five random codes.

Example of valid characters:

2 3 4 5 6 7 8 9  
a b c d e f g h i j k m n o p q r s t u v w x y z

Solutions

Domino Stones

for i in range(7):
    print()
    print(" " * (i * 5), end="")
    for j in range(i, 7):
        print(f'({i}|{j})', end='')

Fibonacci Sequence

f = [0, 1]

for i in range(1, 20):
    next_num = f[i] + f[i - 1]
    if next_num <= 500:
        f.append(next_num)
    else:
        break

print(*f)

Alternative version using a while loop:

f = [0, 1]
print(f[0], f[1], end=' ')

while True:
    next_num = f[-1] + f[-2]
    if next_num >= 500:
        break
    f.append(next_num)
    print(next_num, end=' ')

The Frog Problem

distance = 1
jumps = [1]

while len(jumps) < 50:
    distance /= 2
    jumps.append(distance)

print("Number of jumps:", len(jumps))
print("Total distance:", sum(jumps))

if sum(jumps) >= 2.5:
    print("The frog made it across the road!")
else:
    print("The frog did not reach the other side.")
    print("The jumps approach 2 meters but never exceed it.")

Web Code Generator

import string, random, time

digits = list(range(2, 10))
letters = list(string.ascii_lowercase)
letters.remove('l')

pool = letters + digits

for _ in range(5):
    code = ""
    for _ in range(8):
        char = random.choice(pool)
        code += str(char)
        print(char, end="")
        time.sleep(0.3)
    print()