Memorize Flashcards
1
Q
Fibonacci
A
public static void fibonacci(int num){ int zero = 0, one = 1, sum; for (int i = 0; i < num -1; i++){ sout(zero); sum = zero + one; zero = one; one = sum; } }
2
Q
Finding second min with 1 loop
A
int[] numbers = {2, 3, 7, 1, 1, 7};
int smallest = Math.min(numbers[0], numbers[1]); // 1 int secondSmallest = Math.max(numbers[0], numbers[1]); // 2
//2, 3, 7, 1, 1, 7 for(int number : numbers){ if (number < secondSmallest && number < smallest) { secondSmallest = smallest; smallest = number; } } System.out.println("The smallest number in the array = " + smallest); System.out.println("The second smallest number in the array = " + secondSmallest); }
3
Q
Finding first even & first odd
A
int[] numbers = {0, 5, 3, 2, 4, 7, 10}; /* First even = 0 First odd = 5 */ int countEven = 0; int countOdd = 0; for (int number : numbers) { if (number % 2 == 0 && countEven == 0) { System.out.println("First even number is: " + number); countEven++; } else if (number % 2 == 1 && countOdd == 0) { System.out.println("First odd number is: " + number); countOdd++; } if (countEven == 1 && countOdd == 1) break;
4
Q
Count words
A
String s1 = “Today is Tuesday”;
String[] words = s1.split(“ “);
System.out.println(words.length);
5
Q
Find the longest String in an Array
A
String[] colors = {“red”, “blue”, “yellow”, “brown”, “white”};
String newColor = ""; for (String color : colors) { if (color.length() > newColor.length()) newColor = color; } System.out.println(newColor);
6
Q
Find the greatest
A
int[] numbers = {5, 3, 0, -5}; int greatest = numbers[0]; for (int number : numbers) { if(number > greatest) greatest = number; // greatest = Math.max(greatest, number); } System.out.println(greatest);
7
Q
Searching an Array
A
String[] objects = {“Remote”, “Mouse”, “Mouse”, “Keyboard”, “iPad”};
boolean mouse = false; for (String object : objects) { if (object.equals("Mouse")) { mouse = true; break; } } System.out.println(mouse);
8
Q
Searching an Array
A
String[] objects = {“Remote”, “Mouse”, “Mouse”, “Keyboard”, “iPad”};
boolean board = false; for (String object : objects) { if (object.equals("board")) { board = true; break; } } System.out.println(board);
9
Q
Searching an Array
A
Check if you have an element equals to “iPad”
If so, print true
Else, print false
“Remote”, “Mouse”,”Mouse”, “Keyboard”, “iPad”
String[] objects = {"Remote", "Mouse", "Mouse", "Keyboard", "iPad"}; STEP 1. Arrays.sort(objects); STEP 2. System.out.println(Arrays.binarySearch(objects, "iPad") >= 0); // true System.out.println(Arrays.binarySearch(objects, "Mouse") >= 0); // true System.out.println(Arrays.binarySearch(objects, "Apple") >= 0); // false System.out.println(Arrays.binarySearch(objects, "Keyboard") >= 0); // true System.out.println(Arrays.binarySearch(objects, "Key") >= 0); // false
10
Q
Remove_Duplicates
A
List objects = new ArrayList<>(); objects.add("Cup"); objects.add("Book"); objects.add("Pen"); objects.add("Cup"); objects.add("Book"); objects.add("Book"); objects.add("Pencil"); List uniques = new ArrayList<>();
for (String object : objects) { if (!uniques.contains(object)) uniques.add(object); } System.out.println(uniques); // [Cup, Book, Pen, Pencil]