BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Coding Questions II - 2024

python_logo




Bookmark and Share





bogotobogo.com site search:



Hamming distance

In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different:

HammingDistance.png

Here is the code for the Hamming distance:

def hamming(s1,s2): 
    if len(s1) != len(s2): 
        raise ValueError("Not defined - unequal lenght sequences") 
    return sum(c1 != c2 for c1,c2 in zip(s1,s2))

if __name__ == '__main__': 
    print(hamming("karolin", "kathrin")) # 3 
    print(hamming("karolin", "kerstin")) # 3 
    print(hamming("1011101", "1001001")) # 2
    print(hamming("2173896", "2233796")) # 3
    print(hamming("00000", "11111")) # 5

The first zip will produce [('k', 'k'), ('a', 'a'), ('r', 't'), ('o', 'h'), ('l', 'r'), ('i', 'i'), ('n', 'n')]. and the sum() counts the number of False, for example 3, because the input for sum() should look like this: [False, False, True, True, True, False, False].





Floor operation on integers

Write a floor division function without using '/' or '%' operator. For example, f(5,2) = 5/2 = 2, f(-5,2) = -5/2 = -3.

def floor(a,b):
   count = 0
   sign = 1
   if a < 0: sign = -1
   while True:
      if b == 1: return a
      # positive
      if a >= 0:
         a -= b
         if a < 0: break
      # negative
      else:
         a = -a - b
         a = -a
         if a > 0:
            count += 1
            break
      count += 1
   return count*sign

from random import randint
n = 20
while n > 0:
  a, b = randint(-20,20), randint(1,10)
  print '%s/%s = %s' %(a,b, floor(a,b))
  n -= 1

Output:

11/4 = 2
3/5 = 0
11/5 = 2
10/1 = 10
-6/9 = -1
14/2 = 7
-4/7 = -1
8/6 = 1
7/1 = 7
-7/1 = -7
-20/5 = -4
11/4 = 2
16/10 = 1
-9/7 = -2
-7/6 = -2
-8/2 = -4
13/7 = 1
-3/2 = -2
-10/2 = -5
-6/1 = -6




Fetching every other item in the list

Q1: How do you fetch every other item in the list?

Suppose we have a list like this:

>>> L = [x*10 for x in range(10)]
>>> L
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
  1. We can use enumerate(L) to get indexed series:
    >>> for i,v in enumerate(L):
    ...    if i % 2 == 0:
    ...       print v,
    ... 
    0 20 40 60 80
    
  2. This one may be the simplest solution:
    L2 = L[::2]
    >>> L2
    [0, 20, 40, 60, 80]
    




Python type() - function

What's get printed?

def f(): 
   pass
print type(f())
print type(1J)
print 1j*1j
print type(lambda:None)

Answer:

<type 'NoneType'>
<type 'complex'>
(-1+0j)
<type 'function'>
  1. The argument to the type() call is a return value of a function call, which returns None.
  2. 'lambda arguments: expression' yields a function object.
  3. An imaginary literal yields a complex number with a real part of 0.0.





Dictionary Comprehension

Q2: What's the list comprehension? How about dictionary comprehension?
Construct x^3 from a list of integers, L = [0,1,2,3,4,5]

List comprehension:

>>> L2 = [x**3 for x in L]
>>> L2
[0, 1, 8, 27, 64, 125]

Or:

>>> L2 = [x**3 for x in range(6)]
>>> L2
[0, 1, 8, 27, 64, 125]

Dictionary comprehension:

>>> D1 = dict((k,k**3) for k in L)
>>> D1
{0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125}

Or for Python 3:

>>> D2 = {k:k**3 for k in L}
>>> D2
{1: 1, 2: 8, 3: 27}






Sum

