
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
3-6-9 in Python
Suppose we have a number n, we have to construct a list with each number from 1 to n, except when it is multiple of 3 or has a 3, 6, or 9 in the number, it should be the string "no-fill".
So, if the input is like 20, then the output will be ['1', '2', 'clap', '4', '5', 'clap', '7', '8', 'clap', '10', '11', 'clap', 'clap', '14', 'clap', 'clap', '17', 'clap', 'clap', '20']
To solve this, we will follow these steps −
string := "no-fill"
ls:= make a list of numbers as string from 1 to n
for i in range 0 to size of ls - 1, do
if ls[i] is divisible by 3, then
ls[i]:= string
otherwise when '3' is present in ls[i], then
ls[i]:= string
otherwise when '6' is present in ls[i], then
ls[i]:= string
otherwise when '9' is present in ls[i], then
ls[i]:= string
return ls
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n): string = "no-fill" ls=[str(i) for i in range(1,n+1)] for i in range(len(ls)): if int(ls[i])%3==0: ls[i]=string elif '3' in ls[i]: ls[i]=string elif '6' in ls[i]: ls[i]=string elif '9' in ls[i]: ls[i]=string return ls ob = Solution() print(ob.solve(20))
Input
20
Output
['1', '2', 'clap', '4', '5', 'clap', '7', '8', 'clap', '10', '11', 'clap', 'clap', '14', 'clap', 'clap', '17', 'clap', 'clap', '20']
- Related Articles
- Solve:$(-9)\ +\ (+4)\ +\ (-6)\ +\ (+3)$
- Find $54\div 3\ of\ 6+9$.
- Find the mode of the following data: 3, 3, 7, 4, 5, 3, 5, 6, 8, 9, 5, 3, 5, 3, 6, 9, 7, 4.
- Find the mode of the following data: 3, 5, 7, 4, 5, 3, 5, 6, 8, 9, 5, 3, 5, 3, 6, 9, 7, 4.
- -5[6 ÷ 3 x (-2)] - [9 - (-5) x 2]
- The reduced form of $36 x^{2}-81 y^{2}$ isi$(6 x+9 y)(6 x-9 y) $ii$(6 x+9 y)(4 x-5) $iii.$(9 x+6 y)(9 x-6 y)$iv. $(9 y-6 x)(9 y+6 x) $
- Solve:$9\ +\ (-6)$
- If X = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9} and Y = {3 , 6 , 9 , 12 , 15 , 18} , find $(X - Y)$ and $(Y -X)$ and illustrate them in Venn diagram.
- Solve the following question$3 + 9 \times 6 - 22 ÷ 2 (100÷2)$
- Simplify the following.$\frac{-7}{ 7} + \frac{3}{ 9} + \frac{1}{ 6}$
- Solve:\( \frac{6-4 \sqrt{5}+3 \sqrt{5}-10}{9-6 \sqrt{5}+6 \sqrt{5}-4 \sqrt{25}} \)
- Find the value of x.(i) $3^2 \times (-4)^2 = (-12)^{2x}$(ii) $(\frac{9}{4})^{3} \times (\frac{8}{9})^{3}=2^{6 x}$
- Name the figure formed by the points $A (9,\ 0),\ B (9,\ 6),\ C (–9,\ 6)$ and $D(–9,\ 0)$.
- Fill in the blanks:1) The statement $(3+5)+6=3+(5+6)$ shows that the addition of whole numbers is____.2) ____ is the multiplicative identity of whole numbers.3) $6 \times(7+3)=(6 \times 7)+(6 \times 3)$. This statement showsthat multiplication of whole numbers is____ over addition.4) $(3 \times 5) \times 9=3 \times(5 \times 9)$. This statement shows that multiplication of whole number is____.
- Divide 180 in the ratio 6:9.
