Solve24 problem

Solve24 problem
0

#1

Seems that the test cases are not allowing all correct solutions. Eg for solve24(‘1234’) a valid solution like (1+2+3)*4 is not accepted.


#2

Could you oost your code or if it’s one if the challenges then the link to the challenge?
That would help us in helping you properly.


#3

the link is https://learn.freecodecamp.org/coding-interview-prep/rosetta-code/24-game

code

function solve24(numStr) {

let solution = "no solution exists."

const RES = 24
const perm = [
    "1234", "1243", "1324", "1342", "1423", "1432",
    "2134", "2143", "2314", "2341", "2413", "2431",
    "3124", "3142", "3214", "3241", "3412", "3421",
    "4123", "4132", "4213", "4231", "4312", "4321",
]

function app(v1, v2, op) {
    const a = typeof v1 === 'number' ? v1 : v1.val
    const b = typeof v2 === 'number' ? v2 : v2.val

    const sa = typeof v1 === 'number' ? v1 : v1.str
    const sb = typeof v2 === 'number' ? v2 : v2.str

    if (v1 === false) {
        return false
    }

    if (v2 === false) {
        return false
    }

    if (b === 0 && op === '/') {
        return false
    }

    // console.log(a,b)

    switch (op) {
        case '+': return {
            str: '(' + sa + op + sb + ')',
            val: a + b
        }
        case '-': return {
            str: '(' + sa + op + sb + ')',
            val: a - b
        }
        case '*': return {
            str: '(' + sa + op + sb + ')',
            val: a * b
        }
        case '/': return {
            str: '(' + sa + op + sb + ')',
            val: a / b
        }
        default:
            return false
    }
}

function comb1(a, o1, b, o2, c, o3, d) {
    return app(app(app(a, b, o1), c, o2), d, o3)
}
function comb2(a, o1, b, o2, c, o3, d) {
    return app(app(a, b, o1), app(c, d, o3), o2)
}
function comb3(a, o1, b, o2, c, o3, d) {
    return app(a, app(app(b, c, o2), d, o3), o1)
}
function comb4(a, o1, b, o2, c, o3, d) {
    return app(app(a, app(b, c, o2), o1), d, o3)
}



function cleanup(str) {
    const len = str.length
    return str.substring(1,len-1)
}

function anyComb(f,...rest){
    const result = f(...rest)
    if (result == false) {
        return false
    }
    if (result.val === RES) {
        console.log(result)
        solution = cleanup(result.str)
        return true
    }
} 

function allComb(a, o1, b, o2, c, o3, d) {
    let found = anyComb(comb1,a, o1, b, o2, c, o3, d)
    if ( ! found ) found = anyComb(comb2,a, o1, b, o2, c, o3, d)
    if ( ! found ) found = anyComb(comb3,a, o1, b, o2, c, o3, d)
    if ( ! found ) found = anyComb(comb4,a, o1, b, o2, c, o3, d)
    return found
}

function allOps(a, b, c, d) {
    const _ops = ['+', '-', '*', '/']
    for (let i1 = 0; i1 < 4; i1++) {
        for (let i2 = 0; i2 < 4; i2++) {
            for (let i3 = 0; i3 < 4; i3++) {
                if (allComb(a, _ops[i1], b, _ops[i2], c, _ops[i3], d)) return true
            }
        }
    }
}

function onePerm(perm, arr) {
    const a = arr[perm.charAt(0) - 1]
    const b = arr[perm.charAt(1) - 1]
    const c = arr[perm.charAt(2) - 1]
    const d = arr[perm.charAt(3) - 1]

    return allOps(a, b, c, d)
}


if (numStr.length !== 4) return false
const arr = numStr.split('').map((e) => Number.parseInt(e))

console.log(arr)

for(let i=0; i< perm.length ; i++) {
    if ( onePerm(perm[i],arr)) {
        break
    }
}

console.log(solution)
return solution

}