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
118 changes: 106 additions & 12 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.util.Map;
import java.util.Set;
import java.util.*;

public class Practice {
/**
Expand All @@ -11,7 +10,12 @@ public class Practice {
* @return the sum of the odd numbers in the array
*/
public static int oddSum(int[] nums) {
return 0;
int sum = 0;
if (nums == null) return 0;
for (int num : nums) {
if (num % 2 != 0) sum += num;
}
return sum;
}

/**
Expand All @@ -26,7 +30,15 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
String shortestWord = "";
for (String word : words) {
if (shortestWord.length() == 0) shortestWord = word;
if (word.length() < shortestWord.length()) shortestWord = word;
if (word.length() == shortestWord.length()) {
if (word.compareTo(shortestWord) < 0) shortestWord = word;
}
}
return shortestWord;
}

/**
Expand All @@ -39,7 +51,11 @@ public static String shortestWord(Set<String> words) {
* @throws NullPointerException if ages is null
*/
public static Set<String> adults(Map<String, Integer> ages) {
return null;
Set<String> results = new HashSet<String>();
for (String name : ages.keySet()){
if (ages.get(name) >= 18) results.add(name);
}
return results;
}

/**
Expand All @@ -50,7 +66,14 @@ 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 biggestNumber = head.data;
ListNode<Integer> current = head;
while (current != null) {
if (current.data > biggestNumber) biggestNumber = current.data;
current = current.next;
}
return biggestNumber;
}

/**
Expand All @@ -67,7 +90,19 @@ 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> results = new HashMap<>();
if (head == null) return results;
ListNode<T> current = head;
while (current != null) {
if (results.containsKey(current.data)) {
results.put(current.data, results.get(current.data) + 1);
} else {
results.put(current.data, 1);
}
current = current.next;
}

return results;
}


Expand All @@ -80,7 +115,8 @@ 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;
return 1 + Math.max(levelCount(root.left), levelCount(root.right));
}


Expand Down Expand Up @@ -108,7 +144,26 @@ 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 levelTrack = level;
Queue<BinaryTreeNode<Integer>> q = new LinkedList<>();
q.add(root);
while (levelTrack > 1) {
int i = q.size();
while (i > 0) {
System.out.println("i" + i);
BinaryTreeNode<Integer> current = q.poll();
if (current.right != null) q.add(current.right);
if (current.left != null) q.add(current.left);
i--;
}
levelTrack--;
System.out.println("new lt " + levelTrack);
}
int sum = 0;
for (BinaryTreeNode<Integer> current : q) sum += current.data;
System.out.println(sum);
return sum;
}


Expand All @@ -123,7 +178,22 @@ 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;
return sumMatchTreeHelper(root) == sumMatchListHelper(head);
}

public static int sumMatchTreeHelper(BinaryTreeNode<Integer> root) {
if (root == null) return 0;
return root.data + sumMatchTreeHelper(root.left) + sumMatchTreeHelper(root.right);
}

public static int sumMatchListHelper(ListNode<Integer> head) {
if (head == null) return 0;
int sum = 0;
while (head != null) {
sum += head.data;
head = head.next;
}
return sum;
}

/**
Expand All @@ -136,7 +206,18 @@ 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<>();
graphSum(start, visited);
int sum = 0;
for (Vertex<Integer> current : visited) sum += current.data;
return sum;
}

public static void graphSum(Vertex<Integer> current, Set<Vertex<Integer>> visited) {
if (current == null || visited.contains(current)) return;
visited.add(current);
for (Vertex<Integer> neighbor : current.neighbors) graphSum(neighbor, visited);
}

/**
Expand All @@ -148,6 +229,19 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
if (start == null) return 0;
Set<Vertex<Integer>> visited = new HashSet<>();
sinkCount(start, visited);
int count = 0;
for (Vertex<Integer> current : visited) {
if (current.neighbors.size() == 0 || current.neighbors == null) count++;
}
return count;
}

public static void sinkCount(Vertex<Integer> current, Set<Vertex<Integer>> visited) {
if (current == null || visited.contains(current)) return;
visited.add(current);
for (Vertex<Integer> neighbor : current.neighbors) sinkCount(neighbor, visited);
}
}