* 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
46 lines
1.2 KiB
PowerShell
46 lines
1.2 KiB
PowerShell
$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 |