Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Wednesday, December 25, 2013

Application of Hashtags in Python

#hastags, seen almost all over the web. We see them on twitter, g+, facebook and lots of other websites around the world. After learning something about Regular Expressions, I thought to just develop a simple example showing the use of hashtags in python.

In this example, we are going to use a dictionary to map our Texts, or messages to various hashtags. But for this I recommend you to know about regular expressions. These might seem difficult when you first look at them. Well, at least I find them quite difficult myself. Through regular expressions, we are going to find the hashtag(s) in our message, make it(them) a key(s) in the dictionary and map it to the list of messages.

So, here is the code for doing the same:

import re
from collections import defaultdict
dic = defaultdict(list) #this is going to be our list
hatag = r'#\w+' #the RegEx we will match with
def addtext(string): #this functions adds messages to our dictionary
    hashtags = re.findall(hatag,string) #finds the hashtags
    for x in hashtags: 
        dic[x[1:]].append(string) #makes key-value pairs of hashtags and messages
def search(hashtag): #searches messages given on hashtags.
    return dic[hashtag]

You can download this module here: Mediafire Link

See also:
Programming Languages, Udacity: The first chapter of this course intros Python Regex really great

Saturday, June 22, 2013

How to run a MySQL query using PHP

Simple tutorial!
Prerequisite Knowledge: Basic PHP, and MySQL
Prerequisite Stuff: A server that can run both PHP and MySQL(here is a simple way to set up the same on your system)

Step 1: Create a MySQL conenction!

  1. Define 3 string variables to define, the database host name, username and password
    • $dbhost = 'localhost'; //enter your hostname under quotes;
    • $dbuser='myuser'; //enter your database user name in the quotes;
    • $dbpass='mypassword; //enter your database user password;
  2. Create a MySQL connection using mysql_connect() function
    • $connection = mysqli_connect($dbhost,$dbuser,$dbpass);
    • if(!$connection) echo 'cannot connect to database'; //Checks whether connection is made

Step 2: Select the required database

  1. Define a string variable with database name:
    • $dbname='mydatabse' // Enter your name of database under quotes
  2. Select the databse using mysql_select_db() function
    • $select = mysqli_select_db($connection,$dbname) or die('cannot select database');
      • The die function checks whether the database could be selected or not

Step 3: Lets run the query

  1. Define a string variable for query.
    • $query = 'SELECT * FROM table'; //put your MySQL query in quotes
  2. Run the query using mysql_query() function.
    • $query = mysql_query($query)
    • We define the variable to get the result from MySQL.
Enjoy! Happy coding. Practice the code by understanding it. Yup, I did not include a sum up, understand and code it yourself! You can do it, trust me!

More coding at Let's Code!
Edit: Modified with mysqli extension. 

Friday, June 7, 2013

Python Code Snippets: To get the sum of digits of a number

Sum of digits of number '123' is 1 + 2 + 3 = 6
What could the sum of digits of factorial of 100 which has 158 digits?
Either, you can calculate 100! and sum up the digits using a calculator, or use this simple python function:

"

import math
def sum_digits(n):
   import math
   total = 0
   for i in range(int(math.log10(n)) + 1):
      total += n % 10
      n //= 10
   return total
#taking the input
n = input()
print sum_digits(n)

"

More snippets to come soon...