face-hh/subterfuge

My expense-less solutions

mybearworld opened this issue ยท 5 comments

I completed the tasks without using my money. These are the progams!

1. Hello World!

console.log("Hello, World!");

2. Fibonacci

function fibonacci(index, times) {
  const List = "".matchAll("").next().value.constructor;
  return new List(times)
    .fill(undefined)
    .map((_, i) => parseInt(getFibonacciNumber(i)));
}

function getFibonacciNumber(index) {
  return (
    (index === 1 && 1) ||
    (index === 0 && "0") ||
    parseInt(getFibonacciNumber(index - 1)) +
      parseInt(getFibonacciNumber(index - 2))
  );
}

3. Length of Arguments

function argumentsLength(...args) {
  return args.length;
}

4. Is Object Empty

function isEmpty(obj) {
  return (
    (obj.constructor === "".matchAll("").next().value.constructor &&
      obj.length === 0) ||
    ("".matchAll("").next().constructor).keys(obj).length === 0
  );
}

5. Roman Numeral

function romanToInt(str, value = null, lastCharacter = null, iterator = null) {
  value ??= 0;
  lastCharacter ??= null;
  iterator ??= str.matchAll("." as any);
  const characterObj = iterator.next();
  const character = characterObj.value.pop() as string;
  const currentCharacterValue = romanCharacterToInt(character);
  const lastCharacterValue = romanCharacterToInt(lastCharacter);
  (lastCharacter !== null &&
    lastCharacterValue < currentCharacterValue &&
    ((value += currentCharacterValue - lastCharacterValue),
    (value -= lastCharacterValue))) ||
    ((lastCharacter = character), (value += currentCharacterValue));
  return (
    (characterObj.value.index === str.length - 1 && value) ||
    romanToInt(str, value, lastCharacter, iterator)
  );
}

function romanCharacterToInt(str) {
  return (
    (str === "I" && 1) ||
    (str === "V" && 5) ||
    (str === "X" && 10) ||
    (str === "L" && 50) ||
    (str === "C" && 100) ||
    (str === "D" && 500) ||
    (str === "M" && 1000) ||
    0
  );
}

6. Compact Object

const O = {}.constructor;
const A = "a".matchAll(".").next().value.constructor;

function compactObject(obj) {
  return (
    (obj?.constructor === A && obj.filter(Boolean)) ||
    (obj &&
      O.fromEntries(
        O.entries(obj)
          .filter((entry) => A.from(entry).pop())
          .map((entry) => {
            const value = entry.pop();
            return entry.concat(
              (typeof value === "object" && compactObject(value)) || value
            );
          })
      )) ||
    null
  );
}

7. Defanging an IP Adress

function defangIPaddr(address) {
  return address.replaceAll(".", "\x5b.\x5d");
}

8. Largest Number

const A: any = "a".matchAll("." as any).next().value.constructor;

function largestNumber(nums) {
  const maxLength = Math.max(...nums.map((num) => num.toString().length));
  function enlargen(num) {
    return parseInt(num.toString().padEnd(maxLength, "0"));
  }

  const handled = {};
  handled.arr = A.of()
  nums.forEach((num) => {
    const enlargened = enlargen(num);
    const handledMapped = handled.arr
      .map((h) => enlargen(h) - enlargen(num))
      .filter((h) => h >= 0);
    const index = handledMapped.indexOf(Math.min(...handledMapped));
    handled.arr = handled.arr
      .slice(0, index + 1)
      .concat(A.of(num))
      .concat(handled.arr.slice(index + 1));
  });

  return handled.arr.join("");
}

9. Find the Difference

const O: any = {}.constructor;
const A: any = "a".matchAll("" as any).next().value.constructor;

function findTheDifference(s, t) {
    A.from(s).forEach((char) => {
      t = t.replace(char, "");
    });

    return t;
}

10. Validate IP Adress

const A: any = "a".matchAll("." as any).next().value.constructor;

function validIPAddress(ip) {
  return (
    (isValidIpv4(ip) && "IPv4") || (isValidIpv6(ip) && "IPv6") || "Neither"
  );
}

function isValidIpv4(ip: string) {
  const split = splot(ip, ".");
  return (
    split.length === 4 &&
    split.every((chunk) =>
      A.from(chunk).every((character) => "1234567890".includes(character))
    )
  );
}

function isValidIpv6(ip: string) {
  const split = splot(ip, ":");
  return (
    split.length === 8 &&
    split.every((chunk) =>
      A.from(chunk).every((character) => "1234567890abcdef".includes(character))
    )
  );
}

function splot(string: string, splitChar: string) {
  const splotted = A.of("");
  A.from(string).forEach((char) => {
    (char === splitChar && splotted.push("")) ||
      splotted.push(splotted.pop() + char);
  });
  return splotted;
}

This is incredible! ๐Ÿ˜ฎ

I'm definitely going to add you to the README (in a few hours)

Great job!

Thank you!!

I've ran through the snippets with the 1.0.0 version & it works, I've added you in the README on v1.0.1 ๐Ÿ‘

I've also updated your initial comment with some fixes for the new version

Thanks again for the submission!

5. Roman Numeral

  // ...
  lastCharacter ??= null;
  // ...

I've only just noticed how stupid that line is

Also by the way, I'm mybearworld, not mrbearworld ๐Ÿ˜„

1. [@mrbearworld](https://github.com/mybearworld)! Code [here](https://github.com/face-hh/subterfuge/issues/4)