Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 244 additions & 10 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class Practice {
Expand All @@ -11,7 +16,19 @@ public class Practice {
* @return the sum of the odd numbers in the array
*/
public static int oddSum(int[] nums) {
return 0;
if (nums == null) {
return 0;
}

int sum = 0;

for (int num:nums) {
if (num % 2 == 1 || num % 2 == -1) {
sum += num;
}
}

return sum;
}

/**
Expand All @@ -26,7 +43,39 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
if (words == null) {
throw new NullPointerException();
}

if (words.isEmpty()) {
throw new IllegalArgumentException();
}

int shortestCount = Integer.MAX_VALUE;
String shortestWord = "";

for (String word : words) {
if (word.length() < shortestCount) {
shortestCount = word.length();
shortestWord = word;
}
else if (word.length() == shortestCount) {
for (int i = 0; i < shortestCount; i++) {
char first = word.charAt(i);
char second = shortestWord.charAt(i);

if (first < second) {
shortestWord = word;
break;
}
else if (first > second) {
break;
}
}
}
}

return shortestWord;
}

/**
Expand All @@ -39,7 +88,19 @@ public static String shortestWord(Set<String> words) {
* @throws NullPointerException if ages is null
*/
public static Set<String> adults(Map<String, Integer> ages) {
return null;
if (ages == null) {
throw new NullPointerException();
}

Set<String> adults = new HashSet<>();

for (String name : ages.keySet()) {
if (ages.get(name) >= 18) {
adults.add(name);
}
}

return adults;
}

/**
Expand All @@ -50,7 +111,22 @@ public static Set<String> adults(Map<String, Integer> ages) {
* @throws IllegalArgumentException if head is null
*/
public static int biggestNumber(ListNode<Integer> head) {
return 0;
if (head == null) {
throw new IllegalArgumentException();
}

int biggestNum = Integer.MIN_VALUE;

ListNode<Integer> curr = head;

while (curr != null) {
if (curr.data > biggestNum) {
biggestNum = curr.data;
}
curr = curr.next;
}

return biggestNum;
}

/**
Expand All @@ -67,7 +143,21 @@ public static int biggestNumber(ListNode<Integer> head) {
* @return a frequency map of values in the list
*/
public static <T> Map<T, Integer> frequencies(ListNode<T> head) {
return null;
Map<T, Integer> frequency = new HashMap<>();

ListNode<T> curr = head;

while (curr != null) {
if (frequency.containsKey(curr.data)) {
frequency.put(curr.data, frequency.get(curr.data) + 1);
}
else{
frequency.put(curr.data, 1);
}
curr = curr.next;
}

return frequency;
}


Expand All @@ -80,7 +170,38 @@ public static <T> Map<T, Integer> frequencies(ListNode<T> head) {
* @return the number of levels in the tree
*/
public static int levelCount(BinaryTreeNode<?> root) {
return 0;
if (root == null) {
return 0;
}

int level = 0;

ArrayList<BinaryTreeNode<?>> arr = new ArrayList<>();
Queue<ArrayList<BinaryTreeNode<?>>> queue = new LinkedList<>();

arr.add(root);
queue.offer(arr);

while(!queue.isEmpty()) {
ArrayList<BinaryTreeNode<?>> curr = queue.poll();
ArrayList<BinaryTreeNode<?>> next = new ArrayList<>();

for (BinaryTreeNode<?> node : curr) {
if (node.left != null) {
next.add(node.left);
}
if (node.right != null) {
next.add(node.right);
}
}

if (!next.isEmpty()) {
queue.offer(next);
}
level += 1;
}

return level;
}


Expand Down Expand Up @@ -108,7 +229,52 @@ public static int levelCount(BinaryTreeNode<?> root) {
* @return the sum of the nodes at the given level
*/
public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) {
return 0;
if (root == null || level <= 0) {
return 0;
}

int currLevel = 1;

ArrayList<BinaryTreeNode<Integer>> arr = new ArrayList<>();
Queue<ArrayList<BinaryTreeNode<Integer>>> queue = new LinkedList<>();

arr.add(root);
queue.offer(arr);

while (!queue.isEmpty()) {
if (currLevel == level) {
break;
}

ArrayList<BinaryTreeNode<Integer>> curr = queue.poll();
ArrayList<BinaryTreeNode<Integer>> next = new ArrayList<>();

for (BinaryTreeNode<Integer> node : curr) {
if (node.left != null) {
next.add(node.left);
}
if (node.right != null) {
next.add(node.right);
}
}

if (!next.isEmpty()) {
queue.offer(next);
}
currLevel += 1;
}

if (currLevel < level) {
return 0;
}

int sum = 0;
ArrayList<BinaryTreeNode<Integer>> curr = queue.poll();
for (BinaryTreeNode<Integer> node : curr) {
sum += node.data;
}

return sum;
}


Expand All @@ -123,7 +289,35 @@ public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) {
* @return true if the sums are equal, false otherwise
*/
public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> head) {
return false;
int tree = treehelper(root);
int linked = listnodeHelper(head);

if (tree == linked) {
return true;
}
else {
return false;
}
}

public static int treehelper(BinaryTreeNode<Integer> root) {
if (root == null) {
return 0;
}

return root.data + treehelper(root.left) + treehelper(root.right);
}

public static int listnodeHelper(ListNode<Integer> head) {
int sum = 0;

ListNode<Integer> curr = head;

while (curr != null) {
sum += curr.data;
curr = curr.next;
}
return sum;
}

/**
Expand All @@ -136,7 +330,27 @@ public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> h
* @return the sum of all the vertices
*/
public static int graphSum(Vertex<Integer> start) {
return 0;
if (start == null) {
return 0;
}
Set<Vertex<Integer>> visited = new HashSet<>();
return graphHelper(start, visited);
}

public static int graphHelper(Vertex<Integer> start, Set<Vertex<Integer>> visited) {
if (start == null || visited.contains(start)) {
return 0;
}

visited.add(start);
int sum = 0;
sum += start.data;

for (Vertex<Integer> neighbor : start.neighbors) {
sum += graphHelper(neighbor, visited);
}

return sum;
}

/**
Expand All @@ -148,6 +362,26 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
Set<Vertex<Integer>> visited = new HashSet<>();
return sinkCountHelper(start, visited);
}

public static int sinkCountHelper(Vertex<Integer> start, Set<Vertex<Integer>> visited) {
if (start == null || visited.contains(start)) {
return 0;
}

visited.add(start);
int outdegree = 0;

if (start.neighbors.size() == 0) {
outdegree += 1;
}

for (Vertex<Integer> neighbor : start.neighbors) {
outdegree += sinkCountHelper(neighbor, visited);
}

return outdegree;
}
}