Linux, OpenSource, Programming And Hacks

Jul 21, 2017

Understand Python Dictionary (key-value pair / Hash Map)

Jul 21, 2017
This tutorial is about Python dictionary which is data-structure representing  key-value pairs, you can also call it a HashMap.

One of the very common use of hashmap is finding frequency of characters in a string

Basically a dictionary is like a list but instead of integer index it can be of any type

to define a dictionary we use a key-value pair with a colon between them.

in the example below, "one" maps to 1

also note that a dictionary is un-ordered, it may not show the items in the order it was defined. 

Jul 20, 2017

Python Program for Linear Search using For loop

Jul 20, 2017
In linear search algorithm we match the element to be searched with the entire list of elements. Therefore its a brute force approach.

time complexity: O(n)

Linear Search implementation in Python:

list_of_elements = [4, 2, 8, 9, 3, 7]

x = int(input("Enter number to search: "))

found = False

for i in range(len(list_of_elements)):
 if(list_of_elements[i] == x):
  found = True
  print("%d found at %dth position"%(x,i))
if(found == False):
 print("%d is not in list"%x)

Python Program to find frequency of Characters in a String

We will find the count of repeating characters in a string using python3.

Here we have used dictionary data structure to make a key-value pair for all alphabets and numbers, where the key is the alphabet or number and value is its count.

so this is a kind of hashing algorithm as we are mapping keys and values.

first we initialize all counts to 0

and on scanning the string we increment the count on getting the same character again.

s = input("Enter a string: ") #use raw_input for python2

alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
d1 = {letter: 0 for letter in alphabets} #initializing all alphabets, numbers to count 0

for ch in s:
 d1[ch] += 1 #incrementing count on repeat

for key in d1:

Python Program to Convert Decimal to Binary

Using Loop

To convert Decimal to binary we check if the number is divisible by 2, if yes then we take a string and append 0 to and and if not then append 1 to it and divide the number by 2 and do the same again.

finally we reverse the string and print the result

numb = 121
binary = '' 

 if(numb % 2 == 0):
  binary += '0'
  binary += '1'
 numb = numb//2



Using Inbuilt function

 converting decimal to binary is quite easy

a = bit(121)

Jul 19, 2017

Create a Simple FTP Server and Client in Python

Jul 19, 2017

FTP stands for File transfer protocol, it's used to transfer files between server and client.

Here we will see how to make a simple FTP server in python and we will do some basic example tasks like listing directories of the server and uploading and downloading files to and from the server.

we will be running both the server and client on the same local machine.

Jul 18, 2017

Python Program to find HCF and LCM

Jul 18, 2017
HCF is also called Greatest Common divisor (GCD).
the HCF of two or more number is the largest of all the common factors.

for example:
if we take two numbers, 12 and 18
their common divisor are 1,2,3 and 6. therefore 6 being the biggest is the HCF of 12 and 18.

Here, we are using loop method to find HCF of two numbers. It can also be calculated using recursion.

Jul 16, 2017

Passing argument from Command-line in Python

Jul 16, 2017
Sometimes it's required to use arguments passed from cli to use in our program. Its very simple to do so..

we just need to use sys module's argv method.

import sys


Note that using sys.argv[0] will just give, which is in fact the first argument. 

Jun 10, 2017

How to install and use package control in sublime text 3

Jun 10, 2017
Package Control is the Sublime text package manager with which we can easily search and install thousands of packages thus extending sublime text functionality.

Since Sublime text is cross platform so these instruction will work with all Operating Systems: Linux, Windows and OS X.

Jun 7, 2017

Responsive Iframe / youtube videos embed with Bootstrap

Jun 7, 2017
Hi guys, today we will see how to make iframe videos responsive using bootstrap framework.

Since the embed code for a youtube video is a iframe code, this is work for youtube videos too.

Have a look at this bootstrap link

all you got to do is wrap your iframe code in a embed-responsive like shown below

<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="..."></iframe>

<!-- 4:3 aspect ratio -->
<div class="embed-responsive embed-responsive-4by3">
  <iframe class="embed-responsive-item" src="..."></iframe>

Jun 6, 2017

How to Install Sublime text 3 in ubuntu / linux mint

Jun 6, 2017
Sublime text is a powerful text editor that is available for almost all platforms. It is one of the best text editor for linux and I personally prefer it. It has thousands of packages which add up to its functionalities.

Sublime text is free to use though they ask for a full-time registration sometimes but you may ignore it and continue using it.

Here we will see how to install the latest sublime text 3 from their official repository so that we will be getting updates as well.

May 14, 2017

Use Bluetooth Headphone or Speaker on Ubuntu 16.04 [Solved]

May 14, 2017
I recently got a soundbot wireless portable speaker, which is by the way really great! but It had some issues making it work with ubuntu.

After pairing it was working with Headset Head unit (HSP/HFP) which produced a really bad audio quality.

to get the sound right It should be using the High Fidelity playback (A2DP Sink) but it wouldn't select that profile from the sound preferences.

May 13, 2017

Install latest Node.js version in ubuntu 16.04

May 13, 2017

Node.js is an opensource JavaScript based framework for server-side scripting.

Ubuntu Repository has the older version of node.js

To get the updated version you need to install node.js using the PPA by provided by NodeSource.

for 6.x versions (LTS version)

curl -sL | sudo -E bash -

for 7.x versions (current active release)

curl -sL | sudo -E bash -

after this the PPA will be added and repository will be updated automatically,

now install the updated node.js version

sudo apt-get install nodejs

Apr 8, 2017

How to Add / Append items to a list in Python

Apr 8, 2017
To add items to a list in python is a very general task that you might come across.

Here we have an empty list that we are populating with 10 items using the append method.

n = 10

list = []

for i in range(n):

print list

That's it!

Common Mistake

its a common mistake of doing something like this


for i in range(n):

this will throw an error IndexError: list assignment index out of range

Because the list is empty and list[i] is trying to access index which does not exist!

Apr 7, 2017

Set Permissions to add files to /var/www folder in ubuntu linux

Apr 7, 2017
/var/www is the default root folder of your local web server (like apache), you host all your website files here and access it on the browser with url like or http://localhost

you cannot simply copy paste stuff in this folder using your file browser, It is protected for security reasons, you will get a permission denied error message because by default you don't have write access permission here.

Follow these Steps:

execute these commands to set all permissions for /var/www directory

sudo adduser <username> www-data
sudo chown -R www-data:www-data /var/www
sudo chmod -R g+rwX /var/www
replace <username> with your actual username.

Now you can copy, edit and create files in /var/www folder just like any other folder.

Jan 29, 2017

How to install python-gst (pygst) in ubuntu 16.04

Jan 29, 2017
pygst is the python binding of Gstreamer which is a media-framework, it supports various media handling such as audio playback, video playback and editing.


open up the terminal and enter the following commands

sudo apt-get update
sudo apt-get install python-gst-1.0
sudo apt-get install python-gst-1.0-dbg
sudo apt-get install python-gst0.10

Check if its working properly

open up your python shell and import pygst

aman@vostro:~$ python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygst

if its not installed properly then an error message will be shown: ImportError: No module named pygst