Q3: Sum of all elements in a list ([1,2,3,...,100] with one line of code.

>>> Ans = sum(range(101))
>>> Ans
5050

Q3.b: How about summing only odd elements?

>>> sum(range(1,101,2))

Or:

>>> Ans = sum(x for x in range(101) if x % 2 != 0)
>>> Ans
2500

Or:

>>> Ans = reduce(lambda x,y: x+y, filter(lambda x: x % 2 != 0, range(1,101)))
>>> Ans
2500

Or

>>> sum(filter(lambda x: x % 2, range(1,101)))




Truncating division

Two divisions - '/' and '//':

>>> 5.5/2
2.75
>>> 5.5//2
2.0




Python 2 vs Python 3

Q: What are the differences between Python 2 and Python 3?

Please visit Python 2 vs Python 3.





len(set)

What's the output?

>>> len(set([1,1,2,3,3,3,4]))

Output:

4

set() only retains unique values.





Print a list of file in a directory

We want to print a list of files in a directory including the sub-directories. We may want to do it recursively.

import os

def file_list(dir):
    basedir = dir
    subdir_list = []
    for item in os.listdir(dir):
        fullpath = os.path.join(basedir,item)
        if os.path.isdir(fullpath):
            subdir_list.append(fullpath)
        else:
            print fullpath

    for d in subdir_list:
        file_list(d)

file_list('/dir')

recursive_dir.png

Output:

./d3/f1
./d3/d4/d7/f2
./d3/d4/d7/f3
./d3/d5/d9/d10/f4
./d3/d5/d9/d10/f5


Here is simpler code:

import os

path = './d3'
file_list = []
for root, dir_names, file_names in os.walk(path):
    for f in file_names:
        file_list.append(os.path.join(root,f))
print(file_list)

Output:

['./d3/f1', './d3/d4/d7/f3', './d3/d4/d7/f2', './d3/d5/d9/d10/f4', './d3/d5/d9/d10/f5']






Count occurrence of a character in a Python string

For a given sentence, for example, "The Mississippi River", count the occurrence of a character in the string.

sentence='The Mississippi River'

def count_chars(s):
        s=s.lower()
        count=list(map(s.count,s))
        return (max(count))

print count_chars(sentence)

Let's look into the code, count=list(map(s.count,s))

>>> count = map(s.count,s)
>>> count
[1, 1, 2, 2, 1, 5, 4, 4, 5, 4, 4, 5, 2, 2, 5, 2, 1, 5, 1, 2, 1]

Actually, the each item in the list output represents the occurrence of each character including space:

 T  h  e     M  i  s  s  i  s  s  i  p  p  i     R  i  v  e  r
[1, 1, 2, 2, 1, 5, 4, 4, 5, 4, 4, 5, 2, 2, 5, 2, 1, 5, 1, 2, 1]

Answer = 5

In the line, map(func, sequence), the s.count counts the occurrence of a character while it iterates the character sequence of s.

Or

sentence='The Mississippi River'

def count_chars(s):
    s=s.lower()
    L = [s.count(c) for c in s]
    return max(L)

print (count_chars(sentence))

Or we can use dictionary:

sentence='The Mississippi River'
sl = sentence.lower()

d = {}.fromkeys([x for x in sl],0)

for char in sl:
    d[char] += 1
    
print d

Output:

{' ': 2, 'e': 2, 'i': 5, 'h': 1, 'm': 1, 'p': 2, 's': 4, 'r': 2, 't': 1, 
'v': 1} 


Or we can use dict.get(key,None) method:

sentence='The Mississippi River'
sl = sentence.lower()
d = {}
for c in sl:
    d[c] = d.get(c,0) + 1  
print d

Output:

{' ': 2, 'e': 2, 'i': 5, 'h': 1, 'm': 1, 'p': 2, 's': 4, 'r': 2, 't': 1, 'v': 1}


Or we can use dictionary comprehension:

sentence = "The Mississippi River"
ans = dict((c,sentence.count(c)) for c in sentence)
print ans

Output:

{' ': 2, 'e': 2, 'i': 5, 'h': 1, 'M': 1, 'p': 2, 's': 4, 'R': 1, 'T': 1, 'v': 1, 'r': 1}




Make a prime number list from (1,100)
import math
def isPrime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    for t in range(3, int(math.sqrt(n)+1),2):
        if n % t == 0:
            return False
    return True

print [n for n in range(100) if isPrime(n)]

Output:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

A simpler code:

def isPrime(n):
   if n == 1:
      return False
   for t in range(2,n):
      if n % t == 0:
         return False
   return True

print [n for n in range(1,100) if isPrime(n)]



Not much but a little bit of tweak: generate a list with the first 100 primes:

import math
def isPrime(n):
   if n == 1:
      return False
   elif n == 2:
      return True
   elif n % 2 == 0:
      return False
   else:
      for t in range(3, int(math.sqrt(n)+1), 2):
         if ( n % t == 0 ):
            return False
   return True

count = 1
n = 1
primes = []
while (count <= 100):
   if isPrime(n):
      primes.append(n)
      count += 1
   n += 1

print primes

Run it:

$ python prime2.py
python prime.py
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]






Make a list of n prime numbers
def isPrime(n):
    if n == 1: 
        return False
    if n == 2:
        return True
    for i in range(2,n):
        if n % i == 0:
            return False   
    return True
      
MaxCount = 10 
c = 0  
n = 2
primes = [] 
while (c <= MaxCount):
    if isPrime(n):
        primes.append(n)
        c += 1
    n += 1    

Output for MaxCount = 10:

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]   






Reversing a string - Recursive

When it comes to reversing a string in python, we do have s[::-1], boring!

In the following recursive code, at every call, the first string will go to the end and stored in stack. So, when there is only one character left as an input string, the code starts rewind and retrieve the character one by one. The net effect is it gets the character in reverse. It becomes obvious when we see the printed output below:

def reverse(input):
    print input
    if len(input) <= 1:
	    return input
	
    return reverse(input[1:]) + input[0]

s = 'reverse'	
print(reverse(s))

Output:

reverse
everse
verse
erse
rse
se
e
esrever

See also Reversing words






Reversing a string - Iterative

In the code below, we use range index operation, putting the characters into the list in reverse order. Than we pack them using join():

def reverse(input):
    return ''.join([input[i] for i in range(len(input)-1, -1, -1)])
    # or return ''.join(input[-i-1] for i in range(len(input)))
    # or return input[::-1]
s = 'reverse'	
print(reverse(s))

In the range() function, we used "-1" as a "stop" index and another "-1" as a "step" size.

Output:

esrever

See also Reversing words





Reverse a number

Return a given Number say 12345, in reverse order. So, it should return 54321:

# operates on a number directly
def rev1(n):
  r = 0
  for i in range(len(str(n))):
     r += n % 10
     n = n / 10
     # promote digits except the last digit
     if (n > 0):
       r = r * 10
  return r

# operates on a converted string
def rev2(n):
   sn = str(n)
   r = ''.join(sn[-i-1] for i in range(len(sn)))
   return r

# operates on a converted string - simplest
def rev3(n):
   return str(n)[::-1]

n = 12345
print(rev1(n))
print(rev2(n))
print(rev3(n))    

Output:

54321
54321
54321    




Output?

Q: What's the output from the following code?

def f():
    yield

def g():
    return

print 'f()=', f()
print 'g()=', g()

You can find the answer from generator.send() method.







Merging overlapped range

Q: We have a list of tuples where each tuple is a (start, end). We want to merge all overlapping ranges and return a list of distinct ranges. For example:

[(1, 5), (2, 4), (3, 6)] --->  [(1, 6)]
[(1, 3), (2, 4), (5, 6)] --->  [(1, 4), (5, 6)]

Write a code to produce a list of tuples with distinct ranges. Try with this data:

L = [(4,9), (20, 22), (1, 3), (24, 32), (23, 31), (40,50), (12, 15), (8,13)]

Here is the code:

L = [(4,9), (20, 22), (1, 3), (24, 32), (23, 31), (40,50), (12, 15), (8,13)]
# L = [(1, 5), (2, 4), (3, 6)]
# L = [(1, 3), (2, 4), (5, 6)]
L = sorted(L)
print L

Lnew = []
st = L[0][0]
end = L[0][1]

for item in L[1:]:
    if end >= item[0]:
        if end < item[1]:
            end = item[1]
    else:
        Lnew.append((st,end))
        st = item[0]
        end = item[1]
    
Lnew.append((st,end))

print Lnew

Output:

[(1, 3), (4, 9), (8, 13), (12, 15), (20, 22), (23, 31), (24, 32), (40, 50)]
[(1, 3), (4, 15), (20, 22), (23, 32), (40, 50)]




Conditional expressions (ternary operator)

Q: Outputs the result using ternary operator. 'pass' when the 'expected' is the same as 'returned', otherwise outputs 'fail'. Assume 'expected' = 1, 'returned' = 0.

expected = 1
returned = 0
result = 'pass' if expected == returned else 'fail'
print result




Packing Unpacking

In Python, packing and unpacking refer to operations involving the grouping and unpacking of values in data structures. This often involves tuples, lists, and iterable objects. Let's look at both concepts:

  1. Packing: the process of combining multiple values into a single variable or data structure. Tuples are commonly used for packing
    # Packing values into a tuple
    packed_tuple = 1, 'apple', 3.14
    
    # Packed tuple
    print(packed_tuple)
    # Output: (1, 'apple', 3.14)         
    

  2. Unpacking: the reverse process of extracting values from a data structure (like a tuple or a list) and assigning them to individual variables.
    # Packing values into a tuple
    packed_tuple = 1, 'apple', 3.14
    
    # Packed tuple
    print(packed_tuple)
    # Output: (1, 'apple', 3.14)         
    

    We can also use the * operator for variable-length unpacking, where one variable gets assigned the remaining elements as a list:
    packed_tuple = 1, 'apple', 3.14
        
    first, *rest = packed_tuple
    
    print(first)
    # Output: 1
    
    print(rest)
    # Output: ['apple', 3.14]    
    

  3. Packing and Unpacking in Function Arguments: Packing and unpacking are often used in function arguments.
    # Packing arguments into a tuple
    def example_function(*args):
        print(args)
    
    example_function(1, 'apple', 3.14)
    # Output: (1, 'apple', 3.14)       
    
    # Unpacking arguments from a tuple
    def another_function(a, b, c):
        print(a, b, c)
    
    arguments = (1, 'apple', 3.14)
    another_function(*arguments)
    # Output: 1 apple 3.14
    

    In this example, the *arguments syntax unpacks the values from the tuple and passes them as separate arguments to the function. Packing and unpacking are versatile techniques that enhance the flexibility and readability of our code, especially when working with functions or dealing with multiple values.






Function args

What will be printed?

def getThem(arg1, *arg2):
   print type(arg2), arg2

getThem('uno','dos','tres','cuatro','cinco')

arg2 aggregates the rest of args into a tuple. So, the output should look like this:

<type 'tuple'> ('dos', 'tres', 'cuatro', 'cinco')

How about this one:

def getThem(arg1, **arg2):
   print type(arg2), arg2

getThem('numbers',one='uno',two='dos',three='tres',four='cuatro',five='cinco')

arg2 aggregates the remaining parameters into a dictionary. So, the output is:

<type 'dict'> {'four': 'cuatro', 'three': 'tres', 'five': 'cinco', 'two': 'dos', 'one': 'uno'}




Unpacking args

Will the code below work?

def getThem(a,b,c,d,e):
   print a,b,c,d,e

mList = ['uno','dos','tres','cuatro','cinco']
getThem(mList)

Answer: No.

We'll get an error:

TypeError: getThem() takes exactly 5 arguments (1 given)

So, the code should be changed like this:

getThem(*mList)

*mList will unpack the list into individual elements to be passed to the function.

The unpacking a list can also be used as this:

>>> a, *b, c = [1,2,3,4,5,6,7,8,9,10]
>>> print(b)
[2, 3, 4, 5, 6, 7, 8, 9]

>>> a, *b, c, d = [1,2,3,4,5,6,7,8,9,10]
>>> print(b)
[2, 3, 4, 5, 6, 7, 8]






Finding the 1st revision with a bug

We have (oth-7th) revisions committed to Github, and due to a testing flaw, at the 7th revision we found that a bug has been introduced. We want to find when it was committed:

# r0 r1 r2 r3 r4 r5 r6 r7
# G  ?  ?  ?  ?  ?  ?  B   (G: good, B: buggy)

We may want to use binary search: if found bug, move backwards (previous rev.), if not, mode forwards (later rev.). In the code sample below, we're testing 6 cases:

def hasBug(i):
   if rev[i] == 'B':
      return True
   return False

def find_bug(good, bad):
   st = good
   end = bad
   ret = -1
   mid = (st + end) / 2
   while (mid > good and mid < bad):
      # backwards
      if hasBug(mid):
         end = mid
         ret = mid
         if mid == good + 1: break
      # forwards
      else:
         st = mid
         if mid == end - 1: break
      mid = (st + end ) / 2

   return ret

if __name__ == "__main__":
   rev = ['G','B','B','B','B','B','B','B']
   print 1 == find_bug(0,7)

   rev = ['G','G','B','B','B','B','B','B']
   print 2 == find_bug(0,7)

   rev = ['G','G','G','B','B','B','B','B']
   print 3 == find_bug(0,7)

   rev = ['G','G','G','G','B','B','B','B']
   print 4 == find_bug(0,7)

   rev = ['G','G','G','G','G','B','B','B']
   print 5 == find_bug(0,7)

   rev = ['G','G','G','G','G','G','B','B']
   print 6 == find_bug(0,7)

Note that the find_bug() function returns the first revision # that introduced a bug.

Output:

True
True
True
True




Which one has higher precedence in Python? - NOT, AND , OR

What will be printed out?

>>> True or False and False

Since AND is higher precedence than OR, AND will be evaluated first, "True" will be printed out.

Then, how about this one?

>>> not True or False or not False and True
True

NOT has first precedence, then AND, then OR.





Decorator(@) 1 - with dollar sign($)

We have a function that returns the amount with tax added. We want to prefix the amount with a dollar($) using decorator:

@dollar
def price(amount, tax_rate):
    return amount + amount*tax_rate

How do we want to implement the decorator function, dollar()?

Here is the code:

def dollar(fn):
    def new(*args):
        return '$' + str(fn(*args))
    return new

@dollar
def price(amount, tax_rate):
    return amount + amount*tax_rate

print price(100,0.1)

Output:

$110

Note that we did not modify the decorated function, price(), at all.





Decorator(@) 2 - Basic

This example is not much different from the previous one.

We have a function returning squared number:

def oldFnc(n):
   return n*n

Write a code doubling the return value from oldFnc() just by adding a decorator like this:

@doubleIt
def oldFnc(n):
   return n*n

Here is a code:

def doubleIt(myfnc):
   def doubleItInside(*args):
     return myfnc(*args)*2
   return doubleItInside

@doubleIt
def oldFnc(n):
   return n*n

#oldFnc = doubleIt(oldFnc)

print oldFnc(5)

For more information about decorators and the examples, please check Decorators






Multi-line coding

The following code won't print out properly. What's wrong with the code?

days = ['Monday',
        'Tuesday',
        'Wednesday']

months = ['Jan', \
         'Feb', \
         'Mar']

print "DAYS: %s, MONTHS %s" % 
    (days, months)

days is OK because expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. The months is also OK because backslashes are used to join physical lines, even though they are not required in this case. The print statement will not print the data because 2 logical lines are used without a backslash to join them into a logical line.

So, correct code should look like this:

print "DAYS: %s, MONTHS %s" %\ 
    (days, months)




Recursive binary search

Recursive binary serach:

# returns True if found the item
# otherwise, returns False
def bSearch(a, item):
   if len(a) == 0:
      return False
   mid = len(a)/2
   if a[mid] == item:
      return True
   else:
      if a[mid] < item:
         return bSearch(a[mid+1:], item)
      else:
         return bSearch(a[:mid], item)

if __name__ =="__main__":
   a = [1,3,4,5,8,9,17,22,36,40]
   print bSearch(a, 9)
   print bSearch(a, 10)
   print bSearch(a, 36)

Output:

True
False
True




Iterative binary search

Iterative binary serach:

# returns True if found the item
# otherwise, returns False
def bSearch(a, item):
   st = 0
   end = len(a)-1
   mid = len(a)/2
   left = st
   right = end
   while True:
      # found
      if a[mid] == item:
         return True
      # upper
      elif a[mid] < item:
         left = mid + 1
      # lower
      else:
         right = mid - 1

      mid = (left+right)/2
      if mid < st or mid > end: break
      if left == right and a[mid] != item: break
   return False

if __name__ =="__main__":
   a = [1,3,4,5,8,9,17,22,36,40]
   print bSearch(a, 9)
   print bSearch(a, 10)
   print bSearch(a, 36)
   print bSearch(a, 5)
   print bSearch(a, 22)

Output:

True
False
True
True
True






Pass by reference

List is passed by reference to the function and modifications to the function parameter also effect the original list.

What's the size of the list, (len(mList)).

def addItem(l):
    l += [0]

mList = [1, 2, 3, 4]
addItem(mList)
print len(mList)

Answer: 5





Simple calculator

This is not a real calculator. It's a practice of stack implementation by manipulating list's pop() and append().

The code should pass the following test:

test = {"11++":-1, "1+2": -1, "12+":3, "12+4*":12, "12+4*5+":17, "42-5*7-":3}

The keys are the question strings, and the values are the correct answers.

Note the following:

  1. We should have at least two items in the list whenever we see '+'/'-'/'*' operators. Otherwise, it should issue -1 and exit.
  2. For '-' operation, we need to take care of the the order of pop() operation. In the code, we prepend '-' for the last item in the list and then pops the 2nd to the last after that.

Here is the code:

def sol(input):
   a = []
   print "input=",input
   for c in input:
      if c.isdigit():
         a.append(c)
      elif c == '+':
         if len(a) < 2:
            return -1
         a.append(int(a.pop())+int(a.pop()))
      elif c == '-':
         if len(a) < 2:
            return -1
         a.append(-int(a.pop())+int(a.pop()))
      elif c == '*':
         if len(a) < 2:
            return -1
         a.append(int(a.pop())*int(a.pop()))
      else:
         pass
   return a[0]

test = {"11++":-1, "1+2": -1, "12+":3, "12+4*":12, "12+4*5+":17, "42-5*7-":3}

for k,v in test.items():
   if sol(k) == v:
      print k, v, 'Passed'
      print
   else:
      print k, v, 'Error'
      print

Output:

input= 12+4*
12+4* 12 Passed

input= 42-5*7-
42-5*7- 3 Passed

input= 11++
11++ -1 Passed

input= 12+
12+ 3 Passed

input= 12+4*5+
12+4*5+ 17 Passed

input= 1+2
1+2 -1 Passed





iterator class that returns network interfaces

Write a class that returns network interfaces (eth0, wlan0, ...).

Use subprocess and ip link show command.

import subprocess

class interfaces:
   def __init__(self):
      self.intf = []
      proc = subprocess.Popen(['ip', 'link', 'show'], stdout=subprocess.PIPE)
      line_count = 0
      while True:
         line = proc.stdout.readline()
         if line != '':
            if line_count % 2 == 0:
               self.intf.append(line)
         else:
            break
         line_count += 1
      self.counter = -1
      self.max = len(self.intf)

   def __iter__(self):
      return self

   def next(self):
      if self.counter >= self.max-1:
         raise StopIteration
      self.counter += 1
      return self.intf[self.counter]

f = interfaces()
for i in f:
   print i

Note that we used next() instead of __next__() because we're using Python 2.

Output:

1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default 

2: eth0:  mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000

3: wlan0:  mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000

4: lxcbr0:  mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT group default 

5: docker0:  mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 

8: vmnet1:  mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 1000

9: vmnet8:  mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 1000






Converting domain to ip

We have a file that lists domain names. We want to write a Python function that takes the file and returns a dictionary that has a key for the domain and a value for ip.

For example, the input file ('domains.txt') looks like this:

google.com
yahoo.com

Output is:

{'yahoo.com': '98.139.183.24', 'google.com': '216.58.192.14'}

Hint: we may want to use dig command, and to make the output simpler (only want to get ANSWER section), we can use it with additional options: +noall and +answer.

Here is the code:

import subprocess

def getIPs(file):
   ips = {}
   with open(file) as f:
      for line in f:
         domain = line.strip()
         out = subprocess.check_output(["dig","+noall","+answer", domain])
         ips[domain] = out.split()[4]
   return ips

print getIPs('domains.txt')




How to count the number of instances

We have a class A, and we want to count the number of A instances.

Hint : use staticmethod

class A:
   total = 0

   def __init__(self, name):
     self.name = name
     A.total += 1

   def status():
      print "Total number of instance of A : ", A.total
   status = staticmethod(status)


a1 = A("A1")
a2 = A("A2")
a3 = A("A3")
a4 = A("A4")

A.status()

Output:

Total number of instance of A :  4

We can use decorator (@staticmethod) like this:

   @staticmethod
   def status():
      print "Total number of instance of A : ", A.total




Python profilers - cProfile

Ref : https://docs.python.org/2/library/profile.html

cProfile and profile provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed.

cProfile.png

We can find the explanation of the output from the reference:

In the output from fibo(10), the first line indicates that 21893 calls were monitored. Of those calls, 3 were primitive, meaning that the call was not induced via recursion. The next line: Ordered by: standard name, indicates that the text string in the far right column was used to sort the output. The column headings include:

  1. ncalls
    for the number of calls
  2. tottime
    for the total time spent in the given function (and excluding time made in calls to sub-functions)
  3. percall
    is the quotient of tottime divided by ncalls
  4. cumtime
    is the cumulative time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.
  5. percall
    is the quotient of cumtime divided by primitive calls
  6. filename:lineno(function)
    provides the respective data of each function

When there are two numbers in the first column (for example 21891/1), it means that the function recursed. The second value is the number of primitive calls and the former is the total number of calls. Note that when the function does not recurse, these two values are the same, and only the single figure is printed.





Calling a base class method from a child class that overrides it

The super() can be used when the root class inherits from the object class, and here is the code of calling a base class method from a child class that overrides it:

superClass.png





How do we find the current module name?

A module can find out its own module name by looking at the predefined global variable __name__. If this has the value '__main__', the program is running as a script.

Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking __name__:

# t.py
def f():
    print(f"module name is {__name__}")

if __name__ == '__main__':
    f()

We have a simple Python module t.py with a function f() and a conditional statement that checks if the module is being run as the main program. If it is, it calls the f() function.

  1. The function f() prints the name of the module using the __name__ variable. When a Python script is run, the __name__ variable is set to '__main__'. If the module is imported into another script, __name__ is set to the name of the module.
  2. The conditional statement if __name__ == '__main__': ensures that the f() function is only called if the script is executed directly, not if it is imported as a module in another script.
  3. If we run this script directly (not imported as a module), it will print "module name is main" to the console. However, if we import it as a module in another script, the f() function won't be called automatically.

Run it as a script directly:

$ python t.py
module name is __main__

If we import the t.py, the module name will be t:

# s.py
import t    

$ python s.py
module name is t

Note that if want the f() to be called when imported the conditionals should be commented out:

# t.py
def f():
    print(f"module name is {__name__}")
    
# if __name__ == '__main__':
#    f()

f()

Now, when we import t in another script, the f() function will be executed automatically. Keep in mind that this approach makes the f() function run every time the module is imported, which may or may not be desired depending on the use case.





Why did changing list 'newL' also change list 'L'?

ListCopy.png

We might be wondering why appending an element to newL changed L too.

There are two factors that produce this result:

  1. Variables are simply names that refer to objects. Doing newL = L doesn't create a copy of the list - it creates a new variable newL that refers to the same object L refers to. This means that there is only one object (the list), and both L and newL refer to it.
  2. Lists are mutable, which means that we can change their content.

Please check: Copy an object





Construction dictionary - {key:[]}

We have a list of numbers, and we want to group them by the number of digits:

numbers = [1,256,64,65536,186000,1024,8,16,1905]
d = {}
for n in numbers:
    d.setdefault(len(str(n)),[]).append(n)
print d

Or:

numbers = [1,256,64,65536,186000,1024,8,16,1905]

d = {len(str(i)):[] for i in numbers}
print(f"group_by_digit = {d}")

for x in numbers:
    d[len(str(x))].append(x)
print(f"group_by_digit = {d}")

Output:

{1: [1, 8], 2: [64, 16], 3: [256], 4: [1024, 1905], 5: [65536], 6: [186000]}




Colon separated sequence

Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 3, between 1 and 100 (both included). The numbers obtained should be printed in a colon(:)-separated sequence on a single line as shown below:

7:14:28:35:49:56:70:77:91:98

The code should look like this:

l = []
for n in range(1,101):
    if n % 7 == 0 and n % 3 != 0:
        l.append(str(n))
print ':'.join(l)




Converting binary to integer

We want to convert binary numbers to integers:

"0100,0011,1010,1001" => [4, 3, 10, 9]

Here is the code:

s1 = "0100,0011,1010,1001"
s2 = s1.split(',')
num = []
for s in s2:
    n = 0
    for i,v in enumerate(s[::-1]): # reverse 0100->0010
        n += (2**int(i)) * int(v)
    num.append(n)
print num




9+99+999+9999+...

We want to write a code that computes the value of a+aa+aaa+aaaa with a given `digit` as the value of `a`. Suppose the following input is supplied to the program:

N = 9
DIGIT = 4

Then, the output should be:

9+99+999+9999 = 11106

Here is the code:

N = 9
DIGIT = 4
s = 0
for i in range(1,DIGIT+1):
    s += int(str(N)*i) # '9'*3 = '999'
print s




Calculating balance

We have a file that has deposit(D) and withdrawal(W) records:

D 500
D 300
W 100
D 400
W 100

We want to calculate the balance after reading the bank records, and it should be 1,000. The code looks like this:

f = open("bank.txt")
balance = 0
for line in f:
    item = line.rstrip().split()
    if item[0] == 'D':
        balance += int(item[1])
    else:
        balance -= int(item[1])
print balance
f.close()




Regular expression - findall

We can take only the words composed of digits from a given string using re.findall():

s = """Solar system:
Planets 8
Dwarf Planets: 5
Moons: Known = 149 | Provisional = 24 | Total = 173
Comets: More than 3400
Asteroids: More than 715000"""

import re
d = re.findall('\d+',s)
print d

Output:

['8', '5', '149', '24', '173', '3400', '715000']




Chickens and pigs

We have a count of 35 heads and 94 legs among the chickens and pigs in a farm. How many pigs and how many chickens do we have?

# 35 heads and 94 legs among the chickens and pigs
# c + p = 35
# 2*c + 4*p = 94
heads = 35
legs = 94
for c in range(heads):
    p = 35-c
    if 2*c + 4*p == 94:
            print c,p

The answer is:

23 12




Highest possible product

Find the highest possible product that we can get by multiplying any 3 numbers from an input array:

def mx3(a):
    b = sorted(a)

    m0 = b[0]
    m1 = b[1]
    x1 = b[-1]
    x2 = b[-2]
    x3 = b[-3]

    if m0 < 0 and m1 < 0:
        return max(m0*m1*x1,x1*x2*x3)

    else:
        return x1*x2*x3

l1 = [10,20,5,2,7,9,3,4]   
l2 = [10,-20,5,2,7,9,3,4]
l3 = [-10,-20,5,2,7,9,-3,4]
l4 = [10,-20,5,2,7,9,-3,4]

print mx3(l1)
print mx3(l2)
print mx3(l3)
print mx3(l4)

Output:

1800
630
1800
630




Implement a queue with a limited size

Implement a queue with a size limit of 3.

class Queue:
    def __init__(self):
        self.items = []
    
    def put(self, k,v):
        if len(self.items) >= 3:
            self.items.pop(0)
        self.items.append((k,v))
    
    def get(self,k):
        for x in self.items:
            if k == x[0]:
                return x[1]
        return -1
    
    def __repr__(self):
        return str(self.items)
        
q = Queue()
q.put(1,111), print(q)
q.put(2,222), print(q)
q.put(3,333), print(q)
q.put(4,444), print(q)
print(q.get(4))
print(q.get(10))  

Output:

[(1, 111)]
[(1, 111), (2, 222)]
[(1, 111), (2, 222), (3, 333)]
[(2, 222), (3, 333), (4, 444)]
444
-1






  1. Python Coding Questions I
  2. Python Coding Questions II
  3. Python Coding Questions III
  4. Python Coding Questions IV
  5. Python Coding Questions V
  6. Python Coding Questions VI
  7. Python Coding Questions VII
  8. Python Coding Questions VIII
  9. Python Coding Questions IX
  10. Python Coding Questions X





List of codes Q & A

  1. Merging two sorted list
  2. Get word frequency - initializing dictionary
  3. Initializing dictionary with list
  4. map, filter, and reduce
  5. Write a function f() - yield
  6. What is __init__.py?
  7. Build a string with the numbers from 0 to 100, "0123456789101112..."
  8. Basic file processing: Printing contents of a file - "with open"
  9. How can we get home directory using '~' in Python?
  10. The usage of os.path.dirname() & os.path.basename() - os.path
  11. Default Libraries
  12. range vs xrange
  13. Iterators
  14. Generators
  15. Manipulating functions as first-class objects
  16. docstrings vs comments
  17. using lambdda
  18. classmethod vs staticmethod
  19. Making a list with unique element from a list with duplicate elements
  20. What is map?
  21. What is filter and reduce?
  22. *args and **kwargs
  23. mutable vs immutable
  24. Difference between remove, del and pop on lists
  25. Join with new line
  26. Hamming distance
  27. Floor operation on integers
  28. Fetching every other item in the list
  29. Python type() - function
  30. Dictionary Comprehension
  31. Sum
  32. Truncating division
  33. Python 2 vs Python 3
  34. len(set)
  35. Print a list of file in a directory
  36. Count occurrence of a character in a Python string
  37. Make a prime number list from (1,100)
  38. Reversing a string - Recursive
  39. Reversing a string - Iterative
  40. Reverse a number
  41. Output?
  42. Merging overlapped range
  43. Conditional expressions (ternary operator)
  44. Packing Unpacking
  45. Function args
  46. Unpacking args
  47. Finding the 1st revision with a bug
  48. Which one has higher precedence in Python? - NOT, AND , OR
  49. Decorator(@) - with dollar sign($)
  50. Multi-line coding
  51. Recursive binary search
  52. Iterative binary search
  53. Pass by reference
  54. Simple calculator
  55. iterator class that returns network interfaces
  56. Converting domain to ip
  57. How to count the number of instances
  58. Python profilers - cProfile
  59. Calling a base class method from a child class that overrides it
  60. How do we find the current module name?
  61. Why did changing list 'newL' also change list 'L'?
  62. Constructing dictionary - {key:[]}
  63. Colon separated sequence
  64. Converting binary to integer
  65. 9+99+999+9999+...
  66. Calculating balance
  67. Regular expression - findall
  68. Chickens and pigs
  69. Highest possible product
  70. Implement a queue with a limited size
  71. Copy an object
  72. Filter
  73. Products
  74. Pickle
  75. Overlapped Rectangles
  76. __dict__
  77. Fibonacci I - iterative, recursive, and via generator
  78. Fibonacci II - which method?
  79. Fibonacci III - find last two digits of Nth Fibonacci number
  80. Write a Stack class returning Max item at const time A
  81. Write a Stack class returning Max item at const time B
  82. Finding duplicate integers from a list - 1
  83. Finding duplicate integers from a list - 2
  84. Finding duplicate integers from a list - 3
  85. Reversing words 1
  86. Parenthesis, a lot of them
  87. Palindrome / Permutations
  88. Constructing new string after removing white spaces
  89. Removing duplicate list items
  90. Dictionary exercise
  91. printing numbers in Z-shape
  92. Factorial
  93. lambda
  94. lambda with map/filter/reduce
  95. Number of integer pairs whose difference is K
  96. iterator vs generator
  97. Recursive printing files in a given directory
  98. Bubble sort
  99. What is GIL (Global Interpreter Lock)?
  100. Word count using collections
  101. Pig Latin
  102. List of anagrams from a list of words
  103. lamda with map, filer and reduce functions
  104. Write a code sending an email using gmail
  105. histogram 1 : the frequency of characters
  106. histogram 2 : the frequency of ip-address
  107. Creating a dictionary using tuples
  108. Getting the index from a list
  109. Looping through two lists side by side
  110. Dictionary sort with two keys : primary / secondary keys
  111. Writing a file downloaded from the web
  112. Sorting csv data
  113. Reading json file
  114. Sorting class objects
  115. Parsing Brackets
  116. Printing full path
  117. str() vs repr()
  118. Missing integer from a sequence
  119. Polymorphism
  120. Product of every integer except the integer at that index
  121. What are accessors, mutators, and @property?
  122. N-th to last element in a linked list
  123. Implementing linked list
  124. Removing duplicate element from a list
  125. List comprehension
  126. .py vs .pyc
  127. Binary Tree
  128. Print 'c' N-times without a loop
  129. Quicksort
  130. Dictionary of list
  131. Creating r x c matrix
  132. Transpose of a matrix
  133. str.isalpha() & str.isdigit()
  134. Regular expression
  135. What is Hashable? Immutable?
  136. Convert a list to a string
  137. Convert a list to a dictionary
  138. List - append vs extend vs concatenate
  139. Use sorted(list) to keep the original list
  140. list.count()
  141. zip(list,list) - join elements of two lists
  142. zip(list,list) - weighted average with two lists
  143. Intersection of two lists
  144. Dictionary sort by value
  145. Counting the number of characters of a file as One-Liner
  146. Find Armstrong numbers from 100-999
  147. Find GCF (Greatest common divisor)
  148. Find LCM (Least common multiple)
  149. Draws 5 cards from a shuffled deck
  150. Dictionary order by value or by key
  151. Regular expression - re.split()
  152. Regular expression : re.match() vs. re.search()
  153. Regular expression : re.match() - password check
  154. Regular expression : re.search() - group capturing
  155. Regular expression : re.findall() - group capturin
  156. Prime factors : n = products of prime numbers
  157. Valid IPv4 address
  158. Sum of strings
  159. List rotation - left/right
  160. shallow/deep copy
  161. Converting integer to binary number
  162. Creating a directory and a file
  163. Creating a file if not exists
  164. Invoking a python file from another
  165. Sorting IP addresses
  166. Word Frequency
  167. Printing spiral pattern from a 2D array - I. Clock-wise
  168. Printing spiral pattern from a 2D array - II. Counter-Clock-wise
  169. Find a minimum integer not in the input list
  170. I. Find longest sequence of zeros in binary representation of an integer
  171. II. Find longest sequence of zeros in binary representation of an integer - should be surrounded with 1
  172. Find a missing element from a list of integers
  173. Find an unpaired element from a list of integers
  174. Prefix sum : Passing cars
  175. Prefix sum : count the number of integers divisible by k in range [A,B]
  176. Can make a triangle?
  177. Dominant element of a list
  178. Minimum perimeter
  179. MinAbsSumOfTwo
  180. Ceiling - Jump Frog
  181. Brackets - Nested parentheses
  182. Brackets - Nested parentheses of multiple types
  183. Left rotation - list shift
  184. MaxProfit
  185. Stack - Fish
  186. Stack - Stonewall
  187. Factors or Divisors
  188. String replace in files 1
  189. String replace in files 2
  190. Using list as the default_factory for defaultdict
  191. Leap year
  192. Capitalize
  193. Log Parsing
  194. Getting status_code for a site
  195. 2D-Array - Max hourglass sum
  196. New Year Chaos - list
  197. List (array) manipulation - list
  198. Hash Tables: Ransom Note
  199. Count Triplets with geometric progression
  200. Strings: Check if two strings are anagrams
  201. Strings: Making Anagrams
  202. Strings: Alternating Characters
  203. Special (substring) Palindrome
  204. String with the same frequency of characters
  205. Common Child
  206. Fraudulent Activity Notifications
  207. Maximum number of toys
  208. Min Max Riddle
  209. Poisonous Plants with Pesticides
  210. Common elements of 2 lists - Complexity
  211. Get execution time using decorator(@)
  212. Conver a string to lower case and split using decorator(@)
  213. Python assignment and memory location
  214. shallow copy vs deep copy for compound objects (such as a list)
  215. Generator with Fibonacci
  216. Iterator with list
  217. Second smallest element of a list
  218. *args, **kargs, and positional args
  219. Write a function, fn('x','y',3) that returns ['x1', 'y1', 'x2', 'y2', 'x3', 'y3']
  220. sublist or not
  221. any(), all()
  222. Flattening a list
  223. Select an element from a list
  224. Circularly identical lists
  225. Difference between two lists
  226. Reverse a list
  227. Split a list with a step
  228. Break a list and make chunks of size n
  229. Remove duplicate consecutive elements from a list
  230. Combination of elements from two lists
  231. Adding a sublist
  232. Replace the first occurence of a value
  233. Sort the values of the first list using the second list
  234. Transpose of a matrix (nested list)
  235. Binary Gap
  236. Powerset
  237. Round Robin
  238. Fixed-length chunks or blocks
  239. Accumulate
  240. Dropwhile
  241. Groupby
  242. Simple product
  243. Simple permutation
  244. starmap(fn, iterable)
  245. zip_longest(*iterables, fillvalue=None)
  246. What is the correct way to write a doctest?
  247. enumerate(iterable, start=0)
  248. collections.defaultdict - grouping a sequence of key-value pairs into a dictionary of lists
  249. What is the purpose of the 'self' keyword when defining or calling instance methods?
  250. collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)
  251. zipped
  252. What is key difference between a set and a list?
  253. What does a class's init() method do?
  254. Class methods
  255. Permutations and combinations of ['A','B','C']
  256. Sort list of dictionaries by values
  257. Return a list of unique words
  258. hashlib
  259. encode('utf-8')
  260. Reading in CSV file
  261. Count capital letters in a file
  262. is vs ==
  263. Create a matrix : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  264. Binary to integer and check if it's the power of 2
  265. urllib.request.urlopen() and requests
  266. Game statistics
  267. Chess - pawn race
  268. Decoding a string
  269. Determinant of a matrix - using numpy.linalg.det()
  270. Revenue from shoe sales - using collections.Counter()
  271. Rangoli
  272. Unique characters
  273. Valid UID
  274. Permutations of a string in lexicographic sorted order
  275. Nested list
  276. Consecutive digit count
  277. Find a number that occurs only once
  278. Sorting a two-dimensional array
  279. Reverse a string
  280. Generate random odd numbers in a range
  281. Shallow vs Deep copy
  282. Transpose matrix
  283. Are Arguments in Python Passed by Value or by Reference?
  284. re: Is a string alphanumeric?
  285. reversed()
  286. Caesar's cipher, or shift cipher, Caesar's code, or Caesar shift
  287. Every other words
  288. re: How can we check if an email address is valid or not?
  289. re: How to capture temperatures of a text
  290. re.split(): How to split a text.
  291. How can we merge two dictionaries?
  292. How can we combine two dictionaries?
  293. What is the difference between a generator and a list?
  294. Pairs of a given array A whose sum value is equal to a target value N
  295. Adding two integers without plus
  296. isinstance() vs type()
  297. What is a decorator?
  298. In Python slicing, what does my_list[-3:2:-2] slice do?
  299. Revisit sorting dict - counting chars in a text file
  300. re: Transforming a date format using re.sub
  301. How to replace the newlines in csv file with tabs?
  302. pandas.merge
  303. How to remove duplicate charaters from a string?
  304. Implement a class called ComplexNumber
  305. Find a word frequency
  306. Get the top 3 most frequent characters of a string
  307. Just seen and ever seen
  308. Capitalizing the full name
  309. Counting Consequitive Characters
  310. Calculate Product of a List of Integers Provided using input()
  311. How many times a substring appears in a string
  312. Hello, first_name last_name
  313. String validators
  314. Finding indices that a char occurs in a list
  315. itertools combinations









Python tutorial



Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Python Coding Questions IX

Python Coding Questions X

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...




Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Python tutorial



Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Python Coding Questions IX

Python Coding Questions X

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Selenium WebDriver

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...


Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






OpenCV 3 image and video processing with Python



OpenCV 3 with Python

Image - OpenCV BGR : Matplotlib RGB

Basic image operations - pixel access

iPython - Signal Processing with NumPy

Signal Processing with NumPy I - FFT and DFT for sine, square waves, unitpulse, and random signal

Signal Processing with NumPy II - Image Fourier Transform : FFT & DFT

Inverse Fourier Transform of an Image with low pass filter: cv2.idft()

Image Histogram

Video Capture and Switching colorspaces - RGB / HSV

Adaptive Thresholding - Otsu's clustering-based image thresholding

Edge Detection - Sobel and Laplacian Kernels

Canny Edge Detection

Hough Transform - Circles

Watershed Algorithm : Marker-based Segmentation I

Watershed Algorithm : Marker-based Segmentation II

Image noise reduction : Non-local Means denoising algorithm

Image object detection : Face detection using Haar Cascade Classifiers

Image segmentation - Foreground extraction Grabcut algorithm based on graph cuts

Image Reconstruction - Inpainting (Interpolation) - Fast Marching Methods

Video : Mean shift object tracking

Machine Learning : Clustering - K-Means clustering I

Machine Learning : Clustering - K-Means clustering II

Machine Learning : Classification - k-nearest neighbors (k-NN) algorithm




Machine Learning with scikit-learn



scikit-learn installation

scikit-learn : Features and feature extraction - iris dataset

scikit-learn : Machine Learning Quick Preview

scikit-learn : Data Preprocessing I - Missing / Categorical data

scikit-learn : Data Preprocessing II - Partitioning a dataset / Feature scaling / Feature Selection / Regularization

scikit-learn : Data Preprocessing III - Dimensionality reduction vis Sequential feature selection / Assessing feature importance via random forests

Data Compression via Dimensionality Reduction I - Principal component analysis (PCA)

scikit-learn : Data Compression via Dimensionality Reduction II - Linear Discriminant Analysis (LDA)

scikit-learn : Data Compression via Dimensionality Reduction III - Nonlinear mappings via kernel principal component (KPCA) analysis

scikit-learn : Logistic Regression, Overfitting & regularization

scikit-learn : Supervised Learning & Unsupervised Learning - e.g. Unsupervised PCA dimensionality reduction with iris dataset

scikit-learn : Unsupervised_Learning - KMeans clustering with iris dataset

scikit-learn : Linearly Separable Data - Linear Model & (Gaussian) radial basis function kernel (RBF kernel)

scikit-learn : Decision Tree Learning I - Entropy, Gini, and Information Gain

scikit-learn : Decision Tree Learning II - Constructing the Decision Tree

scikit-learn : Random Decision Forests Classification

scikit-learn : Support Vector Machines (SVM)

scikit-learn : Support Vector Machines (SVM) II

Flask with Embedded Machine Learning I : Serializing with pickle and DB setup

Flask with Embedded Machine Learning II : Basic Flask App

Flask with Embedded Machine Learning III : Embedding Classifier

Flask with Embedded Machine Learning IV : Deploy

Flask with Embedded Machine Learning V : Updating the classifier

scikit-learn : Sample of a spam comment filter using SVM - classifying a good one or a bad one




Machine learning algorithms and concepts

Batch gradient descent algorithm

Single Layer Neural Network - Perceptron model on the Iris dataset using Heaviside step activation function

Batch gradient descent versus stochastic gradient descent

Single Layer Neural Network - Adaptive Linear Neuron using linear (identity) activation function with batch gradient descent method

Single Layer Neural Network : Adaptive Linear Neuron using linear (identity) activation function with stochastic gradient descent (SGD)

Logistic Regression

VC (Vapnik-Chervonenkis) Dimension and Shatter

Bias-variance tradeoff

Maximum Likelihood Estimation (MLE)

Neural Networks with backpropagation for XOR using one hidden layer

minHash

tf-idf weight

Natural Language Processing (NLP): Sentiment Analysis I (IMDb & bag-of-words)

Natural Language Processing (NLP): Sentiment Analysis II (tokenization, stemming, and stop words)

Natural Language Processing (NLP): Sentiment Analysis III (training & cross validation)

Natural Language Processing (NLP): Sentiment Analysis IV (out-of-core)

Locality-Sensitive Hashing (LSH) using Cosine Distance (Cosine Similarity)




Artificial Neural Networks (ANN)

[Note] Sources are available at Github - Jupyter notebook files

1. Introduction

2. Forward Propagation

3. Gradient Descent

4. Backpropagation of Errors

5. Checking gradient

6. Training via BFGS

7. Overfitting & Regularization

8. Deep Learning I : Image Recognition (Image uploading)

9. Deep Learning II : Image Recognition (Image classification)

10 - Deep Learning III : Deep Learning III : Theano, TensorFlow, and Keras









Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master