Skip to content

Instantly share code, notes, and snippets.

@iamkingalvarado
Created October 4, 2018 17:08
Show Gist options
  • Save iamkingalvarado/488a9433365fdce088c6e575751e9c1b to your computer and use it in GitHub Desktop.
Save iamkingalvarado/488a9433365fdce088c6e575751e9c1b to your computer and use it in GitHub Desktop.
Bluecoding test
import java.util.*;
class Test1 {
public static void main(String[] args) {
System.out.println(test1(new int[] {1,2,4,5,6,8,2,5,3,2}));
test2();
System.out.println(test3(10));
System.out.println(test4("anita lava la tina") ? "Palíndromo" : "No palíndromo");
}
/*
1. Create a method that receives an array of 100,000 integers and outputs the frequency with which each integer appears in ascending order
*/
private static String test1(int[] ints) {
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
for (int i = 0; i < ints.length; i++) {
Integer N = ints[i];
String n = N.toString();
if (!hashMap.containsKey(n)) {
hashMap.put(n, 0);
}
hashMap.put(n, hashMap.get(n) + 1);
}
Map<String, Integer> map = new TreeMap<String, Integer>(hashMap);
String[] key = new String[map.size()];
// array to store values of the HashMap
Integer[] value = new Integer[map.size()];
int i = 0;
for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
key[i] = entry.getKey();
value[i++] = entry.getValue();
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (i = 0; i < hashMap.size(); i++)
stringBuilder.append(" [" + key[i] + "," + value[i] + "]");
stringBuilder.append(" ]");
return stringBuilder.toString();
}
/*
2. Write a simple algorithm that prints numbers from 1 to 200. If the number is a multiple of 3, print "Blue" next to the number,
if the number is a multiple of 5 it print "Red", if the number is a multiple of both 3 and 5 it print "BlueRed".
*/
private static void test2() {
for (int i = 1; i <= 200; i++) {
boolean multipleOf3 = i % 3 == 0;
boolean multipleOf5 = i % 5 == 0;
System.out.println(i + (multipleOf3 && multipleOf5 ? " BlueRed" : (multipleOf3 ? " Blue" : (multipleOf5 ? " Red" : ""))));
}
}
/*
3. Enter the function to calculate the factorial of an integer
*/
private static int test3(int factorial) {
int result;
if (factorial == 0 || factorial == 1)
return 1;
result = test3(factorial - 1) * factorial;
return result;
}
/*
4. Create a method for determinate when a word or number is palindrome
*/
public static boolean test4(String sentence) {
char[] chars = sentence.replace(" ", "").toCharArray();
int start = 0;
int end = chars.length - 1;
while (end > start) {
if (chars[start] != chars[end]) {
return false;
}
++start;
--end;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment