perl and python one-liner to scramble Rubik’s cube (and JS)

I was looking some program to generate a sequence to scramble rubik’s cube, but wasn’t able to find one quickly. So I decided to write a oneliner in perl (and then in python). It’s pretty ugly, but should work…

Edit: searching the web with rubik + timer also gives you a scrambler.

Je cherchais un programme pour générer une séquence permettant de mélanger mon rubik’s cube, mais je n’en ai pas trouvé rapidement. Du coup, j’ai décidé de faire une petite ligne en perl (puis en python). C’est vilain, mais ça devrait marcher…

Edit : une recherche avec rubik + timer sur le net donne aussi des « mélangeurs ».


perl -e 'import random; @t=("R","F","U","L","D","B"); @t2=(" ","2","`");for ($i=1;$i<=15+int(rand(10));$i++) {print $t[rand(6)],$t2[rand(3)]," "};print "\n";'

Be careful if you copy/paste this line (it’s a unique line): quotes may be not correct. In particular, if you find

bash: syntax error near unexpected token `”R”,”F”,”U”,”L”,”D”,”B”’

You know that’s probably because of that.

Nothing difficult here, 15+int(rand(10)) is for a formula with 15 to 25 moves, so feel free to adapt ;)

With python:
python -c "from random import choice,randrange;t='RFULDB';t2=' 2\''
for i in range(randrange(15,25)):print choice(t)+choice(t2),"

Here, there is 2 lines. The second start with for. t2 is space,2,\quote, between quotes. (Be careful with copy/paste ;))

With javascript:

var t=new Array("R","F","U","L","D","B");
var t2=new Array(" ","2","'")

for (vari=0;vari<=15+Math.floor(Math.random()*11);vari=vari+1) {
i=Math.floor(Math.random()*6);i2=Math.floor(Math.random()*3);
document.write(t[i],t2[i2]," ");}


perl -e 'import random; @t=("R","F","U","L","D","B"); @t2=(" ","2","`");for ($i=1;$i<=15+int(rand(10));$i++) {print $t[rand(6)],$t2[rand(3)]," "};print "\n";'

Attention si vous copiez/collez cette ligne (c’est une seule ligne) : les guillemets peuvent ne pas être corrects. En particulier, si vous avez :

bash: syntax error near unexpected token `”R”,”F”,”U”,”L”,”D”,”B”’

Vous saurez que ça vient certainement de là.

Rien de compliqué ici, 15+int(rand(10)) est pour obtenir une formule avec 15 à 25 coups, donc adaptez comme vous le sentez ;)

Avec python :
python -c "from random import choice,randrange;t='RFULDB';t2=' 2\''
for i in range(randrange(15,25)):print choice(t)+choice(t2),"

Ici, il y a 2 lignes. La seconde commence par le for. t2 vaut espace,2,\guillemet simple, entre des guillemets simple. (Attention au copier/coller ;))

Avec javascript :

With javascript:

var t=new Array("R","F","U","L","D","B");
var t2=new Array(" ","2","'")

for (vari=0;vari<=15+Math.floor(Math.random()*11);vari=vari+1) {
i=Math.floor(Math.random()*6);i2=Math.floor(Math.random()*3);
document.write(t[i],t2[i2]," ");}

~ by loquehumaine on 2008, July 25 - Friday.

Leave a comment