Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Wednesday, May 8, 2013

Midpoint


Problem: Compute the mid-point of two 2-dimensional points.

Solution in Python:

def compute_midpoint(x1, y1, x2, y2):
    """
    Author: Mayur P Srivastava
    """

    return (x1 + x2) / 2.0, (y1 + y2) / 2.0

Concepts Learned: Maths

Check whether matrix multiplication is possible


Problem: Given dimensions of 2 matrixes, check whether they can be multiplied.

Solution in Python:

def can_multiply(m1, n1, m2, n2):
    """
    Author: Mayur P Srivastava
    """

    return n1 == m2

Concepts Learned: Maths.

Triangle


Problem: Check whether given three 2-dimensional points form a triangle.


Solution in Python:

from __future__ import division

def check_triangle(x1, y1, x2, y2, x3, y3, eps=1e-16):
    """
    Author: Mayur P Srivastava
    """

    dx12 = x1 - x2
    dy12 = y1 - y2
    dx23 = x2 - x3
    dy23 = y2 - y3

    if dx12 == 0 and dx23 == 0:
        return False
    if abs(dx12) < eps and abs(dx23) < eps:
        return False
    if dx12 == 0 or abs(dx12) < eps:
        return True
    if dx23 == 0 or abs(dx23) < eps:
        return True

    slope12 = dy12 / dx12
    slope23 = dy23 / dx23

    return not (abs(slope12 - slope23) < eps)

Concepts Learned: Maths.

Sum of digits


Problem: Compute the sum of digits for the given number as a single digit number.

Example:

sum_of_digits(5674) = 4 (5+6+7+4=22, 2+2=4)


Solution in Python:

def sum_of_digits(n):

    """
    Author: Mayur P Srivastava
    """


    while n > 9:
        n = sum_of_digits2(n)
    return n


def sum_of_digits2(n):
    sum = 0

    while n > 0:
        sum += n % 10
        n = n // 10

    return sum

Concepts Learned: Loops and numbers.

Euclidean Distance


Problem: Compute the Euclidean distance between two 2-dimensional points.

Solution in Python:

import math

def compute_euclidean_distance(x1, y1, x2, y2):
    """
    Author: Mayur P Srivastava
    """

    dx = x2 - x1
    dy = y2 - y1
    return math.sqrt(dx*dx + dy*dy)


Concepts Learned: Numbers.


Quadratic Equation


Problem: Compute the values of x in the given quadratic equations ax^2+ bx + c for given a, b, c.


Solution in Python:



from __future__ import division

import math

def solve(a, b, c, eps=1e-12):
    """
    Author: Mayur P Srivastava
    """

    if abs(a) < eps:
        return None, None

    discriminant = b * b - 4 * a * c
    if discriminant < 0:
        return None, None

    return (-b + math.sqrt(discriminant)) / (2*a), (-b - math.sqrt(discriminant)) / (2*a)



Concepts Learned: Numbers.

Tuesday, May 7, 2013

Reverse the given string.


Problem: Reverse the given string.

Solution in Python:


def reverse(s):

    """
    Author: Mayur P Srivastava
    """


    arr = list(s)    
    n   = len(arr)

    for i in range(n // 2):
        arr[i], arr[n-i-1] = arr[n-i-1], arr[i]

    return "".join(arr)


Concepts Learned: Single loop.