Please, merge my PowerShell examples for all chapters (#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
This commit is contained in:
committed by
Aditya Bhargava
parent
d7de908a82
commit
06ee65d9e5
@@ -0,0 +1,46 @@
|
||||
$graph = @{}
|
||||
$graph["you"] = @("alice", "bob", "claire")
|
||||
$graph["bob"] = @("anuj", "peggy")
|
||||
$graph["alice"] = @("peggy")
|
||||
$graph["claire"] = @("thom", "jonny")
|
||||
$graph["anuj"] = @()
|
||||
$graph["peggy"] = @()
|
||||
$graph["thom"] = @()
|
||||
$graph["jonny"] = @()
|
||||
|
||||
function Test-GRKPersonIsSeller
|
||||
{
|
||||
param($name)
|
||||
return $name[-1] -eq 'm'
|
||||
}
|
||||
|
||||
function Search-GRKPerson
|
||||
{
|
||||
param($name)
|
||||
$search_queue = New-Object System.Collections.Queue
|
||||
$graph[$name] | % {$search_queue.Enqueue($_)}
|
||||
# This array is how you keep track of which people you've searched before.
|
||||
$searched = New-Object System.Collections.Generic.List[System.Object]
|
||||
while ($search_queue.count -gt 0) {
|
||||
$person = $search_queue.Dequeue()
|
||||
# Only search this person if you haven't already searched them.
|
||||
if ($person -notin $searched)
|
||||
{
|
||||
if (Test-GRKPersonIsSeller($person))
|
||||
{
|
||||
Write-Host($person + " is a mango seller!")
|
||||
return $True
|
||||
}
|
||||
else
|
||||
{
|
||||
$graph[$person] | % {$search_queue.Enqueue($_)}
|
||||
# Marks this person as searched
|
||||
$searched.Add($person)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return $False
|
||||
}
|
||||
|
||||
Search-GRKPerson you
|
||||
Reference in New Issue
Block a user