Fundamentals
Arrays
function Sum-OfPositive($NumberArray) { ( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum }
function SumOfPositive($NumberArray)- function Sum-OfPositive($NumberArray)
- {
- ( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum
- }
BeforeAll { . $PSCommandPath.Replace('.Tests.ps1', '.ps1') } Describe "Fixed Tests"{ it "Should pass" { $test1 = @(1, 2, 3, 4, 5) $test2 = @(1, -2, 3, 4, 5) $test3 = @(-1, 2, 3, 4, -5) $test4 = @() $test5 = (-1, -2, -3, -4, -5) Sum-OfPositive($test1) | Should -be 15 Sum-OfPositive($test2) | Should -be 13 Sum-OfPositive($test3) | Should -be 9 Sum-OfPositive($test4) | Should -be 0 Sum-OfPositive($test5) | Should -be 0 } }
$test1 = @(1, 2, 3, 4, 5)$test2 = @(1, -2, 3, 4, 5)$test3 = @(-1, 2, 3, 4, -5)$test4 = @()$test5 = (-1, -2, -3, -4, -5)- BeforeAll {
- . $PSCommandPath.Replace('.Tests.ps1', '.ps1')
- }
- Describe "Fixed Tests"{
- it "Should pass" {
SumOfPositive($test1) | Should be 15}it "Should pass"{SumOfPositive($test2) | Should be 13}it "Should pass"{SumOfPositive($test3) | Should be 9- $test1 = @(1, 2, 3, 4, 5)
- $test2 = @(1, -2, 3, 4, 5)
- $test3 = @(-1, 2, 3, 4, -5)
- $test4 = @()
- $test5 = (-1, -2, -3, -4, -5)
- Sum-OfPositive($test1) | Should -be 15
- Sum-OfPositive($test2) | Should -be 13
- Sum-OfPositive($test3) | Should -be 9
- Sum-OfPositive($test4) | Should -be 0
- Sum-OfPositive($test5) | Should -be 0
- }
it "Should pass"{SumOfPositive($test4) | Should be 0}it "Should pass"{SumOfPositive($test5) | Should be 0}}function SumOfPositive($NumberArray){( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum- }
Fundamentals
Arrays
function SumOfPositive($NumberArray) { ( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum }
function Get-SumOfPositive($NumberArray)- function SumOfPositive($NumberArray)
- {
$Sum =0;foreach($Number in $NumbArray){if($Number -gt 0){$Sum+=$Number}}Write-Output $Sum- ( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum
- }
$test1 = @(1, 2, 3, 4, 5) $test2 = @(1, -2, 3, 4, 5) $test3 = @(-1, 2, 3, 4, -5) $test4 = @() $test5 = (-1, -2, -3, -4, -5) Describe "Fixed Tests"{ it "Should pass" { SumOfPositive($test1) | Should be 15 } it "Should pass"{ SumOfPositive($test2) | Should be 13 } it "Should pass"{ SumOfPositive($test3) | Should be 9 } it "Should pass"{ SumOfPositive($test4) | Should be 0 } it "Should pass"{ SumOfPositive($test5) | Should be 0 } } function SumOfPositive($NumberArray) { ( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum }
- $test1 = @(1, 2, 3, 4, 5)
- $test2 = @(1, -2, 3, 4, 5)
- $test3 = @(-1, 2, 3, 4, -5)
- $test4 = @()
- $test5 = (-1, -2, -3, -4, -5)
- Describe "Fixed Tests"{
it "Should pass"{$test = @(1, 2, 3, 4, 5)Get-SumOfPositive($test) | Should be 15- it "Should pass" {
- SumOfPositive($test1) | Should be 15
- }
- it "Should pass"{
$test = @(1, -2, 3, 4, 5)Get-SumOfPositive($test) | Should be 13- SumOfPositive($test2) | Should be 13
- }
- it "Should pass"{
$test = @(-1, 2, 3, 4, -5)Get-SumOfPositive($test) | Should be 9- SumOfPositive($test3) | Should be 9
- }
- it "Should pass"{
$test = @()Get-SumOfPositive($test) | Should be 0- SumOfPositive($test4) | Should be 0
- }
- it "Should pass"{
$test = (-1, -2, -3, -4, -5)Get-SumOfPositive($test) | Should be 0- SumOfPositive($test5) | Should be 0
- }
}- }
- function SumOfPositive($NumberArray)
- {
- ( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum
- }
Create a simple calculator that given two numbers and an operator returns the value of the arithmetic operation. Here are the operations to support:
-
- addition
-
- subtraction
-
- multiplication
function Calculator ($n1,$s,$n2 ){ Invoke-Expression ("$n1 $s $n2") }
import javax.script.ScriptEngineManager;interface Calculator {static int calculate(int a, int b, char operation) {var engine = new ScriptEngineManager().getEngineByName("JavaScript");try {return Integer.parseInt(String.valueOf(engine.eval(a + " " + operation + " " + b)));} catch (Exception cause) {return 0;}}}- function Calculator ($n1,$s,$n2 ){
- Invoke-Expression ("$n1 $s $n2")
- }
# Easy breezy tests $test1 = Calculator -n1 4 -n2 5 -s '+' $test2 = Calculator -n1 12 -n2 5 -s '-' $test3 = Calculator -n1 20 -n2 3 -s '*' Describe 'FixedTests' { It "Should pass" { $test1 | Should be 9 } It "Should pass" { $test2 | Should be 7 } It "Should pass" { $test3 | Should be 60 } } Describe 'RandomTests' { function ans($n1,$s,$n2 ){ Invoke-Expression ("$n1 $s $n2") } for($i =0; $i -lt 100; $i++){ $s = '+','-','*' | Get-Random $n1 = Get-Random -Maximum 1000 $n2 = Get-Random -Maximum 1000 $ans = ans($n1,$s,$n2) It "Should pass" { Calculator ($n1,$s,$n2)| Should be $ans } } }
import static java.util.concurrent.ThreadLocalRandom.current;import static org.junit.jupiter.api.Assertions.assertEquals;- # Easy breezy tests
- $test1 = Calculator -n1 4 -n2 5 -s '+'
- $test2 = Calculator -n1 12 -n2 5 -s '-'
- $test3 = Calculator -n1 20 -n2 3 -s '*'
import org.junit.jupiter.api.Test;class CalculatorTest {@Testvoid sample() {assertEquals(9, Calculator.calculate(4, 5, '+'));assertEquals(7, Calculator.calculate(12, 5, '-'));assertEquals(60, Calculator.calculate(20, 3, '*'));- Describe 'FixedTests' {
- It "Should pass" {
- $test1 | Should be 9
- }
- It "Should pass" {
- $test2 | Should be 7
- }
- It "Should pass" {
- $test3 | Should be 60
- }
- }
@Testvoid random() {for (int i = 0; i < 100; i++) {var pair = current().ints(2, -1000, 1000).toArray();assertEquals(pair[0] + pair[1], Calculator.calculate(pair[0], pair[1], '+'));assertEquals(pair[0] - pair[1], Calculator.calculate(pair[0], pair[1], '-'));assertEquals(pair[0] * pair[1], Calculator.calculate(pair[0], pair[1], '*'));- Describe 'RandomTests' {
- function ans($n1,$s,$n2 ){
- Invoke-Expression ("$n1 $s $n2")
- }
- for($i =0; $i -lt 100; $i++){
- $s = '+','-','*' | Get-Random
- $n1 = Get-Random -Maximum 1000
- $n2 = Get-Random -Maximum 1000
- $ans = ans($n1,$s,$n2)
- It "Should pass" {
- Calculator ($n1,$s,$n2)| Should be $ans
- }
- }
}- }