Skip to content

Commit c8c01d0

Browse files
committed
Add PowXn and Phone Numbers
1 parent d59b495 commit c8c01d0

File tree

8 files changed

+113
-8
lines changed

8 files changed

+113
-8
lines changed

.github/workflows/powershell.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
run: |
3737
New-Item -Path ".\pester" -type Directory
3838
.\utils\pester-agent.ps1 -TestPath ".\tests\incremental-game-datetime.Tests.ps1" -OutputFile "pester\incremental-game-datetime-pester-results.xml" -CodeCoverageFile "pester\incremental-game-datetime-pester-coverage.xml"
39+
.\utils\pester-agent.ps1 -TestPath ".\tests\powxn.Tests.ps1" -OutputFile "pester\powxn-pester-results.xml" -CodeCoverageFile "pester\powxn-pester-coverage.xml"
40+
.\utils\pester-agent.ps1 -TestPath ".\tests\valid-phone-numbers.Tests.ps1" -OutputFile "pester\valid-phone-numbers-pester-results.xml" -CodeCoverageFile "pester\valid-phone-numbers-pester-coverage.xml"
3941
4042
- name: Upload Pester reports
4143
if: ${{ success() }}

MANIFEST.adoc

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ https://man7.org/linux/man-pages/man1/yes.1.html[Yes (Unix)]
88

99
=== requirements
1010

11-
* Feature parity.
12-
** Repeat a string (and newline) until killing the process.
13-
** Argument sets string.
14-
** No argument sets string to 'y'.
11+
* Feature parity
12+
** Repeat a string (and newline) until killing the process
13+
** Argument sets string
14+
** No argument sets string to 'y'
1515

1616
=== stats
1717

@@ -71,3 +71,50 @@ https://codegolf.stackexchange.com/questions/52785/[Stack Exchange]
7171
=== example use
7272

7373
See xref:tests/incremental-game-datetime.Tests.ps1[test file].
74+
75+
== PowXn
76+
77+
xref:PowXn.ps1[]
78+
79+
=== challenge
80+
81+
https://leetcode.com/problems/powx-n/description/[Leetcode]
82+
83+
=== requirements
84+
85+
* Two parameters
86+
** Base (double)
87+
** Exponent (int)
88+
* No use of "Math.pow()" or any other prexisting power operations
89+
90+
=== stats
91+
92+
* 194 byte(s)
93+
94+
=== example use case(s)
95+
96+
See xref:tests/pownx.Tests.ps1[test file].
97+
98+
== Valid Phone Numbers
99+
100+
xref:valid-phone-numbers.ps1[]
101+
102+
=== challenge
103+
104+
https://leetcode.com/problems/valid-phone-numbers/description/[Leetcode]
105+
106+
=== requirements
107+
108+
* Input a text file named file.txt
109+
* Print "valid" phone numbers from said file
110+
** Valid formats are the following
111+
*** (xxx) xxx-xxxx
112+
*** xxx-xxx-xxxx
113+
114+
=== stats
115+
116+
* 104 bytes
117+
118+
=== example use case(s)
119+
120+
See xref:tests/valid-phone-numbers.Tests.ps1[test file].

PowXn.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
param([double]$x,[int]$n)
2+
if($n -lt 0){$s=$true;[long]$e=-$n}else{$s=$false;[long]$e=$n}
3+
$r=1.0
4+
while($e) {
5+
if($e -band 1){$r*=$x}
6+
$e=$e -shr 1;$x*=$x
7+
}
8+
if($s){$r=1/$r}
9+
return $r

tests/data/phone-numbers.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
987-123-4567
2+
123 456 7890
3+
(123) 456-7890
4+
1-1-706-7938
5+
+31 365-492-5931
6+
37 9-858-6497
7+
+978-878-1324
8+
-686-511-4033
9+
1-234-567-8901
10+
(104 ) 456-7890

tests/powxn.Tests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
BeforeAll {
2+
$leaf_name = ((Get-Item -Path $PSCommandPath).Name).Replace('.Tests.ps1','.ps1')
3+
$repo_root_path = (Get-Item -Path $PSCommandPath).Directory.Parent.Fullname
4+
$script_path = [System.IO.Path]::Combine($repo_root_path,$leaf_name)
5+
}
6+
Describe "incremental-game-datetime.ps1" {
7+
It "Returns <expected> (<Base> to the power of <Exponent>)" -ForEach @(
8+
@{ Base = 2.00000; Exponent = 10; Expected = 1024.00 }
9+
@{ Base = 2.10000; Exponent = 3; Expected = 9.26100 }
10+
@{ Base = 2.00000; Exponent = -2; Expected = 0.25000 }
11+
@{ Base = 2.00000; Exponent = 0; Expected = 1.00000 }
12+
) {
13+
[double]$result = & $script_path -x $Base -n $Exponent
14+
$result | Should -BeLikeExactly $Expected
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
BeforeAll {
2+
$leaf_name = ((Get-Item -Path $PSCommandPath).Name).Replace('.Tests.ps1','.ps1')
3+
$repo_root_path = (Get-Item -Path $PSCommandPath).Directory.Parent.Fullname
4+
$script_path = [System.IO.Path]::Combine($repo_root_path,$leaf_name)
5+
}
6+
Describe "valid-phone-numbers.ps1" {
7+
It "Returns valid formatted phone numbers" {
8+
$data = [System.IO.Path]::Combine($repo_root_path, "tests", "data","phone-numbers.txt")
9+
$result = & $script_path -f $data
10+
foreach ($res in $result) {
11+
$res | Should -Match '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$'
12+
}
13+
}
14+
}

utils/pester-agent.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ param (
1818
[String]
1919
$TestPath,
2020

21-
[Parameter(Mandatory)]
21+
[Parameter()]
22+
[AllowEmptyString()]
2223
[String]
2324
$OutputFile,
2425

@@ -31,9 +32,11 @@ param (
3132
$pesterconfig = New-PesterConfiguration
3233
$pesterconfig.Output.Verbosity = 'Detailed'
3334
$pesterconfig.Run.Path = $TestPath
34-
$pesterconfig.TestResult.Enabled = $true
35-
$pesterconfig.TestResult.OutputPath = $OutputFile
36-
$pesterconfig.TestResult.OutputFormat = 'NUnitXml'
35+
if ($OutputFile) {
36+
$pesterconfig.TestResult.Enabled = $true
37+
$pesterconfig.TestResult.OutputPath = $OutputFile
38+
$pesterconfig.TestResult.OutputFormat = 'NUnitXml'
39+
}
3740
if ($CodeCoverageFile) {
3841
$pesterconfig.CodeCoverage.Enabled = $true
3942
$pesterconfig.CodeCoverage.OutputPath = $CodeCoverageFile

valid-phone-numbers.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
param($f="file.txt")
2+
foreach($l in gc $f){
3+
if($l -match '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$'){$l}
4+
}

0 commit comments

Comments
 (0)