Tech
Infected

Linux, OpenSource, Programming And Hacks

Sep 16, 2017

Python Program for Merge Sort

Sep 16, 2017
We will look at the Merge Sort Algorithm which is used to sort an unsorted list. The input list is divided into two parts and they are solved recursively and then they are merged.

Worst Case Time Complexity: O(n log n)

Worst Case Space Complexity: O(n)


Merge sort is a divide and conquer algorithm.

1. We are given an unsorted list of n numbers 
2. Divide it into two array of equal size
3. Again Divide those two into another two array of equal size
4. Keep on dividing it until one element is left in each array.
5. Keep Merging the sub arrays to produce sorted arrays until there is only one array remaining.. This will be our sorted array.


Aug 10, 2017

Install guvcview (webcam recorder) in ubuntu 16.04 / Linux Mint

Aug 10, 2017
Guvcview is a webcam recorder software for Linux. Its the best recorder I have come so far.
Its simple and uses less resources compared to other webcam applications like Cheese or KDE Kamoso.


It has a two window interface one for just displaying the video and the other for different controls and settings.


Aug 9, 2017

Python Program to Generate Fibonacci Series using Recursion

Aug 9, 2017
Fibonacci Series: A sequence of numbers in which each number is the sum of the previous two numbers.

First 10 Fibonacci numbers: 0,1,1,2,3,5,8,13,21,34,...

In the following python program we have used recursive function to find out the Fibonacci series up to a given term.


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))
  break
 
if(found == False):
 print("%d is not in list"%x)


Also check out Binary Search Algorithm for a better Time complexity O(log n)

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:
 print(key,d1[key])

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 = '' 

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

''.join(reversed(binary))

print(binary)

Using Inbuilt function

 converting decimal to binary is quite easy

a = bit(121)
print(a[2:])

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

print(sys.argv[1])
print(sys.argv[2])








Note that using sys.argv[0] will just give program.py, 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

http://getbootstrap.com/components/#responsive-embed

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>
</div>

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



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 https://deb.nodesource.com/setup_6.x | sudo -E bash -

for 7.x versions (current active release)

curl -sL https://deb.nodesource.com/setup_7.x | 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