Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
3-6-9 in Python
The 3-6-9 game is a popular counting game where players replace certain numbers with a special word. In this Python implementation, we create a list from 1 to n, but replace numbers that are multiples of 3 or contain the digits 3, 6, or 9 with the string "clap".
Problem Statement
Given a number n, construct a list with each number from 1 to n as strings, except when the number is a multiple of 3 or contains the digits 3, 6, or 9 − replace these with "clap".
For example, if the input is 20, the output will be: ['1', '2', 'clap', '4', '5', 'clap', '7', '8', 'clap', '10', '11', 'clap', 'clap', '14', 'clap', 'clap', '17', 'clap', 'clap', '20']
Algorithm
To solve this problem, we follow these steps −
Create a list of numbers from 1 to n as strings
-
For each number in the list:
If the number is divisible by 3, replace with "clap"
If the number contains digit '3', replace with "clap"
If the number contains digit '6', replace with "clap"
If the number contains digit '9', replace with "clap"
Return the modified list
Implementation
class Solution:
def solve(self, n):
replacement = "clap"
numbers = [str(i) for i in range(1, n + 1)]
for i in range(len(numbers)):
if int(numbers[i]) % 3 == 0:
numbers[i] = replacement
elif '3' in numbers[i]:
numbers[i] = replacement
elif '6' in numbers[i]:
numbers[i] = replacement
elif '9' in numbers[i]:
numbers[i] = replacement
return numbers
# Test the solution
solution = Solution()
result = solution.solve(20)
print(result)
['1', '2', 'clap', '4', '5', 'clap', '7', '8', 'clap', '10', '11', 'clap', 'clap', '14', 'clap', 'clap', '17', 'clap', 'clap', '20']
Optimized Version
We can simplify the condition checking by combining digit checks ?
def three_six_nine(n):
result = []
for i in range(1, n + 1):
num_str = str(i)
# Check if multiple of 3 or contains 3, 6, or 9
if i % 3 == 0 or any(digit in num_str for digit in ['3', '6', '9']):
result.append("clap")
else:
result.append(num_str)
return result
# Test with different values
print("n = 15:", three_six_nine(15))
print("n = 10:", three_six_nine(10))
n = 15: ['1', '2', 'clap', '4', '5', 'clap', '7', '8', 'clap', '10', '11', 'clap', 'clap', '14', 'clap'] n = 10: ['1', '2', 'clap', '4', '5', 'clap', '7', '8', 'clap', '10']
How It Works
Let's trace through some examples to understand the logic ?
Number 3: 3 % 3 == 0 (multiple of 3) ? "clap"
Number 6: 6 % 3 == 0 (multiple of 3) ? "clap"
Number 13: Contains digit '3' ? "clap"
Number 16: Contains digit '6' ? "clap"
Number 19: Contains digit '9' ? "clap"
Number 11: Not multiple of 3, no forbidden digits ? "11"
Conclusion
The 3-6-9 game implementation checks each number for two conditions: being a multiple of 3 or containing the digits 3, 6, or 9. The optimized version uses Python's any() function for cleaner code and better readability.
