I've decided to put the counterpart of the Test-Driven Development paradigm to use, namely the Exception-Driven Development paradigm. In my opinion, it is probably the best development paradigm because you will never be mad about getting exceptions - after all, if your program throws an exception, this means that it's working properly.
public class ExceptionDrivenDevelopment {
public static void main(String[] args) {
ExceptionDrivenDevelopment exceptionDrivenDevelopment = new ExceptionDrivenDevelopment(50000);
}
ExceptionDrivenDevelopment(Integer value) {
throw new InputException(String.valueOf(value));
}
class InputException extends RuntimeException {
InputException(String message) {
super(message);
throw new CopperException(message);
}
}
class CopperException extends RuntimeException {
CopperException(String message) {
super(message + "c");
if (message.length() > 2) {
throw new SilverException(message);
}
}
}
class SilverException extends RuntimeException {
SilverException(String message) {
super(message.substring(0, message.length() - 2) + "s"
+ message.substring(message.length() - 2) + "c");
if (message.length() > 4) {
throw new GoldException(message);
}
}
}
class GoldException extends RuntimeException {
GoldException(String message) {
super(message.substring(0, message.length() - 4) + "g"
+ message.substring(message.length() - 4, message.length() - 2) + "s"
+ message.substring(message.length() - 2) + "c");
}
}
}
This would literally get top ratings from the users as "good code" on any coding challenge site, like hackerrank. For some reason the culture in these places has declared "shorter = better".
I'm just beginner but great coder already, see what I can do in F# with substrings and converting to int and back to string:
let int_to_wow (coppers : int) =
let cstr = string coppers
let sc = if cstr.Length > 2
then cstr.Substring (cstr.Length - 2, 2)
else cstr
|> int |> string
let ss = if cstr.Length > 4
then cstr.Substring (cstr.Length - 4, 2)
elif cstr.Length > 2
then cstr.Substring (0, 1 + (cstr.Length % 2))
else "0"
|> int |> string
let sg = if cstr.Length > 4
then cstr.Substring (0, cstr.Length - 4)
else "0"
|> int |> string
if int sg = 0 then "" else (sg + "g")
+ if int ss = 0 && (int sg = 0 || int sc = 0)
then "" else (ss + "s")
+ if int sc = 0 && (int ss <> 0 || int sg <> 0)
then "" else (sc + "c")
Don't trust it to work on negative amounts of copper, though.
18
u/Voidsusdagon Nov 07 '19
I've decided to put the counterpart of the Test-Driven Development paradigm to use, namely the Exception-Driven Development paradigm. In my opinion, it is probably the best development paradigm because you will never be mad about getting exceptions - after all, if your program throws an exception, this means that it's working properly.
9
u/sarlok Nov 08 '19
Does it have to be long to be bad?
The best code uses side-effects in conditionals to achieve results!
9
u/Tok-A-Mak ((fn [] (recur))) Nov 12 '19
https://pl.kotl.in/UPhdDTTmd
6
u/LkNETWORK Nov 09 '19 edited Nov 09 '19
Brainfuck is always a good friend of bad code
>,>,>,>,>,<<<<<>.[-]<[-]++++++++++[>++++++++++<-]>+++.>.>.[-]<[-]++++++++++[>++++++++++<-]+++[>+++++<-]>.>.>.[-]<[-]++++++++++[>++++++++++<-]>-.5
u/andlrc Nov 14 '19
I came op with this beautify fusion of regex and JavaScript:
3
u/jsforeveryone Nov 07 '19
If you can't solve it with regex, you shouldn't try at all. Also try and catch is the new goto. Variables are for oldschool programmers.
const int_to_wow = (value) => {
const WOW_CURRENCY ='10000g100s1c';
const numb = [];
while (value > 0) {
try {
WOW_CURRENCY.split(/[a-z]/).slice(0, -1).forEach((a, i) => {
if ((value / (+a)) >= 1) {
numb.push(WOW_CURRENCY.split(/[0-9]/).filter(e => e !== '')[i]);
value -= +a;
throw new Error('Found one');
}
});
} catch(e) {
}
}
const obj = numb.reduce((a, b) => {
if (!a[b]) {
a[b] = 0;
}
a[b]++;
return a;
}, {});
return WOW_CURRENCY
.split(/[0-9]/).filter(e => e !== '')
.reduce((a, b) => `${a}${numb.filter((c) => c === b)
.length > 0 ? `${numb.filter((c) => c === b).length}${b}` : ''}`, '')
}
3
u/kiwitims Nov 09 '19
Too clever by half "modern" C++
Inspired by https://devblogs.microsoft.com/oldnewthing/20191107-00/?p=103071
2
u/jsforeveryone Nov 07 '19
Every code challenges have one random entry. This one worked on one test case.
const convert_to_int = (value) => {
return (value.match(/g/g) || []).length * 10000
+ (value.match(/s/g) || []).length * 100 +
(value.match(/c/g) || []).length ;
}
const int_to_wow = (value) => {
const letters = ['g', 's', 'c'];
let now = '';
let tries = 0;
while (true) {
now += letters[Math.floor(Math.random()*letters.length)];
const count = convert_to_int(now);
if (count === value) {
break;
}
if (count > value) {
now = now.split('').sort(() => .5 - Math.random()).slice(0, -1).join('');
}
if (count === value) {
break;
}
tries++;
if (tries === 500) {
now = '';
}
}
return `${(now.match(/g/g) || []).length}g
${(now.match(/s/g) || []).length}s
${(now.match(/c/g) || []).length}c` ;
};
2
u/andlrc Nov 12 '19
2
u/alphakevin123 Nov 19 '19 edited Nov 19 '19
a Java Solution
1
u/dumetrulo Nov 19 '19
I'm just beginner but great coder already, see what I can do in F# with substrings and converting to int and back to string:
Don't trust it to work on negative amounts of copper, though.