Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,45 @@
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
template <class S>
class BestShuffle {
public:
BestShuffle() : rd(), g(rd()) {}
S operator()(const S& s1) {
S s2 = s1;
shuffle(s2.begin(), s2.end(), g);
for (unsigned i = 0; i < s2.length(); i++)
if (s2[i] == s1[i])
for (unsigned j = 0; j < s2.length(); j++)
if (s2[i] != s2[j] && s2[i] != s1[j] && s2[j] != s1[i]) {
swap(s2[i], s2[j]);
break;
}
ostringstream os;
os << s1 << endl << s2 << " [" << count(s2, s1) << ']';
return os.str();
}
private:
static int count(const S& s1, const S& s2) {
auto count = 0;
for (unsigned i = 0; i < s1.length(); i++)
if (s1[i] == s2[i])
count++;
return count;
}
random_device rd;
mt19937 g;
};
int main(int argc, char* arguments[]) {
BestShuffle<basic_string<char>> bs;
for (auto i = 1; i < argc; i++)
cout << bs(basic_string<char>(arguments[i])) << endl;
return 0;
}

View file

@ -2,55 +2,47 @@
#define system'routines.
#define extensions.
// --- shuffle ---
#symbol shuffle = (:aLiteral)
[
#var aLength := aLiteral length.
#var aShuffled := literalControl toArray:aLiteral.
control forrange &int:0 &int::(aLength-1) &do: (&int:i)
#class(extension)op
{
#method shuffled
[
#var aChar := aLiteral@i.
(aChar == (aShuffled@i)) ?
[
control forrange &int:0 &int::(aLength-1) &do: (&int:j)
[
#var anAltChar := aShuffled@j.
(anAltChar != aChar)and:[ anAltChar != (aLiteral@i) ]and:[aChar != (aLiteral@j)] ?
[
arrayControl exchange:i &with:j &in:aShuffled.
#var anOriginal := self toArray.
#var aShuffled := self toArray.
#break.
].
0 to:(anOriginal length - 1) &doEach: (:i)
[
0 to:(anOriginal length - 1) &doEach: (:j)
[
((i != j)and:[anOriginal@i != (aShuffled@j)]and:[anOriginal@j != (aShuffled@i)]) ?
[
aShuffled exchange:i:j.
].
].
].
].
^ Summing new:(String new) foreach:aShuffled literal.
].
^ aShuffled summarize:(String new) literal.
]
// --- ShuffleScore ---
#method score : anOriginalText
[
#var aShuffled := self toArray.
#var anOriginal := anOriginalText toArray.
#var aScore := Integer new.
#symbol scoreShuffle = (:anOriginal:aShuffled)
[
#var aScore := Integer new.
0 to:(anOriginal length - 1) &doEach: (:i)
[ ((anOriginal @ i) == (aShuffled @ i)) ? [ aScore += 1. ]. ].
control forrange &int:0 &int:(anOriginal length - 1) &do: (&int:i)
[ (anOriginal @ i) == (aShuffled @ i) ? [ aScore += 1. ]. ].
^ aScore value.
].
// --- Program ---
^ aScore value.
]
}
#symbol program =
[
control foreach:("abracadabra", "seesaw", "grrrrrr", "pop", "up", "a") &do: aWord
("abracadabra", "seesaw", "grrrrrr", "pop", "up", "a") run &each: aWord
[
#var aShuffled := shuffle:aWord.
#var aShuffled := aWord shuffled.
consoleEx writeLine:"The best shuffle of ":aWord:" is ":aShuffled:"(":(scoreShuffle:aWord:aShuffled):")".
console writeLine:"The best shuffle of ":aWord:" is ":aShuffled:"(":(aShuffled score:aWord):")".
].
console readChar.

View file

@ -0,0 +1,40 @@
package shuffle
import java.util.Random
object BestShuffle {
operator fun invoke(s1: String) : String {
val s2 = s1.toCharArray()
s2.shuffle()
for (i in s2.indices)
if (s2[i] == s1[i])
for (j in s2.indices)
if (s2[i] != s2[j] && s2[i] != s1[j] && s2[j] != s1[i]) {
val tmp = s2[i]
s2[i] = s2[j]
s2[j] = tmp
break
}
return s1 + ' ' + String(s2) + " (" + s2.count(s1) + ')'
}
private fun CharArray.shuffle() {
val rand = Random()
for (i in size - 1 downTo 1) {
val r = rand.nextInt(i + 1)
val tmp = this[i]
this[i] = this[r]
this[r] = tmp
}
}
private fun CharArray.count(s1: String) : Int {
var count = 0
for (i in indices)
if (s1[i] == this[i])
count++
return count
}
}
fun main(words: Array<String>) = words forEach { println(BestShuffle(it)) }

View file

@ -1,42 +1,41 @@
/*REXX program to find the best shuffle (for a character string). */
parse arg list /*get words from the command line*/
if list='' then list='tree abracadabra seesaw elk grrrrrr up a' /*def.?*/
w=0 /*widest word , for prettifing. */
do i=1 for words(list)
w=max(w,length(word(list,i))) /*the maximum word width so far. */
end /*i*/
w=w+5 /*add five spaces to widest word.*/
do n=1 for words(list) /*process the words in the list. */
$=word(list,n) /*the original word in the list. */
new=bestShuffle($) /*shufflized version of the word.*/
say 'original:' left($,w) 'new:' left(new,w) 'count:' kSame($,new)
/*REXX program finds the best shuffle (for any list of words (characters). */
parse arg @ /*get some words from the command line.*/
if @='' then @ = 'tree abracadabra seesaw elk grrrrrr up a' /*use default? */
w=0 /*width of the longest word; for output*/
do i=1 for words(@) /* [↓] process all the words in list. */
w=max(w, length(word(@, i))) /*set the maximum word width (so far). */
end /*i*/ /* [↑] ··· finds the widest word in @.*/
w=w+9 /*add 9 blanks, the output looks nicer.*/
do n=1 for words(@) /*process all the words in the @ list. */
$=word(@,n) /*get the original word in the @ list. */
new=bestShuffle($) /*get a shufflized version of the word.*/
say 'original:' left($,w) 'new:' left(new,w) 'count:' kSame($,new)
end /*n*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BESTSHUFFLE subroutine──────────────*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────BESTSHUFFLE subroutine────────────────────*/
bestShuffle: procedure; parse arg x 1 ox; Lx=length(x)
if Lx<3 then return reverse(x) /*fast track these puppies. */
if Lx<3 then return reverse(x) /*fast track these small # of puppies. */
do j=1 for Lx-1 /*first take care of replications*/
do j=1 for Lx-1; jp=j+1 /* [↓] handle any possible replicates.*/
a=substr(x,j ,1)
b=substr(x,j+1,1); if a\==b then iterate
_=verify(x,a); if _==0 then iterate /*switch 1st rep with some char. */
y=substr(x,_,1); x=overlay(a,x,_)
x=overlay(y,x,j)
rx=reverse(x); _=verify(rx,a); if _==0 then iterate /*¬ enuf unique*/
y=substr(rx,_,1); _=lastpos(y,x) /*switch 2nd rep with later char.*/
x=overlay(a,x,_); x=overlay(y,x,j+1) /*OVERLAYs: a fast way to swap*/
b=substr(x,j+1,1); if a\==b then iterate /*ignore replicates.*/
_=verify(x,a); if _==0 then iterate /*switch 1st replicate with some char. */
y=substr(x,_,1); x=overlay(a,x,_)
x=overlay(y,x,j)
rx=reverse(x); _=verify(rx,a); if _==0 then iterate /*¬enough uniqueness*/
y=substr(rx,_,1); _=lastpos(y,x) /*switch 2nd replicate with later char.*/
x=overlay(a,x,_); x=overlay(y,x,jp) /*OVERLAYs: a fast way to swap chars. */
end /*j*/
do k=1 for Lx /*take care of same o'-same o's. */
a=substr(x, k,1)
b=substr(ox,k,1); if a\==b then iterate
if k==Lx then x=left(x,k-2)a || substr(x,k-1,1) /*last case*/
else x=left(x,k-1)substr(x,k+1,1)a || substr(x,k+2)
do k=1 for Lx /*handle cases of possible replicates. */
a=substr( x,k,1)
b=substr(ox,k,1); if a\==b then iterate /*skip replicate. */
if k==Lx then x=left(x,k-2)a || substr(x,k-1,1) /*handle last case*/
else x=left(x,k-1)substr(x,k+1,1)a || substr(x,k+2)
end /*k*/
return x
/*──────────────────────────────────KSAME procedure─────────────────────*/
kSame: procedure; parse arg x,y; k=0
do m=1 for min(length(x),length(y))
k=k + (substr(x,m,1) == substr(y,m,1))
end /*m*/
/*──────────────────────────────────KSAME procedure───────────────────────────*/
kSame: procedure; parse arg x,y; k=0
do m=1 for min(length(x), length(y)); k=k + (substr(x,m,1) == substr(y,m,1))
end /*m*/
return k

View file

@ -0,0 +1,29 @@
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 & ")"
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)
Next