diff --git a/README.md b/README.md new file mode 100644 index 0000000..58e26d3 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +## Coderbyte challenges in Java. + +This repository contains a collection of code challenges from Coderbyte.com. + +Solutions are grouped into three difficulty levels: + +- easy +- medium +- hard diff --git a/src/easy/LetterCapitalize.java b/src/easy/LetterCapitalize.java new file mode 100644 index 0000000..ccf0338 --- /dev/null +++ b/src/easy/LetterCapitalize.java @@ -0,0 +1,37 @@ +package easy; + +/** + * Have the function LetterCapitalize(str) take the str parameter + * being passed and capitalize the first letter of each word. + * Words will be separated by only one space. + */ +public class LetterCapitalize { + + /** + * Letter Capitalize function. + * + * @param str input string + * @return string with the letters capitalised + */ + private static String letterCapitalize(String str) { + String[] splitWords = str.split(" "); + for (int i = 0; i < splitWords.length; i++) { + String word = splitWords[i]; + splitWords[i] = word.substring(0, 1).toUpperCase() + word.substring(1); + } + return String.join(" ", splitWords); + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = letterCapitalize("The soul becomes dyed with the color of its thoughts."); + System.out.println(result1); + var result2 = letterCapitalize("The universe is change; our life is what our thoughts make it."); + System.out.println(result2); + } + +} diff --git a/src/easy/LetterChanges.java b/src/easy/LetterChanges.java new file mode 100644 index 0000000..3c86ee0 --- /dev/null +++ b/src/easy/LetterChanges.java @@ -0,0 +1,54 @@ +package easy; + +/** + * Have the function LetterChanges(str) take the str parameter + * being passed and modify it using the following algorithm. + * Replace every letter in the string with the letter + * following it in the alphabet (i.e. c becomes d, z becomes a). + * Then capitalize every vowel in this new string (a-e-i-o-u) + * and finally return this modified string. + */ +public class LetterChanges { + + + /** + * Letter Changes function. + * + * @param str input string + * @return modified string + */ + private static String letterChanges(String str) { + + char[] alphabet = {'b', 'c', 'd', 'E', 'f', 'g', 'h', 'I', 'j', 'k', 'l', + 'm', 'n', 'O', 'p', 'q', 'r', 's', 't', 'U', 'v', 'w', 'x', 'y', 'z', 'A'}; + char[] charArray = str.toLowerCase().toCharArray(); + StringBuilder output = new StringBuilder(); + + for (int i = 0; i < str.length(); i++) { + char letter = str.charAt(i); + boolean isLetter = letter >= 'a' && letter <= 'z'; + if (isLetter) { + output.append(alphabet[charArray[i] - 97]); + } else { + output.append(letter); + } + } + + return output.toString(); + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = letterChanges("anthology"); + System.out.println(result1); + var result2 = letterChanges("equilibrium"); + System.out.println(result2); + var result3 = letterChanges("oxford"); + System.out.println(result3); + } + +} diff --git a/src/easy/MeanMode.java b/src/easy/MeanMode.java new file mode 100644 index 0000000..8d83e4d --- /dev/null +++ b/src/easy/MeanMode.java @@ -0,0 +1,65 @@ +package easy; + +import java.util.HashMap; +import java.util.Map; + +/** + * Have the function MeanMode(arr) take the array of numbers stored in arr + * and return 1 if the mode equals the mean, 0 if they don't equal each other + * (i.e. [5, 3, 3, 3, 1] should return 1 because the mode (3) equals the mean (3)). + * The array will not be empty, will only contain positive integers, + * and will not contain more than one mode. + */ +public class MeanMode { + + /** + * Mean Mode function. + * + * @param arr input array. + * @return 1 if the mode equals the mean, 0 if they don't equal each other + */ + private static String meanMode(int[] arr) { + int sum = 0; + int modeKey = 0; + int modeVal = 0; + + Map modeMap = new HashMap<>(); + for (int item : arr) { + modeMap.put(item, 0); + } + + for (int value : arr) { + sum += value; + int val = modeMap.get(value); + if (val > 0) { + modeMap.put(value, val + 1); + } else { + modeMap.put(value, 1); + } + } + + for (Map.Entry item : modeMap.entrySet()) { + int itemKey = item.getKey(); + int itemVal = item.getValue(); + if (itemVal > modeVal) { + modeVal = itemVal; + modeKey = itemKey; + } + } + + return modeKey == (sum / arr.length) ? "1" : "0"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = meanMode(new int[]{5, 3, 3, 3, 1}); + System.out.println(result1); + var result2 = meanMode(new int[]{64, 64, 64, 64, 64, 64, 64, 64, 1024}); + System.out.println(result2); + } + +} diff --git a/src/easy/MultiplicativePersistence.java b/src/easy/MultiplicativePersistence.java new file mode 100644 index 0000000..50b29a0 --- /dev/null +++ b/src/easy/MultiplicativePersistence.java @@ -0,0 +1,48 @@ +package easy; + +/** + * Have the function MultiplicativePersistence(num) take the num parameter being passed + * which will always be a positive integer + * and return its multiplicative persistence which is + * the number of times you must multiply the digits in num until you reach a single digit. + * --- + * For example: if num is 39 then your program + * should return 3 because 3 * 9 = 27 then 2 * 7 = 14 + * and finally 1 * 4 = 4 then you stop at 4. + */ +public class MultiplicativePersistence { + + /** + * Multiplicative Persistence function. + * + * @param num input number + * @return the number of times you must multiply + */ + private static int multiplicativePersistence(int num) { + int times = 0; + int multiplied = num; + while (multiplied > 9) { + int product = 1; + String[] intArr = Integer.toString(multiplied).split(""); + for (String i : intArr) { + product *= Integer.parseInt(i); + } + multiplied = product; + times++; + } + return times; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = multiplicativePersistence(2677889); + System.out.println(result1); + var result2 = multiplicativePersistence(8192); + System.out.println(result2); + } + +} diff --git a/src/easy/NonrepeatingCharacter.java b/src/easy/NonrepeatingCharacter.java new file mode 100644 index 0000000..0c6b212 --- /dev/null +++ b/src/easy/NonrepeatingCharacter.java @@ -0,0 +1,54 @@ +package easy; + +import java.util.HashMap; + +/** + * Have the function NonrepeatingCharacter(str) + * take the str parameter being passed, + * which will contain only alphabetic characters and spaces, + * and return the first non-repeating character. + * --- + * For example: if str is "agettkgaeee" then your program should return k. + * The string will always contain at least one character and there will + * always be at least one non-repeating character. + */ +public class NonrepeatingCharacter { + + /** + * Non-repeating Character function. + * + * @param str input string + * @return the first non-repeating character + */ + private static String nonrepeatingCharacter(String str) { + + char[] charArr = str.toLowerCase().toCharArray(); + HashMap freq = new HashMap<>(); + + for (int c : charArr) { + Integer count = freq.get(c); + freq.put(c, count == null ? 1 : ++count); + } + + for (int c : charArr) { + Integer count = freq.get(c); + if (count == 1) { + return String.valueOf((char) c); + } + } + return "false"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var res1 = nonrepeatingCharacter("Beauty in things exists in the mind which contemplates them"); + System.out.println(res1); + var res2 = nonrepeatingCharacter("A wise man apportions his beliefs to the evidence"); + System.out.println(res2); + } + +} diff --git a/src/easy/NumberAddition.java b/src/easy/NumberAddition.java new file mode 100644 index 0000000..36f0e15 --- /dev/null +++ b/src/easy/NumberAddition.java @@ -0,0 +1,46 @@ +package easy; + +/** + * Have the function NumberSearch(str) take the str parameter, + * search for all the numbers in the string, add them together, + * then return that final number. + * --- + * For example: if str is "88Hello 3World!" the output should be 91. + * You will have to differentiate between single digit numbers + * and multiple digit numbers like in the example above. + * So "55Hello" and "5Hello 5" should return two different answers. + * Each string will contain at least one letter or symbol. + */ +public class NumberAddition { + + /** + * Number Addition function. + * + * @param str input string + * @return the final number + */ + private static int numberAddition(String str) { + String cleaned = str.replaceAll("[^0-9]", " "); + String[] splitNum = cleaned.split(" +"); + int sum = 0; + for (String c : splitNum) { + if (!c.equals("")) { + sum += Integer.parseInt(c); + } + } + return sum; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = numberAddition("Chillhouse Mix 2 (2001)"); + System.out.println(result1); + var result2 = numberAddition("Cafe del Mar 5 (1998)"); + System.out.println(result2); + } + +} diff --git a/src/easy/Palindrome.java b/src/easy/Palindrome.java new file mode 100644 index 0000000..3de6759 --- /dev/null +++ b/src/easy/Palindrome.java @@ -0,0 +1,62 @@ +package easy; + +/** + * Have the function Palindrome(str) take the str parameter + * being passed and return the string true + * if the parameter is a palindrome, + * (the string is the same forward as it is backward) + * otherwise return the string false. + * For example: "racecar" is also "racecar" backwards. + * Punctuation and numbers will not be part of the string. + */ +public class Palindrome { + + /** + * Palindrome function. + * + * @param str input string + * @return "true" is the string is a palindrome + */ + private static String palindrome(String str) { + StringBuilder reversed = new StringBuilder(); + String cleaned = str.replaceAll(" ", ""); + reversed.append(cleaned).reverse(); + return cleaned.equals(reversed.toString()) && str.length() > 0 ? "true" : "false"; + } + + /** + * An improved function checking if a given string is a palindrome. + * It compares two halves of a string, checking if a letter + * from the first half matches the other half in the reverse order. + * + * @param str input string + * @return true if the string is a palindrome + */ + private static boolean isPalindrome(String str) { + char[] strArr = str.toCharArray(); + int len = strArr.length; + for (int i = 0; i < len / 2; i++) { + if (strArr[i] != strArr[len - i - 1]) { + return false; + } + } + return true; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = palindrome("dont nod"); + System.out.println(result1); + var result2 = isPalindrome("dont nod"); + System.out.println(result2); + var result3 = palindrome("rats live on no evil star"); + System.out.println(result3); + var result4 = isPalindrome("rats live on no evil star"); + System.out.println(result4); + } + +} diff --git a/src/easy/PalindromeCreator.java b/src/easy/PalindromeCreator.java new file mode 100644 index 0000000..53bf456 --- /dev/null +++ b/src/easy/PalindromeCreator.java @@ -0,0 +1,94 @@ +package easy; + +/** + * Have the function PalindromeCreator(str) take the str parameter being passed + * and determine if it is possible to create a palindromic string + * of minimum length 3 characters by removing 1 or 2 characters. + * --- + * For example: if str is "abjchba" then you can remove the characters jc to produce "abhba" + * which is a palindrome. For this example your program should return the two characters + * that were removed with no delimiter and in the order they appear in the string, so jc. + * --- + * If 1 or 2 characters cannot be removed to produce a palindrome, + * then return the string not possible. If the input string is already a palindrome, + * your program should return the string palindrome. + * --- + * The input will only contain lowercase alphabetic characters. + * Your program should always attempt to create the longest palindromic substring + * by removing 1 or 2 characters (see second sample test case as an example). + * The 2 characters you remove do not have to be adjacent in the string. + */ +public class PalindromeCreator { + + /** + * A support function checking if a given string is a palindrome. + * + * @param str input string + * @return true if the string is a palindrome + */ + private static boolean isPalindrome(String str) { + char[] strArr = str.toCharArray(); + int len = strArr.length; + for (int i = 0; i < len / 2; i++) { + if (strArr[i] != strArr[len - i - 1]) { + return false; + } + } + return true; + } + + /** + * Palindrome Creator function. + * + * @param str input string + * @return characters to be removed or "palindrome" or "not possible" + */ + private static String palindromeCreator(String str) { + if (isPalindrome(str)) { + return "palindrome"; + } + + for (int i = 0; i < str.length(); i++) { + StringBuilder combo = new StringBuilder(); + for (int k = 0; k < str.length(); k++) { + if (k != i) { + combo.append(str.charAt(k)); + } + } + if (isPalindrome(combo.toString()) && combo.length() >= 3) { + return String.valueOf(str.charAt(i)); + } + } + + for (int i = 0; i < str.length(); i++) { + for (int j = i; j < str.length(); j++) { + StringBuilder combo = new StringBuilder(); + for (int k = 0; k < str.length(); k++) { + if (k != i && k != j) { + combo.append(str.charAt(k)); + } + } + if (isPalindrome(combo.toString()) && combo.length() >= 3) { + return String.valueOf(str.charAt(i)) + str.charAt(j); + } + } + } + + return "not possible"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result2 = palindromeCreator("racecar"); + System.out.println(result2); + var result1 = palindromeCreator("vhhgghhgghhk"); + System.out.println(result1); + var result3 = palindromeCreator("rats live on no evil stars"); + System.out.println(result3); + } + +} diff --git a/src/easy/PowersOfTwo.java b/src/easy/PowersOfTwo.java new file mode 100644 index 0000000..89acb00 --- /dev/null +++ b/src/easy/PowersOfTwo.java @@ -0,0 +1,35 @@ +package easy; + +/** + * Have the function PowersofTwo(num) take the num parameter being passed + * which will be an integer and return the string true if it's a power of two. + * If it's not return the string false. + * For example if the input is 16 then your program should return the string true + * but if the input is 22 then the output should be the string false. + */ +public class PowersOfTwo { + + /** + * Powers of Two function. + * + * @param num input number + * @return the string true if it's a power of two. + */ + private static String powerOfTwo(int num) { + int bitwise = num & num - 1; + return num != 0 && bitwise == 0 ? "true" : "false"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = powerOfTwo(15); + System.out.println(result1); + var result2 = powerOfTwo(64); + System.out.println(result2); + } + +} diff --git a/src/easy/SimpleAdding.java b/src/easy/SimpleAdding.java new file mode 100644 index 0000000..6684057 --- /dev/null +++ b/src/easy/SimpleAdding.java @@ -0,0 +1,33 @@ +package easy; + +/** + * Have the function SimpleAdding(num) add up all the numbers from 1 to num. + * For example: if the input is 4 then your program + * should return 10 because 1 + 2 + 3 + 4 = 10. + * For the test cases, the parameter num will be any number from 1 to 1000. + */ +public class SimpleAdding { + + /** + * Simple Adding function. + * + * @param num input number + * @return the sum of numbers + */ + private static int simpleAdding(int num) { + return num * (num + 1) / 2; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = simpleAdding(100); + System.out.println(result1); + var result2 = simpleAdding(8); + System.out.println(result2); + } + +} diff --git a/src/easy/SimpleEvens.java b/src/easy/SimpleEvens.java new file mode 100644 index 0000000..5978515 --- /dev/null +++ b/src/easy/SimpleEvens.java @@ -0,0 +1,40 @@ +package easy; + +/** + * Have the function SimpleEvens(num) check whether + * every single number in passed in parameter is even. + * If so, return the string true, otherwise return the string false. + * For example: if num is 4602225 your program should + * return the string false because 5 is not an even number. + */ +public class SimpleEvens { + + /** + * Simple Evens function. + * + * @param num input number + * @return "true" if a number is even + */ + private static String simpleEvens(Integer num) { + String[] digits = num.toString().split(""); + for (String digit : digits) { + if (Integer.parseInt(digit) % 2 != 0) { + return "false"; + } + } + return "true"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + String result1 = simpleEvens(222252); + System.out.println(result1); + String result2 = simpleEvens(228); + System.out.println(result2); + } + +} diff --git a/src/easy/SimpleSymbols.java b/src/easy/SimpleSymbols.java new file mode 100644 index 0000000..73a2295 --- /dev/null +++ b/src/easy/SimpleSymbols.java @@ -0,0 +1,56 @@ +package easy; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Have the function SimpleSymbols(str) take the str parameter being passed + * and determine if it is an acceptable sequence by + * either returning the string true or false. + * The str parameter will be composed of + and = symbols + * with several characters between them (i.e. ++d+===+c++==a) + * and for the string to be true each letter must be surrounded + * by a + symbol. So the string to the left would be false. + * The string will not be empty and will have at least one letter. + */ +public class SimpleSymbols { + + /** + * Simple Symbols function. + * + * @param str input string + * @return "true" if a sequence is acceptable + */ + private static String simpleSymbols(String str) { + Pattern pattern1 = Pattern.compile("(?=(\\+\\w\\+))"); + Pattern pattern2 = Pattern.compile("[a-z]"); + Matcher matcher1 = pattern1.matcher(str); + Matcher matcher2 = pattern2.matcher(str); + int count1 = 0; + int count2 = 0; + int i1 = 0; + int i2 = 0; + while (matcher1.find(i1)) { + count1++; + i1 = matcher1.start() + 1; + } + while (matcher2.find(i2)) { + count2++; + i2 = matcher2.start() + 1; + } + return count1 == count2 ? "true" : "false"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = simpleSymbols("=+e++r+f+v+"); + System.out.println(result1); + var result2 = simpleSymbols("=+e++r+ff+v+"); + System.out.println(result2); + } + +} diff --git a/src/easy/SnakeCase.java b/src/easy/SnakeCase.java new file mode 100644 index 0000000..f6cd148 --- /dev/null +++ b/src/easy/SnakeCase.java @@ -0,0 +1,38 @@ +package easy; + +/** + * Have the function SnakeCase(str) take the str parameter being passed + * and return it in proper snake case format where each word is lowercased + * and separated from adjacent words via an underscore. + * The string will only contain letters and some combination + * of delimiter punctuation characters separating each word. + */ +public class SnakeCase { + + /** + * Snake Case function. + * + * @param str input string + * @return a string in a snake case format + */ + private static String snakeCase(String str) { + return str + .toLowerCase() + .replaceAll("([^a-z])", " ") + .replaceAll(" +", "_") + .trim(); + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = snakeCase("Revolt is the right of the people"); + System.out.println(result1); + var result2 = snakeCase("Fortitude is the guard and support of the other virtues"); + System.out.println(result2); + } + +} diff --git a/src/easy/StringMerge.java b/src/easy/StringMerge.java new file mode 100644 index 0000000..148f261 --- /dev/null +++ b/src/easy/StringMerge.java @@ -0,0 +1,42 @@ +package easy; + +/** + * Have the function StringMerge(str) read the str parameter being passed + * which will contain a large string of alphanumeric characters with + * a single asterisk character splitting the string evenly into two separate strings. + * Your goal is to return a new string by pairing up the characters + * in the corresponding locations in both strings. + * For example: if str is "abc1*kyoo" then your program should return the string akbyco1o + * because a pairs with k, b pairs with y, etc. + * The string will always split evenly with the asterisk in the center. + */ +public class StringMerge { + + /** + * String Merge function. + * + * @param str input string + * @return a new string with paired up characters + */ + public static String stringMerge(String str) { + StringBuilder output = new StringBuilder(); + String[] strArr = str.trim().split("\\*"); + String str1 = strArr[0]; + String str2 = strArr[1]; + for (int i = 0; i < str1.length(); i++) { + output.append(str1.charAt(i)).append(str2.charAt(i)); + } + return output.toString(); + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + String result = stringMerge("123hg*aaabb"); + System.out.println(result); + } + +} diff --git a/src/easy/Superincreasing.java b/src/easy/Superincreasing.java new file mode 100644 index 0000000..5978550 --- /dev/null +++ b/src/easy/Superincreasing.java @@ -0,0 +1,45 @@ +package easy; + +/** + * Have the function Superincreasing(arr) take the array of numbers stored in arr + * and determine if the array forms a superincreasing sequence + * where each element in the array is greater than the sum of all previous elements. + * The array will only consist of positive integers. + * For example: if arr is [1, 3, 6, 13, 54] then your program + * should return the string "true" because it forms a superincreasing sequence. + * If a superincreasing sequence isn't formed, then your program + * should return the string "false" + */ +public class Superincreasing { + + /** + * Superincreasing function. + * + * @param arr input array + * @return "true" if is a superincreasing sequence + */ + private static String superincreasing(int[] arr) { + int sum = arr[0]; + for (int i = 1; i < arr.length; i++) { + if (arr[i] > sum) { + sum += arr[i]; + } else { + return "false"; + } + } + return "true"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = superincreasing(new int[]{1, 3, 6, 13, 54}); + System.out.println(result1); + var result2 = superincreasing(new int[]{3, 3}); + System.out.println(result2); + } + +} diff --git a/src/easy/SwapCase.java b/src/easy/SwapCase.java new file mode 100644 index 0000000..cecce6e --- /dev/null +++ b/src/easy/SwapCase.java @@ -0,0 +1,42 @@ +package easy; + +/** + * Have the function SwapCase(str) take the str parameter + * and swap the case of each character. + * For example: if str is "Hello World" the output should be hELLO wORLD. + * Let numbers and symbols stay the way they are. + */ +public class SwapCase { + + /** + * Swap Case function. + * + * @param str input string + * @return the output string + */ + private static String swapCase(String str) { + StringBuilder out = new StringBuilder(); + char[] chars = str.toCharArray(); + for (char c : chars) { + if (Character.isLowerCase(c)) { + out.append(Character.toUpperCase(c)); + } else { + out.append(Character.toLowerCase(c)); + } + } + return out.toString(); + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = swapCase("The Livin' Free EP"); + System.out.println(result1); + var result2 = swapCase("Selected MP3"); + System.out.println(result2); + } + +} diff --git a/src/easy/ThirdGreatest.java b/src/easy/ThirdGreatest.java new file mode 100644 index 0000000..e656f7d --- /dev/null +++ b/src/easy/ThirdGreatest.java @@ -0,0 +1,45 @@ +package easy; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; + +/** + * Have the function ThirdGreatest(strArr) take the array of strings stored in strArr + * and return the third-largest word within it. + * --- + * So for example: if strArr is ["hello", "world", "before", "all"] your output + * should be world because "before" is 6 letters long, and "hello" and "world" are both 5, + * but the output should be world because it appeared as the last 5-letter word in the array. + * --- + * If strArr was ["hello", "world", "after", "all"] the output should + * be after because the first three words are all 5 letters long, so return the last one. + * The array will have at least three strings and each string will only contain letters. + */ +public class ThirdGreatest { + + /** + * Third-Greatest function. + * + * @param strArr input array of strings + * @return the third-longest word + */ + private static String thirdGreatest(String[] strArr) { + Arrays.sort(strArr, Collections.reverseOrder()); + Arrays.sort(strArr, Comparator.comparingInt(String::length)); + return strArr[strArr.length - 3]; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = thirdGreatest(new String[]{"flowers", "decorate", "soul", "sleep"}); + System.out.println(result1); + var result2 = thirdGreatest(new String[]{"surrounded", "darkness", "awakened", "within"}); + System.out.println(result2); + } + +} diff --git a/src/easy/ThreeSum.java b/src/easy/ThreeSum.java new file mode 100644 index 0000000..882b1c4 --- /dev/null +++ b/src/easy/ThreeSum.java @@ -0,0 +1,48 @@ +package easy; + +/** + * Have the function ThreeSum(arr) take the array of integers stored in arr, + * and determine if any three distinct numbers (excluding the first element) + * in the array can sum up to the first element in the array. + * --- + * For example: if arr is [8, 2, 1, 4, 10, 5, -1, -1] then + * there are actually three sets of triplets + * that sum to the number 8: [2, 1, 5], [4, 5, -1] and [10, -1, -1]. + * --- + * Your program should return the string true + * if 3 distinct elements sum to the first element, + * otherwise your program should return the string false. + * The input array will always contain at least 4 elements. + */ +public class ThreeSum { + + /** + * Three Sum function. + * + * @param arr input array + * @return "true" if 3 distinct elements sum to the first element + */ + private static String threeSum(int[] arr) { + for (int i = 1; i < arr.length; i++) { + for (int j = i + 1; j < arr.length; j++) { + for (int k = j + 1; k < arr.length; k++) { + if (arr[i] + arr[j] + arr[k] == arr[0]) { + return "true"; + } + } + } + } + return "false"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result = threeSum(new int[]{8, 1, 2, 3, 4, 5, 7}); + System.out.println(result); + } + +} diff --git a/src/easy/TimeConvert.java b/src/easy/TimeConvert.java new file mode 100644 index 0000000..faecb64 --- /dev/null +++ b/src/easy/TimeConvert.java @@ -0,0 +1,37 @@ +package easy; + +/** + * Have the function TimeConvert(num) take the num parameter being passed + * and return the number of hours and minutes the parameter converts to + * (i.e. if num = 63 then the output should be 1:3). + * Separate the number of hours and minutes with a colon. + */ +public class TimeConvert { + + /** + * Time Convert function. + * + * @param num input number + * @return the number of hours and minutes + */ + private static String timeConvert(int num) { + int hours = num / 60; + int minutes = num % 60; + return hours + ":" + minutes; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = timeConvert(63); + System.out.println(result1); + var result2 = timeConvert(178); + System.out.println(result2); + var result3 = timeConvert(249); + System.out.println(result3); + } + +} diff --git a/src/easy/TwoSum.java b/src/easy/TwoSum.java new file mode 100644 index 0000000..d23ee2d --- /dev/null +++ b/src/easy/TwoSum.java @@ -0,0 +1,50 @@ +package easy; + +/** + * Have the function TwoSum(arr) take the array of integers stored in arr, + * and determine if any two numbers (excluding the first element) + * in the array can sum up to the first element in the array. + * --- + * For example: if arr is [7, 3, 5, 2, -4, 8, 11], + * then there are actually two pairs that sum to the number 7: [5, 2] and [-4, 11]. + * --- + * Your program should return all pairs, + * with the numbers separated by a comma, + * in the order the first number appears in the array. + * Pairs should be separated by a space. + * So for the example above, your program would return: 5,2 -4,11 + */ +public class TwoSum { + + /** + * Two Sum function. + * + * @param arr input array + * @return all pairs + */ + private static String twoSum(int[] arr) { + StringBuilder output = new StringBuilder(); + for (int i = 1; i < arr.length; i++) { + for (int j = i + 1; j < arr.length; j++) { + if (arr[i] + arr[j] == arr[0]) { + if (output.length() > 0) { + output.append(" "); + } + output.append(arr[i]).append(",").append(arr[j]); + } + } + } + return output.length() == 0 ? "-1" : output.toString(); + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result = twoSum(new int[]{8, 1, 2, 3, 4, 5, 7}); + System.out.println(result); + } + +} diff --git a/src/easy/VowelCount.java b/src/easy/VowelCount.java new file mode 100644 index 0000000..a4f5dc0 --- /dev/null +++ b/src/easy/VowelCount.java @@ -0,0 +1,44 @@ +package easy; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Have the function VowelCount(str) take the str string + * parameter being passed and return the number of vowels + * the string contains (i.e. "All cows eat grass and moo" would return 8). + * Do not count y as a vowel for this challenge. + */ +public class VowelCount { + + /** + * Vowel Count function. + * + * @param str input string + * @return the number of vowels in a string + */ + private static int vowelCount(String str) { + Pattern pattern = Pattern.compile("[aeiou]"); + Matcher matcher = pattern.matcher(str); + int i = 0; + int count = 0; + while (matcher.find(i)) { + count++; + i = matcher.start() + 1; + } + return count; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = vowelCount("I cannot sleep unless I am surrounded by books."); + System.out.println(result1); + var result2 = vowelCount("Life itself is a quotation."); + System.out.println(result2); + } + +} diff --git a/src/easy/WordCount.java b/src/easy/WordCount.java new file mode 100644 index 0000000..1681577 --- /dev/null +++ b/src/easy/WordCount.java @@ -0,0 +1,34 @@ +package easy; + +/** + * Have the function WordCount(str) take the str string + * parameter being passed and return the number of words the string + * contains (e.g. "Never eat shredded wheat or cake" would return 6). + * Words will be separated by single spaces. + */ +public class WordCount { + + /** + * Word Count function. + * + * @param str input string + * @return the number of words the string + */ + private static int wordCount(String str) { + String[] words = str.split(" "); + return str.length() > 0 ? words.length : 0; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = wordCount("The mind was dreaming. The world was its dream."); + System.out.println(result1); + var result2 = wordCount("I have always imagined that Paradise will be a kind of library."); + System.out.println(result2); + } + +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy