0% found this document useful (0 votes)
1K views

Milestone 2

Here are the steps to solve this problem: 1. Declare and initialize an int array original to store the decoded original array. 2. Initialize original[original.length-1] with the last element of input1 array. This will be the last element of original array. 3. Iterate from original.length-2 to 0: - original[i] = input1[i] - input1[i+1] 4. output1 = original[0] 5. Initialize a variable sum and iterate over original array to calculate sum: - sum += original[i] 6. Return a Result object with output1 and sum. So in pseudocode

Uploaded by

Akhil Vinnakota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Milestone 2

Here are the steps to solve this problem: 1. Declare and initialize an int array original to store the decoded original array. 2. Initialize original[original.length-1] with the last element of input1 array. This will be the last element of original array. 3. Iterate from original.length-2 to 0: - original[i] = input1[i] - input1[i+1] 4. output1 = original[0] 5. Initialize a variable sum and iterate over original array to calculate sum: - sum += original[i] 6. Return a Result object with output1 and sum. So in pseudocode

Uploaded by

Akhil Vinnakota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

FindStringCode Helper

User
FindStringCode

Crazy Zak has designed the below steps which can be applied on any given string
(sentence) to produce a number in java7.

STEP1. In each word, find the Sum of the Difference between the first letter and
the last letter, second letter and the penultimate letter, and so on till the
center of the word.

STEP2. Concatenate the sums of each word to form the result.


For example -
If the given string is "WORLD WIDE WEB"
STEP1. In each word, find the Sum of the Difference between the first letter and
the last letter, second letter and the penultimate letter, and so on till the
center of the word.

WORLD = [W-D]+[O-L]+[R] = [23-4]+[15-12]+[18] = [19]+[3]+[18] = [40]


WIDE = [W-E]+[1-D] = [23-5]+[9-4] = [18]+[5] = [23]
WEB = [W-B]+[E] = [23-2]+[5] = [21]+[5] = [26]

STEP2. Concatenate the sums of each word to form the result


[40] [23] [26]
[402326]
The answer (output) should be the number 402326.
NOTE1:The value of each letter is its position in the English alphabet system i.e.
a=A=1, b=B=2, c=C=3, and so on till z=Z=26.
So, the result will be the same for "WORLD WIDE WEB" or "World Wide Web" or "world
wide web" or any other combination of uppercase and lowercase letters.

IMPORTANT Note: In Step1, after subtracting the alphabets, we should use the
absolute values for calculating the sum. For instance, in the below example, both
[H-O] and [E-L] result in negative number -7, but the positive number 7 (absolute
value of -7) is used for calculating the sum of the differences.
Hello = [H-O]+[E-L]+[L] = [8-15]+[5-12]+[12] = [7]+[7]+[12] = [26]

Assumptions: The given string (sentence) will contain only alphabet characters and
there will be only one space character between any two consecutive words.
You are expected to help Zak, by writing a function that takes a string (sentence)
as input, performs the above mentioned processing on the sentence and returns the
result (number).
Example 1:
input1 "World Wide Web"
output1 = 402326

Example2:
input1 = "Hello World"
output1 = 2640

Explanation:
Hello = [H-O]+[E-L]+[L] = [8-15]+[5-12]+[12] = [7]+[7]+[12] = [26]
World = [W-D]+[O-L]+[R] = [23-4]+[15-12]+[18] = [19]+[3]+[18] = [40]
Result = Number formed by concatenating [26] and [40] = 2640

