Practice problems for coding

Practice problems for coding

Anything that has not been tried before seems a nightmare to start, may be that can be said for the coding as well. But, one has to start at some point and possibly this can be a good time to do so; here we will be discussing about two of the very standard yet simple problem. At the same time these problems will help you understand the much needed coding basics about Looping and Conditional statements. If one understands the logic then which language one is using won’t affect much. And Python can be a very good option as being a high level programming language, it eases the labour of coding thanks to it’s simple syntax, so the only task is to generate the logic.

Loops in Python – Python like any other language has two loops, i.e. while and for.

  • while loops- while loops are used when the statements needs to be executed as long as it is True.
while test expression  :
        expression or statement
  • for loops- for loops are used to iterates over items or variable var of any seq sequence type (number, list or string), in the sequential order of their appearance. Python for loop has one superiority that it can also work with list and strings as well.
for var in seq :
    expression or statement

Conditional statements in Python – Conditional statements are those statements which performs computations or execute the set of commands depending on whether the test expression holds true or false. In Python, there are three conditionals statements –

  • if statement – the set of commands is executed only when the test expression holds True vale.
if test expression:
   statements  #body of if 
  • if-else – the set of commands in the if body executes when the test expression holds True value, otherwise it will executes the commands in the else body.
if test expression:
   statements  #body of if 
else:
   statements  #body of else 
  • elif – elif(else-if) allows to test multiple expressions, the set of commands executes as soon as the conditions holds True .
if test_expression1:
   statements
elif test_expression2:
   statements
elif test_expression3:
   statements
else:
   statements

You can also refer this to know more about loops and conditional statements in Python.

Let’s see the two problems that we have talked about in the starting.

First problem is print Fibonacci sequence , a series of numbers such that each term in the series is the sum of preceding two numbers.

$ F_n = F_{n-1} + F_{n-2} $

For example – $ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 . . . $

$ 1, 5, 6, 11, 17, 28, 45, 73, 118 . . . $

There can be two cases –

  • Write the code that can print the series with the first two number that is specified by you.
  • Write the code that can print the series with any first two number that is fed to it.

Second problem is print arrays of asterisk, following 16 patterns can be tried with increasing levels of difficulties, these patterns will require loops like for and while and conditional statements like if, if-else, elif etc.

Patterns $7$ to $9$ and $12$ to $14$ can either be made using single loop or combinations of previous patterns, but the former one is preferred as it may require knowledge of nesting.

P.S. – Following commands can still print the patterns, but prefer not to do that way.

print("*\\n* *\\n* * *\\n* * * *\\n* * * * *")
*
* *
* * *
* * * *
* * * * *
print("        *\\n      * *\\n    * * *\\n  * * * *\\n* * * * *")
        *
      * *
    * * *
  * * * *
* * * * *
Categories: Programming

Leave a Reply