Make the frequent cases fast and the rare case correct

Category Archives: Miscellaneous

C# – when to use .NET framework class library (FCL) type vs an alias

I started to wonder if i should use string keyword which is an alias of System.String or String class name while coding in C# and after reading the three posts of stackoverflow 1 , 2 and 3 I have decided to follow the conventions suggested by this blogger

The blogger says “In my case I use string when I think of it as a simple type, just like I do with int, char, byte etc. Simple types aren’t classes/objects tho, so when I decide to use a method declared in the underlaying .NET Framework type in C# I use the class name, that is String, Integer, Char, Byte etc. Hence I use “string” in declarations and “String” when using the static methods and properties, like “String.IsNullOrEmpty(string)” or “String.Empty”.

Although both generate the same intermediate language.

LINQ way to populate a 2-D array from a text file

Found this cool method to populate a 2-D array while reading a file which contains a 2-D array split by comma – courtesy this post in stackoverflow.

for example

Note the array values of array[0][3] and array[2][3] are followed by a spaces. Your text file needs to have a space after C and J so that the below one liner statement can read it as a 4×4 matrix. I showed this for odd case scenario of a matrix in a text file

string[][] array = File.ReadAllLines("file path").Select(s => s.Split(',')).ToArray();

Using Regex to solve Diophantine equations – Perl

I stumbled upon this cool concept of solving algebraic equations especially the tougher Diophantine equation using regex.

I decided to aggregate the information i found into this article and its worth to know this super cool concept.

You can find a nice introduction about solving algebra equations using regex in this blog post by author of regex cookbook.

Diophantine equation is an indeterminate polynomial equation that allows the variables to be integers only. Diophantine problems have fewer equations than unknown variables and involve finding integers that work correctly for all equations – Wikipedia.

The Oldest plays piano puzzle ends up being a Diophantine equation. In the perl monks you can see how the author uses regex to derive the different possible solutions to x*y*z = 72 equation and hence find a solution to the puzzle.

However this could only work for positive integers and a positive constant as described in this forum.

But if you know before hand that your variables and constant need to be positive for example the piano puzzle( the ages cant be negative) its worthwhile to use a regex to get those values.

# 3x + 2y + 5z = 40
$_ = 'o' x 40;
$a = 'o' x 3;
$b = 'o' x 2;
$c = 'o' x 5;
$_ =~ /^((?:$a)+)((?:$b)+)((?:$c)+)$/;
print "x = ", length($1)/length($a), "\n";
print "y = ", length($2)/length($b), "\n";
print "z = ", length($3)/length($c), "\n";


> perl
x = 11
y = 1
z = 1

ACM – Jolly Jumpers – Python

Problem taken from here

A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance,
1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.

Sample Input

1 4 2 3
1 4 2 -1 6
Sample Output

Not jolly


import math
n = 4
inp = [1,4,2,3]
out = []
for i in range(1,n):
val = 0
for i in range(1,n):
        if out[i-1] == i:
                val = val+1
if val == n-1:
        print "jolly"
        print "not jolly"

$ python

n = 5
inp = [1,4,2,-1,6]

$ python
not jolly

Thanks for reading !

O(nlogn) algorithm for finding whether there exists a pair of elements from two different Sets (same size) that add up to a specified sum

To find if a pair of elements exists (one or more) from two sets of same size that equal a predefined sum.

import time
import random
def twosetsum(S1,S2):
        sum = 18000000
        n = m = len(S2)

        start = 0
        end = m-1
        while start < n and end >= 0 :  #O(n)
                if S1[start] + S2[end] > sum :
                        end = end-1
                elif S1[start] + S2[end] < sum :
                        start = start+1
                        return 1
start = time.strftime('%s')
S1 = range(1,9000001) #O(nlgn) sorted list
S2 = range(1,9000001) #O(nlgn) sorted list

value = twosetsum(S1,S2)

if value == 1 :
        print "Yes there is a pair"
        print "No there is no pair"
end = time.strftime('%s')
time = int(end) - int(start)
print "Time to execute the Algorithm =", time ,"Second(s)"


labuser@ubuntu:~$ python
Yes there is a pair
Time to execute the Algorithm = 8 Second(s)

Input : sum = 19000000
labuser@ubuntu:~$ python
No there is no pair
Time to execute the Algorithm = 8 Second(s)

overall complexity O(n log n) + O(n log n) + O(n) = O(n log n)