mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat: rewrite programming-challenges CLI (#3)
This commit is contained in:
@ -22,4 +22,4 @@ You may assume that there won't be exceptional situations (like stack underflow
|
||||
|
||||
## Examples
|
||||
|
||||
See the `input-output.json` file for examples of input/output.
|
||||
See the `test` folder for examples of input/output.
|
||||
|
@ -1,34 +0,0 @@
|
||||
[
|
||||
{
|
||||
"input": [""],
|
||||
"output": 0
|
||||
},
|
||||
{
|
||||
"input": ["3"],
|
||||
"output": 3
|
||||
},
|
||||
{
|
||||
"input": ["3.5"],
|
||||
"output": 3.5
|
||||
},
|
||||
{
|
||||
"input": ["1 3 +"],
|
||||
"output": 4
|
||||
},
|
||||
{
|
||||
"input": ["1 3 *"],
|
||||
"output": 3
|
||||
},
|
||||
{
|
||||
"input": ["1 3 -"],
|
||||
"output": -2
|
||||
},
|
||||
{
|
||||
"input": ["4 2 /"],
|
||||
"output": 2
|
||||
},
|
||||
{
|
||||
"input": ["5 3 + 12 8 + * 10 /"],
|
||||
"output": 16
|
||||
}
|
||||
]
|
@ -1,5 +0,0 @@
|
||||
# javascript-solution - reverse-polish-notation
|
||||
|
||||
Programming language : JavaScript
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 29 September 2020.
|
@ -1,31 +0,0 @@
|
||||
const operations = {
|
||||
'+': (n1, n2) => n1 + n2,
|
||||
'-': (n1, n2) => n1 - n2,
|
||||
'*': (n1, n2) => n1 * n2,
|
||||
'/': (n1, n2) => n1 / n2
|
||||
}
|
||||
|
||||
function solution(value) {
|
||||
if (value.length === 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const stack = []
|
||||
const values = value.split(' ')
|
||||
values.forEach((value) => {
|
||||
const number = Number(value)
|
||||
if (!isNaN(number)) {
|
||||
stack.push(number)
|
||||
} else if (operations.hasOwnProperty(value)) {
|
||||
const number1 = stack.pop()
|
||||
const number2 = stack.pop()
|
||||
const operation = operations[value]
|
||||
const result = operation(number2, number1)
|
||||
stack.push(result)
|
||||
}
|
||||
})
|
||||
|
||||
return stack.pop()
|
||||
}
|
||||
|
||||
module.exports = solution
|
@ -0,0 +1,3 @@
|
||||
# reverse-polish-notation/javascript/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 6 June 2021.
|
@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "module"
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
import readline from 'readline'
|
||||
|
||||
const input = []
|
||||
const readlineInterface = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
})
|
||||
readlineInterface.on('line', (value) => {
|
||||
input.push(value)
|
||||
})
|
||||
readlineInterface.on('close', solution)
|
||||
|
||||
const operations = {
|
||||
'+': (number1, number2) => number1 + number2,
|
||||
'-': (number1, number2) => number1 - number2,
|
||||
'*': (number1, number2) => number1 * number2,
|
||||
'/': (number1, number2) => number1 / number2
|
||||
}
|
||||
|
||||
function solution () {
|
||||
console.log(reversePolishNotation(input[0]))
|
||||
}
|
||||
|
||||
function reversePolishNotation(value) {
|
||||
if (value.length === 0) {
|
||||
return 0
|
||||
}
|
||||
const stack = []
|
||||
const values = value.split(' ')
|
||||
values.forEach((value) => {
|
||||
const number = Number(value)
|
||||
if (!isNaN(number)) {
|
||||
stack.push(number)
|
||||
} else if (operations.hasOwnProperty(value)) {
|
||||
const number1 = stack.pop()
|
||||
const number2 = stack.pop()
|
||||
const operation = operations[value]
|
||||
const result = operation(number2, number1)
|
||||
stack.push(result)
|
||||
}
|
||||
})
|
||||
return stack.pop()
|
||||
}
|
1
challenges/reverse-polish-notation/test/1/input.txt
Normal file
1
challenges/reverse-polish-notation/test/1/input.txt
Normal file
@ -0,0 +1 @@
|
||||
3
|
1
challenges/reverse-polish-notation/test/1/output.txt
Normal file
1
challenges/reverse-polish-notation/test/1/output.txt
Normal file
@ -0,0 +1 @@
|
||||
3
|
1
challenges/reverse-polish-notation/test/2/input.txt
Normal file
1
challenges/reverse-polish-notation/test/2/input.txt
Normal file
@ -0,0 +1 @@
|
||||
3.5
|
1
challenges/reverse-polish-notation/test/2/output.txt
Normal file
1
challenges/reverse-polish-notation/test/2/output.txt
Normal file
@ -0,0 +1 @@
|
||||
3.5
|
1
challenges/reverse-polish-notation/test/3/input.txt
Normal file
1
challenges/reverse-polish-notation/test/3/input.txt
Normal file
@ -0,0 +1 @@
|
||||
1 3 +
|
1
challenges/reverse-polish-notation/test/3/output.txt
Normal file
1
challenges/reverse-polish-notation/test/3/output.txt
Normal file
@ -0,0 +1 @@
|
||||
4
|
1
challenges/reverse-polish-notation/test/4/input.txt
Normal file
1
challenges/reverse-polish-notation/test/4/input.txt
Normal file
@ -0,0 +1 @@
|
||||
1 3 *
|
1
challenges/reverse-polish-notation/test/4/output.txt
Normal file
1
challenges/reverse-polish-notation/test/4/output.txt
Normal file
@ -0,0 +1 @@
|
||||
3
|
1
challenges/reverse-polish-notation/test/5/input.txt
Normal file
1
challenges/reverse-polish-notation/test/5/input.txt
Normal file
@ -0,0 +1 @@
|
||||
1 3 -
|
1
challenges/reverse-polish-notation/test/5/output.txt
Normal file
1
challenges/reverse-polish-notation/test/5/output.txt
Normal file
@ -0,0 +1 @@
|
||||
-2
|
1
challenges/reverse-polish-notation/test/6/input.txt
Normal file
1
challenges/reverse-polish-notation/test/6/input.txt
Normal file
@ -0,0 +1 @@
|
||||
4 2 /
|
1
challenges/reverse-polish-notation/test/6/output.txt
Normal file
1
challenges/reverse-polish-notation/test/6/output.txt
Normal file
@ -0,0 +1 @@
|
||||
2
|
1
challenges/reverse-polish-notation/test/7/input.txt
Normal file
1
challenges/reverse-polish-notation/test/7/input.txt
Normal file
@ -0,0 +1 @@
|
||||
5 3 + 12 8 + * 10 /
|
1
challenges/reverse-polish-notation/test/7/output.txt
Normal file
1
challenges/reverse-polish-notation/test/7/output.txt
Normal file
@ -0,0 +1 @@
|
||||
16
|
Reference in New Issue
Block a user