Sunday, October 18, 2015

Stars Patterns

*
**
***
****
*****
 

1
1 2
1 2 3
1 2 3 4

 

* * * * *
* * *
* *
*

     * 
     **
    ***
  ****
 *****
 
 
         1
       232
      34543
     4567654
    567898765  
 
 
1
121
12321
1234321
123454321



1234
5678
9012
3456
7890
 
 
         *
      *  *
   *  *  *
*  *  *  *
 
*                         *
* * *              * * *
* * * * *   * * * * *
* * * * * * * * * * *
 
 
 
*                         *
   *                   *
*     *             *     *
   *     *       *     *
*     *      *      *     *
   *     *       *     *
*     *             *     *
   *                   *
*                         *
 
 
  *   *   *   *   *
      *   *   *   *
        *   *   *
          *   *
            *
          *   *
        *   *   *
      *   *   *   *
    *   *   *   *   *
 
 
                *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
        * * * * * * *
          * * * * *
            * * *
              *
            * * *
          * * * * *
 
 


     *
       * *
         * * *
           * * * *
         * * *
       * *
     *
 
 
 
 
* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *
 
 
 
 
* * * * * * * * * * * * * * * * *
  * * * * * * *   * * * * * * *
    * * * * *       * * * * *
      * * *           * * *
        * * * * * * * * *
          * * * * * * *
            * * * * *
              * * *
                *
 
 
 
 
      *
    * * *
  * * * * *
* * * * * * *
*           *
* *       * *
* * *   * * *
* * * * * * *
* * *   * * *
* *       * *
*           *
* * * * * * *
  * * * * *
    * * *
      *
 
 
 
* * * * * * * * * * * * * * * * * * * * * * * * *
  *           *   *           *   *           *
    *       *       *       *       *       *
      *   *           *   *           *   *
        *               *               *
      *   *           *   *           *   *
    *       *       *       *       *       *
  *           *   *           *   *           *
* * * * * * * * * * * * * * * * * * * * * * * * *
 
 
 
 
 
 5
 5 4
 5 4 3
 5 4 3 2
 5 4 3 2 1
 
 
 
333
22
1
 
 
 1
 22
 333
 4444
 55555
 
 
 1 2 3 4 5
 2 3 4 5
 3 4 5
 4 5
 5
 
 
            *
          *   *
        *       *
      *           *
    *               *
 
 
 
 
1
01
101
0101
10101
 
 
 
1 
1 0 
0 1 0 
0 1 0 1 
1 0 1 0 1 
# # # # #
 # # # #
  # # #
   # #
    #
 
 
 
55555
54444
54333
54322
54321
 
 
 
      
 
 
  
 
        
 
 
 
 
 
     

 
  
 

 

 

Check if a string contains unique characters

def isuniq(string1):
    uchars=set()
    for c in string1:
        if c  in uchars:
            return False
        else:
            uchars.add(c)
    return True
   
print isuniq('parag')
   

Matrix Multiplication

3 * 3 matrix multiplication

matrix1=[[2]*3] * 3
matrix2=[[10] * 3] * 3
matrix3=[[0] * 3] * 3
print matrix1
print matrix2

for i in range(3):
    for j in range(3):
        matrix3[i][j]=matrix1[i][j] * matrix2[i][j]
print matrix3

__main__

When you run a python script directly (example: $ python script.py), you want to set a starting point of the script. Python scripts are interpreted from the first line, then goes to the second line and so on ...

import module

def my_function():
  # code here

x = my_function()

But you want to make your code more structured, so you come up with this:

import module

def my_function():
  # code here

def main():
  x = my_function()

# the program starts from here
main()

This is good, but problem is, if you import the script from another script (from module import *), the main() function gets executed, but you may not want to do this. You want to call the main() function only when this script is exclusively executed. And you can do this using __name__ == "__main__".

import module

def my_function():
   # code here

def main():
   x = my_function()

# the program starts from here
if __name__ == "__main__":
   main()

Thus you can make your script to make it a reusable script (import from another script) and a standalone script as well.

concatenate two dictionaries

>>> dict_1 = {1: 'a', 2: 'b', 3: 'c'}
>>> dict_2 = {4: 'd', 5: 'e', 6: 'f'}
>>> dict_1
{1: 'a', 2: 'b', 3: 'c'}
>>> dict_2
{4: 'd', 5: 'e', 6: 'f'}
>>> dict_1.update(dict_2)
>>> dict_2
{4: 'd', 5: 'e', 6: 'f'}
>>> dict_1
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f'}
>>>

Retrive tweets using tweepy in Python

import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import Stream
from tweepy.streaming import StreamListener
tweets=[]

#Variables that contains the user credentials to access Twitter API
access_token = "access token
access_token_secret = "access token secret"
consumer_key = "consumer key "
consumer_secret ="consumer secret"

auth=tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)

api=tweepy.API(auth)
public_tweets=api.home_timeline()

for tweets in public_tweets:
    print tweets.text.encode('utf-8', 'ignore')

Matrix addition in Python


3 * 3 matrix addition

matrix1=[[2]*3] * 3
matrix2=[[10] * 3] * 3
matrix3=[[0] * 3] * 3
print matrix1
print matrix2

for i in range(3):
    for j in range(3):
        matrix3[i][j]=matrix1[i][j] + matrix2[i][j]
print matrix3