0% found this document useful (0 votes)
13 views4 pages

Test 1

The document contains a Java class named Test1 that implements several methods for processing strings and arrays, including calculating the sum of integers in a string, sorting strings while ignoring spaces, reversing an array, and finding the sum of the two largest numbers in an array. It also defines a Node class for creating a tree structure and includes a method to calculate the average of node values in the tree. The main method demonstrates the functionality of these methods with various test cases.

Uploaded by

Surendra D
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)
13 views4 pages

Test 1

The document contains a Java class named Test1 that implements several methods for processing strings and arrays, including calculating the sum of integers in a string, sorting strings while ignoring spaces, reversing an array, and finding the sum of the two largest numbers in an array. It also defines a Node class for creating a tree structure and includes a method to calculate the average of node values in the tree. The main method demonstrates the functionality of these methods with various test cases.

Uploaded by

Surendra D
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/ 4

import java.util.

ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import org.apache.commons.lang3.RandomUtils;

public class Test1 {


public static void getSumOfNumbers(String s) {
/*
* Please implement this method to calculate the sum of all integers
* found in the parameter String. You can assume that integers are
* separated from other parts with one or more spaces (' ' symbol). For
* example, s="12 some text 3 7", result: 22 (12+3+7=22)
*/
s = s.replaceAll("[\\D]+", " ");
String[] numbers = s.split(" ");
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
try {
sum += Integer.parseInt(numbers[i]);
} catch (Exception e) {

}
}
System.out.println("Sum of numbers equal to:" + sum);

public static void sortIgnoringSpaces(String[] array) {


/*
* Please implement this method to sort a given array of Strings in
* alphabetical order ignoring spaces (' ' symbols) within the strings.
*/
// List<String> toSortStrings = Arrays.asList(array);

String[] arr =
Arrays.stream(array).map(String::trim).filter(Predicate.isEqual("").negate())
.toArray(String[]::new);

Arrays.sort(arr);
System.out.println("Answer for sort ignoring space:" +
Arrays.toString(arr));
for (String s : arr) {
System.out.println(s);

}
}

public static void reverseArray(String[] array) {


/*
* Please implement this method to reverse array where the order of
* elements has been reversed from the original array. E.g. given {"a",
* "b", "c", "d"}, result is {"d", "c", "b", "a"}
*/

int i;
String temp;
for (i = 0; i < array.length / 2; i++) {
temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
System.out.println("Answer for reverse array:" +
Arrays.toString(array));
for (String s : array) {
System.out.println(s);
}
}

private static void sumOfTwoLargestNumbers(int[] sumArray) {


int maxVal = Integer.MIN_VALUE;
int secondMaxval = Integer.MAX_VALUE;
for (int value : sumArray) {
if (value > maxVal) {
secondMaxval = maxVal;
maxVal = value;
} else if (value < maxVal && value > secondMaxval) {
secondMaxval = value;
}
}
int sumOfTwo = maxVal + secondMaxval;
System.out.println("Sum of the two largest numbers is " + sumOfTwo);
}

// Please do not change this helper class

public static class Node {


int val;
List<Node> children;

int getValue() {
return val;
}

void setValue(int val) {


this.val = val;
}

List<Node> getChildren() {
return children;
}

void setChildren(List<Node> children) {


this.children = children;
}
}

public static void getAverage(Node root) {


/*
* Please implement this method to calculate the average of all node
* values (Node.getValue()) in the tree. root c1 c2 c3 c4 c5 c6
*
* The codes must able to support any tree structures even the orphan
* root which doesn't have children. You can create any helper function
* as needed.
*/
int sum = 0;
int count = 0;
if (root == null) {
return;
}

sum = sum + root.val;


count = count + 1;
int avg = sum / count;
System.out.println("Average of the node values equals to " + avg);
}

public static void main(String args[]) {


try {
// sum
getSumOfNumbers("text mix with 112 and 222 with numbers 2
278 991");
System.out.println("////////////////////////");
System.out.println();

// sort
String[] array = new String[] { " ", "test", "ABC", "why", "
", "HLB", "webiste", "google", "1", "9",
"-111" };
sortIgnoringSpaces(array);
System.out.println("////////////////////////");
System.out.println();

// reverse
reverseArray(new String[] { "first", "second", "third", "fourth",
"fifth", "sixth", "seventh" });
System.out.println("////////////////////////");
System.out.println();

// sum two largest


int[] sumArray = { 43, 12, 12, 44, 47, 9, 34, 58, 3, 11, 4, 21 };
sumOfTwoLargestNumbers(sumArray);
System.out.println("////////////////////////");
System.out.println();

// average

Node root = new Node();


root.setValue(RandomUtils.nextInt(0, 100));
Node c1 = new Node();
c1.setValue(RandomUtils.nextInt(0, 100));
Node c2 = new Node();
c2.setValue(RandomUtils.nextInt(0, 100));
List<Node> list = new ArrayList<>();
list.add(c1);
list.add(c2);
root.setChildren(list);

Node c3 = new Node();


c3.setValue(RandomUtils.nextInt(0, 100));
list = new ArrayList<>();
list.add(c3);

c2.setChildren(list);
Node c4 = new Node();
c4.setValue(RandomUtils.nextInt(0, 100));
Node c5 = new Node();
c5.setValue(RandomUtils.nextInt(0, 100));
Node c6 = new Node();
c6.setValue(RandomUtils.nextInt(0, 100));
list = new ArrayList<>();
list.add(c4);
list.add(c5);
list.add(c6);
c3.setChildren(list);

getAverage(root);

int total = root.getValue() + c1.getValue() + c2.getValue() +


c3.getValue() + c4.getValue() + c5.getValue()
+ c6.getValue();
double ans = (double) total / 7;
System.out.println("Correct answer: " + ans);

} catch (Exception e) {
System.out.print(e.toString());
}
}

You might also like