Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 207c0d5

Browse files
Add Queue DS Array DS Coding Exercises
1 parent a0d6c8c commit 207c0d5

File tree

11 files changed

+343
-0
lines changed

11 files changed

+343
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Ignore this file: Instructor Code (For Testing) */
2+
3+
import java.util.*;
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import com.udemy.ucp.*;
9+
10+
public class Evaluate {
11+
Exercise app = new Exercise();
12+
int[] arr = null;
13+
int B = 0;
14+
15+
// Test Cases
16+
@Test
17+
public void test_reverse_of_3_elements(){
18+
arr = new int[]{1, 2, 3, 4, 5};
19+
B = 3;
20+
int[] user_ans = app.reverseBElements(arr,B);
21+
int[] ans = new int[]{3, 2, 1, 4, 5};
22+
assertTrue(Arrays.equals(user_ans, ans));
23+
}
24+
@Test
25+
public void test_reverse_of_2_elements_I(){
26+
arr = new int[]{9, 8, 7, 6, 5};
27+
B = 2;
28+
int[] user_ans = app.reverseBElements(arr,B);
29+
int[] ans = new int[]{8, 9, 7, 6, 5};
30+
assertTrue(Arrays.equals(user_ans, ans));
31+
}
32+
@Test
33+
public void test_reverse_of_2_elements_II(){
34+
arr = new int[]{9, 8, 7, 6, 5, 4};
35+
B = 2;
36+
int[] user_ans = app.reverseBElements(arr,B);
37+
int[] ans = new int[]{8, 9, 7, 6, 5, 4};
38+
assertTrue(Arrays.equals(user_ans, ans));
39+
}
40+
@Test
41+
public void test_reverse_of_4_elements(){
42+
arr = new int[]{1, 2, 3, 4};
43+
B = 4;
44+
int[] user_ans = app.reverseBElements(arr,B);
45+
int[] ans = new int[]{4, 3, 2, 1};
46+
assertTrue(Arrays.equals(user_ans, ans));
47+
}
48+
@Test
49+
public void test_reverse_of_1_element(){
50+
arr = new int[]{5, 6, 7};
51+
B = 1;
52+
int[] user_ans = app.reverseBElements(arr,B);
53+
int[] ans = new int[]{5, 6, 7};
54+
assertTrue(Arrays.equals(user_ans, ans));
55+
}
56+
@Test
57+
public void test_reverse_of_0_elements(){
58+
arr = new int[]{10, 20, 30, 40, 50};
59+
B = 0;
60+
int[] user_ans = app.reverseBElements(arr,B);
61+
int[] ans = new int[]{10, 20, 30, 40, 50};
62+
assertTrue(Arrays.equals(user_ans, ans));
63+
}
64+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// TODO: Implement the logic using custom queue
2+
public class Exercise {
3+
public int[] reverseBElements(int[] arr, int B) {
4+
// Write your code here
5+
6+
}
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class MyQueue {
2+
3+
public MyQueue(int size){
4+
5+
}
6+
7+
public void insert(int j){
8+
9+
}
10+
11+
public int remove(){
12+
13+
}
14+
15+
public int peekFront(){
16+
17+
}
18+
19+
public boolean isEmpty(){
20+
21+
}
22+
23+
public boolean isFull(){
24+
25+
}
26+
27+
}
28+
29+
/**
30+
* Your MyQueue object will be instantiated and called as such:
31+
* MyQueue queue = new MyQueue(length);
32+
* queue.push(c);
33+
* int parameter_2 = queue.remove();
34+
* int parameter_3 = queue.peekFront();
35+
* boolean parameter_4 = queue.isEmpty();
36+
* boolean parameter_5 = queue.isFull();
37+
*/
38+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public class MyQueue {
2+
3+
private int maxSize; // initializes the set number of slots
4+
private int[] queArray; // slots to main the data
5+
private int front; // this will be the index position for the element in the front
6+
private int rear; // going to be the index position for the element at the back of the line
7+
private int nItems; // counter to maintain the number of items in our queue
8+
9+
public MyQueue(int size){
10+
this.maxSize = size;
11+
this.queArray = new int[size];
12+
front = 0; // index position of the first slot of the array
13+
rear = -1; // there is no item in the array yet to be considered as the last item.
14+
nItems =0; // we don't have elements in the array yet
15+
}
16+
17+
public void insert(int j){
18+
if(rear == maxSize -1){
19+
rear = -1;
20+
}
21+
rear++;
22+
queArray[rear] = j;
23+
nItems ++;
24+
}
25+
26+
public int remove(){ // remove item from the front of the queue
27+
int temp = queArray[front];
28+
front++;
29+
if(front == maxSize){
30+
front = 0; // we can set front back to the 0th index so that we can utilize the entire array again
31+
}
32+
nItems --;
33+
return temp;
34+
}
35+
36+
public int peekFront(){
37+
return queArray[front];
38+
}
39+
40+
public boolean isEmpty(){
41+
return (nItems ==0);
42+
}
43+
44+
public boolean isFull(){
45+
return (nItems == maxSize);
46+
}
47+
48+
public void view(){
49+
System.out.print("[ ");
50+
for(int i = 0; i < queArray.length; i++){
51+
System.out.print(queArray[i]+ " ");
52+
}
53+
System.out.print("]");
54+
}
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Custom Queue Solution
2+
public class Solution {
3+
4+
public int[] reverseBElements(int[] arr, int B) {
5+
MyQueue queue = new MyQueue(B); // Initialize queue of size B
6+
7+
// Insert first B elements into the queue
8+
for (int i = 0; i < B; i++) {
9+
queue.insert(arr[i]);
10+
}
11+
12+
// Remove from queue in reverse order and update array
13+
for (int i = B - 1; i >= 0; i--) {
14+
arr[i] = queue.remove();
15+
}
16+
17+
return arr; // Return updated array
18+
}
19+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Ignore this file: Instructor Code (For Testing) */
2+
3+
import java.util.*;
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import com.udemy.ucp.*;
9+
10+
public class Evaluate {
11+
Exercise app = new Exercise();
12+
int[] arr = null;
13+
int B = 0;
14+
15+
// Test Cases
16+
@Test
17+
public void test_reverse_of_3_elements(){
18+
arr = new int[]{1, 2, 3, 4, 5};
19+
B = 3;
20+
int[] user_ans = app.reverseBElements(arr,B);
21+
int[] ans = new int[]{3, 2, 1, 4, 5};
22+
assertTrue(Arrays.equals(user_ans, ans));
23+
}
24+
@Test
25+
public void test_reverse_of_2_elements_I(){
26+
arr = new int[]{9, 8, 7, 6, 5};
27+
B = 2;
28+
int[] user_ans = app.reverseBElements(arr,B);
29+
int[] ans = new int[]{8, 9, 7, 6, 5};
30+
assertTrue(Arrays.equals(user_ans, ans));
31+
}
32+
@Test
33+
public void test_reverse_of_2_elements_II(){
34+
arr = new int[]{9, 8, 7, 6, 5, 4};
35+
B = 2;
36+
int[] user_ans = app.reverseBElements(arr,B);
37+
int[] ans = new int[]{8, 9, 7, 6, 5, 4};
38+
assertTrue(Arrays.equals(user_ans, ans));
39+
}
40+
@Test
41+
public void test_reverse_of_4_elements(){
42+
arr = new int[]{1, 2, 3, 4};
43+
B = 4;
44+
int[] user_ans = app.reverseBElements(arr,B);
45+
int[] ans = new int[]{4, 3, 2, 1};
46+
assertTrue(Arrays.equals(user_ans, ans));
47+
}
48+
@Test
49+
public void test_reverse_of_1_element(){
50+
arr = new int[]{5, 6, 7};
51+
B = 1;
52+
int[] user_ans = app.reverseBElements(arr,B);
53+
int[] ans = new int[]{5, 6, 7};
54+
assertTrue(Arrays.equals(user_ans, ans));
55+
}
56+
@Test
57+
public void test_reverse_of_0_elements(){
58+
arr = new int[]{10, 20, 30, 40, 50};
59+
B = 0;
60+
int[] user_ans = app.reverseBElements(arr,B);
61+
int[] ans = new int[]{10, 20, 30, 40, 50};
62+
assertTrue(Arrays.equals(user_ans, ans));
63+
}
64+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// TODO: Implement the logic using Collections Framework
2+
import java.util.Queue;
3+
import java.util.LinkedList;
4+
public class Exercise {
5+
public int[] reverseBElements(int[] arr, int B) {
6+
// Write your code here
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Collections Framework Solution
2+
import java.util.Queue;
3+
import java.util.LinkedList;
4+
5+
public class Solution {
6+
7+
public int[] reverseBElements(int[] arr, int B) {
8+
Queue<Integer> queue = new LinkedList<>(); // Initialize a queue using LinkedList
9+
10+
// Insert the first B elements into the queue
11+
for (int i = 0; i < B; i++) {
12+
queue.offer(arr[i]); // Enqueue elements
13+
}
14+
15+
// Remove from the queue in reverse order and update the array
16+
for (int i = B - 1; i >= 0; i--) {
17+
arr[i] = queue.poll(); // Dequeue elements and place them in reverse order
18+
}
19+
20+
return arr; // Return the updated array
21+
}
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Ignore this file: Instructor Code (For Testing) */
2+
3+
import java.util.*;
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import com.udemy.ucp.*;
9+
10+
public class Evaluate {
11+
Exercise app = new Exercise();
12+
13+
// Test Cases
14+
@Test
15+
public void test_max_profit_1(){
16+
int[] input1 = new int[]{7, 1, 5, 3, 6, 4};
17+
assertEquals(app.maxProfit(input1), 5);
18+
}
19+
20+
@Test
21+
public void test_max_profit_2(){
22+
int[] input2 = new int[]{7, 6, 4, 3, 1};
23+
assertEquals(app.maxProfit(input2), 0);
24+
}
25+
26+
@Test
27+
public void test_max_profit_3(){
28+
int[] input2 = new int[]{3, 8, 6, 7, 2};
29+
assertEquals(app.maxProfit(input2), 5);
30+
}
31+
32+
@Test
33+
public void test_max_profit_4(){
34+
int[] input2 = new int[]{1, 2, 3, 4, 5};
35+
assertEquals(app.maxProfit(input2), 4);
36+
}
37+
38+
@Test
39+
public void test_max_profit_5(){
40+
int[] input2 = new int[]{5, 4, 3, 2, 10};
41+
assertEquals(app.maxProfit(input2), 8);
42+
}
43+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Exercise {
2+
public int maxProfit(int[] prices) {
3+
// Write your code here
4+
5+
}
6+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /