Skip to content

Commit 63a3246

Browse files
authored
Merge pull request ardallie#1 from ardallie/easy
20 easy challenges
2 parents b142da9 + 4037669 commit 63a3246

19 files changed

+1028
-0
lines changed

src/easy/AdditivePersistence.java

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 AdditivePersistence(num) take the num parameter being passed
5+
* which will always be a positive integer
6+
* and return its additive persistence which is the number of times
7+
* you must add the digits in num until you reach a single digit.
8+
* ---
9+
* For example: if num is 2718 then your program
10+
* should return 2 because 2 + 7 + 1 + 8 = 18
11+
* and 1 + 8 = 9, and you stop at 9.
12+
*/
13+
public class AdditivePersistence {
14+
15+
/**
16+
* Additive Persistence function.
17+
*
18+
* @param num input number
19+
* @return additive persistence which is the number of times
20+
*/
21+
private static int additivePersistence(int num) {
22+
int times = 0;
23+
int added = num;
24+
while (added > 9) {
25+
int sum = 0;
26+
String[] intArr = Integer.toString(added).split("");
27+
for (String i : intArr) {
28+
sum += Integer.parseInt(i);
29+
}
30+
added = sum;
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 = additivePersistence(199);
43+
System.out.println(result1);
44+
var result2 = additivePersistence(913);
45+
System.out.println(result2);
46+
}
47+
48+
}

src/easy/AlphabetSoup.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package easy;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Have the function AlphabetSoup(str) take the str string parameter being passed
7+
* and return the string with the letters in alphabetical order (i.e. hello becomes ehllo).
8+
* Assume numbers and punctuation symbols will not be included in the string.
9+
*/
10+
public class AlphabetSoup {
11+
12+
/**
13+
* Alphabet Soup function.
14+
*
15+
* @param str input string
16+
* @return the string with the letters in alphabetical order
17+
*/
18+
private static String alphabetSoup(String str) {
19+
char[] letters = str.toCharArray();
20+
Arrays.sort(letters);
21+
return String.valueOf(letters);
22+
}
23+
24+
/**
25+
* Entry point.
26+
*
27+
* @param args command line arguments
28+
*/
29+
public static void main(String[] args) {
30+
var result1 = alphabetSoup("leftfield");
31+
System.out.println(result1);
32+
var result2 = alphabetSoup("underworld");
33+
System.out.println(result2);
34+
}
35+
36+
}

src/easy/ArithGeo.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package easy;
2+
3+
/**
4+
* Have the function ArithGeo(arr) take the array of numbers stored in arr
5+
* and return the string "Arithmetic" if the sequence follows an arithmetic pattern
6+
* or return "Geometric" if it follows a geometric pattern.
7+
* ---
8+
* If the sequence doesn't follow either pattern return -1.
9+
* An arithmetic sequence is one where the difference between
10+
* each of the numbers is consistent, where in a geometric sequence,
11+
* each term after the first is multiplied by some constant or common ratio.
12+
* ---
13+
* Arithmetic example: [2, 4, 6, 8] and Geometric example: [2, 6, 18, 54].
14+
* Negative numbers may be entered as parameters, 0 will not be entered,
15+
* and no array will contain all the same elements.
16+
*/
17+
public class ArithGeo {
18+
19+
/**
20+
* Arith Geo function.
21+
*
22+
* @param arr input array of integers
23+
* @return the string "Arithmetic" if the sequence follows an arithmetic pattern
24+
*/
25+
private static String arithGeo(int[] arr) {
26+
27+
int arithInterval = arr[1] - arr[0];
28+
int geoInterval = arr[1] / arr[0];
29+
int arithCount = 0;
30+
int geoCount = 0;
31+
32+
for (int i = 0; i < arr.length - 1; i++) {
33+
if (arr[i + 1] - arr[i] == arithInterval) {
34+
arithCount++;
35+
}
36+
if (arr[i + 1] / arr[i] == geoInterval) {
37+
geoCount++;
38+
}
39+
}
40+
41+
if (arithCount == arr.length - 1) {
42+
return "Arithmetic";
43+
}
44+
45+
if (geoCount == arr.length - 1) {
46+
return "Geometric";
47+
}
48+
49+
return "-1";
50+
}
51+
52+
/**
53+
* Entry point.
54+
*
55+
* @param args command line arguments
56+
*/
57+
public static void main(String[] args) {
58+
var result1 = arithGeo(new int[]{2, 4, 6, 8});
59+
System.out.println(result1);
60+
var result2 = arithGeo(new int[]{2, 6, 18, 54});
61+
System.out.println(result2);
62+
var result3 = arithGeo(new int[]{-3, -4, -5, -6, -7});
63+
System.out.println(result3);
64+
}
65+
66+
}

src/easy/ArrayAdditionOne.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package easy;
2+
3+
import static java.lang.Math.pow;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* Have the function ArrayAdditionI(arr) take the array of numbers stored in arr
9+
* and return the string true if any combination of numbers in the array
10+
* (excluding the largest number) can be added up to equal the largest number in the array,
11+
* otherwise return the string false.
12+
* ---
13+
* For example: if arr contains [4, 6, 23, 10, 1, 3] the output
14+
* should return true because 4 + 6 + 10 + 3 = 23.
15+
* ---
16+
* The array will not be empty, will not contain all the same elements,
17+
* and may contain negative numbers.
18+
*/
19+
public class ArrayAdditionOne {
20+
21+
/**
22+
* Left pad the string with zeroes,
23+
* e.g. padLeft("fade", 8) -> "0000fade"
24+
*
25+
* @param str string to be padded
26+
* @param len new fixed length after applying the padding
27+
* @return padded string (e.g. 000000xxx)
28+
*/
29+
private static String padLeft(String str, int len) {
30+
return String.format("%" + len + "s", str).replace(" ", "0");
31+
}
32+
33+
/**
34+
* Array Addition I function.
35+
*
36+
* @param arr input array of integers
37+
* @return "true" if any combination can be added up to equal the largest number in the array
38+
*/
39+
private static String arrayAdditionOne(int[] arr) {
40+
Arrays.sort(arr);
41+
int largest = arr[arr.length - 1];
42+
int oneChar = "1".charAt(0);
43+
44+
for (int i = 0; i < pow(2, arr.length); i++) {
45+
String bin = Integer.toBinaryString(i);
46+
String combo = padLeft(bin, arr.length - 1);
47+
int sum = 0;
48+
for (int j = 0; j < combo.length(); j++) {
49+
if (combo.charAt(j) == oneChar) {
50+
sum += arr[j];
51+
}
52+
if (sum == largest) {
53+
return "true";
54+
}
55+
}
56+
}
57+
58+
return "false";
59+
}
60+
61+
/**
62+
* Entry point.
63+
*
64+
* @param args command line arguments
65+
*/
66+
public static void main(String[] args) {
67+
var result1 = arrayAdditionOne(new int[]{4, 6, 23, 10, 1, 3});
68+
System.out.println(result1);
69+
var result2 = arrayAdditionOne(new int[]{2, 6, 18});
70+
System.out.println(result2);
71+
}
72+
73+
}

src/easy/BasicRomanNumerals.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package easy;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Have the function BasicRomanNumerals(str) read str which will be a string of Roman numerals.
7+
* The numerals being used are: I for 1, V for 5, X for 10, L for 50,
8+
* C for 100, D for 500 and M for 1000.
9+
* In Roman numerals, to create a number like 11 you simply add a 1 after the 10,
10+
* so you get XI. But to create a number like 19, you use the subtraction notation
11+
* which is to add I before an X or V (or add an X before an L or C).
12+
* So 19 in Roman numerals is XIX.
13+
* ---
14+
* The goal of your program is to return the decimal equivalent of the Roman numeral given.
15+
* For example: if str is "XXIV" your program should return 24
16+
*/
17+
public class BasicRomanNumerals {
18+
19+
/**
20+
* Basic Roman Numerals function.
21+
*
22+
* @param str input string
23+
* @return the decimal equivalent of the Roman numeral given
24+
*/
25+
private static int basicRomanNumerals(String str) {
26+
HashMap<String, Integer> numerals = new HashMap<>();
27+
numerals.put("M", 1000);
28+
numerals.put("D", 500);
29+
numerals.put("C", 100);
30+
numerals.put("X", 10);
31+
numerals.put("L", 50);
32+
numerals.put("V", 5);
33+
numerals.put("I", 1);
34+
// reversing the string makes the parsing easier
35+
char[] reversed = new StringBuilder(str).reverse().toString().toCharArray();
36+
int result = 0;
37+
int prevValue = 0;
38+
for (char cr : reversed) {
39+
int thisValue = numerals.get(Character.toString(cr));
40+
if (thisValue > prevValue) {
41+
result += thisValue;
42+
} else {
43+
result -= thisValue;
44+
}
45+
prevValue = thisValue;
46+
}
47+
return result;
48+
}
49+
50+
/**
51+
* Entry point.
52+
*
53+
* @param args command line arguments
54+
*/
55+
public static void main(String[] args) {
56+
var result1 = basicRomanNumerals("XXIV");
57+
System.out.println(result1);
58+
var result2 = basicRomanNumerals("XLVI");
59+
System.out.println(result2);
60+
}
61+
62+
}

src/easy/BinaryReversal.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package easy;
2+
3+
/**
4+
* Have the function BinaryReversal(str) take the str parameter being passed,
5+
* which will be a positive integer, take its binary representation
6+
* (padded to the nearest N * 8 bits), reverse that string of bits,
7+
* and then finally return the new reversed string in decimal form.
8+
* ---
9+
* For example: if str is "47" then the binary version of this integer is 101111,
10+
* but we pad it to be 00101111. Your program should reverse this binary string
11+
* which then becomes: 11110100 and then finally return
12+
* the decimal version of this string, which is 244.
13+
*/
14+
public class BinaryReversal {
15+
16+
/**
17+
* Left pad the string with zeroes,
18+
* e.g. padLeft("fade", 8) -> "0000fade"
19+
*
20+
* @param str string to be padded
21+
* @param len new fixed length after applying the padding
22+
* @return padded string (e.g. 000000xxx)
23+
*/
24+
private static String padLeft(String str, int len) {
25+
return String.format("%" + len + "s", str).replace(" ", "0");
26+
}
27+
28+
/**
29+
* Binary Reversal function.
30+
*
31+
* @param str input string
32+
* @return the decimal version of this string
33+
*/
34+
private static int binaryReversal(String str) {
35+
String binStr = Integer.toBinaryString(Integer.parseInt(str));
36+
int add = binStr.length() % 8 == 0 ? 0 : 1;
37+
int pad = add + binStr.length() / 8;
38+
String padStr = padLeft(binStr, pad * 8);
39+
StringBuilder result = new StringBuilder();
40+
for (int i = padStr.length() - 1; i >= 0; i--) {
41+
result.append(Character.getNumericValue(padStr.charAt(i)));
42+
}
43+
return Integer.parseInt(result.toString(), 2);
44+
}
45+
46+
/**
47+
* Entry point.
48+
*
49+
* @param args command line arguments
50+
*/
51+
public static void main(String[] args) {
52+
var result1 = binaryReversal("47");
53+
System.out.println(result1);
54+
var result2 = binaryReversal("2");
55+
System.out.println(result2);
56+
}
57+
58+
}

src/easy/BitwiseOne.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package easy;
2+
3+
/**
4+
* Have the function BitwiseOne(strArr) take the array of strings stored in strArr,
5+
* which will only contain two strings of equal length that represent binary numbers,
6+
* and return a final binary string that performed the bitwise OR operation
7+
* on both strings.
8+
* ---
9+
* A bitwise OR operation places a 0 in the new string
10+
* where there are zeroes in both binary strings,
11+
* otherwise it places a 1 in that spot.
12+
* ---
13+
* For example: if strArr is ["1001", "0100"] then your program
14+
* should return the string "1101"
15+
*/
16+
public class BitwiseOne {
17+
18+
/**
19+
* Bitwise One function.
20+
*
21+
* @param strArr an array of two binary strings
22+
* @return a binary string that performed the bitwise OR operation on both strings
23+
*/
24+
private static String bitwiseOne(String[] strArr) {
25+
String s1 = strArr[0];
26+
String s2 = strArr[1];
27+
StringBuilder result = new StringBuilder();
28+
for (int i = 0; i < s1.length(); i++) {
29+
int lgOr = Character.getNumericValue(s1.charAt(i)) | Character.getNumericValue(s2.charAt(i));
30+
result.append(lgOr);
31+
}
32+
return result.toString();
33+
}
34+
35+
/**
36+
* Entry point.
37+
*
38+
* @param args command line arguments
39+
*/
40+
public static void main(String[] args) {
41+
var result1 = bitwiseOne(new String[]{"001110", "100000"});
42+
System.out.println(result1);
43+
var result2 = bitwiseOne(new String[]{"110001", "111100"});
44+
System.out.println(result2);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)