From a687cb8795a47228ad57cbf5d3d06ef4252ab83f Mon Sep 17 00:00:00 2001 From: ardallie Date: Fri, 13 Aug 2021 22:26:10 +0100 Subject: [PATCH 01/25] Letter Capitalize --- src/easy/LetterCapitalize.java | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/easy/LetterCapitalize.java 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); + } + +} From 32545f7c5544b364ca4d1eb416b9468031f5622c Mon Sep 17 00:00:00 2001 From: ardallie Date: Fri, 13 Aug 2021 22:41:05 +0100 Subject: [PATCH 02/25] Letter Changes --- src/easy/LetterChanges.java | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/easy/LetterChanges.java 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); + } + +} From 719c204eda67ddfd707419eb71bb5563aa1397c0 Mon Sep 17 00:00:00 2001 From: ardallie Date: Fri, 13 Aug 2021 22:44:56 +0100 Subject: [PATCH 03/25] Longest Word --- src/easy/LongestWord.java | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/easy/LongestWord.java diff --git a/src/easy/LongestWord.java b/src/easy/LongestWord.java new file mode 100644 index 0000000..d37aac0 --- /dev/null +++ b/src/easy/LongestWord.java @@ -0,0 +1,43 @@ +package easy; + +/** + * Have the function LongestWord(sen) take the sen + * parameter being passed and return the largest word in the string. + * If there are two or more words that are the same length, + * return the first word from the string with that length. + * Ignore punctuation and assume sen will not be empty. + */ +public class LongestWord { + + /** + * Longest Word function. + * + * @param sen input string + * @return the longest word in a sentence + */ + private static String longestWord(String sen) { + String longest = ""; + String cleanWords = sen.replaceAll("[^a-zA-Z0-9 ]", ""); + String[] splitWords = cleanWords.split(" "); + for (String splitWord : splitWords) { + if (splitWord.length() > longest.length()) { + longest = splitWord; + } + } + return longest; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var r1 = longestWord("Dwell on the beauty of life. " + + "Watch the stars, and see yourself running with them."); + System.out.println(r1); + var r2 = longestWord("The happiness of your life depends upon the quality of your thoughts."); + System.out.println(r2); + } + +} From 35ae53bcfb94fd36538a014b0908cc54003f87da Mon Sep 17 00:00:00 2001 From: ardallie Date: Fri, 13 Aug 2021 22:58:09 +0100 Subject: [PATCH 04/25] Mean Mode --- src/easy/MeanMode.java | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/easy/MeanMode.java 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); + } + +} From 7038c816dea76d5c948192b488a0cc7de41f683c Mon Sep 17 00:00:00 2001 From: ardallie Date: Sat, 14 Aug 2021 13:04:05 +0100 Subject: [PATCH 05/25] Multiplicative Persistence --- src/easy/MultiplicativePersistence.java | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/easy/MultiplicativePersistence.java 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); + } + +} From 8b69a492efa9f5f401f22587edf576303435b091 Mon Sep 17 00:00:00 2001 From: ardallie Date: Sat, 14 Aug 2021 13:16:39 +0100 Subject: [PATCH 06/25] Non-repeating character --- src/easy/NonrepeatingCharacter.java | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/easy/NonrepeatingCharacter.java 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); + } + +} From ecb4fa9491c04f77a328380de2131ce4d70e1a4f Mon Sep 17 00:00:00 2001 From: ardallie Date: Sat, 14 Aug 2021 13:26:05 +0100 Subject: [PATCH 07/25] Number Addition --- src/easy/NumberAddition.java | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/easy/NumberAddition.java 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); + } + +} From cfc601c5d27fba4c7c78153471816d56a2573d67 Mon Sep 17 00:00:00 2001 From: ardallie Date: Sat, 14 Aug 2021 13:31:21 +0100 Subject: [PATCH 08/25] Palindrome --- src/easy/Palindrome.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/easy/Palindrome.java diff --git a/src/easy/Palindrome.java b/src/easy/Palindrome.java new file mode 100644 index 0000000..8670eb8 --- /dev/null +++ b/src/easy/Palindrome.java @@ -0,0 +1,39 @@ +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"; + } + + /** + * Entry point. + * + * @param args command line arguments + */ + public static void main(String[] args) { + var result1 = palindrome("dont nod"); + System.out.println(result1); + var result2 = palindrome("rats live on no evil star"); + System.out.println(result2); + } + +} From 96c0cd4e3568800a95cb61eade6c5655226e33d5 Mon Sep 17 00:00:00 2001 From: ardallie Date: Sat, 14 Aug 2021 14:22:46 +0100 Subject: [PATCH 09/25] Palindrome checker - alternative --- src/easy/Palindrome.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/easy/Palindrome.java b/src/easy/Palindrome.java index 8670eb8..3de6759 100644 --- a/src/easy/Palindrome.java +++ b/src/easy/Palindrome.java @@ -24,6 +24,25 @@ private static String palindrome(String str) { 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. * @@ -32,8 +51,12 @@ private static String palindrome(String str) { public static void main(String[] args) { var result1 = palindrome("dont nod"); System.out.println(result1); - var result2 = palindrome("rats live on no evil star"); + 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); } } From 93c4822ae131c018b6a2b16aca39752ee8c2875c Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 12:19:11 +0100 Subject: [PATCH 10/25] Palindrome Creator --- src/easy/PalindromeCreator.java | 94 +++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/easy/PalindromeCreator.java 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); + } + +} From 155a3ee960ad828a0cc4e19f8d662a3facf9392b Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 12:27:21 +0100 Subject: [PATCH 11/25] Powers of Two --- src/easy/PowersOfTwo.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/easy/PowersOfTwo.java 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); + } + +} From 131c9b59abd5f60364144864f0630695d6bfdd39 Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 12:44:41 +0100 Subject: [PATCH 12/25] Simple Adding --- src/easy/SimpleAdding.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/easy/SimpleAdding.java 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); + } + +} From 4371d6d1b1f08f675729ebfe105411eb4bb77a9b Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 12:56:09 +0100 Subject: [PATCH 13/25] Simple Evens --- src/easy/LongestWord.java | 43 --------------------------------------- src/easy/SimpleEvens.java | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 43 deletions(-) delete mode 100644 src/easy/LongestWord.java create mode 100644 src/easy/SimpleEvens.java diff --git a/src/easy/LongestWord.java b/src/easy/LongestWord.java deleted file mode 100644 index d37aac0..0000000 --- a/src/easy/LongestWord.java +++ /dev/null @@ -1,43 +0,0 @@ -package easy; - -/** - * Have the function LongestWord(sen) take the sen - * parameter being passed and return the largest word in the string. - * If there are two or more words that are the same length, - * return the first word from the string with that length. - * Ignore punctuation and assume sen will not be empty. - */ -public class LongestWord { - - /** - * Longest Word function. - * - * @param sen input string - * @return the longest word in a sentence - */ - private static String longestWord(String sen) { - String longest = ""; - String cleanWords = sen.replaceAll("[^a-zA-Z0-9 ]", ""); - String[] splitWords = cleanWords.split(" "); - for (String splitWord : splitWords) { - if (splitWord.length() > longest.length()) { - longest = splitWord; - } - } - return longest; - } - - /** - * Entry point. - * - * @param args command line arguments - */ - public static void main(String[] args) { - var r1 = longestWord("Dwell on the beauty of life. " - + "Watch the stars, and see yourself running with them."); - System.out.println(r1); - var r2 = longestWord("The happiness of your life depends upon the quality of your thoughts."); - System.out.println(r2); - } - -} 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); + } + +} From 9b6d07079bc635d592bced096c52bcc1d6c5eb1a Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 15:55:35 +0100 Subject: [PATCH 14/25] Simple Symbols --- src/easy/SimpleSymbols.java | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/easy/SimpleSymbols.java 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); + } + +} From 85cf1f7323db87aa031b3ab24e25169304487d38 Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 18:00:05 +0100 Subject: [PATCH 15/25] Snake Case --- src/easy/SnakeCase.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/easy/SnakeCase.java 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); + } + +} From e819b93acd5ccb0defbda7378320e4f5b1d8155d Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 18:06:50 +0100 Subject: [PATCH 16/25] String Merge --- src/easy/StringMerge.java | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/easy/StringMerge.java 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); + } + +} From 6cc868de1e898ab39ca97058b23235943ca8f888 Mon Sep 17 00:00:00 2001 From: ardallie Date: Sun, 15 Aug 2021 18:22:41 +0100 Subject: [PATCH 17/25] Superincreasing --- src/easy/Superincreasing.java | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/easy/Superincreasing.java 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); + } + +} From dfd840a59d4a21772cf64cb585fc38b531f67cd1 Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 21:54:33 +0100 Subject: [PATCH 18/25] Swap Case --- src/easy/SwapCase.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/easy/SwapCase.java 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); + } + +} From 80a4c89c5ec4afcec8e9f505cf46e371d8c2cac3 Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 22:05:23 +0100 Subject: [PATCH 19/25] Third Greatest --- src/easy/ThirdGreatest.java | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/easy/ThirdGreatest.java 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); + } + +} From 207a1091e96912c9dc4bd66ce93488b020b75d5f Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 22:09:04 +0100 Subject: [PATCH 20/25] Three Sum --- src/easy/ThreeSum.java | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/easy/ThreeSum.java 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); + } + +} From 77d95c2ce478ae3308085064f812125a434042a4 Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 22:22:48 +0100 Subject: [PATCH 21/25] Time Convert --- src/easy/TimeConvert.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/easy/TimeConvert.java 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); + } + +} From ffe2fad476e08aa91136d83952a10a3f9db88ed5 Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 22:32:29 +0100 Subject: [PATCH 22/25] Two Sum --- src/easy/TwoSum.java | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/easy/TwoSum.java 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); + } + +} From 058f92b2b6d065aa5514d533ad8f91b77f2be342 Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 22:36:17 +0100 Subject: [PATCH 23/25] Vowel Count --- src/easy/VowelCount.java | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/easy/VowelCount.java 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); + } + +} From b0a3c0f1fa06497ad70a6c816c358cdb9240cfcd Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 22:41:20 +0100 Subject: [PATCH 24/25] Word Count --- src/easy/WordCount.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/easy/WordCount.java 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); + } + +} From 9b775dee9bce5fe34e28f656be9b9228f0acc441 Mon Sep 17 00:00:00 2001 From: ardallie Date: Mon, 16 Aug 2021 22:52:11 +0100 Subject: [PATCH 25/25] Add README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 README.md 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