Skip to content

Easy challenges - part 2 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Aug 16, 2021
Merged
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions src/easy/LetterCapitalize.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
54 changes: 54 additions & 0 deletions src/easy/LetterChanges.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
65 changes: 65 additions & 0 deletions src/easy/MeanMode.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Integer, Integer> 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);
}

}
48 changes: 48 additions & 0 deletions src/easy/MultiplicativePersistence.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
54 changes: 54 additions & 0 deletions src/easy/NonrepeatingCharacter.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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);
}

}
46 changes: 46 additions & 0 deletions src/easy/NumberAddition.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
62 changes: 62 additions & 0 deletions src/easy/Palindrome.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Loading