Can anybody solve this please ????????????????????????

Posted by Sandeepkasar under C# on 4/11/2012 | Points: 10 | Views : 4872 | Status : [Member] | Replies : 1
Hi friends I was asked to solve a puzzle during an Interview with in 2.5 hrs

Need a program which will do generic sorting of two sets of 5 inputs and produce a single output of 10 values in ascending order.
The first set of input will be numbers only (including negative numbers and decimals).
The second set will be characters A-J only (case insensitive). Each of the characters is assigned progressive values based on Fibonacci numbers.
So here, A = 0; B = 1; C = 1; D = 2; E = 3; F = 5; G =8; H = 13; I = 21 and J = 34. No other characters are permitted.

Rules:
No hard coding of values for C-J allowed. The values for each of these characters need to be derived using the relation: C = B+A; D = C+B . etc.
Please note the values for A = 0 and B = 1 can be hard coded.
Need clean exception handling so any bad input is rejected correctly.
Any kind of input entry format, as it works to your convenience, is allowed. It can be a single string with comma separated values or can be entry of one value at a time, etc.
Use of in-built function(s) for sorting is acceptable.
You are welcome to refer to internet or books and this is not viewed negatively. However, please give appropriate credits, if any such material was consulted.

Sample Test:
----------------
First Set:
1
2
-1
0.5
5
Second Set:
A
D
E
H
b
==============
Output:
=============
-1
A
0.5
1
b
2
D
E
5
H




Responses

Posted by: Ajayk3 on: 4/23/2012 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

ArrayList HT = new ArrayList();

Console.WriteLine("Enter Numbers :");

for (int i = 0; i < 5; i++)
{
ABC obj = new ABC();
obj.str = Console.ReadLine();
obj.dbl = Convert.ToDouble(obj.str);
HT.Add(obj);
}

Console.WriteLine("Enter A to J:");

for (int i = 5; i < 10; i++)
{
ABC obj = new ABC();
obj.str = Console.ReadLine();
obj.dbl = returnValue(obj.str);
HT.Add(obj);
}



ArrayList arrObj = new ArrayList();

for (int pass = 1; pass <= HT.Count - 2; pass++)
{
for (int i = 0; i <= HT.Count - 2; i++)
{
ABC obj1 = (ABC)HT[i];
ABC obj2 = (ABC)HT[i + 1];
if (obj1.dbl > obj2.dbl)
{
ABC temp = (ABC)HT[i + 1];
HT[i + 1] = HT[i];
HT[i] = temp;

}
}

}



foreach (ABC obj1 in HT)
{
Console.WriteLine(obj1.str.ToString());
}

Console.ReadLine();
}


private static double returnValue(string str)
{
switch (str.ToUpper())
{
case "A":
return 0;
case "B":
return 1;
case "C":
return returnValue("A") + returnValue("B");
case "D":
return returnValue("B") + returnValue("C");
case "E":
return returnValue("C") + returnValue("D");
case "F":
return returnValue("D") + returnValue("E");
case "G":
return returnValue("E") + returnValue("F");
case "H":
return returnValue("F") + returnValue("G");
case "I":
return returnValue("G") + returnValue("H");
case "J":
return returnValue("H") + returnValue("I");
default: return 0;
}
}



}

public class ABC
{
string _str = string.Empty;

public string str { get { return _str; } set { _str = value; } }

double _dbl = 0.0;

public double dbl { get { return _dbl; } set { _dbl = value; } }

}
}

Validation is still missing.(i think u can do it)

PLEASE MARK AS ANSWER IF IT HELPS YOU.

Sandeepkasar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response