India Map with python Pattern program

A guide for generating a map of India with an encoded string in a pattern of the exclamation mark (!).

What is a pattern?

Usually, a pattern means a repeated form or design especially one that is used to decorate something. In software development, a pattern (or design pattern) is a written document that describes a general solution to a design problem that recurs repeatedly in many projects.

What is a pattern program?

Pattern programs are nothing but patterns consisting of numbers, alphabets, or symbols in a particular form. We can solve these kinds of pattern programs using for loop conditions.

Map of india with python pattern program

How the code works?

Basically, the string is a run-length encoding of the map of India. Alternating characters in the string stores how many times to draw space, and how many times to draw an exclamation mark consecutively.

Outer for loop?

The loop goes over the characters in the string. Each iteration increases the value of b by one and assigns the next character in the string to a.

Inner for loop?

This loop draws individual characters and a newline whenever it reaches the end of the line.
Let's consider the following statement:
c = c // 9

As 'Z' represents number 90 in ASCII, 90/9 will give us 10 which is a newline character. Decimal 33 is ASCII for '!'. Toggling the low-order bit of 33 gives you 32, which is ASCII for space. This causes '!' to be printed if b is odd, and a blank space to be printed if b is even.

Below is the code for the Indian map that is written in python-

a = 10
b = 0
c = 10

s = ("TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs"
     " UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPe"
     "HBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFA"
     "gcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm S"
     "On TNn ULo0ULo#ULo-WHq!WFs XDt!")

a = ord(s[b])
  
while a != 0
    if b < 170
        a = ord(s[b]) 
        b += 1
          
        while a > 64
            a -= 1
            c += 1
              
            if c == 90
                c = c // 9
                print(end = chr(c)) 
            else
                print(chr(33 ^ (b & 0X01)), end = '') 
    else
        break
  

Output:

map of india drawn with pattern of exclaimation marks in python

How to print colored text with python?

This is a common question and asked everywhere on the internet many times on sites like Github, StackOverflow, etc.

It is possible to print colored text in the terminal with the help of python. We need a python module called termcolor for printing colored text in the terminal, use the following command to install the module:

pip install termcolor

termcolor is a python module for ANSII color formatting for output in the terminal.

import sys
from termcolor import colored, cprint
  
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')
  
print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')
  
for i in range(10):
    cprint(i, 'magenta', end=' ')

cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)

Output:

printing colored text with python in the terminal
There are several ways to print colored text with python. The most common ways to do that are using the built-in modules or using ANSII escape characters.

Using built-in modules

1. ‘Colorama’ module: Cross-platform printing of colored text can then be done using Colorama’s constant shorthand for ANSI escape sequences.

2. ‘termcolor’ module: termcolor is a python module for ANSII Color formatting for output in the terminal.

Using ANSII escape codes

The most common way to print colored text is by printing ANSI escape sequences directly. This can be delivered in different formats such as:

1. Build Functions to call: We can build functions to call particular color-named functions to execute the relevant ANSI Escape Sequence.

2. Build a class of colors: Create a class to allot background and foreground colors and call them.

3. Iterating functions: We can design iterating & self-generating ANSI Escape sequence, functions.

You can watch this video on youtube for better understanding:


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Hello! Myself Tejas Mahajan. I am an Android developer, Programmer, UI/UX designer, Student and Navodayan.