Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My expense-less solutions #4

Closed
mybearworld opened this issue Feb 12, 2024 · 5 comments
Closed

My expense-less solutions #4

mybearworld opened this issue Feb 12, 2024 · 5 comments

Comments

@mybearworld
Copy link

mybearworld commented Feb 12, 2024

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;
}
@face-hh
Copy link
Owner

face-hh commented Feb 12, 2024

This is incredible! 😮

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

Great job!

@mybearworld
Copy link
Author

Thank you!!

@face-hh
Copy link
Owner

face-hh commented Feb 12, 2024

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!

@face-hh face-hh closed this as completed Feb 12, 2024
@mybearworld
Copy link
Author

mybearworld commented Feb 12, 2024

5. Roman Numeral

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

I've only just noticed how stupid that line is

@mybearworld

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants