Skip to content

Commit 3aeba10

Browse files
authored
Merge pull request ardallie#2 from ardallie/easy
Easy challenges - part 2
2 parents 63a3246 + 9b775de commit 3aeba10

23 files changed

+1058
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Coderbyte challenges in Java.
2+
3+
This repository contains a collection of code challenges from Coderbyte.com.
4+
5+
Solutions are grouped into three difficulty levels:
6+
7+
- easy
8+
- medium
9+
- hard

src/easy/LetterCapitalize.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package easy;
2+
3+
/**
4+
* Have the function LetterCapitalize(str) take the str parameter
5+
* being passed and capitalize the first letter of each word.
6+
* Words will be separated by only one space.
7+
*/
8+
public class LetterCapitalize {
9+
10+
/**
11+
* Letter Capitalize function.
12+
*
13+
* @param str input string
14+
* @return string with the letters capitalised
15+
*/
16+
private static String letterCapitalize(String str) {
17+
String[] splitWords = str.split(" ");
18+
for (int i = 0; i < splitWords.length; i++) {
19+
String word = splitWords[i];
20+
splitWords[i] = word.substring(0, 1).toUpperCase() + word.substring(1);
21+
}
22+
return String.join(" ", splitWords);
23+
}
24+
25+
/**
26+
* Entry point.
27+
*
28+
* @param args command line arguments
29+
*/
30+
public static void main(String[] args) {
31+
var result1 = letterCapitalize("The soul becomes dyed with the color of its thoughts.");
32+
System.out.println(result1);
33+
var result2 = letterCapitalize("The universe is change; our life is what our thoughts make it.");
34+
System.out.println(result2);
35+
}
36+
37+
}

src/easy/LetterChanges.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package easy;
2+
3+
/**
4+
* Have the function LetterChanges(str) take the str parameter
5+
* being passed and modify it using the following algorithm.
6+
* Replace every letter in the string with the letter
7+
* following it in the alphabet (i.e. c becomes d, z becomes a).
8+
* Then capitalize every vowel in this new string (a-e-i-o-u)
9+
* and finally return this modified string.
10+
*/
11+
public class LetterChanges {
12+
13+
14+
/**
15+
* Letter Changes function.
16+
*
17+
* @param str input string
18+
* @return modified string
19+
*/
20+
private static String letterChanges(String str) {
21+
22+
char[] alphabet = {'b', 'c', 'd', 'E', 'f', 'g', 'h', 'I', 'j', 'k', 'l',
23+
'm', 'n', 'O', 'p', 'q', 'r', 's', 't', 'U', 'v', 'w', 'x', 'y', 'z', 'A'};
24+
char[] charArray = str.toLowerCase().toCharArray();
25+
StringBuilder output = new StringBuilder();
26+
27+
for (int i = 0; i < str.length(); i++) {
28+
char letter = str.charAt(i);
29+
boolean isLetter = letter >= 'a' && letter <= 'z';
30+
if (isLetter) {
31+
output.append(alphabet[charArray[i] - 97]);
32+
} else {
33+
output.append(letter);
34+
}
35+
}
36+
37+
return output.toString();
38+
}
39+
40+
/**
41+
* Entry point.
42+
*
43+
* @param args command line arguments
44+
*/
45+
public static void main(String[] args) {
46+
var result1 = letterChanges("anthology");
47+
System.out.println(result1);
48+
var result2 = letterChanges("equilibrium");
49+
System.out.println(result2);
50+
var result3 = letterChanges("oxford");
51+
System.out.println(result3);
52+
}
53+
54+
}

src/easy/MeanMode.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package easy;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Have the function MeanMode(arr) take the array of numbers stored in arr
8+
* and return 1 if the mode equals the mean, 0 if they don't equal each other
9+
* (i.e. [5, 3, 3, 3, 1] should return 1 because the mode (3) equals the mean (3)).
10+
* The array will not be empty, will only contain positive integers,
11+
* and will not contain more than one mode.
12+
*/
13+
public class MeanMode {
14+
15+
/**
16+
* Mean Mode function.
17+
*
18+
* @param arr input array.
19+
* @return 1 if the mode equals the mean, 0 if they don't equal each other
20+
*/
21+
private static String meanMode(int[] arr) {
22+
int sum = 0;
23+
int modeKey = 0;
24+
int modeVal = 0;
25+
26+
Map<Integer, Integer> modeMap = new HashMap<>();
27+
for (int item : arr) {
28+
modeMap.put(item, 0);
29+
}
30+
31+
for (int value : arr) {
32+
sum += value;
33+
int val = modeMap.get(value);
34+
if (val > 0) {
35+
modeMap.put(value, val + 1);
36+
} else {
37+
modeMap.put(value, 1);
38+
}
39+
}
40+
41+
for (Map.Entry<Integer, Integer> item : modeMap.entrySet()) {
42+
int itemKey = item.getKey();
43+
int itemVal = item.getValue();
44+
if (itemVal > modeVal) {
45+
modeVal = itemVal;
46+
modeKey = itemKey;
47+
}
48+
}
49+
50+
return modeKey == (sum / arr.length) ? "1" : "0";
51+
}
52+
53+
/**
54+
* Entry point.
55+
*
56+
* @param args command line arguments
57+
*/
58+
public static void main(String[] args) {
59+
var result1 = meanMode(new int[]{5, 3, 3, 3, 1});
60+
System.out.println(result1);
61+
var result2 = meanMode(new int[]{64, 64, 64, 64, 64, 64, 64, 64, 1024});
62+
System.out.println(result2);
63+
}
64+
65+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package easy;
2+
3+
/**
4+
* Have the function MultiplicativePersistence(num) take the num parameter being passed
5+
* which will always be a positive integer
6+
* and return its multiplicative persistence which is
7+
* the number of times you must multiply the digits in num until you reach a single digit.
8+
* ---
9+
* For example: if num is 39 then your program
10+
* should return 3 because 3 * 9 = 27 then 2 * 7 = 14
11+
* and finally 1 * 4 = 4 then you stop at 4.
12+
*/
13+
public class MultiplicativePersistence {
14+
15+
/**
16+
* Multiplicative Persistence function.
17+
*
18+
* @param num input number
19+
* @return the number of times you must multiply
20+
*/
21+
private static int multiplicativePersistence(int num) {
22+
int times = 0;
23+
int multiplied = num;
24+
while (multiplied > 9) {
25+
int product = 1;
26+
String[] intArr = Integer.toString(multiplied).split("");
27+
for (String i : intArr) {
28+
product *= Integer.parseInt(i);
29+
}
30+
multiplied = product;
31+
times++;
32+
}
33+
return times;
34+
}
35+
36+
/**
37+
* Entry point.
38+
*
39+
* @param args command line arguments
40+
*/
41+
public static void main(String[] args) {
42+
var result1 = multiplicativePersistence(2677889);
43+
System.out.println(result1);
44+
var result2 = multiplicativePersistence(8192);
45+
System.out.println(result2);
46+
}
47+
48+
}

src/easy/NonrepeatingCharacter.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package easy;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Have the function NonrepeatingCharacter(str)
7+
* take the str parameter being passed,
8+
* which will contain only alphabetic characters and spaces,
9+
* and return the first non-repeating character.
10+
* ---
11+
* For example: if str is "agettkgaeee" then your program should return k.
12+
* The string will always contain at least one character and there will
13+
* always be at least one non-repeating character.
14+
*/
15+
public class NonrepeatingCharacter {
16+
17+
/**
18+
* Non-repeating Character function.
19+
*
20+
* @param str input string
21+
* @return the first non-repeating character
22+
*/
23+
private static String nonrepeatingCharacter(String str) {
24+
25+
char[] charArr = str.toLowerCase().toCharArray();
26+
HashMap<Integer, Integer> freq = new HashMap<>();
27+
28+
for (int c : charArr) {
29+
Integer count = freq.get(c);
30+
freq.put(c, count == null ? 1 : ++count);
31+
}
32+
33+
for (int c : charArr) {
34+
Integer count = freq.get(c);
35+
if (count == 1) {
36+
return String.valueOf((char) c);
37+
}
38+
}
39+
return "false";
40+
}
41+
42+
/**
43+
* Entry point.
44+
*
45+
* @param args command line arguments
46+
*/
47+
public static void main(String[] args) {
48+
var res1 = nonrepeatingCharacter("Beauty in things exists in the mind which contemplates them");
49+
System.out.println(res1);
50+
var res2 = nonrepeatingCharacter("A wise man apportions his beliefs to the evidence");
51+
System.out.println(res2);
52+
}
53+
54+
}

src/easy/NumberAddition.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package easy;
2+
3+
/**
4+
* Have the function NumberSearch(str) take the str parameter,
5+
* search for all the numbers in the string, add them together,
6+
* then return that final number.
7+
* ---
8+
* For example: if str is "88Hello 3World!" the output should be 91.
9+
* You will have to differentiate between single digit numbers
10+
* and multiple digit numbers like in the example above.
11+
* So "55Hello" and "5Hello 5" should return two different answers.
12+
* Each string will contain at least one letter or symbol.
13+
*/
14+
public class NumberAddition {
15+
16+
/**
17+
* Number Addition function.
18+
*
19+
* @param str input string
20+
* @return the final number
21+
*/
22+
private static int numberAddition(String str) {
23+
String cleaned = str.replaceAll("[^0-9]", " ");
24+
String[] splitNum = cleaned.split(" +");
25+
int sum = 0;
26+
for (String c : splitNum) {
27+
if (!c.equals("")) {
28+
sum += Integer.parseInt(c);
29+
}
30+
}
31+
return sum;
32+
}
33+
34+
/**
35+
* Entry point.
36+
*
37+
* @param args command line arguments
38+
*/
39+
public static void main(String[] args) {
40+
var result1 = numberAddition("Chillhouse Mix 2 (2001)");
41+
System.out.println(result1);
42+
var result2 = numberAddition("Cafe del Mar 5 (1998)");
43+
System.out.println(result2);
44+
}
45+
46+
}

src/easy/Palindrome.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package easy;
2+
3+
/**
4+
* Have the function Palindrome(str) take the str parameter
5+
* being passed and return the string true
6+
* if the parameter is a palindrome,
7+
* (the string is the same forward as it is backward)
8+
* otherwise return the string false.
9+
* For example: "racecar" is also "racecar" backwards.
10+
* Punctuation and numbers will not be part of the string.
11+
*/
12+
public class Palindrome {
13+
14+
/**
15+
* Palindrome function.
16+
*
17+
* @param str input string
18+
* @return "true" is the string is a palindrome
19+
*/
20+
private static String palindrome(String str) {
21+
StringBuilder reversed = new StringBuilder();
22+
String cleaned = str.replaceAll(" ", "");
23+
reversed.append(cleaned).reverse();
24+
return cleaned.equals(reversed.toString()) && str.length() > 0 ? "true" : "false";
25+
}
26+
27+
/**
28+
* An improved function checking if a given string is a palindrome.
29+
* It compares two halves of a string, checking if a letter
30+
* from the first half matches the other half in the reverse order.
31+
*
32+
* @param str input string
33+
* @return true if the string is a palindrome
34+
*/
35+
private static boolean isPalindrome(String str) {
36+
char[] strArr = str.toCharArray();
37+
int len = strArr.length;
38+
for (int i = 0; i < len / 2; i++) {
39+
if (strArr[i] != strArr[len - i - 1]) {
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
46+
/**
47+
* Entry point.
48+
*
49+
* @param args command line arguments
50+
*/
51+
public static void main(String[] args) {
52+
var result1 = palindrome("dont nod");
53+
System.out.println(result1);
54+
var result2 = isPalindrome("dont nod");
55+
System.out.println(result2);
56+
var result3 = palindrome("rats live on no evil star");
57+
System.out.println(result3);
58+
var result4 = isPalindrome("rats live on no evil star");
59+
System.out.println(result4);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)
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