Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOPIC 5: Assignment Answers #13

Open
alyeffy opened this issue Feb 25, 2019 · 0 comments
Open

TOPIC 5: Assignment Answers #13

alyeffy opened this issue Feb 25, 2019 · 0 comments
Labels
5 Standard Operations and Algorithms assignment Assignment-related issues
Milestone

Comments

@alyeffy
Copy link
Owner

alyeffy commented Feb 25, 2019

Here are the answers for the homework assignments for Topic 5. Please let me know if you have any questions/concerns about them!

Algorithms (Basic Version pp 206 - 209) Review Questions
NOTE: The following solutions for these review questions might not be the be-all and end-all. If you think your solution might be correct, let me know and I can take a look for you.

  1. Replace all the highs and lows.
public int[] replaceHighAndLow(int[] arr) {
    int[] tempArrray = new int[arr.length];

    for (int i = 0; i < arr.length; i++;) {
        if (arr[i] > 750) {
            tempArray[i] = 1000;
        } else if (arr[i] < 250) {
            tempArray[i] = 0;
        } else {
            tempArray[i] = arr[i];
        }
    }
    return tempArray;
}
  1. Determine if the numbers in an ArrayList are always increasing.
private static boolean isIncreasing(ArrayList<Integer> arr) {
    for (int i = 1; i < arr.size(); i++) {
        if (arr.get(i) <= arr.get(i - 1))
            return false;
    }
    return true;
}
  1. Find all the songs that contain the world "Love" in the title.
private static int findCount(String[][] arr, String target) {
    int count = 0;
    for (int r = 0; r < arr.length; r++) {
        for (int c = 0; c < arr[r].length; c++) {
            if (arr[r][c].indexOf(target) != -1)
                count++;
         }
    }
    return count;
}
  1. Determine the relative strength index of a stock.
public int[] overpriced(double[] rsiValues) {
    int[] temp = new int[rsiValues.length];
    for (int i = 0; i < rsiValues.length; i++) {
        if (rsiValues[i] >= 70 )
            temp[i] = 1;
        else
            temp[i] = 0;
    }
    return temp;
}
  1. Fill an array with random chosen even numbers.
public int[] onlyEvens(int arraySize, int range) {
    int[] evens = new int[arraySize];

    for (int i = 0; i < arraySize; i++) {
        int number = (int) (Math.random() * range);
        while (number % 2 != 0) {
            number = (int) (Math.random() * range);
        }
        evens[i] = number;
    }
    return evens;
}
  1. Determine if the rate is increasing.
private static boolean rateIsIncreasing(ArrayList<Double> stockPrices) {
    for (int i = 2; i < stockPrices.size(); i++) {
        if ((stockPrices.get(i) - stockPrices.get(i-1)) <= (stockPrices.get(i-1) - stockPrices.get(i-2)))
            return false;
    }
    return true;
}

Sorting and Searching Question Set

Q A
1 E
2 D
3 C
4 B
5 C
6 E
7 C
8 A
9 A
10 C
11 B
12 D
13 A
14 E
15 A
16 B
17 B
18 A
19 D
20 A
21 E
22 C
23 D
24 C
25 E
26 D
27 D
28 B
29 B
30 B
31 A
32 D
33 E
34 D
35 A
36 C
@alyeffy alyeffy added 5 Standard Operations and Algorithms assignment Assignment-related issues labels Feb 25, 2019
@alyeffy alyeffy added this to the Topic 5 Items milestone Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 Standard Operations and Algorithms assignment Assignment-related issues
Projects
None yet
Development

No branches or pull requests

1 participant