Find us on Google+ Kill the code

Monday, 30 March 2015

Sum of series 1 to N must be 0 where N >=3 Algorithm in Python

Last week one of my friend told me about one of his interview question. He told me the definition of the algorithm he was asked in interview. And here is the definition.

The sum of numbers 1 to N (where N >= 3) must be zero, you can use plus (+) or minus (-) sign before any of the number. For example

for N = 3
- 1- 2+3=0

for N = 4

You have to find all the possibilities for any N >=3 number.

Here is the solution to above algorithm in Python. Python is the very handy and easy language to implement such algorithms.

 import itertools 
 n = input() 
 a = [] 
 for i in range(n): 
 sign = list(itertools.product(['+','-'],repeat=n-1)) 
 result = [] 
 for l in sign: 
   sum1 = a[0] 
   index = 0 
   for s in l: 
     index += 1 
       sum1 += a[index] 
       sum1 -= a[index] 
   if(sum1 == 0): 
 for l in result: 
   index = 0 
   print a[0], 
   for a1 in l: 
     index += 1 
     print a1, 
     print a[index], 
   print(" = 0") 

Please share your views in comment section.

Sunday, 22 September 2013

Change background color of MDIParent Form in VB.Net

#Region "Change Background Color of MDIParent : bgcolor()"
    'to change the background color of mdiparent
    'for more :

    Private Sub bgColor()
    Dim child As Control
    For Each child In Me.Controls
        If TypeOf child Is MdiClient Then
            child.BackColor = Color.CadetBlue
        Exit For
        End If
    child = Nothing
    End Sub
#End Region

Sunday, 31 March 2013

Rope Intranet : Google Code Jam

 Problem Statement 
A company is located in two very tall buildings. The company intranet connecting the buildings consists of many wires, each connecting a window on the first building to a window on the second building.
You are looking at those buildings from the side, so that one of the buildings is to the left and one is to the right. The windows on the left building are seen as points on its right wall, and the windows on the right building are seen as points on its left wall. Wires are straight segments connecting a window on the left building to a window on the right building.
You've noticed that no two wires share an endpoint (in other words, there's at most one wire going out of each window). However, from your viewpoint, some of the wires intersect midway. You've also noticed that exactly two wires meet at each intersection point.
On the above picture, the intersection points are the black circles, while the windows are the white circles.
How many intersection points do you see?
The first line of the input gives the number of test cases, T. T test cases follow. Each case begins with a line containing an integer N, denoting the number of wires you see.
The next N lines each describe one wire with two integers Ai and Bi. These describe the windows that this wire connects: Ai is the height of the window on the left building, and Bi is the height of the window on the right building.

Rope Intranet : Google Code Jam

public class rope_intranet {
    static int input[][] = {{5,6},{7,7},{4,3},{2,1},{1,5}};
    public static void main(String args[]) {
        int row = input.length;
        int count = 0;
        for(int t=0;t input[i][1]) {
        System.out.println("count = " + count);