Showing posts with label Dot Net Collections. Show all posts
Showing posts with label Dot Net Collections. Show all posts

Friday, January 3, 2014

Snake Game

Snake is an old computer game. There are many versions, but all involve maneuvering a "snake" around the screen, trying to avoid running the snake into itself or an obstacle.
Simulate the game through the application, though it is very simplified version.

Consider a 10 X 10 board/matrix (refer the following diagram, where every integer value is indicating the cell number). The snake move Suppose the snake starts to move from cell number 15. The snake can move to 4 possible directions: north (N), east (E), south (S) and west (W). If the snake moves to N, then it actually moves to cell number 5. If it moves to S, then it actually moves to cell number 25. Similarly if the snake moves to E or W, which means snake is moving to cell number 26 or 24 respectively. So, with every move, the snake covers a cell and consequentially it leaves a trail along the path.

Now consider a sequence of moves, NEEESSWWNNN. That means the snake first moves from its initial position (cell number 15) to cell number 5, then takes a right turn and covers the cell number 6, 7 and 8. After that it takes a down turn and covers 18 and 28, before it takes a left turn and covers cell number 27 and 26. At last it takes again upward turn and covers cell number 16 first and meets the same trail, i.e., cell number 6. It means the snake has run on to itself. In this case, the snake made 9 successful moves and on the 10th move it runs into itself.
Again consider the sequence of moves, SSEEEEEEEE. That means from starting position (cell number 15, the snake moves downward till cell 35 and then takes a right turn to cover cell number 36 to 40, before it runs out of board. Before running out of board it made 7 moves.



Again consider sequence of moves: WWSSSEEEESSSS. That means the snake first made a left run from start position (cell number 15) and covered cell number14 and 13 and then a downward move and continued till cell number 43. Then it took a right turn to go till cell number 47 and again moved downwards and continued till cell number 87. This time the snake neither did run onto itself nor run out of board. It completed all the 13 moves successfully.


Description: Complete the given method to return the number of moves as well as the result of the moves (i.e., “SUCCESS” OR “RANOUTOFBOARD” OR “RANONTOITSELF”) for a given sequence of moves in string format.

Input: Start position as integer value and sequence of moves as string value

Output: Number of moves (made successfully or made before it ran out of board or before it ran onto itself) along with the result of the moves (i.e., “SUCCESS” OR “RANOUTOFBOARD” OR “RANONTOITSELF”).

UTC
Input
Output
UTC04_01
53 and SSSSEEENNEEEEESS
{13, RANOUTOFBOARD }
UTC04_02
3 and SSSSEESSSSWNNNNNNNN
{14, RANONTOITSELF}
UTC04_03
15 and WWSSSEEEESSSS
{13, SUCCESS}

Monday, December 30, 2013

Sort Names by First Name

Names are stored in the pattern last name first pattern [lastName, firstName].
For example given the name “Sharma, Rakesh”, “Rakesh” is first name and “Sharma” is last name.
Complete the method provided to return the collection of strings sorted based on their first name.

Method:
public static String[ ] SortByFirstName(string[ ] names)
{ 
   // code here 
}
Sample Input & Output: 
UTC
Sample Input
Sample Output
01
“Sharma, Rakesh”, “Patil, Parthiv”, “Gowda, Anil”,
“Prasad, Vishnu”, “Khan, Amir”

“Khan, Amir”, “Gowda, Anil”, “Patil, Parthiv”, “Sharma, Rakesh”, “Prasad, Vishnu”

02
“Krishna, Gopal”, “Page,
Larry”, “King, Gavin”,
“Swamy, Krishna”

“King, Gavin” , “Krishna, Gopal”, “Swamy, Krish-na”, “Page, Larry”


Consecutive Characters count

Complete the method to print the consecutive characters and the number of times appearing in a String.
public static void PrintConsecutiveCharacters(string input)
{ 
   //write code
}
Input & Output: 
UTC
Sample Input
Sample Output
01
“I saw a CD player and a modem in ccd”

CD 2
DE 1 
02
“Student List do not exist in system”


ST 4
DE 1
NO 1

Wednesday, November 27, 2013

Dot Net: Collections

Count the frequency of different words in a sentence.

Objective:
To choose appropriate collection class for storing key/value pair.

Problem Statement:
Write a program to create a list of words present in a sentence passed from command line. The list also contains count of the appearance of each word in that sentence. The program has to display the words in sorted order and its frequency.

If the input is: “DotNet is technology and DotNet is interoperable and DotNet is simple”.


Output should be:  [and = 2, DotNet =3, interoperable = 1, is = 3, simple =1, technology=1]