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

Expand All @@ -11,7 +15,18 @@ 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 != 0)
{
sum += num;
}
}

return sum;
}

/**
Expand All @@ -26,7 +41,25 @@ 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.size() == 0) throw new IllegalArgumentException();

ArrayList<String> list = new ArrayList<>();
list.addAll(words);
Collections.sort(list);

String shortestWord = list.get(0);

for (String word : words)
{
if (word.length() < shortestWord.length())
{
shortestWord = word;
}
}

return shortestWord;
}

/**
Expand All @@ -39,7 +72,20 @@ 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> mySet = new HashSet<>();

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

return mySet;
}

/**
Expand All @@ -50,7 +96,21 @@ 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> current = head;
int biggest = current.data;

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

/**
Expand All @@ -67,7 +127,25 @@ 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;

if (head == null) return new HashMap<>();

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

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

return valuesMap;
}


Expand All @@ -80,7 +158,22 @@ 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;

int levels = 0;

if (root == null) return 0;

if (root.left == null && root.right == null)
{
return 1;
}

int leftLevels = levelCount(root.left);
int rightLevels = levelCount(root.right);

levels = Math.max(leftLevels, rightLevels);

return 1 + levels;
}


Expand Down Expand Up @@ -108,9 +201,20 @@ 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) return 0;

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

int sum = 0;

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

sum = leftSum + rightSum;

return sum;
}

/**
* Returns true if the sum of the values in a given tree is equal to the sum
Expand All @@ -123,9 +227,38 @@ 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) {

if (sumLinkedList(head) == sumTree(root)) return true;

return false;
}

public static int sumLinkedList(ListNode<Integer> head)
{
int total = 0;
ListNode<Integer> current = head;

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

return total;
}

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

int sumLeft = sumTree(root.left);
int sumRight = sumTree(root.right);

int total = root.data + sumLeft + sumRight;

return total;
}

/**
* Returns the sum of all the vertices in a graph that are reachable from a given
* starting vertex.
Expand All @@ -136,7 +269,24 @@ 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;
HashSet<Vertex<Integer>> myVisited = new HashSet<>();

return graphSumHelper(start, myVisited);
}

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

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

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

return total;
}

/**
Expand All @@ -148,6 +298,28 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
HashSet<Vertex<Integer>> myVisited = new HashSet<>();

return sinkCountHelper(start, myVisited);
}

public static int sinkCountHelper(Vertex<Integer> vertex, Set<Vertex<Integer>> visited) {

if (vertex == null || visited.contains(vertex)) return 0;

int sinkCount = 0;
visited.add(vertex);

if (vertex.neighbors.size() == 0)
{
sinkCount += 1;
}

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

return sinkCount;
}
}