Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.debug.settings.onBuildFailureProceed": true
}
129 changes: 120 additions & 9 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class Practice {
Expand All @@ -11,7 +15,12 @@ public class Practice {
* @return the sum of the odd numbers in the array
*/
public static int oddSum(int[] nums) {
return 0;
if (nums==null||nums.length==0) return 0;
int sum = 0;
for (int i: nums) {
if (i%2==1||i%2==-1) sum+= i;
}
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;
if (words==null) throw new NullPointerException();
if (words.size()==0) throw new IllegalArgumentException();
int shortestlength = Integer.MAX_VALUE;
String shortest = "";
for (String word : words) {
if (word.length()<shortestlength) {
shortest = word;
shortestlength = word.length();
}
if (word.length()==shortestlength) {
if(word.compareTo(shortest)<0) shortest = word;
}
}
return shortest;
}

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

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

/**
Expand All @@ -67,7 +100,18 @@ 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 Map.of();
Map<T, Integer> frequency = new HashMap<>();
while(head!=null) {
if (frequency.containsKey(head.data)) {
frequency.put(head.data, frequency.get(head.data)+1);
}
else {
frequency.put(head.data, 1);
}
head=head.next;
}
return frequency;
}


Expand All @@ -80,7 +124,10 @@ 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,6 +155,33 @@ 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) {
if (root==null) return 0;
Queue<BinaryTreeNode<Integer>> queue = new LinkedList<>();
queue.offer(root);
int currlevel = 1;
int levelsum = 0;
while (!queue.isEmpty()) {
int levelsize = queue.size();
if (currlevel == level) {
for (int i = 0; i < levelsize; i++) {
BinaryTreeNode<Integer> node = queue.poll();
levelsum += node.data;
}
return levelsum;
}
else {
for (int j = 0; j < levelsize; j++) {
BinaryTreeNode<Integer> node = queue.poll();
if (node.left!=null) {
queue.offer(node.left);
}
if (node.right!=null) {
queue.offer(node.right);
}
}
currlevel++;
}
}
return 0;
}

Expand All @@ -123,7 +197,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;
if (root==null&&head==null) return true;
if (root==null||head==null) return false;
Queue<BinaryTreeNode<Integer>> queue = new LinkedList<>();
int treesum = 0;
queue.offer(root);
while (!queue.isEmpty()) {
BinaryTreeNode<Integer> node = queue.poll();
if (node.left!=null) queue.offer(node.left);
if (node.right!=null) queue.offer(node.right);
treesum+=node.data;
}
int listsum = 0;
while(head!=null) {
listsum+= head.data;
head=head.next;
}
return treesum==listsum;
}

/**
Expand All @@ -136,7 +226,17 @@ 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>> visited = new HashSet<Vertex<Integer>>();
return graphSumHelper(start, visited);
}
public static int graphSumHelper(Vertex<Integer> start, Set<Vertex<Integer>> visited) {
if (start==null||visited.contains(start)) return 0;
visited.add(start);
int sum = start.data;
for (Vertex<Integer> neighbour : start.neighbors) {
sum += graphSumHelper(neighbour, visited);
}
return sum;
}

/**
Expand All @@ -148,6 +248,17 @@ 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>> visited = new HashSet<>();
return sinkCountHelper(start, visited);
}
public static int sinkCountHelper(Vertex<Integer> start, Set<Vertex<Integer>> visited) {
if (start==null||visited.contains(start)) return 0;
int sinkcount = 0;
visited.add(start);
if (start.neighbors.size()==0) sinkcount++;
for (Vertex<Integer> neighbour : start.neighbors) {
sinkcount += sinkCountHelper(neighbour, visited);
}
return sinkcount;
}
}