Additional Coding Problem
Additional Coding Problem
1)
2)
3) You have to create a function isPalindrome() and it should return lambda expression
performing the action:
isPlaindrome(): The lambda expression should return true if number is palindrome
or false if it is not palindrome.
Input Format:
Output Format
2
…
…..
121
13
Output:
PALINDROME
NOT PALINDROME
4) Given an integer array, find the length of its longest increasing sequence.
The prototype of the function is as below:
public static void longestIncreasingSeq(int input1, int[] input2);
The function takes as input an integer input1 and an integer array input2, containing
input1 number of integers, and sets the output1 variable to the length of its LIS
(longest increasing sequence).
5) DigitSum
The labels on a trader's boxes display a large number (integer). The trader wants to label
the boxes with a single digit ranging from 1 to 9. He decides to perform digit sum on this
large number, continuously till he gets a single digit number. NOTE: In mathematics, the
digit-sum of a given integer is the sum of all its digits, (e.g.: the digit sum of 84001 is
calculated as 8+4+0+0+1 = 13, the digit sum of 13 is 1+3 = 4). Write a function (method)
that takes as input a large number and returns a single digit by performing continuous
digitSum on this number, and on the resulting numbers, till the resulting number is a
single digit number in the range 1 to 9.
Example 1: If the large number whose single-digit digitSum is to be found is 976592,
the process is as below – 9+7+6+5+9+2 = 38 3+8 = 11 1+1 = 2 Thus, the single-digit
digitSum
for the number 976592 is 2.
Example 2: If the large number whose single-digit digitSum is to be found is 123456, the
process is as below – 1+2+3+4+5+6 = 21 2+1 = 3 Thus, the single-digit digitSum for the
number 123456 is 3. For negative numbers, the result should also be in negative.
Example 3: If the large number whose single-digit digitSum is to be found is -123456,
the answer would be -3. The prototype of the function is: int digitSum(int input1);
where, input1 is the large number whose single-digit digitSum is to be found.
6) Given an array of strings, group anagrams together. (String with the same
characters and same frequency, further order of characters can be different).
Example:
7) Battleship 8x8*
The purpose of this exercise is to train you to work with wrapper classes and their static
methods.
Description
Battleship8x8 represents a 8x8 map for Battleship game. An important detail is that
Battleship8x8 uses a ships field of long type to store ships locations and a shots field of long
type to register shots.
Fields of long type value store 64 bits each. Consider them as 8 rows per 8 cells. '0' bits
represent empty cells, '1' bits represent cells seized by ships or registered shots.
public boolean shoot(String shot) - Registers a shot and returns true if the shot hits a
ship. A shot is a combination of one of A-H letters and one of 1-8 digits
public String state() - Returns a string representing state of the map. Map string is 8
lines per 8 characters separated by "\n". Use following symbols:
o '.' - an empty cell
You must not add or change new fields. Consider using static methods of wrapper classes.
Example
/*
11110000
00000111
00000000
00110000
00000010
01000000
00000000
00000000
*/
long map = 0b11110000_00000111_00000000_00110000_00000010_01000
000_00000000_00000000L;
Consider rows be marked with digits and columns be marked with letters:
/*
ABCDEFGH
┌────────
1│11110000
2│00000111
3│00000000
4│00110000
5│00000010
6│01000000
7│00000000
8│00000000
*/
Consider the following list of shots:
☒☐☐☐....
.×...☐☐☐
..×.....
..☐☒....
.........☐.
.☐......
........
........
8) Create an ArrayList with four string elements and perform the following operations:
9) Change the third element.
10) Remove the fourth element.
11) Display the total no. of elements.
12) Traverse all the elements using for-each loop.
13) Display all the elements after sorting.
14) Remove all the elements of the ArrayList.
15) Create a HashMap object with four key-value pairs and perform the following operations:
16) Access the second value.
17) Remove the third item.
18) Display the total no. of items.
19) Display the keys and values separately.
20) Demonstrate a program that implements the retrieval of elements from Set using
Iterator.