Skip to content

Commit e2401bd

Browse files
committed
Write map method for PowerShell
1 parent bc11f7b commit e2401bd

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<#
2+
.NOTES
3+
===========================================================================
4+
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.135
5+
Created on: 2/21/2017 9:31 PM
6+
Created by: June Blender
7+
Organization: SAPIEN Technologies, Inc.
8+
Filename:
9+
===========================================================================
10+
.DESCRIPTION
11+
A description of the file.
12+
#>
13+
14+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
15+
$script = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
16+
. "$directory\$script"
17+
18+
Describe "ConvertTo-Range" {
19+
20+
Context "SmallToLarge_StandardValues" {
21+
It "Mid-range test" {
22+
# map(60, 40, 90, 0, 255) is 102.0
23+
ConvertTo-Range -Value 60 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 255 | Should Be 102
24+
}
25+
It "Min-range test" {
26+
# map(40, 40, 90, 0, 255) is 0
27+
ConvertTo-Range -Value 40 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 255 | Should Be 0
28+
}
29+
It "Max-range test" {
30+
# map(90, 40, 90, 0, 255) is 255
31+
ConvertTo-Range -Value 90 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 255 | Should Be 255
32+
}
33+
}
34+
35+
Context "LargeToSmall_StandardValues" {
36+
It "Mid-range test" {
37+
# map(60, 10, 500, 40, 90) is 45
38+
ConvertTo-Range -Value 60 -Range1_Start 10 -Range1_End 500 -Range2_Start 40 -Range2_End 90 | Should Be 45
39+
}
40+
It "Min-range test" {
41+
# map(250, 10, 500, 40, 90) is 64
42+
ConvertTo-Range -Value 250 -Range1_Start 10 -Range1_End 500 -Range2_Start 40 -Range2_End 90 | Should Be 64
43+
}
44+
It "Max-range test" {
45+
# map(450, 10, 500, 40, 90) is 84
46+
ConvertTo-Range -Value 450 -Range1_Start 10 -Range1_End 500 -Range2_Start 40 -Range2_End 90 | Should Be 85
47+
}
48+
}
49+
50+
Context "SmallToLarge_OutOfRange" {
51+
It "Mid-range test" {
52+
# map(30, 40, 90, 0, 100) is -20
53+
ConvertTo-Range -Value 30 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 100 | Should Be -20
54+
}
55+
It "Min-range test" {
56+
# map(100, 40, 90, 0, 255) is 306
57+
ConvertTo-Range -Value 100 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 255 | Should Be 306
58+
}
59+
It "Max-range test" {
60+
# map(300, 40, 90, 0, 255) is 1326
61+
ConvertTo-Range -Value 300 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 255 | Should Be 1326
62+
}
63+
}
64+
65+
Context "FunStuff" {
66+
It "Negative_Range1" {
67+
# map(-80, -90, -40, 0, 255) is 51
68+
ConvertTo-Range -Value -80 -Range1_Start -90 -Range1_End -40 -Range2_Start 0 -Range2_End 255 | Should Be 51
69+
}
70+
It "Negative_Range2" {
71+
# map(80, 40, 90, -100, -10) is -28
72+
ConvertTo-Range -Value 80 -Range1_Start 40 -Range1_End 90 -Range2_Start -100 -Range2_End -10 | Should Be -28
73+
}
74+
It "Temperatures: F->C" {
75+
# map(68, 32, 212, 0, 100) is 20
76+
ConvertTo-Range -Value 68 -Range1_Start 32 -Range1_End 212 -Range2_Start 0 -Range2_End 100 | Should Be 20
77+
}
78+
It "Temperatures: C->F" {
79+
# map(30, 0, 100, 32, 212) is 86
80+
ConvertTo-Range -Value 30 -Range1_Start 0 -Range1_End 100 -Range2_Start 32 -Range2_End 212 | Should Be 86
81+
}
82+
It "Min > Max Range" {
83+
# map(60, 90, 40, 0, 255) is 153
84+
ConvertTo-Range -Value 60 -Range1_Start 90 -Range1_End 40 -Range2_Start 0 -Range2_End 255 | Should Be 153
85+
}
86+
It "won't work" {
87+
# map(40, 40, 40, 0, 100) is NaN
88+
{ ConvertTo-Range -Value 40 -Range1_Start 40 -Range1_End 40 -Range2_Start 0 -Range2_End 100 } | Should Throw
89+
}
90+
}
91+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<#
2+
.NOTES
3+
===========================================================================
4+
SAPIEN Friday PowerShell Puzzle, February 24, 2017
5+
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.136
6+
Created on: 2/24/2017 4:43 PM
7+
Created by: June Blender
8+
Organization: SAPIEN Technologies, Inc
9+
Filename: ConvertTo-Range.ps1
10+
===========================================================================
11+
#>
12+
13+
14+
<#
15+
.SYNOPSIS
16+
Converts a value from its current range to a target range.
17+
18+
.DESCRIPTION
19+
ConvertTo-Range converts a value from its position in a current range to the same relative position in a target range.
20+
21+
This function takes a current range (range 1), a target range (range 2), and a value in the current range. It returns the equivalent value in the target range.
22+
23+
For example, if the current range is 0-10, the target range is 20-30, and the value is 5, it returns 25, which is the equivalent value in the 20-30 range.
24+
25+
.PARAMETER Value
26+
Specifies the integer value to be converted from the current range to the target range.
27+
28+
Although this value is evaluated based on its position in the current range, it can be any value, including a value outside of the current range and the target range.
29+
30+
.PARAMETER Range1_Start
31+
Specifies the starting value in the current range. Enter an integer value. The value of Range1_Start can be less than or greater than the value of Range1_End.
32+
33+
.PARAMETER Range1_End
34+
Specifies the ending value in the current range. Enter an integer value. The value of Range1_End can be less than or greater than the value of Range1_Start.
35+
36+
.PARAMETER Range2_Start
37+
Specifies the starting value in the target range. Enter an integer value. The value of Range2_Start can be less than or greater than the value of Range2_End.
38+
39+
.PARAMETER Range2_End
40+
Specifies the ending value in the target range. Enter an integer value. The value of Range2_End can be less than or greater than the value of Range2_Start.
41+
42+
.EXAMPLE
43+
PS C:\> ConvertTo-Range -Value 40 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 100
44+
0
45+
46+
This command maps the range 40-90 to the range 0-100. Then, given a value of 40 in the first range, it returns 0, the equivalent value in the second range.
47+
48+
.EXAMPLE
49+
PS C:\> ConvertTo-Range -Value 300 -Range1_Start 40 -Range1_End 90 -Range2_Start 0 -Range2_End 100
50+
520
51+
52+
This command demonstrates that the test value does not need to be within the starting or the target range. The resulting value in the target range is proportional to the input value in the current range.
53+
54+
.NOTES
55+
Based on the map() method in the Java Processing library
56+
https://processing.org/reference/map_.html
57+
58+
Thanks to Andy Cahn at Chrysalis Schools for the math help.
59+
#>
60+
function ConvertTo-Range
61+
{
62+
# Your solution goes here
63+
}

0 commit comments

Comments
 (0)