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

Expand All @@ -11,7 +14,16 @@ 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 +38,31 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
if(words.isEmpty()){
throw new IllegalArgumentException();
}
// unsure how to do this without a long word at the start
String shortWord = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
for (String string : words) {
if(string == null){
throw new NullPointerException();
}
if(string.length() < shortWord.length()){
shortWord = string;
}
else if(string.length() == shortWord.length()) {
for (int i = 0; i < string.length(); i++) {
if(string.charAt(i) != shortWord.charAt(i)){
if((int) string.charAt(i) < (int) shortWord.charAt(i)){
shortWord = string;
}
break;
}
continue;
}
}
}
return shortWord;
}

/**
Expand All @@ -39,6 +75,10 @@ public static String shortestWord(Set<String> words) {
* @throws NullPointerException if ages is null
*/
public static Set<String> adults(Map<String, Integer> ages) {
if(ages.isEmpty()) {
throw new NullPointerException();
}

return null;
}

Expand All @@ -50,7 +90,18 @@ 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 || head.data == null){
throw new IllegalArgumentException();
}
int largest = head.data;
int prevLarge = Integer.MIN_VALUE;
if(head.next != null){
prevLarge = biggestNumber(head.next);
}
if(largest < prevLarge){
largest = prevLarge;
}
return largest;
}

/**
Expand All @@ -67,7 +118,19 @@ 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> frequency = new HashMap<>();
frequenciesHelper(head, frequency);
return frequency;
}
public static <T> void frequenciesHelper(ListNode<T> head, Map<T, Integer> frequency) {
if(head == null) return;
if(!frequency.containsKey(head.data)) {
frequency.put(head.data, 1);
}
else{
frequency.put(head.data, frequency.get(head.data) + 1);
}
frequenciesHelper(head.next, frequency);
}


Expand Down Expand Up @@ -136,7 +199,25 @@ 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>> used = new HashSet<>();
if(start == null) return 0;
int sum = start.data;
used.add(start);
// maybe error if .neighbors is empty
sum = graphSumHelper(start.neighbors, sum, used);
return sum;
}
public static int graphSumHelper(List<Vertex<Integer>> nums, int sum, Set<Vertex<Integer>> used) {
for (Vertex<Integer> num : nums) {
if(!used.contains(num)){
sum += num.data;
used.add(num);
// resets sum
// could condense but -_-
sum += graphSumHelper(num.neighbors, 0, used);
}
}
return sum;
}

/**
Expand All @@ -148,6 +229,25 @@ 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>> used = new HashSet<>();
used.add(start);
int total = sinkCountHelper(start.neighbors, used);
return total;
}
public static int sinkCountHelper(List<Vertex<Integer>> nums, Set<Vertex<Integer>> used) {
int total = 0;
// Have to use is empty
// doesn't recognize == null (IDK why)
if(nums.isEmpty()){
total = 1;
}
for (Vertex<Integer> vertex : nums) {
if(!used.contains(vertex)){
used.add(vertex);
total += sinkCountHelper(vertex.neighbors, used);
}
}
return total;
}
}