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
137 changes: 127 additions & 10 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,17 @@ 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 count = 0;


for(int i = 0; i < nums.length; i++){
if(nums[i] % 2 != 0){
count += nums[i];
}
}
return count;

}

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


String shortest = null;


for (String word : words) {
if(shortest == null) shortest = word;
else if(word.length() < shortest.length()) shortest = word;
else if(word.length() == shortest.length()){
if(word.compareTo(shortest) < 0) shortest = word;
}
}


return shortest;
}

/**
Expand All @@ -39,7 +67,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;
if(ages == null) throw new NullPointerException();
Set<String> set = new HashSet<>();

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

/**
Expand All @@ -50,7 +84,16 @@ 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();

ListNode<Integer> temp = head;
int max = Integer.MIN_VALUE;

while(temp!= null){
if(temp.data > max) max = temp.data;
temp = temp.next;
}
return max;
}

/**
Expand All @@ -67,7 +110,22 @@ 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> map = new HashMap<>();
if(head == null) return map;

ListNode<T> temp = head;

while(temp != null){
if(!map.containsKey(temp.data)){
map.put(temp.data, map.getOrDefault(temp.data, 0)+1);
}else{
map.put(temp.data, map.getOrDefault(temp.data, 0)+1);
}

temp = temp.next;
}

return map;
}


Expand All @@ -80,7 +138,12 @@ 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 left = levelCount(root.left);
int right = levelCount(root.right);

return Math.max(left, right) + 1;
}


Expand Down Expand Up @@ -108,7 +171,15 @@ 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 sumAtLevel(root, level,1);
}

private static int sumAtLevel(BinaryTreeNode<Integer> root, int level, int curLvl){
if(root == null) return 0;

if(curLvl == level) return root.data;

return sumAtLevel(root.left, level, curLvl+1) + sumAtLevel(root.right, level, curLvl+1);
}


Expand All @@ -123,7 +194,23 @@ 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 sumTree = sumOfTree(root);
int sumList = 0;

ListNode<Integer> temp = head;
while(temp != null){
sumList += temp.data;
temp = temp.next;
}

return sumList == sumTree;
}

private static int sumOfTree(BinaryTreeNode<Integer> root){
if(root == null) return 0;
int sum = sumOfTree(root.left) + sumOfTree(root.right) + root.data;

return sum;
}

/**
Expand All @@ -136,7 +223,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;
Set<Vertex<Integer>> visited = new HashSet<>();
return graphSum(start, visited);
}

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

visited.add(start);
int total = start.data;

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

return total;
}

/**
Expand All @@ -148,6 +249,22 @@ 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 sinkCount(start, visited);
}

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

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

if(start.neighbors.isEmpty()) sink++;

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

return sink;
}
}