code:
import java.io.*;
import java.util.*;
// Read only region start
class UserMainCode
{
public int findStringCode (String input1){
// Read only region end
// write code here...
String s = input1.toUpperCase();
String[] words = s.split(" ");
int[] codes = new int[words.length];

for (int i = 0; i < words.length; i++) {


int sum = 0;
char[] chars = words[i].toCharArray();
int n = chars.length;

for (int j = 0; j < n / 2; j++) {


int diff = Math.abs(chars[j] - chars[n - 1 - j]);
sum += diff;
}

if (n % 2 == 1) {
sum += chars[n / 2] - 'A' + 1;
}

codes[i] = sum;
}

// Concatenate the codes to form the result

String result = "";


for (int code : codes) {
result += String.valueOf(code);
}

// Return the result

return Integer.parseInt(result);
// Read only region end
    }
-----------------------------------------------------------------------------------
--------------------------------------------------
2.get code through strings

profile picture
Get Code Through Strings Farah is one of the few associates in Global Safe Lockers
Corp Limited, who has access to the company's exclusive locker that holds
confidential information related to her division. The PIN to the locker gets
changed every two days. Farah receives the PIN in the form of a string which she
needs to decode to get the single-digit numeric PIN in java7

The numeric PIN can be obtained by adding the lengths of each word of the string to
get the total length, and then continuously adding the digits of the total length
till we get a single digit.
For example, if the string is "Wipro Technologies", the numeric PIN will be 8

Explanation:
Length of the word "Wipro" = 5
Length of the word "Technologies" = 12
Let us add all the lengths to get the Total Length = 5+12=17
The Total Length = 17, which is not a single-digit, so now let us continuously add
all digits till we get a single digit i.e. 1+ 7 = 8
Therefore, the single-digit numeric PIN = 8
Farah approaches you to write a program that would generate the single-digit
numeric PIN if the string is input into the program. Help Farah by writing the
function (method) that takes as input a string input1 that represents the sentence,
and returns the single- digit numeric PIN.
Assumptions: For this assignment, let us assume that the given string will always
contain more than one word.
Let's see one more example
• If the given string is "The Good The Bad and The Ugly", the numeric PIN would be
= 5

Explanation:
Let us add lengths of all words to get the Total Length = 3+4+3+3+3+3+4=23 Total
Length = 23, which is not yet a single digit, so let us continue adding all digits
of the Total Length, i.e. 2+3 = 5
Therefore, single-digit numeric PIN = 5

Code:
import java.io.*;
import java.util.*;
//Read only region start
class UserMainCode
{
public int getCodeThroughStrings (String input1){
// Read only region end
// write code here...
int code = 0;
String[] words = input1.split(" ");
for (String word : words) {
code += word.length();
}

while (code > 9){


int temp = code;
code = 0;
while (temp > 0) {
code += temp % 10;
temp = temp / 10;
}
}
return code;
    }

-----------------------------------------------------------------------------------
-------------------------------------------------------------------
3.Addition using Strings

How to Attempt?

Addition using Strings: Write a function that takes two numbers in string format
and forms a string containing the sum (addition) of these two numbers in java7

Assumption(s):
• The input strings will contain only numeric digits
• The input strings can be of any large lengths
• The lengths of the two input string need not be the same
• The input strings will represent only positive numbers
For example -
If input strings are "1234" and "56", the output string should be "1290"

• If input strings are "56" and "1234", the output string should be "1290"

If input strings are "123456732128989543219” and “987612673489652", the output


string should be "123457719741663032871"
NOTE: In Java & C#, this logic can be easily implemented using BigInteger. However
for the sake of enhancing your programming skills, you are recommended to solve
this question without using BigInteger.

Code:
import java.io.*;
import java.util.*;
// Read only region start class UserMainCode
{
public string addNumberStrings (String input1, String input2)
{
// Read only region end
// write code here...
int n1 = input1.length();
int n2 = input2.length();
int carry = 0;
int sum = 0;
StringBuffer result = new StringBuffer();

for (int i = n1 - 1, j = n2 - 1; i >= 0 || j >= 0; i--, j--) {


int digit1 = i >= 0 ? input1.charAt(i) - '0' : 0;
int digit2 = j >= 0 ? input2.charAt(j) - '0' : 0;
sum = digit1 + digit2 + carry;
carry = sum / 10;
result.append(sum % 10);
}

if (carry > 0) {
result.append(carry);
}

return result.reverse().toString();
    }
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------
x 4.Simple Encoded Array

User
How to Attempt?

Simple Encoded Array 1: Maya has stored few confidential numbers in an array (array
of int). To ensure that others do not find the numbers easily, she has applied a
simple encoding in java7

Encoding used: Each array element has been substituted with a value that is the sum
of its original value and its succeeding element's value.
i.e. arr[i] = original value of arr[i] + original value of arr[i+1] e.g. value in
arr[0] = original value of arr[0] + original value of arr[1]
Also note that value of last element i.e. arr[last index] remains unchanged.
For example,
If the encoded array is (7,6,8,16,12,3}
The original array should have been (2,5,1,7,9,3}
Provided the encoded array, you are expected to find the- a. First number (value in
index 0) in the original array

b. Sum of all numbers in the original array


Write the logic in the function find Original FirstAndSum(int[] input1, int
input2): where,
input1 represents the encoded array, and
input2 represents the number of elements in the array input1

The method is expected to -


*find the value of the first number of the original array and store it in the
member output1 and

*find the sum of all numbers in the original array and store it in the member
output2
Note that the output1 and output2 should be returned as - members of a Result
object (if the code is being written in Java, C# or C++) members of a Result struct
(if the code is being written in C)
Assumption: The array elements can be positive and/or negative numbers
Example 1:
If the encoded array is (7,6,8,16,12,3}
The Original array should have been (2,5,1,7,9,3} So, First number in original
array = 2
Sum of all numbers in original array = 27

Example 2:
If the encoded array is {-2,-7,-12,-15}
The Original array should have been (8.-10.3.-15)
So, First number in original array = 8 Sum of all numbers in original array = -14

code:
import java.io.*;
import java.util.*;
//Read only region start
class UserMainCode
{
public class Result{
public final int output1;
public final int output2;
public Result(int out1, int out2){
output1 = out1;
output2 = out2;
}
}
public Result findoriginal FirstAndSum(int[] input1, int input2){
// Read only region end
//Write code here...
int firstNumber = 0;
int sum = 0;
for (int i = 0; i < input2; i++) {
if (i == input2 - 1) {
// The last element of the encoded array is the original element
firstNumber = input1[i];
} else {
// The original element is the difference between the encoded
element and the next encoded element
firstNumber = input1[i] - input1[i + 1];
}
sum += firstNumber;
}
return new Result(firstNumber, sum);
    }
}
-----------------------------------------------------------------------------------
------------------

You might also like