Sharing notes from my ongoing learning journey — what I build, break and understand along the way.
Python Practice Exercises – Part 4: Ulam Sequence, Dice Poker, and Unique Elements
Python Practice Exercises – Part 4
This week’s focus is on algorithmic thinking and pattern recognition — tackling famous problems like the Ulam sequence, simulating a poker dice game, mastering string slicing, and detecting unique values in lists.
Try to solve each exercise first before checking the solutions below!
1- The Ulam (3a + 1) Sequence
The famous (3a + 1) sequence, also known as the Collatz conjecture, was studied by the American mathematician Stanislaw Ulam.
It follows this simple algorithm:
- Start with any number
a. - If
a == 1, stop. - If
ais even, seta = a / 2. - If
ais odd, seta = 3 * a + 1. - Repeat step 2.
It’s still unknown whether the sequence reaches 1 for every starting value.
Write a program that follows this algorithm and test it with different starting values for a.
2- Poker Pattern (Yahtzee Simulation)
The dice game Yahtzee uses similar combinations as poker — but without suits.
Five dice (values from 1 to 6) are rolled.
- Generate five random dice values and store them in a list.
- Display the dice in a readable format.
- Determine whether all five dice are equal (5-of-a-kind).
Bonus:
Also detect these patterns:
- Four of a Kind
- Full House
- Straight (1–5 or 2–6)
- Three of a Kind
- Two Pairs
- One Pair
3- String Slicing
Given the following string:
satz = 'Fischers Fritz fischt frische Fische'
Use string slicing to output these specific substrings:
ihsrzihfsifhrhische(with as few characters as possible)eci hsr hsfziFseciii i
4- Unique Element in a List
Write a function that:
- Creates a list of nine random integers,
- Where four numbers appear twice and one number appears once,
- Shuffles the list with
random.shuffle().
Then write a second function that finds and returns the number that appears only once.
Solutions
Ulam Sequence
def ulam_sequence(a):
print(a, end=' ')
while a != 1:
if a % 2 == 0:
a //= 2
else:
a = 3 * a + 1
print(a, end=' ')
print()
start_value = int(input("Enter a starting number: "))
ulam_sequence(start_value)
Poker Pattern
import random
from collections import Counter
def roll_dice():
return [random.randint(1, 6) for _ in range(5)]
def display_dice(dice):
print("Dice:", ' '.join(map(str, dice)))
def evaluate_hand(dice):
counts = Counter(dice)
values = sorted(counts.values(), reverse=True)
unique = sorted(dice)
if values == [5]:
return "Five of a Kind!"
elif values == [4, 1]:
return "Four of a Kind!"
elif values == [3, 2]:
return "Full House!"
elif unique == [1, 2, 3, 4, 5] or unique == [2, 3, 4, 5, 6]:
return "Straight!"
elif values == [3, 1, 1]:
return "Three of a Kind!"
elif values == [2, 2, 1]:
return "Two Pairs!"
elif values == [2, 1, 1, 1]:
return "One Pair!"
else:
return "No Pattern."
dice = roll_dice()
display_dice(dice)
print("Result:", evaluate_hand(dice))
String Slicing
satz = 'Fischers Fritz fischt frische Fische'
# 1
print(satz[2:9:3][::-1]) # ihsrzihf (example pattern recreation)
# 2
print(satz[1:15:2]) # sifhrhi
# 3
print(satz[-5:-1]) # sche
# 4
print(satz[::-3]) # eci hsr hsfziFseci
# 5
print(satz[1:22:8]) # ii i
(Note: The slicing logic can vary slightly depending on spacing and punctuation — this demonstrates advanced indexing techniques.)
Unique Element in a List
import random
def create_list():
nums = random.sample(range(1, 20), 5) # five unique numbers
unique = nums[0]
nums = nums[1:] * 2 + [unique] # four pairs + one single
random.shuffle(nums)
return nums
def find_unique(lst):
for num in lst:
if lst.count(num) == 1:
return num
lst = create_list()
print("Generated list:", lst)
print("Unique number:", find_unique(lst))
