Skip to content

Instantly share code, notes, and snippets.

@rabestro
rabestro / isogram.sh
Created March 10, 2023 16:59
Determine if a word or phrase is an isogram.
#!/usr/bin/env bash
main () {
local -r phrase=${1@L}
local symbols=${phrase//[[:space:]-]/}
for letter in {a..z}
do
symbols="${symbols/$letter/}"
done
@rabestro
rabestro / if.awk
Last active February 13, 2023 20:48
The script extracts the condition expressions from the Java code
#!/use/bin/env gawk --exec
#
# Copyright (c) 2023 Jegors Čemisovs
# License: Apache-2.0 license
#
# The script extracts if statements from Java code.
#
BEGIN {
RS = "[[:space:]]*[;{][[:space:]]*"
}
@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.
@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 / 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 / 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()
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 / 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})
@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 / 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();