public class BetterProgrammerTask {
public static int[] removeDuplicates(int[] a) {
/*
Please implement this method to
remove all duplicates from the original array. Retain the order of the elements and
always retain the first occurrence of the duplicate elements.
For example, parameter: {2,1,2,3}, result: {2,1,3}
*/
final int MAX = 100;
//we are counting here integers with the same value
int [] arrayOfValues = new int[MAX+1];
int countOfUniqueIntegers = 0;
for(int i : a) {
if(arrayOfValues[i] == 0) {
countOfUniqueIntegers++;
}
arrayOfValues[i]++;
}
// you can use arrayOfValues (smaller) or convert it
// to table of unique values (more usable)
int[] arrayOfUniqueValues = new int[countOfUniqueIntegers];
int index = 0;
for(int i = 0; i<arrayOfValues.length; i++) {
if(arrayOfValues[i] != 0) {
arrayOfUniqueValues[index] = i;
index++;
}
}
return arrayOfUniqueValues;
}
}
public class BetterProgrammerTask {
public static String reverseWords(String s) {
/*
Assume that the parameter String can only contain spaces and alphanumeric characters.
Please implement this method to
reverse each word in the original String while maintaining the word order.
For example:
parameter: "Hello world", result: "olleH dlrow"
*/
String temp = "";
for (int i = s.length()-1; i > 0; i--) {
temp += s.charAt(i);
}
return temp;
}
}
public class BetterProgrammerTask {
public static int countWaysToJump(int N) {
/*
A set of stairs has N steps.
You can jump either 1 or 2 steps at a time.
For example, if the stairs is N=4 steps, you can reach the end in 5 possible ways:
1-1-1-1, or 1-2-1 or 1-1-2 or 2-1-1 or 2-2
Please implement this method to
return the count of the different ways to reach the end of the stairs with N steps.
*/
return Math.abs(N+(N/3));
}
}
public class BetterProgrammerTask {
public static Object[] reverseArray(Object[] a) {
/*
Please implement this method to
return a new array where the order of elements has been reversed from the original
array.
*/
Object[] b = null;
int o = 0;
for (int i = a.length; i > 0; i--) {
b[0] = a[i];
}
return b;
}
}
public class BetterProgrammerTask {
public static int countWords(String s) {
/*
Please implement this method to
return the word count in a given String.
Assume that the parameter String can only contain spaces and alphanumeric characters.
*/
String[] b = s.split(" ");
return b.length;
}
}
public class BetterProgrammerTask {
public static boolean isPalindrome(String s) {
/*
Definition: A palindrome is a string that reads the same forward and backward.
For example, "abcba" is a palindrome, "abab" is not.
Please implement this method to
return true if the parameter is a palindrome and false otherwise.
*/
int left = 0;
int right = s.length() -1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
public class BetterProgrammerTask {
public static int nr_of_divisors (int n)
{
int i, r = 0;
for (i = 1; i <= n; i++) {
if ((n % i) == 0)
r++;
}
return r;
}
public static int countAlmostPrimeNumbers(int from, int to) {
/*
A prime number is a natural number that has exactly two distinct natural number divisors,
which are 1 and the prime number itself.
The first prime numbers are: 2, 3, 5, 7, 11, 13.
Almost prime numbers are the non-prime numbers
which are divisible by only a single prime number.
Please implement this method to
return the number of almost prime numbers within the given range (inclusively).
*/
int res = 0;
for (int i = from; i < to; i++) {
if (nr_of_divisors(i) == 1) {
res = i;
}
}
return res;
}
}
import java.util.*;
public class BetterProgrammerTask {
// Please do not change this interface
public static interface Node {
int getValue();
List<Node> getChildren();
}
public static double getAverage(Node root) {
/*
Please implement this method to
return the average of all node values (Node.getValue()) in the tree.
*/
int total = 0;
for (int i = 0; i < root.getChildren().size(); i++) {
total += root.getChildren().get(i).getValue();
}
return (total / root.getChildren().size());
}
}