Algorithm Assignmedxgfxgx+nt Statements
Algorithm Assignmedxgfxgx+nt Statements
A squad of robotic rovers are to be landed by NASA on a plateau on Mars. This plateau, which is curiously rectangular,
must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to
send back to Earth.
A rovers location and position is represented by a combination of x and y coordinates and a letter representing one of
the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position
might be 0, 0, S, which means the rover is in the upper-left corner and facing South.
In order to instruct a rover to move forward in the direction its facing, NASA sends a simple string comprising of a
sequence of 'M'. Each M means move forward one grid point if the point lies in the limits of the grid, otherwise remain
in the same position without moving forward, and maintain the same heading direction and throw an error message
indicating the point lies out of the defined limits.
Assume that the location directly South from (x, y) is (x, y+1), where x is the horizontal axis and y is the vertical axis.
Also assume the directions are as per the diagram below:
Input Specification
First line of the input is the lower-right coordinates of the plateau, and the
upper-left coordinates are assumed to be 0, 0.
Second line of the input gives the coordinates representing the
position of the rover on the grid and its heading direction
Next line is a series of 'M' telling the rover to explore the plateau by moving one location for each 'M'.
Output Specification
Print the final coordinates with the direction of heading of the rover, all
separated by a space, otherwise throw the error message if the location
coordinates land beyond the given defined boundary and print the present
coordinates.
Sample Input1 Sample Output1 Sample Input2 Sample Output2
55
12N
MM
10N
55
12N
MMM
12N
OutOfBoundsException
Abacus I
Consider the 8 * 4 (8 rows 4 columns ) matrix given below. The matrix contains two all zeros rows and one all -1 rows .
Four rows are having values 1000, 100, 10 and 1 for the columns 1, 2, 3 and 4 respectively and one row is having
values 5000, 500, 50 and 5.
5000
500
50
5
0
0
0
0
-1
-1
-1
-1
0
0
0
0
1000
100
10
1
1000
100
10
1
1000
100
10
1
1000
100
10
1
We can represent any positive number between 0 and 10000 using the matrix explained above. The matrix represented
above is at the initial or zero state i.e it is representing the number 0 (Zero). The procedure of reading the matrix is
given below:
In each column the numbers coming between -1 and 0 (both above -1 and below -1) will be added. For
example for first column if 5000 is coming immediately above -1 and 1000, 1000, 1000 are coming
immediately below of -1 then 5000 should be added to 1000, 1000 and 1000 (5000+1000+1000+1000) and the
result will be 8000. Remember you have to add till 0 only. Refer to the column representation below:0
5000
-1
1000
1000
1000
0
1000
After finding the result of all the four columns using the procedure mentioned above , the results of all the four
columns will be added to get the number represented by the matrix.
For example the matrix given below is representing the number 7758
0
0
0
0
5000
500
50
5
-1
-1
-1
-1
1000
100
0
1
1000
100
10
1
0
0
10
1
1000
100
10
0
1000
100
10
1
The result of first column is 5000+1000+1000 = 7000. The result of second column is 500+100+100 = 700.
The result of third column is 50. The result of fourth column is 5+1+1+1 = 8. So the number represented by the
matrix shown above is 7000+700+50+8 = 7758.
Write a programme which will represent a given number between 0 and 10000 by the matrix just explained in previous
page. Your programme should ask to enter a number between 0 and 10000 (excluding 0 and 10000) and output the
resultant matrix representing the entered number.
Input specification :A number between 0 and 10000
Output specification :The matrix representing the number. Print each row of the matrix in a new line with the contents of the row seperated by
a double space. For example :0 0 0 0
5000 500 50 5
-1 -1 -1 -1
1000 100 0 1
1000 100 10 1
0 0 10 1
1000 100 10 0
1000 100 10 1
Abacus II
Consider the 8 * 4 (8 rows 4 columns ) matrix given below. The matrix contains two all zeros rows and one all -1 rows .
Four rows are having values 1000, 100, 10 and 1 for the columns 1, 2, 3 and 4 respectively and one row is having
values 5000, 500, 50 and 5.
5000
500
50
5
0
0
0
0
-1
-1
-1
-1
0
0
0
0
1000
100
10
1
1000
100
10
1
1000
100
10
1
1000
100
10
1
We can represent any positive number between 0 and 10000 using the matrix explained above. The matrix represented
above is at the initial or zero state i.e it is representing the number 0 (Zero). The procedure of reading the matrix is
given below: In each column the numbers coming between -1 and 0 (both above -1 and below -1) will be added. For
example for first column if 5000 is coming immediately above -1 and 1000, 1000, 1000 are coming
immediately below of -1 then 5000 should be added to 1000, 1000 and 1000 (5000+1000+1000+1000) and the
result will be 8000. Remember you have to add the numbers till 0 only. Refer to the column representation
below:0
5000
-1
1000
1000
1000
0
1000
After finding the result of all the four columns using the procedure mentioned above , the results of all the four
columns will be added to get the number represented by the matrix.
For example the matrix given below is representing the number 7758
0
0
0
0
5000
500
50
5
-1
-1
-1
-1
1000
100
0
1
1000
100
10
1
0
0
10
1
1000
100
10
0
1000
100
10
1
The result of first column is 5000+1000+1000 = 7000. The result of second column is 500+100+100 = 700.
The result of third column is 50. The result of fourth column is 5+1+1+1 = 8. So the number represented by the
matrix shown above is 7000+700+50+8 = 7758.
Write a programme which will output a number represented by the matrix just explained in the previous page. Your
programme should ask to enter the contents of the 8 * 4 (8 rows 4 column) matrix and then display the output
represented by the entered matrix.
Input specification
The matrix representing the number. Enter each row of the matrix in a new line with the contents of the row seperated
by a single space. For example :0 0 0 0
5000 500 50 5
-1 -1 -1 -1
1000 100 0 1
1000 100 10 1
0 0 10 1
1000 100 10 0
1000 100 10 1
Output specification
The number represented by the entered matrix. For example for the matrix entered above the number should be 7758.
CrossWord
Consider the 5 * 5 (5 rows and 5 columns) matrix given below :0
1
2
3
4
0
*
*
*
*
*
1 2 3
* * *
* C *
B A L
* T *
* * *
4
*
*
L
*
*
The matrix shown above is containing two words CAT and BALL. CAT is printed vertically and BALL is printed
horizontally. Rest of the elements are represented by '*' only. The first character of the string CAT is represented at first
row and second column (1,2). That means the co-ordinate of first character of string CAT i.e 'C' is (1,2). The remaining
characters of the string CAT i.e 'A' and 'T' are placed vertically just below the first character 'C' in the same column.
There is a common character between string CAT and string BALL. The common character is 'A'. The representation of
the second string BALL is based upon the common character 'A'. The string BALL is represented horizontally in the
same row which is containing the common character 'A'.
Write a program which accepts two strings as input and represents them in a 10 * 10 matrix in the similar manner as
explained above.
Input specification:
First line of the input comprises of the string to be placed vertically and followed by a space, then followed by
the space separated coordinates indicating the location of the first character of the string
Second line of the input comprises of the string to be placed horizontally intersecting with the string placed
vertically
Output specification:
The 10 * 10 matrix with both the strings placed appropriately. And the remaining of the matrix contents should
be represented by the character '*'.
Each row of the matrix should be terminated by a new line and the contents of the row be seperated by a single
space. For example :Assumptions:
a) The strings length will be >= 3 and <= 5.
b) Both the strings remain within the bounds of 10 * 10 matrix.
c) There is one and only one same character among the two strings.
SampleInput 1 SampleOutput 1
CAT 1 2
BALL
*
*
*
*
*
*
*
*
*
*
* * *
* C *
B A L
* T *
* * *
* * *
* * *
* * *
* * *
* * *
*
*
L
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
SampleInput 2
SampleOutput 2
INDIA 3 5
GOA
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* * *
* * *
* * *
* * I
* * N
* * D
* * I
G O A
* * *
* * *
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
Happy Numbers
Let the sum of the squares of the digits of a positive integer s 0 be represented by s1. In a similar way, let the sum of the
squares of the digits of s 1 be represented by s 2, and so on. If s i=1 for some 1 i, then the original integer s 0 is said to be
happy. For example, starting with 7 gives the sequence 7, 49 (=7^2), 97 (=4^2+9^2), 130 (=9^2+7^2), 10 (=1^2+3^2),
1 (=1^2), so 7 is a happy number, which reaches 1 on 6 iterations.
The first few happy numbers are 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, . The
number of iterations i required for these to reach 1 are, respectively, 1, 6, 2, 3, 5, 4, 4, 3, 4, 5, 5, 3, .... A number that is
not happy is called unhappy. Once it is known whether a number is happy (unhappy), then any number in the sequence
s1, s2, s3, ... will also be happy (unhappy). Unhappy numbers have eventually periodic sequences of s i which do not
reach 1 (e.g., 4, 16, 37, 58, 89, 145, 42, 20, 4, ...). You need to write a program to find all the happy numbers in a given
closed interval, which reach 1 within 10 iterations.
Input Specification
It is a single line input of two positive integers separated by a space.
Output Specification
Print all happy numbers in the interval and the number of iterations required by it to reach 1, separated by a
space and each in a new line.
Sample Input
7 11
Sample Input
44 68
Sample Output
76
10 2
Sample Output
44 5
49 5
68 3
The Palindrome
A word is a unit of language that carries meaning and consists of one or more morphemes which are linked more or less
tightly together, and has a phonetical value. A word is called a palindrome if it reads the same when you reverse it.
Your program should take an input as a string and produce the largest palindrome in the string. If the string contains
two palindromes, which are equal in length and are the largest among all possible palindromes in the string, then your
program should output the lexicographically first largest palindrome.
Input Specification
It is a single line input of a string.
Output Specification
Print the largest and lexicographically first palindrome in the string, on a new line.
Sample Input
Sample Input
dearmadamdear
database
Sample Output
madam
Sample Output
aba
In this problem we will assume that the keypad of our cell phone is arranged as
follows.
In the above grid each cell represents one key. Here SP means a space. In order to
type the letter a, we must press that key once, however to type b the same key
must be repeatedly pressed twice and for c three times. In the same manner, one
key press for d, two for e and three for f. This is also applicable for the
remaining keys and letters. Note that it takes a single press to type a space.
Input Specification
The first line of input will be a set of Words. A word is formed by the
letters A-Z and a-z and has at most 40 letters. The only symbols that appear in the input are the alphabetic
letters and white spaces.
Output Specification
Print the number of key presses required to type the message and terminated by new line character.
Sample Input
welcome to ulab
Sample output
29