Skip to content
48 changes: 48 additions & 0 deletions src/easy/AdditivePersistence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package easy;

/**
* Have the function AdditivePersistence(num) take the num parameter being passed
* which will always be a positive integer
* and return its additive persistence which is the number of times
* you must add the digits in num until you reach a single digit.
* ---
* For example: if num is 2718 then your program
* should return 2 because 2 + 7 + 1 + 8 = 18
* and 1 + 8 = 9, and you stop at 9.
*/
public class AdditivePersistence {

/**
* Additive Persistence function.
*
* @param num input number
* @return additive persistence which is the number of times
*/
private static int additivePersistence(int num) {
int times = 0;
int added = num;
while (added > 9) {
int sum = 0;
String[] intArr = Integer.toString(added).split("");
for (String i : intArr) {
sum += Integer.parseInt(i);
}
added = sum;
times++;
}
return times;
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = additivePersistence(199);
System.out.println(result1);
var result2 = additivePersistence(913);
System.out.println(result2);
}

}
36 changes: 36 additions & 0 deletions src/easy/AlphabetSoup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package easy;

import java.util.Arrays;

/**
* Have the function AlphabetSoup(str) take the str string parameter being passed
* and return the string with the letters in alphabetical order (i.e. hello becomes ehllo).
* Assume numbers and punctuation symbols will not be included in the string.
*/
public class AlphabetSoup {

/**
* Alphabet Soup function.
*
* @param str input string
* @return the string with the letters in alphabetical order
*/
private static String alphabetSoup(String str) {
char[] letters = str.toCharArray();
Arrays.sort(letters);
return String.valueOf(letters);
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = alphabetSoup("leftfield");
System.out.println(result1);
var result2 = alphabetSoup("underworld");
System.out.println(result2);
}

}
66 changes: 66 additions & 0 deletions src/easy/ArithGeo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package easy;

/**
* Have the function ArithGeo(arr) take the array of numbers stored in arr
* and return the string "Arithmetic" if the sequence follows an arithmetic pattern
* or return "Geometric" if it follows a geometric pattern.
* ---
* If the sequence doesn't follow either pattern return -1.
* An arithmetic sequence is one where the difference between
* each of the numbers is consistent, where in a geometric sequence,
* each term after the first is multiplied by some constant or common ratio.
* ---
* Arithmetic example: [2, 4, 6, 8] and Geometric example: [2, 6, 18, 54].
* Negative numbers may be entered as parameters, 0 will not be entered,
* and no array will contain all the same elements.
*/
public class ArithGeo {

/**
* Arith Geo function.
*
* @param arr input array of integers
* @return the string "Arithmetic" if the sequence follows an arithmetic pattern
*/
private static String arithGeo(int[] arr) {

int arithInterval = arr[1] - arr[0];
int geoInterval = arr[1] / arr[0];
int arithCount = 0;
int geoCount = 0;

for (int i = 0; i < arr.length - 1; i++) {
if (arr[i + 1] - arr[i] == arithInterval) {
arithCount++;
}
if (arr[i + 1] / arr[i] == geoInterval) {
geoCount++;
}
}

if (arithCount == arr.length - 1) {
return "Arithmetic";
}

if (geoCount == arr.length - 1) {
return "Geometric";
}

return "-1";
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = arithGeo(new int[]{2, 4, 6, 8});
System.out.println(result1);
var result2 = arithGeo(new int[]{2, 6, 18, 54});
System.out.println(result2);
var result3 = arithGeo(new int[]{-3, -4, -5, -6, -7});
System.out.println(result3);
}

}
73 changes: 73 additions & 0 deletions src/easy/ArrayAdditionOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package easy;

import static java.lang.Math.pow;

import java.util.Arrays;

/**
* Have the function ArrayAdditionI(arr) take the array of numbers stored in arr
* and return the string true if any combination of numbers in the array
* (excluding the largest number) can be added up to equal the largest number in the array,
* otherwise return the string false.
* ---
* For example: if arr contains [4, 6, 23, 10, 1, 3] the output
* should return true because 4 + 6 + 10 + 3 = 23.
* ---
* The array will not be empty, will not contain all the same elements,
* and may contain negative numbers.
*/
public class ArrayAdditionOne {

/**
* Left pad the string with zeroes,
* e.g. padLeft("fade", 8) -> "0000fade"
*
* @param str string to be padded
* @param len new fixed length after applying the padding
* @return padded string (e.g. 000000xxx)
*/
private static String padLeft(String str, int len) {
return String.format("%" + len + "s", str).replace(" ", "0");
}

/**
* Array Addition I function.
*
* @param arr input array of integers
* @return "true" if any combination can be added up to equal the largest number in the array
*/
private static String arrayAdditionOne(int[] arr) {
Arrays.sort(arr);
int largest = arr[arr.length - 1];
int oneChar = "1".charAt(0);

for (int i = 0; i < pow(2, arr.length); i++) {
String bin = Integer.toBinaryString(i);
String combo = padLeft(bin, arr.length - 1);
int sum = 0;
for (int j = 0; j < combo.length(); j++) {
if (combo.charAt(j) == oneChar) {
sum += arr[j];
}
if (sum == largest) {
return "true";
}
}
}

return "false";
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = arrayAdditionOne(new int[]{4, 6, 23, 10, 1, 3});
System.out.println(result1);
var result2 = arrayAdditionOne(new int[]{2, 6, 18});
System.out.println(result2);
}

}
62 changes: 62 additions & 0 deletions src/easy/BasicRomanNumerals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package easy;

import java.util.HashMap;

/**
* Have the function BasicRomanNumerals(str) read str which will be a string of Roman numerals.
* The numerals being used are: I for 1, V for 5, X for 10, L for 50,
* C for 100, D for 500 and M for 1000.
* In Roman numerals, to create a number like 11 you simply add a 1 after the 10,
* so you get XI. But to create a number like 19, you use the subtraction notation
* which is to add I before an X or V (or add an X before an L or C).
* So 19 in Roman numerals is XIX.
* ---
* The goal of your program is to return the decimal equivalent of the Roman numeral given.
* For example: if str is "XXIV" your program should return 24
*/
public class BasicRomanNumerals {

/**
* Basic Roman Numerals function.
*
* @param str input string
* @return the decimal equivalent of the Roman numeral given
*/
private static int basicRomanNumerals(String str) {
HashMap<String, Integer> numerals = new HashMap<>();
numerals.put("M", 1000);
numerals.put("D", 500);
numerals.put("C", 100);
numerals.put("X", 10);
numerals.put("L", 50);
numerals.put("V", 5);
numerals.put("I", 1);
// reversing the string makes the parsing easier
char[] reversed = new StringBuilder(str).reverse().toString().toCharArray();
int result = 0;
int prevValue = 0;
for (char cr : reversed) {
int thisValue = numerals.get(Character.toString(cr));
if (thisValue > prevValue) {
result += thisValue;
} else {
result -= thisValue;
}
prevValue = thisValue;
}
return result;
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = basicRomanNumerals("XXIV");
System.out.println(result1);
var result2 = basicRomanNumerals("XLVI");
System.out.println(result2);
}

}
58 changes: 58 additions & 0 deletions src/easy/BinaryReversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package easy;

/**
* Have the function BinaryReversal(str) take the str parameter being passed,
* which will be a positive integer, take its binary representation
* (padded to the nearest N * 8 bits), reverse that string of bits,
* and then finally return the new reversed string in decimal form.
* ---
* For example: if str is "47" then the binary version of this integer is 101111,
* but we pad it to be 00101111. Your program should reverse this binary string
* which then becomes: 11110100 and then finally return
* the decimal version of this string, which is 244.
*/
public class BinaryReversal {

/**
* Left pad the string with zeroes,
* e.g. padLeft("fade", 8) -> "0000fade"
*
* @param str string to be padded
* @param len new fixed length after applying the padding
* @return padded string (e.g. 000000xxx)
*/
private static String padLeft(String str, int len) {
return String.format("%" + len + "s", str).replace(" ", "0");
}

/**
* Binary Reversal function.
*
* @param str input string
* @return the decimal version of this string
*/
private static int binaryReversal(String str) {
String binStr = Integer.toBinaryString(Integer.parseInt(str));
int add = binStr.length() % 8 == 0 ? 0 : 1;
int pad = add + binStr.length() / 8;
String padStr = padLeft(binStr, pad * 8);
StringBuilder result = new StringBuilder();
for (int i = padStr.length() - 1; i >= 0; i--) {
result.append(Character.getNumericValue(padStr.charAt(i)));
}
return Integer.parseInt(result.toString(), 2);
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = binaryReversal("47");
System.out.println(result1);
var result2 = binaryReversal("2");
System.out.println(result2);
}

}
47 changes: 47 additions & 0 deletions src/easy/BitwiseOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package easy;

/**
* Have the function BitwiseOne(strArr) take the array of strings stored in strArr,
* which will only contain two strings of equal length that represent binary numbers,
* and return a final binary string that performed the bitwise OR operation
* on both strings.
* ---
* A bitwise OR operation places a 0 in the new string
* where there are zeroes in both binary strings,
* otherwise it places a 1 in that spot.
* ---
* For example: if strArr is ["1001", "0100"] then your program
* should return the string "1101"
*/
public class BitwiseOne {

/**
* Bitwise One function.
*
* @param strArr an array of two binary strings
* @return a binary string that performed the bitwise OR operation on both strings
*/
private static String bitwiseOne(String[] strArr) {
String s1 = strArr[0];
String s2 = strArr[1];
StringBuilder result = new StringBuilder();
for (int i = 0; i < s1.length(); i++) {
int lgOr = Character.getNumericValue(s1.charAt(i)) | Character.getNumericValue(s2.charAt(i));
result.append(lgOr);
}
return result.toString();
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = bitwiseOne(new String[]{"001110", "100000"});
System.out.println(result1);
var result2 = bitwiseOne(new String[]{"110001", "111100"});
System.out.println(result2);
}

}
Loading