update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
|
|
@ -0,0 +1,81 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import java.util.List
|
||||
|
||||
runSample(arg)
|
||||
return
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method pancakeSort(tlist = List, debug = (1 == 0)) private static returns List
|
||||
|
||||
if tlist.size() > 1 then do
|
||||
loop i_ = tlist.size() by -1 while i_ > 1
|
||||
maxPos = 0
|
||||
loop a_ = 0 while a_ < i_
|
||||
if Rexx tlist.get(a_) > Rexx tlist.get(maxPos) then maxPos = a_
|
||||
end a_
|
||||
if maxPos = i_ - 1 then iterate i_
|
||||
if maxPos > 0 then pancakeFlip(tlist, maxPos + 1, debug)
|
||||
pancakeFlip(tlist, i_, debug)
|
||||
end i_
|
||||
end
|
||||
return tlist
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method pancakeFlip(tlist = List, offset, debug = (1 == 0)) private static returns List
|
||||
z_ = offset - 1
|
||||
pl = 3
|
||||
if debug then do
|
||||
plx = offset.length()
|
||||
if plx > pl then pl = plx
|
||||
say ' flip{1-'offset.right(pl, 0)'} Before:' tlist
|
||||
end
|
||||
loop i_ = 0 while i_ < z_
|
||||
Collections.swap(tlist, i_, z_)
|
||||
z_ = z_ - 1
|
||||
end i_
|
||||
if debug then do
|
||||
say ' flip{1-'offset.right(pl, 0)'} After:' tlist
|
||||
end
|
||||
return tlist
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method runSample(arg) private static
|
||||
|
||||
isTrue = (1 == 1)
|
||||
isFalse = \isTrue
|
||||
|
||||
parse arg debug .
|
||||
if '-debug'.abbrev(debug.lower(), 2) then debug = isTrue
|
||||
else debug = isFalse
|
||||
|
||||
lists = sampleData()
|
||||
loop il = 1 to lists[0]
|
||||
clist = words2list(lists[il])
|
||||
say ' Input:' clist
|
||||
say 'Output:' pancakeSort(clist, debug)
|
||||
say
|
||||
end il
|
||||
|
||||
return
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method sampleData() private static
|
||||
lists = ''
|
||||
i_ = 0
|
||||
i_ = i_ + 1; lists[0] = i_; lists[i_] = '1 4 3 5 2 9 8 7 6'
|
||||
i_ = i_ + 1; lists[0] = i_; lists[i_] = '10 -9 8 -7 6 -5 4 -3 2 -1 0 -10 9 -8 7 -6 5 -4 3 -2 1'
|
||||
i_ = i_ + 1; lists[0] = i_; lists[i_] = '88 18 31 44 4 0 8 81 14 78 20 76 84 33 73 75 82 5 62 70 12 7 1'
|
||||
i_ = i_ + 1; lists[0] = i_; lists[i_] = '10 10.0 10.00 1 -10.0 10. -1'
|
||||
i_ = i_ + 1; lists[0] = i_; lists[i_] = 'To be or not to be that is the question'
|
||||
i_ = i_ + 1; lists[0] = i_; lists[i_] = '1'
|
||||
return lists
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method words2list(wordlist) private static returns List
|
||||
|
||||
clist = ArrayList()
|
||||
loop w_ = 1 to wordlist.words()
|
||||
clist.add(wordlist.word(w_))
|
||||
end w_
|
||||
|
||||
return clist
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#lang racket
|
||||
|
||||
(define (pancake-sort l)
|
||||
(define (flip l n) (append (reverse (take l n)) (drop l n)))
|
||||
(for/fold ([l l]) ([i (in-range (length l) 1 -1)])
|
||||
(let* ([i2 (cdr (for/fold ([m #f]) ([x l] [j i])
|
||||
(if (and m (<= x (car m))) m (cons x j))))]
|
||||
[l (if (zero? i2) l (flip l (add1 i2)))])
|
||||
(flip l i))))
|
||||
|
||||
(pancake-sort (shuffle (range 0 10)))
|
||||
;; => '(0 1 2 3 4 5 6 7 8 9)
|
||||
|
|
@ -1,28 +1,19 @@
|
|||
class Array
|
||||
def pancake_sort!
|
||||
num_flips = 0
|
||||
self.size.downto(2) do |i|
|
||||
end_idx = i - 1
|
||||
max_idx = self[0..end_idx].each_with_index.max_by {|e| e[0]}.last
|
||||
(self.size-1).downto(1) do |end_idx|
|
||||
max = self[0..end_idx].max
|
||||
max_idx = self[0..end_idx].index(max)
|
||||
next if max_idx == end_idx
|
||||
|
||||
if max_idx > 0
|
||||
self[0..max_idx] = self[0..max_idx].reverse
|
||||
|
||||
if $DEBUG
|
||||
num_flips += 1
|
||||
p [num_flips, self]
|
||||
end
|
||||
p [num_flips += 1, self] if $DEBUG
|
||||
end
|
||||
|
||||
self[0..end_idx] = self[0..end_idx].reverse
|
||||
|
||||
if $DEBUG
|
||||
num_flips += 1
|
||||
p [num_flips, self]
|
||||
end
|
||||
p [num_flips += 1, self] if $DEBUG
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue