Skip to content

Instantly share code, notes, and snippets.

@rabestro
rabestro / EnumToString.java
Last active April 21, 2020 14:30
How to convert Enum to String
import java.util.stream.*;
public class EnumToString {
public static void main(String[] args) {
String colors = Stream.of(MyColors.values())
.map(Enum::toString)
.collect(Collectors.joining(", "));
System.out.println(colors);
@rabestro
rabestro / README.md
Created May 11, 2020 09:13
For loop and ranges → The roots of equation

There are numbers a,b,c,d. Output in ascending order all the integers between 0 and 1000 (inclusively), which are the roots of the equation

a⋅x^3 + b⋅x^2 + c⋅x + d = 0

If a specified interval does not contain any roots of the equation, do not output anything.

@rabestro
rabestro / Main.java
Last active July 26, 2020 15:54
Check sudoku of any dimension
import java.util.Scanner;
import java.util.stream.IntStream;
import static java.util.stream.IntStream.range;
public class Main {
public static void main(String[] args) {
final var scanner = new Scanner(System.in);
final var n = scanner.nextInt();
@rabestro
rabestro / WTF.java
Last active December 14, 2020 09:27
Bad Code Coding Challenge - Print the multiplication table
public class WTF {
public static void main(String[] args) {
Integer W, F=0x2F;
for (int T; (W=T=F)>0 & W==F; ++F)
System.out.printf((W=T-=057)!=0 && 0!=(T%=011) ? "%5d":"%n%5d",(1+W/9)*++T);
}
}
@rabestro
rabestro / Turtle.java
Last active December 16, 2020 20:21
Bad Code Coding Challenge #51 - Dude, where's my turtle?
// Bad Code Coding Challenge #51 - Dude, where's my turtle?
// https://www.reddit.com/r/badcode/comments/k8eh7a/bad_code_coding_challenge_51_dude_wheres_my_turtle/
public class Turtle {
public static void main(String[] args) {
System.out.println(java.text.MessageFormat.format(
"x = {1}, y = {0}, direction = {2, choice, 0#north|1#east|2#south|3#west}",
new java.util.Scanner(System.in).useDelimiter("\\R").tokens()
.mapToInt(s -> Integer.parseInt(s.substring(7).trim()) << s.charAt(0) / 'r' * ' ' / 2)
.mapToObj(data -> new Integer[] {(int)(short) data, (data >> 16) / 90})
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int findSecondLargestNumber(int[] numbers) {
// write your code here
return Arrays.stream(numbers)
.distinct()
.map(i -> -i)
@rabestro
rabestro / Main.java
Created March 24, 2021 17:11
The length of the sequence
import java.util.Scanner;
import static java.util.function.Predicate.not;
class Main {
public static void main(String[] args) {
System.out.println(
new Scanner(System.in)
.tokens()
.takeWhile(not("0"::equals))
.count()
@rabestro
rabestro / ObjectsIsNull.java
Last active August 16, 2021 09:07
Optional.isNull(obj) vs obj == null
import java.util.Scanner;
import java.util.Objects;
public class Main
{
public static void main(String[] args) {
Object obj = null;
if (obj == null) {
@rabestro
rabestro / warodds.js
Last active July 2, 2022 13:04
Warlords Battle Calculator
export default function calculateBattle(defArmy, attArmy) {
const MAX_ATTACKER_ARMY = 8;
const MAX_DEFENDER_ARMY = MAX_ATTACKER_ARMY * 4;
const HITS = 2;
const ATTACKER_STATS = MAX_ATTACKER_ARMY * HITS + 1;
const DEFENDER_STATS = MAX_DEFENDER_ARMY * HITS + 1;
const STATS = ATTACKER_STATS + DEFENDER_STATS;
const cache = new Array(ATTACKER_STATS * DEFENDER_STATS);
return calculateStatistics(1, defArmy.length * HITS, attArmy.length * HITS);
@rabestro
rabestro / change.awk
Last active February 5, 2023 17:19
Correctly determine the fewest number of coins to be given to a customer such that the sum of the coins' value would equal the correct amount of change.
#!/usr/bin/gawk --exec
#
# Copyright (c) 2023 Jegors Čemisovs
# License: Apache-2.0 license
#
# Correctly determine the fewest number of coins to be given to a customer
# such that the sum of the coins' value would equal the correct amount of change.
#
# The first line of the file is a list where the nominations
# and all subsequent lines are the amounts to provide change.