Skip to content

Commit 06ee65d

Browse files
oleg-glushkoegonSchiele
authored andcommitted
Please, merge my PowerShell examples for all chapters (egonSchiele#106)
* PowerShell 01_introduction_to_algorithms example * PowerShell 02_selection_sort example * PowerShell 03_recursion examples * PowerShell 04_quicksort examples * PowerShell 05_hash_tables examples * PowerShell 06_breadth-first_search example * PowerShell 07_dijkstras_algorithm example * PowerShell 08_greedy_algorithms example * Powershell 09_dynamic_programming example
1 parent d7de908 commit 06ee65d

File tree

16 files changed

+436
-0
lines changed

16 files changed

+436
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function Search-GRKBinary
2+
{
3+
param ($list, $item)
4+
5+
# $low and $high keep track of which part of the list you'll search in.
6+
$low = 0;
7+
$high = $list.Length - 1;
8+
9+
# While you haven't narrowed it down to one element ...
10+
while ($low -le $high)
11+
{
12+
# ... check the middle element
13+
$mid = [int](($low + $high) / 2);
14+
$guess = $list[$mid];
15+
# Found the item.
16+
if ($guess -eq $item)
17+
{
18+
return $mid;
19+
}
20+
# The guess was too high.
21+
if ($guess -gt $item)
22+
{
23+
$high = $mid - 1
24+
}
25+
# The guess was too low
26+
else
27+
{
28+
$low = $mid + 1
29+
}
30+
31+
}
32+
33+
# Item doesn't exist
34+
return $null;
35+
}
36+
37+
$mylist = (1, 3, 5, 7, 9);
38+
Search-GRKBinary $mylist 3 # => 1
39+
Search-GRKBinary $mylist -1 # => $null
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Finds the smallest value in an array
2+
function Find-GRKSmallest
3+
{
4+
param ($arr)
5+
# Stores the smallest value
6+
$smallest = $arr[0]
7+
# Stores the index of the smallest value
8+
$smallest_index = 0
9+
0 .. ($arr.count - 1) | ForEach-Object {
10+
if ($arr[$_] -lt $smallest) {
11+
$smallest_index = $_
12+
$smallest = $arr[$_]
13+
}
14+
}
15+
return $smallest_index
16+
}
17+
18+
# Sort array
19+
function Sort-GRKSelection
20+
{
21+
param ($arr)
22+
$newArr = New-Object System.Collections.Generic.List[System.Object]
23+
1 .. $arr.count | ForEach-Object {
24+
# Finds the smallest element in the array and adds it to the new array
25+
$smallest = Find-GRKSmallest $arr
26+
$newArr.Add($arr[$smallest])
27+
$arr.RemoveAt($smallest)
28+
}
29+
return $NewArr
30+
}
31+
32+
# Can't use a default "@()" range notation as it creates static arrays
33+
$arr = New-Object System.Collections.Generic.List[System.Object]
34+
$arr.AddRange((5, 3, 6, 2, 10))
35+
Sort-GRKSelection $arr
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function Write-GRKCountdown
2+
{
3+
param ($i)
4+
# base case
5+
if ($i -le 0)
6+
{
7+
return 0
8+
}
9+
# recursive case
10+
else
11+
{
12+
write-Host($i)
13+
return Write-GRKCountdown($i-1)
14+
}
15+
}
16+
17+
Write-GRKCountdown 5

03_recursion/PowerShell/02_greet.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function Write-GRKGreet2
2+
{
3+
param ($name)
4+
Write-Host("how are you, " + $name + "?")
5+
}
6+
7+
function Write-GRKBye
8+
{
9+
Write-Host "ok bye!"
10+
}
11+
12+
function Write-GRKGreet
13+
{
14+
param($name)
15+
Write-Host("hello, " + $name + "!")
16+
Write-GRKGreet2 $name
17+
Write-Host "getting ready to say bye..."
18+
Write-GRKBye
19+
}
20+
21+
Write-GRKGreet adit
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function Get-GRKFact
2+
{
3+
param ($x)
4+
if ($x -eq 1)
5+
{
6+
return 1
7+
}
8+
else
9+
{
10+
return $x * (Get-GRKFact ($x-1))
11+
}
12+
}
13+
14+
Get-GRKFact 5
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function Get-GRKSum
2+
{
3+
param($arr)
4+
$total = 0
5+
foreach ($x in $arr)
6+
{
7+
$total += $x
8+
}
9+
return $total
10+
}
11+
12+
Get-GRKSum (1, 2, 3, 4)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function Get-GRKSum
2+
{
3+
param($list)
4+
if ($list -eq $null)
5+
{
6+
return 0
7+
}
8+
return $list[0] + (Get-GRKSum ($list | Select-Object -Skip 1))
9+
10+
}
11+
12+
Get-GRKSum (1, 2, 3, 4)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Get-GRKCount
2+
{
3+
param($list)
4+
if ($list -eq $null)
5+
{
6+
return 0
7+
}
8+
return 1 + (Get-GRKCount ($list | Select-Object -Skip 1))
9+
}
10+
11+
Get-GRKCount ("one", "two", "three", "four")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function Get-GRKMax
2+
{
3+
param($lst)
4+
if ($lst.count -eq 0)
5+
{
6+
return $null
7+
}
8+
if ($lst.count -eq 1)
9+
{
10+
return $lst[0]
11+
}
12+
else
13+
{
14+
$sub_max = Get-GRKMax ($lst | Select-Object -Skip 1)
15+
if ($lst[0] -gt $sub_max)
16+
{
17+
return $lst[0]
18+
}
19+
else
20+
{
21+
return $sub_max
22+
}
23+
}
24+
}
25+
26+
27+
Get-GRKMax (33, 7, 6, 100, 15, 13)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function Get-GRKQuickSort
2+
{
3+
param($array)
4+
if ($array.count -lt 2)
5+
{
6+
# base case, arrays with 0 or 1 element are already "sorted"
7+
if ($null -ne $array)
8+
{
9+
return , $array
10+
}
11+
else
12+
{
13+
return $null
14+
}
15+
}
16+
else
17+
{
18+
# recursive case
19+
$pivot = $array[0]
20+
# sub-array of all the elements less than the pivot
21+
$less = @($array | Select-Object -Skip 1 | Where-Object {$_ -le $pivot})
22+
# sub-array of all the elements greater than the pivot
23+
$greater = @($array | Select-Object -Skip 1 | Where-Object {$_ -gt $pivot})
24+
return @((Get-GRKQuickSort $less) + , $pivot + (Get-GRKQuickSort $greater))
25+
}
26+
}
27+
28+
Get-GRKQuickSort (5, 3, 6, 2, 10)

0 commit comments

Comments
 (0)