Created
April 7, 2020 16:20
-
-
Save sarg/f0b952520b37dbc4a97bc1e9c6b1f18f to your computer and use it in GitHub Desktop.
test problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Mask all digits in credit card number unless they are first or last four characters. | |
Never mask credit cards less than 6 characters | |
*/ | |
class CreditCard { | |
/** | |
* Mask credit card number for display. | |
*/ | |
public static String maskify(String creditCardNumber) { | |
// Never mask credit cards with less than 6 characters. | |
if (creditCardNumber.length() < 6) return creditCardNumber; | |
char[] chars = creditCardNumber.toCharArray(); | |
for (int i = 0; i < chars.length; i++) { | |
// Mask all digits (0-9) with #, unless they are first or last four characters. | |
if (i == 0 || i >= chars.length - 4) continue; | |
// Never mask non-digit characters. | |
if (chars[i] >= '0' && chars[i] <= '9') { | |
chars[i] = '#'; | |
} | |
} | |
return new String(chars); | |
} | |
} | |
/* | |
Add ordinal indicator to a number as specified by english rules: | |
https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers | |
*/ | |
class Challenge { | |
/** | |
* Append ordinal indicator to the given number. | |
* @param number - natural number | |
* @return string representation with ordinal indicator | |
*/ | |
public static String numberToOrdinal( Integer number ) { | |
String suffix = number > 0 ? "th" : ""; | |
if ((number / 10) % 10 != 1) { | |
switch (number % 10) { | |
case 1: suffix = "st"; break; | |
case 2: suffix = "nd"; break; | |
case 3: suffix = "rd"; break; | |
} | |
} | |
return number.toString() + suffix; | |
} | |
} | |
import java.util.*; | |
/* | |
Implement RPN calculator. Allowed are +-*/ integers and floats. Return float. | |
Ignore division by zero and possible stack errors. | |
*/ | |
public class Calc { | |
private interface MathOps { | |
float operation(float a, float b); | |
} | |
private static void apply(Stack<Float> stack, MathOps math) { | |
float op1 = stack.pop(); | |
float op2 = stack.pop(); | |
stack.push(math.operation(op2, op1)); | |
} | |
/** | |
* Evaluate RPN expression. Only + - * / on float/int arguments are supported. | |
* All numbers are treated as floats so expect precision quirks. | |
* | |
* @param expr - reverse polish notation expression | |
* @return evaluation result (last operation on stack) | |
* @throws NumberFormatException for unknown tokens, e.g. "5 4 ." | |
* @throws EmptyStackException for unbalanced expressions, e.g. "5 +" | |
*/ | |
public double evaluate(String expr) { | |
StringTokenizer tokenizer = new StringTokenizer(expr, " "); | |
Stack<Float> stack = new Stack<Float>(); | |
while (tokenizer.hasMoreTokens()) { | |
String token = tokenizer.nextToken(); | |
switch (token) { | |
case "+": apply(stack, (a, b) -> a + b); break; | |
case "-": apply(stack, (a, b) -> a - b); break; | |
case "*": apply(stack, (a, b) -> a * b); break; | |
case "/": apply(stack, (a, b) -> a / b); break; | |
default: | |
stack.push(Float.parseFloat(token)); | |
} | |
} | |
return stack.empty() ? 0 : stack.pop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment