If you're worried about overflow or underflow, do your math inside a checked
block. It'll throw an OverflowException if the result wouldn't fit.
using System; public class Multiplication { // Multiply two integers // Throws error if the product is outside of the int range. // This would otherwise silently error and return the wrong product. public long Multiply(int a, int b) { checked { return a * b; } } }
- using System;
- public class Multiplication
- {
- // Multiply two integers
- // Throws error if the product is outside of the int range.
- // This would otherwise silently error and return the wrong product.
- public long Multiply(int a, int b) {
long res = (long)a * (long)b;// make sure it's still a valid intif (res > Int32.MaxValue || res < Int32.MinValue)throw new InvalidOperationException("Product is outside range of int");return res;- checked { return a * b; }
- }
- }
namespace Solution { using NUnit.Framework; using System; [TestFixture] public class SolutionTest { // Test Happy Path [TestCase(-5,-1,5)] [TestCase(-5, 1,-5)] [TestCase(5,0,0)] [TestCase(5,5,25)] [TestCase(856659,5,4283295)] public void testValidMultiplication(int a, int b, int expected) { Multiplication test = new Multiplication(); long actual = test.Multiply(a, b); Assert.AreEqual(expected, actual); } // Test outside valid int ranges [TestCase(856659,-6523158)] [TestCase(856659,6523158)] [TestCase(Int32.MaxValue,Int32.MaxValue)] [TestCase(Int32.MaxValue,Int32.MinValue)] public void testInvalidIntMultiplication(int a, int b) { Multiplication test = new Multiplication(); long actual; Assert.Throws<OverflowException>(() => actual = test.Multiply(a,b)); } } }
- namespace Solution {
- using NUnit.Framework;
- using System;
- [TestFixture]
- public class SolutionTest
- {
- // Test Happy Path
- [TestCase(-5,-1,5)]
- [TestCase(-5, 1,-5)]
- [TestCase(5,0,0)]
- [TestCase(5,5,25)]
- [TestCase(856659,5,4283295)]
- public void testValidMultiplication(int a, int b, int expected)
- {
- Multiplication test = new Multiplication();
- long actual = test.Multiply(a, b);
- Assert.AreEqual(expected, actual);
- }
- // Test outside valid int ranges
- [TestCase(856659,-6523158)]
- [TestCase(856659,6523158)]
- [TestCase(Int32.MaxValue,Int32.MaxValue)]
- [TestCase(Int32.MaxValue,Int32.MinValue)]
- public void testInvalidIntMultiplication(int a, int b) {
- Multiplication test = new Multiplication();
- long actual;
Assert.Throws<InvalidOperationException>(() => actual = test.Multiply(a,b));- Assert.Throws<OverflowException>(() => actual = test.Multiply(a,b));
- }
- }
- }
Return addresses are just normal stack entries, and nasm lets you jump to an address from a register.
global stack_push, stack_pop, stack_peek ; , stack_is_empty section .text stack_push: xchg rsi, [rsp] jmp rsi stack_pop: pop rsi pop rax jmp rsi stack_peek: pop rsi mov rax, [rsp] jmp rsi
- global stack_push, stack_pop, stack_peek ; , stack_is_empty
- section .text
- stack_push:
push rsiret- xchg rsi, [rsp]
- jmp rsi
- stack_pop:
- pop rsi
ret- pop rax
- jmp rsi
- stack_peek:
- pop rsi
push rsiret- mov rax, [rsp]
- jmp rsi
- get rid of comparisons; just let boolean conversion take care of it
- and is better represented as
*
Got rid of most of the ifs
function nextGeneration(grid) { return grid.map((row, rowIndex) => { return row.map((cell, colIndex) => { if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) { let neighboursCount = grid[rowIndex][colIndex + 1] + grid[rowIndex][colIndex - 1] + grid[rowIndex + 1][colIndex + 1] + grid[rowIndex + 1][colIndex] + grid[rowIndex + 1][colIndex - 1] + grid[rowIndex - 1][colIndex + 1] + grid[rowIndex - 1][colIndex] + grid[rowIndex - 1][colIndex - 1]; return 0 + (neighboursCount === 3 || (cell === 1 && neighboursCount === 2)); } return 0; }); }); }
- function nextGeneration(grid) {
- return grid.map((row, rowIndex) => {
- return row.map((cell, colIndex) => {
- if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
let neighboursCount = (grid[rowIndex][colIndex + 1] === 1) + (grid[rowIndex][colIndex - 1] === 1) + (grid[rowIndex + 1][colIndex] === 1) + (grid[rowIndex - 1][colIndex] === 1) + (grid[rowIndex + 1][colIndex + 1] === 1) + (grid[rowIndex + 1][colIndex - 1] === 1) + (grid[rowIndex - 1][colIndex + 1] === 1) + (grid[rowIndex - 1][colIndex - 1] === 1);if (cell === 1) {if (neighboursCount === 2 || neighboursCount === 3 ) {return 1;}} else {if (neighboursCount === 3 ) {return 1;}}return 0;- let neighboursCount = grid[rowIndex][colIndex + 1] +
- grid[rowIndex][colIndex - 1] +
- grid[rowIndex + 1][colIndex + 1] +
- grid[rowIndex + 1][colIndex] +
- grid[rowIndex + 1][colIndex - 1] +
- grid[rowIndex - 1][colIndex + 1] +
- grid[rowIndex - 1][colIndex] +
- grid[rowIndex - 1][colIndex - 1];
- return 0 + (neighboursCount === 3 || (cell === 1 && neighboursCount === 2));
- }
- return 0;
- });
- });
- }