/** * Creates a box with optional boxes inside */ const box = (code, boxes) => ({ code, (boxes ? { boxes } : {}) }); /** * Returns a copy of a box with bomb */ function withBomb(box) { return { box, bomb: true }; } /** * Finds the bomb in a box */ function pathToBomb(box, path = []) { const newPath = [path, box.code]; if (box.bomb) return newPath; if (!Array.isArray(box.boxes)) return []; return traverseBoxes(box.boxes, newPath); } /** * Finds the bomb in an array of boxes */ function traverseBoxes(boxes, path = []) { return boxes.reduce( (result, box) => (result.length > 0 ? result : pathToBomb(box, path)), [] ); } /** * The main function */ function findTheBomb(boxes) { if (boxes.length === 0) throw new Error('Empty array'); return traverseBoxes(boxes).join(' > '); } // Example usage /* const myBoxes = [ box('A', [ box('B', [ withBomb(box('C')) ]) ]) ]; console.log(findTheBomb(myBoxes)); // "A > B > C" */
export interface Box {code: stringbomb?: booleanboxes?: Box[]}- /**
- * Creates a box with optional boxes inside
- */
export const box = (code: string, boxes?: Box[]): Box => ({ code, ...( boxes ? { boxes } : {} )});- const box = (code, boxes) => ({ code, ...(boxes ? { boxes } : {}) });
- /**
- * Returns a copy of a box with bomb
- */
export function withBomb(box: Box) {return { ...box, bomb: true }- function withBomb(box) {
- return { ...box, bomb: true };
- }
- /**
- * Finds the bomb in a box
- */
export function pathToBomb(box: Box, path: string[] = []): string[] {const newPath = [...path, box.code]if (box.bomb) return newPathif (!Array.isArray(box.boxes)) return []return traverseBoxes(box.boxes, newPath)}- function pathToBomb(box, path = []) {
- const newPath = [...path, box.code];
- if (box.bomb) return newPath;
- if (!Array.isArray(box.boxes)) return [];
- return traverseBoxes(box.boxes, newPath);
- }
- /**
- * Finds the bomb in an array of boxes
- */
export function traverseBoxes(boxes: Box[], path: string[] = []): string[] {return boxes.reduce((result: string[], box) => (result.length > 0 ? result : pathToBomb(box, path)), [])- function traverseBoxes(boxes, path = []) {
- return boxes.reduce(
- (result, box) => (result.length > 0 ? result : pathToBomb(box, path)),
- []
- );
- }
- /**
- * The main function
- */
export function findTheBomb(boxes: Box[]): string {if (boxes.length === 0) throw new Error('Empty array')return traverseBoxes(boxes).join(' > ')- function findTheBomb(boxes) {
- if (boxes.length === 0) throw new Error('Empty array');
- return traverseBoxes(boxes).join(' > ');
- }
- // Example usage
- /*
- const myBoxes = [
- box('A', [
- box('B', [
- withBomb(box('C'))
- ])
- ])
- ];
- console.log(findTheBomb(myBoxes)); // "A > B > C"
- */
Thirdkoopavs.MonubiJustin23 days ago
How many more minutes
let d = 90; o = d < 3 ? "Almost there": "yuh gotta wait a little" console.log(o);
let distance = 90; // try changing this value to test different outputs- let d = 90;
output = distance < 3 ? "Almost there": "yuh gotta wait a little"console.log(output);- o = d < 3 ? "Almost there": "yuh gotta wait a little"
- console.log(o);
Thirdkoopavs.lMahesvara12 months ago
Dumb Rock Paper Scissors
function dumbRockPaperScissors(p1, p2) { if (p1 === p2) return "Draw"; const con = { Scissors: "Paper", Rock: "Scissors", Paper: "Rock", }; if (p2 === con[p1]) return "Player 1 wins"; return "Player 2 wins"; }
function dumbRockPaperScissors(player1, player2) {if (player1 === player2) return "Draw";- function dumbRockPaperScissors(p1, p2) {
- if (p1 === p2) return "Draw";
const condition = {- const con = {
- Scissors: "Paper",
- Rock: "Scissors",
- Paper: "Rock",
- };
if (player2 === condition[player1]) return "Player 1 wins";- if (p2 === con[p1]) return "Player 1 wins";
- return "Player 2 wins";
- }
Thirdkoopavs.aymxn12 months ago
Only unique characters
Strings
Thirdkoopavs.nikhil danekhu12 months ago
Code Golf: XOR
Thirdkoopavs.c4pe3 years ago
Divide by 3 without division operation
function dividedByThree(int $number): bool { if ($number === 0) {return false;} return $number % 3 === 0; }
- function dividedByThree(int $number): bool
- {
$abs = abs($number);if($abs < 10){return $abs == 3 || $abs == 6 || $abs == 9;}$sum = array_sum(str_split($abs,1));return dividedByThree($sum);- if ($number === 0) {return false;}
- return $number % 3 === 0;
- }
def colour_of_fruit(fruit): #Reds if fruit == "Apple": return "Red" if fruit == "Raspberry": return "Red" if fruit == "Strawberry": return "Red" #Orange if fruit == "Orange": return "Orange" #Yellows if fruit == "Banana": return "Yellow" if fruit == "Lemon": return "Yellow" if fruit == "Pineapple": return "Yellow" #Greens if fruit == "Avocado": return "Green" if fruit == "Lime": return "Green" if fruit == "Melon": return "Green" if fruit == "Pear": return "Green" #Blues if fruit == "Blueberry": return "Blue" if fruit == "Huckleberry": return "Blue" #Purples if fruit == "Plum": return "Purple" if fruit == "Grape": return "Purple" if fruit == "Maquiberry": return "Purple" return "Not a fruit!"
colour_of_fruit = lambda fruit : {"Apple": "Red", "Raspberry": "Red", "Strawberry": "Red","Orange": "Orange","Banana": "Yellow", "Lemon": "Yellow","Pineapple": "Yellow", "Avocado": "Green", "Lime": "Green","Melon": "Green", "Pear": "Green", "Blueberry": "Blue","Huckleberry": "Blue", "Plum": "Purple", "Grape": "Purple","Maquiberry": "Purple"}.get(fruit, "Not a fruit!")- def colour_of_fruit(fruit):
- #Reds
- if fruit == "Apple": return "Red"
- if fruit == "Raspberry": return "Red"
- if fruit == "Strawberry": return "Red"
- #Orange
- if fruit == "Orange": return "Orange"
- #Yellows
- if fruit == "Banana": return "Yellow"
- if fruit == "Lemon": return "Yellow"
- if fruit == "Pineapple": return "Yellow"
- #Greens
- if fruit == "Avocado": return "Green"
- if fruit == "Lime": return "Green"
- if fruit == "Melon": return "Green"
- if fruit == "Pear": return "Green"
- #Blues
- if fruit == "Blueberry": return "Blue"
- if fruit == "Huckleberry": return "Blue"
- #Purples
- if fruit == "Plum": return "Purple"
- if fruit == "Grape": return "Purple"
- if fruit == "Maquiberry": return "Purple"
- return "Not a fruit!"
public class Kumite { public static boolean boolCheck(boolean[] bools) { if (bools[0] && bools[1]) return true; if (bools[0] && bools[2]) return true; if (bools[1] && bools[2]) return true; return false; } }
- public class Kumite {
- public static boolean boolCheck(boolean[] bools) {
return bools[0] ? bools[1] || bools[2] : bools[1] && bools[2];- if (bools[0] && bools[1]) return true;
- if (bools[0] && bools[2]) return true;
- if (bools[1] && bools[2]) return true;
- return false;
- }
- }
Thirdkoopavs.Samir200420203 years ago
Relative Numbers
function relativeNumbers(a, b) { let m = Math.min(a, b)**(1/2) for (let i = 2; i <= m; i++) { //If A and B share a divisor if (a % i === 0 && b % i === 0) { return "non relative"; } } return "relative"; }
- function relativeNumbers(a, b) {
let arrOne = [];let arrTwo = [];for (let i = 2; i < 10; i++) {if (a % i === 0 && a !== i) {arrOne.push(i);}if(b % i === 0 && b !== i){arrTwo.push(i)}}for(let i = 0; i < arrOne.length; i++){for(let j = 0; j < arrTwo.length; j++){if(arrOne[i] === arrTwo[j]){return "non relative"}else return "relative"- let m = Math.min(a, b)**(1/2)
- for (let i = 2; i <= m; i++) {
- //If A and B share a divisor
- if (a % i === 0 && b % i === 0) {
- return "non relative";
- }
- }
- return "relative";
- }
Mathematics
function calcTokenCost($price, $token) { //Changes the price to be our alt token if ($price > 0) {$price = $token * round($price / $token);} $res = [$token, $price]; //Grabs the max between the original price and the delimiter return max($res); }
- function calcTokenCost($price, $token) {
return max($token, $price ? (int) $token * round($price / $token) : 0);- //Changes the price to be our alt token
- if ($price > 0) {$price = $token * round($price / $token);}
- $res = [$token, $price];
- //Grabs the max between the original price and the delimiter
- return max($res);
- }
Thirdkoopavs.vbetsun3 years ago