Skip to content
  • Home
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Lux)
  • No Skin
Collapse
Ukraine Tryzub and Canada Leaf
  1. UKRAINIANS ON THE CANADA MAP
  2. Categories
  3. Вільне спілкування
  4. (UA) IT/Tech Ukrainians in Canada

(UA) IT/Tech Ukrainians in Canada

Scheduled Pinned Locked Moved Вільне спілкування
647.4k Posts 3.6k Posters 371.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Alex
    wrote on last edited by
    #593249
    задача была не юзать евал
    1 Reply Last reply
    0
  • t139189834T Offline
    t139189834T Offline
    Ві тя 🇺🇦
    wrote on last edited by
    #593250
    от класичний розв'язок: function evaluateExpression(expression) { const operations = { add: (a, b) => a + b, subtract: (a, b) => a - b, multiply: (a, b) => a * b, divide: (a, b) => a / b, }; function parseArguments(args) { return args.split(',').map(arg => parseFloat(arg.trim())); } function evaluate(expr) { const stack = []; let currentFunction = ''; let currentArgs = ''; for (let i = 0; i < expr.length; i++) { const char = expr[i]; if (char === '(') { // Start of a function call stack.push({ func: currentFunction.trim(), args: '' }); currentFunction = ''; } else if (char === ')') { // End of a function call const { func, args } = stack.pop(); const resolvedArgs = parseArguments(currentArgs.trim()); const result = operations[func](...resolvedArgs); if (stack.length > 0) { // Append result to the parent's arguments stack[stack.length - 1].args += result + ','; } else { // Return the final result return result; } currentArgs = ''; } else if (char === ',') { // Argument separator if (stack.length > 0) { stack[stack.length - 1].args += currentArgs.trim() + ','; } currentArgs = ''; } else if (stack.length > 0) { // Add to the current argument currentArgs += char; } else { // Collect the function name currentFunction += char; } } throw new Error('Invalid expression'); } return evaluate(expression); } // Example usage const input = "multiply(add(subtract(5, multiply(7, 4)), divide(30, 7)), 7)"; console.log(evaluateExpression(input)); // Outputs the evaluated result
    1 Reply Last reply
    0
  • t139189834T Offline
    t139189834T Offline
    Ві тя 🇺🇦
    wrote on last edited by
    #593251
    те ж саме без eval: function evaluateExpression(expression) { // Define the operations const operations = { add: (a, b) => a + b, subtract: (a, b) => a - b, multiply: (a, b) => a * b, divide: (a, b) => a / b, }; // Recursive function to evaluate the expression function parse(expr) { // Match the innermost function and its arguments const regex = /([a-zA-Z]+)\(([^()]*)\)/; const match = expr.match(regex); if (!match) { // If no more functions, return the number directly return parseFloat(expr); } const [_, func, args] = match; const argValues = args.split(',').map(arg => parse(arg.trim())); if (!(func in operations)) { throw new Error(`Unknown function: ${func}`); } // Compute the result for the current function const result = operations[func](...argValues); // Replace the evaluated function call in the expression const newExpr = expr.replace(match[0], result); return parse(newExpr); // Recursively evaluate the remaining expression } // Start parsing and evaluating return parse(expression); } // Example usage const input = "multiply(add(subtract(5, multiply(7, 4)), divide(30, 7)), 7)"; console.log(evaluateExpression(input)); // Outputs the evaluated result
    1 Reply Last reply
    0
  • t139189834T Offline
    t139189834T Offline
    Ві тя 🇺🇦
    wrote on last edited by
    #593252
    чат жпт вміє в корявий жс: function evaluateExpression(expression) { // Define the mathematical operations const operations = { add: (a, b) => a + b, subtract: (a, b) => a - b, multiply: (a, b) => a * b, divide: (a, b) => a / b, }; // Replace function names with JavaScript syntax const sanitizedExpression = expression.replace( /(add|subtract|multiply|divide)\(/g, (_, fn) => `operations.${fn}(` // Replace with operations object reference ); // Evaluate the sanitized expression try { const result = eval(sanitizedExpression); // Caution: eval can be unsafe return result; } catch (error) { throw new Error(`Invalid expression: ${error.message}`); } } // Example usage const input = "multiply(add(subtract(5, multiply(7, 4)), divide(30, 7)), 7)"; console.log(evaluateExpression(input)); // Outputs the evaluated result
    1 Reply Last reply
    0
  • t385897444T Offline
    t385897444T Offline
    Max Vedeneev
    wrote on last edited by
    #593253
    Якщо я правильно поняв що там відбувається, то це не єффективне рішення
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Alex
    wrote on last edited by
    #593254
    фактически это евал только без него
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Alex
    wrote on last edited by
    #593255
    я дома гляну на классическое решение. хотя мне кажется моё с заменой более простым
    1 Reply Last reply
    0
  • t385897444T Offline
    t385897444T Offline
    Max Vedeneev
    wrote on last edited by
    #593256
    То якась дічь з евалом))
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Alex
    wrote on last edited by
    #593257
    но можно и без регекса так же искать открытые/закрытые скобки, тогда будет быстрее на больших объемах операций
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Alex
    wrote on last edited by
    #593258
    с другой стороны его и понять проще
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Alex
    wrote on last edited by
    #593259
    по перфомансу будет не быстрым, бо есть регекс
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Artem K. 🇺🇦
    wrote on last edited by
    #593260
    Доречі, я не помітив різницю між епічними та просто високими налаштуваннями. Можно понизити та отримати більш стабільний фпс
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Dmitry Smirnov
    wrote on last edited by
    #593261
    🤣🤣🤣
    1 Reply Last reply
    0
  • t335448128T Offline
    t335448128T Offline
    Duck
    wrote on last edited by
    #593262
    Ну ти теж на 1060 мабуть граєш, так? 😉
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Dmitry Smirnov
    wrote on last edited by
    #593263
    ні фрізів, ні лагів, хз чому там писали про оптимізацію
    1 Reply Last reply
    0
  • T Offline
    T Offline
    Dmitry Smirnov
    wrote on last edited by
    #593264
    85-160 фпс на епічних налаштуваннях
    1 Reply Last reply
    0
  • t139189834T Offline
    t139189834T Offline
    Ві тя 🇺🇦
    wrote on last edited by
    #593265
    підправив пару багів, результат з прикладу: -131
    1 Reply Last reply
    0
  • t335448128T Offline
    t335448128T Offline
    Duck
    wrote on last edited by
    #593266
    Це я так дивився на порівняння пс5 вс пс5про, там з кропом х5 не завжди різниця була помітна 😁
    1 Reply Last reply
    0
  • t139189834T Offline
    t139189834T Offline
    Ві тя 🇺🇦
    wrote on last edited by
    #593267
    класичне проходить по стрінзі 1 раз тобто О(н)
    1 Reply Last reply
    0
  • t139189834T Offline
    t139189834T Offline
    Ві тя 🇺🇦
    wrote on last edited by
    #593268
    продовжили для українців страхівку?
    1 Reply Last reply
    0

  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups