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
126 changes: 117 additions & 9 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

Expand All @@ -11,7 +13,14 @@ 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 sum;
for(int num : nums){
if(num%2 == 1 || num%2 == -1){
sum += num;
}
}
return sum;
}

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

/**
Expand All @@ -39,7 +61,13 @@ 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> returnable = new HashSet<>();
for(String name : ages.keySet()){
if(ages.get(name) >= 18){
returnable.add(name);
}
}
return returnable;
}

/**
Expand All @@ -50,7 +78,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;
int kingNum = Integer.MIN_VALUE;
while(head != null){
if(head.data > kingNum){
kingNum = head.data;
}
head = head.next;
}
return kingNum;
}

/**
Expand All @@ -67,7 +102,17 @@ 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> returnable = new HashMap<>();
while(head != null){
if(!returnable.containsKey(head.data)){
returnable.put(head.data, 1);
}
else{
returnable.replace(head.data, returnable.get(head.data)+1);
}
head = head.next;
}
return returnable;
}


Expand All @@ -80,7 +125,14 @@ 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;
return levelCountHelper(root, 0);
}

public static int levelCountHelper(BinaryTreeNode<?> root, int level) {
if (root == null) return level;
int leftLevel = levelCountHelper(root.left, level + 1);
int rightLevel = levelCountHelper(root.right, level + 1);
return Math.max(leftLevel, rightLevel);
}


Expand Down Expand Up @@ -108,7 +160,13 @@ 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;
return sumAtLevelHelper(root, level);
}

public static int sumAtLevelHelper(BinaryTreeNode<Integer> root, int level) {
if (root == null) return 0;
if(level == 1) return root.data;
return sumAtLevelHelper(root.left, level - 1) + sumAtLevelHelper(root.right, level - 1);
}


Expand All @@ -123,9 +181,28 @@ 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) {
int sumList = sumMatchList(head);
int sumTree = sumMatchTree(root);
if(sumList == sumTree) return true;
return false;
}

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

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

/**
* Returns the sum of all the vertices in a graph that are reachable from a given
* starting vertex.
Expand All @@ -136,7 +213,21 @@ 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;
int sum = 0;
Set<Vertex<Integer>> visited = new HashSet<>();
graphSumHelper(start, visited);
for(Vertex<Integer> cur : visited){
sum+= cur.data;
}
return sum;
}
public static void graphSumHelper(Vertex<Integer> start, Set<Vertex<Integer>> visited){
if (start == null || visited.contains(start)) return;
visited.add(start);
for(Vertex<Integer> neighbor : start.neighbors){
graphSumHelper(neighbor, visited);
}
}

/**
Expand All @@ -148,6 +239,23 @@ 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;
int sum = 0;
Set<Vertex<Integer>> visited = new HashSet<>();
sinkCountHelper(start, visited);
for(Vertex<Integer> cur : visited){
if(cur.neighbors.size() == 0 || cur.neighbors == null){
sum++;
}
}
return sum;
}

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