face-hh / subterfuge

A CLI tool designed to gamify your TypeScript learning.
Apache License 2.0
101 stars 10 forks source link

Another not buying run #12

Closed nk980113 closed 4 months ago

nk980113 commented 4 months ago

Mostly by using Reflect.get getting global constants; All pure JS solutions

1. Hello World

console.log('Hello, World!')

2. Fibonacci

And yap, I used Binet formula for this one

const nthFib = (() => {
    const sqrt5 = Math.sqrt(5);
    const a = (1 + sqrt5) / 2;
    const b = 1 - a;
    return (n) => Math.round((a ** n - b ** n) / sqrt5);
})();

const A = Reflect.get(globalThis, 'Ar' + 'ray');

function fibonacci(_, n) {
    return A(n).fill(undefined).map((_, i) => nthFib(i));
}

3. Length of Arguments

function argumentsLength() {
    return arguments.length;
}

4. Is Object Empty

const A = Reflect.get(globalThis, 'Ar' + 'ray');

function isEmpty(obj) {
    return obj instanceof A ? obj.length === 0 : Reflect.ownKeys(obj).length === 0;
}

5. Roman Numeral

const A = Reflect.get(globalThis, 'Ar' + 'ray');

function romanToInt(str) {
    const nums = A.from(str).map((c) => c === 'I' ? 1 : c === 'V' ? 5 : c === 'X' ? 10 : c === 'L' ? 50 : c === 'C' ? 100 : c === 'D' ? 500 : 1000);
    const maxs = nums.reduceRight((a, c) => {
        a.unshift(Math.max(c, a.at(0) || -Infinity));
        return a;
    }, A());
    return nums.reduce((a, v, i) => a + (v < maxs.at(i) ? -v : v), 0);
}

6. Compact Object

const A = Reflect.get(globalThis, 'Ar' + 'ray');

function compactObject(obj) {
    return obj ? (() => {
        const newObj = obj instanceof A ? A() : {};
        Reflect.ownKeys(obj).forEach((k) => {
            Reflect.get(obj, k) && (obj instanceof A ? k !== 'length' : true) && Reflect.set(newObj, k, Reflect.get(obj, k));
        });
        return newObj;
    })() : obj;
}

7. Defanging an IP Address

Just saw other's solution and knew how stupid I am

function split(str, sep) {
    return Reflect.get(String.prototype, 'split').call(str, sep);
}

function defangIPaddr(address) {
    return split(address, '.').join(String.fromCharCode(91) + '.]');
}

8. Largest Number

const A = Reflect.get(globalThis, 'Ar' + 'ray');

function permute(num) {
    switch (num.length) {
        case 0:
            return A();
        case 1:
            return A(num);
        case 2:
            return A(num, num.slice().reverse())
        default:
            return num.flatMap((v, i) => {
                const rest = num.filter((_, i2) => i2 !== i);
                return permute(rest).map((a) => {
                    a.unshift(v);
                    return a;
                });
            });

    }
}

function largestNumber(num) {
    return String(Math.max(...permute(num).map((v) => parseInt(v.join('')))));
}

9. Find the Difference

const A = Reflect.get(globalThis, 'Ar' + 'ray');

function findTheDifference(s, t) {
    const tArr = A.from(t);
    A.from(s).forEach((c) => {
        tArr.splice(tArr.indexOf(c), 1);
    });
    return Reflect.get(tArr, 0);
}

10. Validate IP Address

function split(str, sep) {
    return Reflect.get(String.prototype, 'split').call(str, sep);
}

const A = Reflect.get(globalThis, 'Ar' + 'ray');

function validIPAddress(ip) {
    return isIPv4(ip) ? 'IPv4' :
        isIPv6(ip) ? 'IPv6' : 'Neither';
}

function isIPv4(ip) {
    const ipArr = split(ip, '.').map((v) => parseInt(v));
    return ipArr.length === 4 && ipArr.every((v) => v >= 0 && v <= 255);
}

function isIPv6(ip) {
    const ipArr = split(ip, ':').map((v) => parseInt(v, 16));
    return ipArr.length === 8 && ipArr.every((v) => v >= 0 && v <= 65535);
}
face-hh commented 4 months ago

Hey, amazing submission!

Unfortunately if I add more entries to the README (which is already at 3), I'll have to verify each submission - and I don't really have time for that.

So instead, I'll label the issue as "no-buying submission" and close it. Others what wish to browse through the submissions can do it through the issue label feature.

Sorry for the inconvenience, and again, wonderful submission! :D