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;
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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);
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Count words

A

String s1 = “Today is Tuesday”;
String[] words = s1.split(“ “);

    System.out.println(words.length);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly