Skip to content
228 changes: 213 additions & 15 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,21 @@ 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 i = 0; i < nums.length; i++)
{
if(nums[i] % 2 != 0)
{
sum += nums[i];
}
}
return sum;
}

/**
Expand All @@ -26,7 +39,33 @@ 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("words is null");
if(words.isEmpty())
throw new IllegalArgumentException("words is empty");

String smallest = "";
for(String word : words) //to get a word for starting length is not 0
{
smallest = word;
break;
}

for(String word : words)
{
if(word.length() < smallest.length())
{
smallest = word;
}
else if(smallest.length() == word.length())
{
if(smallest.compareTo(word) > 0)
{
smallest = word;
}
}
}
return smallest;
}

/**
Expand All @@ -39,7 +78,21 @@ 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("map is null");
}
Set<String> names = new HashSet<>();

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

return names;
}

/**
Expand All @@ -50,7 +103,24 @@ 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("head is null");
}

ListNode<Integer> current = head;
int biggest = head.data;

while(current.next != null)
{
if(current.data > biggest)
{
biggest = current.data;
}
current = current.next;
}

return biggest;
}

/**
Expand All @@ -67,9 +137,30 @@ 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;
}
//go through list
//add each new value from list as key in map with a value of 1
//if a value from list appears again increase the value for said key in map
//return the map

Map<T, Integer> frequencyMap = new HashMap<>();
ListNode<T> current = head;

while(current != null)
{
if(!frequencyMap.containsKey(current.data))
{
frequencyMap.put(current.data, 1);
}
else
{
frequencyMap.put(current.data, frequencyMap.get(current.data)+1);
}

current = current.next;
}

return frequencyMap;
}

/**
* Returns the number of levels in the tree.
Expand All @@ -80,9 +171,23 @@ 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);

if(left > right)
{
return left + 1;
}
else
{
return right + 1;
}
}

/**
* Returns the sum at a specified level in a binary tree.
Expand All @@ -108,9 +213,21 @@ 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 < 1)
{
return 0;
}

if(level == 1)
{
return root.data;
}

int left = sumAtLevel(root.left, level - 1);
int right = sumAtLevel(root.right, level - 1);

return left + right;
}

/**
* Returns true if the sum of the values in a given tree is equal to the sum
Expand All @@ -123,7 +240,37 @@ 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;
if(root == null && head == null)
{
return true;
}
else if((root == null && head != null) || (root != null && head == null))
{
return false;
}

int treeSum = treeSum(root);

int listSum = 0;
ListNode<Integer> current = head;

while(current != null)
{
listSum += current.data;
current = current.next;
}

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

int sum = root.data + treeSum(root.left) + treeSum(root.right);
return sum;
}

/**
Expand All @@ -136,7 +283,29 @@ 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 sumGraph(start, visited);
}
public static int sumGraph(Vertex<Integer> vert, Set<Vertex<Integer>> visited)
{
if(visited.contains(vert))
{
return 0;
}

visited.add(vert);
int sum = vert.data;

for(Vertex<Integer> n : vert.neighbors)
{
sum += sumGraph(n, visited);
}

return sum;
}

/**
Expand All @@ -148,6 +317,35 @@ 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<>();
return countOutDegrees(start, visited);
}
public static int countOutDegrees(Vertex<Integer> vert, Set<Vertex<Integer>> visited)
{
if(visited.contains(vert))
{
return 0;
}

visited.add(vert);

if(vert.neighbors == null || vert.neighbors.isEmpty()) //base case no neighbors
{
return 1;
}

int count = 0;

for(Vertex<Integer> n : vert.neighbors)
{
count += countOutDegrees(n, visited);
}

return count;
}
}