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

 Live Demo

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']

Updated on: 02-Sep-2020

608 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements