Skip to content

Instantly share code, notes, and snippets.

@megastary
Created June 26, 2021 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megastary/c6e2c621dbd2e05885edd38aac1cfd7f to your computer and use it in GitHub Desktop.
Save megastary/c6e2c621dbd2e05885edd38aac1cfd7f to your computer and use it in GitHub Desktop.
# Author: Jakub Šindelář megastary@houby-studio.eu
# Date: 2021-06-26
# Solution definition
function flipfront {
[CmdletBinding()]
param (
[ValidateCount(2, [Int32]::MaxValue)]
[ValidateRange(0, [Int32]::MaxValue)]
[Int32[]]$Array,
[ValidateScript( { ($_ -ge 2) -and ($_ -le $Array.Count) })]
[Int32]$Number
)
# Reverse first n elements with either slice or create copy and use Reverse()
#$Reverse = $Array[0..($Number - 1)]
#[System.Array]::Reverse($Reverse)
$Array[($Number - 1) .. - 0]
# Output rest of the array in regular order
if ($Number -ne $Array.Count) {
$Array[$Number..($Array.Count - 1)]
}
}
function sort_flipfront {
[CmdletBinding()]
param (
[ValidateCount(2, [Int32]::MaxValue)]
[ValidateRange(0, [Int32]::MaxValue)]
[Int32[]]$Array
)
# Convert to ArrayList, which allows additions
$Array = [System.Collections.ArrayList]$Array
$SortedCount = 0
# Loop until entire array isn't sorted
while ($Array.Count -ne ($SortedCount)) {
# Get index of highest number in unsorted portion of the array
$MaxValueIndex = $Array.IndexOf(([int]$($Array[0..($Array.Count - $SortedCount - 1)] | Measure-Object -Maximum).Maximum))
Write-Verbose "Highest number $($Array[$MaxValueIndex]) found at index $MaxValueIndex."
# Modify array using flipfront based on highest number position
if ($MaxValueIndex -eq ($Array.Count - $SortedCount - 1)) {
Write-Verbose 'Highest number is already at the end of the unsorted array, incrementing sorted array length and skipping loop.'
$SortedCount++
Write-Verbose "The array sorting progress is currently $SortedCount/$($Array.Count)."
continue
}
elseif ($MaxValueIndex -eq 1) {
Write-Verbose 'Highest number is at the second position in the array, flipping first two elements in the array.'
[System.Collections.ArrayList]$Array = flipfront($Array)(2)
}
elseif ($MaxValueIndex -gt 1) {
Write-Verbose 'Flipping highest number to first position in the array.'
[System.Collections.ArrayList]$Array = flipfront($Array)($MaxValueIndex + 1)
}
else {
Write-Verbose 'Highest number is already at the first position of the array, skipping first flipfront() call.'
}
# Reverse unsorted array, moving highest number to last unsorted position in the array
[System.Collections.ArrayList]$UnsortedArray = $Array[0..($Array.Count - $SortedCount - 1)]
[System.Collections.ArrayList]$ChangedArray = flipfront($UnsortedArray)($UnsortedArray.Count)
# If there are sorted numbers in the array, append it at the end of the reversed unsorted array
if ($SortedCount -gt 0) {
$SortedArray = $Array[($Array.Count - $SortedCount)..($Array.Count - 1)]
$SortedArray | ForEach-Object {
$ChangedArray.Add($_) | Out-Null
}
}
# Push reversed array with appended sorted array as new array object and increase sorted number counter
$Array = $ChangedArray
$SortedCount++
Write-Verbose "The array sorting progress: $SortedCount/$($Array.Count)."
}
# Return resulting array
$Array
}
# Test result with Pester
# Install with: Install-Module Pester -MinimumVersion 4.0.0 -Force -SkipPublisherCheck
# Import tested script
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
}
Describe 'flipfront' {
Context '([0, 1, 2, 3, 4], 2)' {
It 'returns 1, 0, 2, 3, 4' {
flipfront(0, 1, 2, 3, 4)(2) | Should -Be @(1, 0, 2, 3, 4)
}
}
Context '([0, 1, 2, 3, 4], 3)' {
It 'returns 2, 1, 0, 3, 4' {
flipfront(0, 1, 2, 3, 4)(3) | Should -Be @(2, 1, 0, 3, 4)
}
}
Context '([0, 1, 2, 3, 4], 5)' {
It 'returns 4, 3, 2, 1, 0' {
flipfront(0, 1, 2, 3, 4)(5) | Should -Be @(4, 3, 2, 1, 0)
}
}
Context '([1, 2, 2, 2], 3)' {
It 'returns 2, 2, 1, 2' {
flipfront(1, 2, 2, 2)(3) | Should -Be @(2, 2, 1, 2)
}
}
}
Describe 'sort_flipfront' {
Context '([3, 1, 2, 1])' {
It 'returns 1, 1, 2, 3' {
sort_flipfront(3, 1, 2, 1) | Should -Be @(1, 1, 2, 3)
}
}
Context '([3, 4, 2, 1])' {
It 'returns 1, 2, 3, 4' {
sort_flipfront(3, 4, 2, 1) | Should -Be @(1, 2, 3, 4)
}
}
Context '([3, 4, 2, 1, 8, 7, 9])' {
It 'returns 1, 2, 3, 4, 7, 8, 9' {
sort_flipfront(3, 4, 2, 1, 8, 7, 9) | Should -Be @(1, 2, 3, 4, 7, 8, 9)
}
}
Context '([10k integers])' {
It 'returns sorted 10k integers' {
[Int32[]]$Array = Get-Content "$PSScriptRoot\10000-integers.txt"
[System.Collections.ArrayList]$Sorted = $Array
$Sorted.Sort()
sort_flipfront($Array) | Should -Be $Sorted
}
}
}
@megastary
Copy link
Author

This gist is my PowerShell solution to challenge https://old.reddit.com/r/dailyprogrammer/comments/np3sio/20210531_challenge_392_intermediate_pancake_sort/

Bonus challenge result: 19970

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment