September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,48 +1,47 @@
#define system.
#define system'routines.
#define extensions.
import system'routines.
import extensions.
#class(extension)op
extension op
{
#method shuffled
shuffled
[
#var anOriginal := self toArray.
#var aShuffled := self toArray.
var anOriginal := self toArray.
var aShuffled := self toArray.
0 to:(anOriginal length - 1) &doEach: (:i)
0 to(anOriginal length - 1) do(:i)
[
0 to:(anOriginal length - 1) &doEach: (:j)
0 to(anOriginal length - 1) do(:j)
[
((i != j)and:[anOriginal@i != (aShuffled@j)]and:[anOriginal@j != (aShuffled@i)]) ?
if ((i != j) && $(anOriginal[i] != aShuffled[j]) && $(anOriginal[j] != aShuffled[i]))
[
aShuffled exchange:i:j.
aShuffled exchange(i,j)
].
].
].
^ aShuffled summarize:(String new) literal.
^ aShuffled summarize(String new); literal
]
#method score : anOriginalText
score : anOriginalText
[
#var aShuffled := self toArray.
#var anOriginal := anOriginalText toArray.
#var aScore := Integer new.
var aShuffled := self toArray.
var anOriginal := anOriginalText toArray.
var aScore := Integer new.
0 to:(anOriginal length - 1) &doEach: (:i)
[ ((anOriginal @ i) == (aShuffled @ i)) ? [ aScore += 1. ]. ].
0 to(anOriginal length - 1) do(:i)
[ if (anOriginal[i] == aShuffled[i]) [ aScore append:1 ] ].
^ aScore value.
]
}
#symbol program =
program =
[
("abracadabra", "seesaw", "grrrrrr", "pop", "up", "a") run &each: aWord
("abracadabra", "seesaw", "grrrrrr", "pop", "up", "a") forEach(:aWord)
[
#var aShuffled := aWord shuffled.
var aShuffled := aWord shuffled.
console writeLine:"The best shuffle of ":aWord:" is ":aShuffled:"(":(aShuffled score:aWord):")".
console printLine("The best shuffle of ",aWord," is ",aShuffled,"(",aShuffled score:aWord,")").
].
console readChar.

View file

@ -1,6 +0,0 @@
abracadabra, raababacdar, (0)
seesaw, ewaess, (0)
elk, lke, (0)
grrrrrr, rrrrrgr, (5)
up, pu, (0)
a, a, (1)

View file

@ -0,0 +1,34 @@
def count(s): reduce s as $i (0;.+1);
def swap($i;$j):
.[$i] as $x | .[$i] = .[$j] | .[$j] = $x;
# Input: an array
# Output: a best shuffle
def bestShuffleArray:
. as $s
| reduce range(0; length) as $i (.;
. as $t
| (first(range(0; length)
| select( $i != . and
$t[$i] != $s[.] and
$s[$i] != $t[.] and
$t[$i] != $t[.])) as $j
| swap($i;$j))
// $t # fallback
);
# Award 1 for every spot which changed:
def score($base):
. as $in
| count( range(0;length)
| select($base[.] != $in[.]) );
# Input: a string
# Output: INPUT, BESTSHUFFLE, (NUMBER)
def bestShuffle:
. as $in
| explode
| . as $s
| bestShuffleArray
| "\($in), \(implode), (\( length - score($s) ))" ;

View file

@ -0,0 +1,2 @@
"abracadabra", "seesaw", "elk", "grrrrrr", "up", "a", "antidisestablishmentarianism"
| bestShuffle

View file

@ -0,0 +1,52 @@
# v0.6
function bestshuffle(str::String)::Tuple{String,Int}
s = Vector{Char}(str)
# Count the supply of characters.
cnt = Dict{Char,Int}(c => 0 for c in s)
for c in s; cnt[c] += 1 end
# Allocate the result
r = similar(s)
for (i, x) in enumerate(s)
# Find the best character to replace x.
best = x
rankb = -2
for (c, rankc) in cnt
# Prefer characters with more supply.
# (Save characters with less supply.)
# Avoid identical characters.
if c == x; rankc = -1 end
if rankc > rankb
best = c
rankb = rankc
end
end
# Add character to list. Remove it from supply.
r[i] = best
cnt[best] -= 1
if cnt[best] == 0; delete!(cnt, best) end
end
# If the final letter became stuck (as "ababcd" became "bacabd",
# and the final "d" became stuck), then fix it.
i = length(s)
if r[i] == s[i]
for j in 1:i
if r[i] != s[j] && r[j] != s[i]
r[i], r[j] = r[j], r[i]
break
end
end
end
score = sum(x == y for (x, y) in zip(r, s))
return r, score
end
for word in ("abracadabra", "seesaw", "elk", "grrrrrr", "up", "a")
shuffled, score = bestshuffle(word)
println("$word: $shuffled ($score)")
end

View file

@ -1,5 +1,3 @@
package shuffle
import java.util.Random
object BestShuffle {

View file

@ -3,20 +3,19 @@ func best_shuffle(String orig) -> (String, Number) {
var s = orig.chars
var t = s.shuffle
s.range.each { |i|
s.range.each { |j|
for i (^s) {
for j (^s) {
if (i!=j && t[i]!=s[j] && t[j]!=s[i]) {
t[i, j] = t[j, i];
break;
t[i, j] = t[j, i]
break
}
}
}
var word = t.join;
(word, orig ^ word -> count("\0"));
(t.join, s ~Z== t -> count(true))
}
<abracadabra seesaw elk grrrrrr up a>.each { |word|
for word (<abracadabra seesaw elk grrrrrr up a>) {
var (sword, score) = best_shuffle(word)
"%-12s %12s: %d\n".printf(word, sword, score)
}

View file

@ -1,29 +1,52 @@
Function shuffle(s)
Dim arr()
ReDim arr(Len(s)-1)
Set objrandom = CreateObject("System.Random")
For i = 1 To Len(s)
l = Mid(s,i,1)
Do
n = objrandom.Next_2(0,Len(s))
If arr(n) = "" Then
arr(n) = l
Exit Do
End If
Loop
Next
shuffled_word = Join(arr,"")
score = 0
For j = 1 To Len(s)
If Mid(s,j,1) = Mid(shuffled_word,j,1) Then
score = score + 1
End If
Next
shuffle = shuffled_word & ",(" & score & ")"
'Best Shuffle Task
'VBScript Implementation
Function bestshuffle(s)
Dim arr:Redim arr(Len(s)-1)
'The Following Does the toCharArray() Functionality
For i = 0 To Len(s)-1
arr(i) = Mid(s, i + 1, 1)
Next
arr = shuffler(arr) 'Make this line a comment for deterministic solution
For i = 0 To UBound(arr):Do
If arr(i) <> Mid(s, i + 1, 1) Then Exit Do
For j = 0 To UBound(arr)
If arr(i) <> arr(j) And arr(i) <> Mid(s, j + 1, 1) And arr(j) <> Mid(s, i + 1, 1) Then
tmp = arr(i)
arr(i) = arr(j)
arr(j) = tmp
End If
Next
Loop While False:Next
shuffled_word = Join(arr,"")
'This section is the scorer
score = 0
For k = 1 To Len(s)
If Mid(s,k,1) = Mid(shuffled_word,k,1) Then
score = score + 1
End If
Next
bestshuffle = shuffled_word & ",(" & score & ")"
End Function
Function shuffler(array)
Set rand = CreateObject("System.Random")
For i = UBound(array) to 0 Step -1
r = rand.next_2(0, i + 1)
tmp = array(i)
array(i) = array(r)
array(r) = tmp
Next
shuffler = array
End Function
'Testing the function
word_list = Array("abracadabra","seesaw","elk","grrrrrr","up","a")
For Each word In word_list
WScript.StdOut.WriteLine word & "," & shuffle(word)
WScript.StdOut.WriteLine word & "," & bestshuffle(word)
Next

View file

@ -0,0 +1,15 @@
fcn bestShuffle(str){
s:=str.split("").shuffle(); // -->List
if(not s) return(str,str.len()); // can't shuffle "" or "a"
n:=str.len();
foreach i in (n){
foreach j in (n){
if (i!=j and s[i]!=str[j] and s[j]!=str[i]){
s.swap(i,j);
break;
}
}
}
return(s.concat(), s.zipWith('==,str).sum(0));
}

View file

@ -0,0 +1,5 @@
ss:=T("abracadabra","immediately","grrrrrr","seesaw","pop","up","a","");
foreach s in (ss){
ns,cnt:=bestShuffle(s);
println("%s --> %s (%d)".fmt(s,ns,cnt));
}