Gere-me algumas placas apropriadas!


15

O cenário: você é um designer de software que trabalha para uma empresa administrada pelo governo que cria placas de carros e outros veículos. Você foi solicitado a desenvolver um software que gera placas. Antes de você começar a trabalhar, seus chefes estabeleceram essas regras básicas.


Uma placa de carro não pode conter:

  • ASS
  • 666
  • 69<any number here>
  • <any number here>69
  • KKK
  • SHT

Regras e requisitos:

  • A matrícula deve ser gerada aleatoriamente.
  • Depois que uma placa aleatória é gerada, a mesma placa não pode ser gerada novamente.
  • Você deve produzir pelo menos 200 matrículas exclusivas . Você pode gerar mais, se quiser .
  • Você pode armazenar placas geradas em um arquivo para "lembrá-las" delas.
  • A placa do veículo contém 2 seções, uma contendo apenas três letras e uma contendo apenas três números, separadas por um traço, como este: 233-ADFou ADF-233.
  • Você só pode usar números e letras maiúsculas.
  • As matrículas podem ser gravadas em stdout ou em um arquivo.
  • Cada "lado" de uma placa de carro conterá três números ou letras.
  • Este é um , a resposta mais curta e mais popular ganha. O vencedor será escolhido após sete dias.

Regras gerais

  • A resposta deve incluir, sem limitação, o seguinte.
  • Nome do idioma
  • Contador de caracteres.
  • Tamanho do arquivo.
  • Como o código é executado.
  • O próprio código.
  • Exemplo: caracteres Python 234 ou Python 23mb .

Se eu precisar esclarecer detalhes adicionais, mencione-o nos comentários e eu o adicionarei ao meu post. De qualquer forma, boa sorte, e me dê algumas placas apropriadas!


Atualização 1: O vencedor será escolhido um pouco antes.

Acontece que eu tenho que ir em uma viagem em breve, por isso estarei escolhendo um vencedor por volta das 00:00 UTC de 25 de julho. Depois que o vencedor for escolhido, você ainda poderá enviar inscrições, apenas saiba que um vencedor foi escolhido. Bai.


Atualização 2: Vencedores!

Temos vencedores! Yay! Queijo e vinho para todos que participaram! Aqui está quem ganhou.

  • 1º lugar: Àngel - Bash (95 caracteres)
  • 2º lugar: Martin Büttner - Mathematica (182 bytes)
  • 2º lugar: Emilio M Bumachar - Pyg (92?)
  • 2º lugar: Peter Taylor - Golfscript (98 caracteres)
  • 3º lugar: Mark Thomas - Ruby (127 caracteres)

Uau, três empates em segundo lugar. Uau. A competição terminou, mas fique à vontade para enviar inscrições, se quiser. Bai!



2
" Depois que uma placa aleatória é gerada, a mesma placa não pode ser gerada novamente. " E as placas não aleatórias?
Peter Taylor

4
A maneira óbvia de fazer isso (e provavelmente a maneira como funciona na vida real) é gerar os números em ordem.
Peter Taylor

5
"Gerado aleatoriamente" é impreciso. Eu presumo que você quer dizer "uniformemente seleccionadas aleatoriamente a partir das placas de licença legal não utilizados" em vez de, digamos, uniformemente seleccionadas aleatoriamente a partir das placas legais começandoAAA-
Peter Taylor

1
informe quantos dígitos deve estar em cada placa, e quais caracteres são válidos - que varia de país para país
haskeller orgulhoso

13
Agora estou um pouco tentado a escrever um programa para gerar placas aleatórias que contenham uma sequência obscena ou inapropriada, não encontrada em sua lista.
Ilmari Karonen

Respostas:


12

bash (95 caracteres)

Salve o script como mem uma pasta no seu PATH com o conjunto de bits de execução.

Run as bash m. The plates are stored in file p

l(){ tr -dc $1</dev/urandom|head -c3;};egrep -ve"ASS|666|69|KKK|SHT" -fp>>p<<<`l 0-9`-`l A-Z`;m

This is equivalent to running the following:

# Print three random numbers and three random letters
echo $(tr -dc 0-9 < /dev/urandom | head -c3)-$(tr -dc A-Z < /dev/urandom | head -c3) |

# Print only plates not matching the blacklist or any line of p
# Append the plates ton p
egrep -v -e "ASS|666|69|KKK|SHT" -f p >> p

# Execute itself again
m 

Caveat: The final m should actually be exec m (+5 chars) in order to avoid leaving processes waiting for completion (but you can have thousands without much problem)

Credit goes to http://www.cyberciti.biz/faq/linux-random-password-generator/ for the idea of using tr -dc


Is it to me or the uniqueness requirement is missing?
Cristian Ciupitu

1
@Cristian-Ciupitu: The grep -v is excluding both the blacklist and the list of plates we already generated (grep is expecting p to contain one pattern per line, but as the plates don't contain regular expression metacharacters, they only match themselves). We only generate one or zero plates per iteration, so after each iteration the full (updated) list of plates to exclude will be read by grep. :D
Ángel

6

PYG - 92

Pe(Se(Re.sub(".*(666|69|ASS|KKK|SHT).*","",J(RSm(STuc*3,3)+[j]+RSm(STd*3,3)))for j in'-'*K))

Now able to select uniformly from all unused plates, keeping to OP's specs, while being shorter by 1 more character.

It's theoretically possible that the list of 999 plates will contain enough repetitions so that the trimmed set will be less than 200. But the odds of that are infinitesimally small. In ten trials, the lowest length I got was 994.

EDIT: changed 999 to K (which is pyg for 1000), to save two chars at the advice of bitpwner.


1
Did you filter out the disallowed values? Don't see any KKK or 666 anywhere.
Vectorized

5
@bitpwner: I use only the digits from 0 to 5 and letters from A to J, so the disallowed values cannot occur.
Emilio M Bumachar

Very clever! Nobody ever said those others had to be used. Nice.
Kjeld Schmidt

1
OP agreed to the randomness being "uniformly selected at random from the unused legal licence plates". See comments in question. Unless your definition of uniform means uniform from any range. And you can change 999 to K, saving u 2 chars.
Vectorized

3
Not sure I'd count output as random if there's valid plates that are impossible to generate...
Alconja

5

Mathematica, 182 bytes

Ugh, this is long

l={};While[Length[l=Union@Pick[l,StringFreeQ[l,"ASS"|"666"|"69"|"KKK"|"SHT"]]]<200,AppendTo[l,RandomSample[FromCharacterCode/@{48+9~(r=RandomInteger)~3,65+25~r~3}]~Riffle~"-"<>""]];l

Ungolfed

l = {};
While[
  Length[
    l = Union@
      Pick[l, StringFreeQ[l, "ASS" | "666" | "69" | "KKK" | "SHT"]]
  ] < 200,
  AppendTo[l, 
   RandomSample[
      FromCharacterCode /@ {48 + 9~(r = RandomInteger)~3, 
        65 + 25~r~3}]~Riffle~"-" <> ""]
  ];
l

Pretty straight-forward. Generates random plates, and filters out duplicates and forbidden ones until 200 are found.


5

GolfScript (98 chars)

260{3?}:^~,{.10^+`-3>'-'+\10^/26^+26base(;{65+}%+.-1%}%{'ASSKKKSHT66669'3/{1$\?)!},*},{,^^rand}$n*

This generates all possible licence plates in a random order using some ugly base conversion followed by filtering. There are a lot of them, so don't expect it to execute quickly, but the question didn't place any constraints on execution time.


4

JavaScript (ES6) - 213

It can probably be improved. Tested on Firefox Console.

Change that alert to a console.log() if you want to test

r=x=>~~(Math.random()*x)+'';l=x=>[...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'][r(26)];a=[];while(a.length<200)p=r(10)+r(10)+r(10)+'-'+l()+l()+l(),!/ASS|666|69|KKK|SHT/.test(p)&&a.indexOf(p)<0&&a.push(p);a.forEach(x=>alert(x))

Wow, that has to be the longest single line of code I've ever seen.
DatEpicCoderGuyWhoPrograms

2
@DatEpicCoderGuyWhoPrograms you obviously haven't seen my 400+ char single-line JS regex ;)
Eric Lagergren

@eric_lagergren That sounds like it would have been a pain to write...
DatEpicCoderGuyWhoPrograms

3
@DatEpicCoderGuyWhoPrograms You can write it over multiple lines then remove the unnecessary whitespaces. There are multiple online services who offer that + other shortening (replacing function /var etc. names) to reduce js file sizes and subsequently save bandwidth.
SBoss

1
@DatEpicCoderGuyWhoPrograms optimize afterwards. I figure out how to correctly solve the problem, then I try and find shortcuts, and then I finally optimize my code :P a lot simpler
Eric Lagergren

4

Ruby — 136 133 129 characters

Hideous. Think there's room for improvement, though. Just drop the code in irb or pry and hit enter to run:

f=->*t{[0,1,2].map{t.sample}*''}
g=->l{(a=f[*?A..?Z]+?-+f[*?0..?9];l|=[a]if/69|666|ASS|SHT|KKK/!~a)until l.size>199;l}
puts g[[]]

4

Ruby, 127 chars

My attempt at a "readable" Ruby version:

a=[]
until a.size==200 do
  p="#{rand(899)+100}-#{('A'..'Z').to_a.sample(3).join}"
  a<<p unless p=~/69|666|ASS|SHT|KKK/
end
puts a

Note that this generates compliant license plates, but won't generate the entire set of possible plates (as with most of these answers). That doesn't seem to be a requirement.
Mark Thomas

4

Python 2.7 - 258 chars

I'm not a professional programmer or anything, so I'd say I'm satisfied with the result.

import random as o
r=o.randint
t,j,k=[],0,""
b=["SHT","KKK","ASS","69","666"]
for i in range(200):
 l,j=b[0],b[4]
 while any(w in l for w in b):
  l,j="",""
  for i in range(3):
   l+=chr(r(65,90))
   j+=str(r(0,9))
 t.append(l+'-'+j)
print "\n".join(set(t))

Filesize is 4.0 K, run with python file.py !


Shouldn't that pass be a continue? You can also save some chars by indenting with a 1 space instead of 4.
Cristian Ciupitu

Also for i in range(0,200): could be replaced with for i in range(200):.
Cristian Ciupitu

@CristianCiupitu I couldn't get it to do the full 200 when using continue.. But pass did the trick. Plus, it's shorter. And when I tried for i in range(200), it only did 199 ^^ I counted them afterwards by making a duplicates variable and putting duplicates += 1 before pass and counting the occurrences for - in the list/string.
Adam

@CristianCiupitu range(200) worked after all - the 4 spaces in the code block are actually \t and replacing them by 1 space didn't change the character count... Thanks for the suggestions!
Adam

1
A user suggested in a suggested edit to simply remove if k in t: pass, as it does nothing.
Doorknob

3

Python - 208

Hi heres my stab at license plate generation. This solution is similar to @bitpwner's solution but without the string module and instead of a list for the license plate I chose to use a set and its also allows numbers first.

import random as r,re
f=r.randint
l=lambda x:chr(f(65, 90))if x else`f(0,9)`
d=set()
while len(d)<200:
 k=f(0,1);j=1-k;c=l(k)+l(k)+l(k)+'-'+l(j)+l(j)+l(j)
 if not(re.search("666|69|ASS|KKK|SHT",c)):d.add(c)

Sample output:

set(['DQJ-641', '086-QRY', '981-GAZ', 'UHN-718', '114-VMI', 'GLO-887',  ...

3

Python, 252 bytes

Here's my contribution. I'm impressed with it, but I know others have done better with python.

from random import randint as r
f=()
while len(f)<200:
 t=str(r(0,999))
 if not("666" in t or "69" in t):
  u=''.join(chr(r(65,90)) for _ in [1,2,3])
  if not("KKK" in u or "SHT" in u or "ASS" in u):f+=("%s-%s"%(t.zfill(3),u),)
 f=tuple(set(f))
print f

2

Python - 165

Those imports...

import random as r,re
j="666|69|ASS|KKK|SHT"
t=r.randint
while len(j)<2e3:
 exec"x="+"chr(t(0,25)+65)+"*3+"'-'"+"+`t(0,9)`"*3
 if not re.search(j,x):print x;j+='|'+x

If there is a need to begin randomly with either numbers or alphabets, which I don't think is really needed, then 190.

import random as r,re
j="666|69|ASS|KKK|SHT"
t=r.randint
while len(j)<2e3:
 exec"x="+"chr(t(0,25)+65)+"*3+"'-'"+"+`t(0,9)`"*3
 x=x[::r.choice((-1,1))]
 if not re.search(j,x):print x;j+='|'+x

Chars or bytes?
DatEpicCoderGuyWhoPrograms


I count 208 chars, and does your solution allow numbers first?
Willem

@willem The four spaces for indentation are actually tabs, and alphabets first only.
Vectorized

2

PHP 341 324 320

Was the best I could do.

<?$a="p";$b=fopen($a,'a+');while($c<200){$d=rand(100,999);$e='';for($f=0;$f<3;++$f)$e.=chr(rand(65,90));$g=(rand(1,2)==1)?"$d-$e":"$e-$d";$h=array('ASS','666','69','kkk','SHT');$i=1;foreach($h as $j)!preg_match("/$j/",$g)?:++$i;if($i==1){$k=fread($b,filesize($a));if(!strpos($k,$g)){fwrite($b,$g);echo"$g<br />";++$c;}}}

To run the code just save as a .php file and browse to it on any web server. It will attempt to create the blacklist file p.txt if it does not exist already. However you may need to define it with a full server path if you do not have root access.

The code itself is here pre golfification:

<?
// create random plate
// check it does not break rules
// check is not on all time blacklist file
// Add to blacklist file
// Output to screen

// open file handle
$file = "p"; // filename and path if not root access
$fh = fopen($file, 'a+');

// do 200
while($x<200) {

    // get random number
    $rand_number = rand(100,999);

    // get random letters
    $letters = '';
    for($y=0; $y<3; ++$y) $letters .= chr(rand(65,90));


    // mix up combination
    $string = (rand(1,2)==1) ? "$rand_number-$letters" : "$letters-$rand_number";

    // assume is ok
    $ok = 1;

    // Set checks to be excluded on new plates.
    $checks = array('ASS','666','69','kkk','SHT');

    // do the exclusions
    foreach ($checks as $check) !preg_match("/$check/", $string) ? : ++$ok;


    // if all ok, check is not on the blacklist
    if($ok == 1) {

        // read blacklist
        $blacklist = fread($fh, filesize($file));

        // if not on blacklist, add it to file, echo it to output, increment counter
        if (!strpos($blacklist, $string)) {
            fwrite($fh, $string);
            echo "$string<br />";
            ++$x;
        }
    }
}

Was as short as I could get it :-(

Sample Output

XWU-888
PUD-534
355-QXG
WDE-402
113-QID
362-YBW
TBK-594
939-XDT
148-ARZ
838-ICY
723-ZDA
.... does exactly 200 new plates.

EDIT: tidied up a couple of if statements to use short form.


If the plates do not have to be mixed up - ie can be numbers then letters only, I could lose this line $string = (rand(1,2)==1) ? "$rand_number-$letters" : "$letters-$rand_number";
Paul Drewett

1
You could use a single-character filename (e.g., p instead of p.txt) and save yourself 4 characters, too.
Mark

@Mark I didn't know you could do that. Tested it and it worked fine. Read up on file extensions and they do not work quite the way I thought they did. Thank you, that was very interesting.
Paul Drewett

1

Delphi, 161 bytes

Here is my take on this. It outputs license plates to stdout without line feed between them. If LF is needed (not specified in the rules), than that adds extra 4 bytes.

Golfed version:

var S,L:string;begin repeat Str(100+Random(69),S);S:=S+'-';while Length(S)<7do S:=S+Chr(65+Random(10));if Pos(S,L)=0then L:=L+S;until Length(L)>1393;Write(L)end.

Ungolfed:

var
  S, L: string;
begin
  repeat
    Str(100 + Random(69), S); // generate and add first three numbers
    S := S + '-'; // add dash
    while Length(S) < 7 do // generate and add last three letters
      S := S + Chr(65 + Random(10));
    if Pos(S, L) = 0 then // check if its not in the L string and add it
      L := L + S;
  until Length(L) > 1393; // exit loop once L string has more than 1393 chars (199 * 7 = 1393)
  Write(L); // output L to stdout
end.

How to run:

app.exe > plates.txt

1

PHP, 267

This is about as short as I can get it.

<?php $g=file("p",2)?:[];$b=["ASS","666","KKK","SHT"];for($i=0;$i<200;){$m="A";$n=rand(702,18277);for($j=0;$j<$n;$j++){$m++;}$m.=-rand(100,999);if(!(strpos($m,"69")|in_array($m,$b)|in_array($m,$g))){$g[]=$m;echo"$m\n";$i++;}}file_put_contents("p",implode("\n",$g));?>

Plates are stored in file "p".

<?php
$g=file("p",2)?:[]; // Read existing plates
$b=["ASS","666","KKK","SHT"]; // Don't generate these
for($i=0;$i<200;){ // 200 plates
    $m="A"; // Base letter
    $n=rand(702,18277); // 3 random letters
    for($j=0;$j<$n;$j++){$m++;} // Increment until letters are reached (SLOW, but short)
    $m.=-rand(100,999); // Add a dash and three numbers
    if(!(strpos($m,"69")|in_array($m,$b)|in_array($m,$g))){ // Check that it's valid and unused
        $g[]=$m;echo"$m\n";$i++; // Echo it, add it to used array and increment counter
    }
}
file_put_contents("p",implode("\n",$g)); // Save the plates
?>

1

R, 229 characters

I'm sure this could be improved:

l=function(x)paste0(sample(x,3,r=T),collapse="")
a=function()c(l(LETTERS),l(0:9))
A=list()
for(i in 1:200)while(any(sapply(c("ASS","666","69","KKK","SHT"),grepl,A[[i]]<-a()))|A[i]%in%A[-i])A[[i]]=a()
lapply(A,paste,collapse="-")

Run in the console, prints a list of license plates.


1

Cobra - 198

class P
    def main
        l,r=[],Random()
        while l.count<200
            a,b=r.next(1000),''
            for i in 3,b+='[r.next(65,91)to char]'
            if not ('69'in'[a]'or 666==a or b in'ASS KKK SHT'),l+=['[a]-'+b]
        print l

1

ECMAScript 6 - 155 168 158

Warning: 200 alert dialogs (change alert to console.log to test)

for(i=0,s={},r=Math.random,l=x=>String.fromCharCode(65+r()*26);i<200;)/ASS|666|69|KKK|SHT/.test(p=(r()+'-'+l()+l()+l()).slice(-7))?0:s[p]=s[p]||(alert(p),i++)

Edit: Oops. Original version printed duplicates...

Edit 2: Closer to the original score now - switched from a set to an associative array with some fugly duplicate checks allowing it to print as it goes

Tested in Firefox console.


Congratulations improving my solution
William Barbosa

I'm getting a "Syntax error" on: l=x=>String.fromCharCode(65+r()*26);. I guess I don't know what's happening with l=x=>...
Kevin Fegan

@KevinFegan - I'm guessing you're not using Firefox... f=a=>b is an ES6 feature that's basically shorthand for function f(a) { b } and it's only(?) supported by Firefox at the moment.
Alconja

Yes, I'm using IE 9, and it definately doesn't work here. I'll try it on Firefox. Thanks.
Kevin Fegan

1

JavaScript (ES6) 184

As usual, test in FireFox console and change alert to console.log or be prepared to press escape 200 times.

R=x=>Math.random()*++x|0
for(l='ABCDEFGHIKJLMNOPQRSTUVWXYZ',i=0,u={};i<200;)
!(/69|666|ASS|SHT|KKK/.test(k=l[R(25)]+l[R(25)]+l[R(25)]+'-'+R(9)+R(9)+R(9))&u[k])&&alert(k,u[k]=++i);

I don't know about the console, but web pages are only allowed 5 free alerts before Firefox starts offering to disable them: mxr.mozilla.org/mozilla-central/…
Neil

@Neil of course, but offering to disable is not disabling. Shouldn't you enjoy 200 (or more codegolf.stackexchange.com/a/32278/21348) popups?
edc65

1

Python3, 257 chars

import string as X,re,random as R
I=[0,1,2]
s={}
while len(s)<200:
 L=R.sample([[R.choice(X.digits) for i in I],[R.choice(X.ascii_uppercase) for i in I]],2);L=''.join(L[0]+['-']+L[1])
 if re.search('ASS|KKK|SHT|69|666',L) or L in s:continue
 print(L);s[L]=0

Sample output:

# python3 shortened.py
EUN-215
546-SIL
464-ZTR
XIX-794

1
You can save a byte by separating line 5 and 6 by ; instead of \n .
undergroundmonorail

@undergroundmonorail, you're right, thank you!
Cristian Ciupitu

1

PHP,167

while(count($a)<200){$c="";for(;++$y&3;)$c.=chr(rand(65,90));$d=rand(100,999);$c=rand()&1?"$d-$c":"$c-$d";preg_match("/ASS|666|69|KKK|SHT/",$c)||$a[$c]=1;}print_r($a);

that's 100 chars less than current PHP's best :)

while(count($a)<200)
{
    $c="";
    for(;++$y&3;) $c.=chr(rand(65,90));
    $d=rand(100,999);
    $c=rand()&1?"$d-$c":"$c-$d";
    preg_match("/ASS|666|69|KKK|SHT/",$c)||$a[$c]=1;
}
print_r($a);

hope you like it. In case it is allowed:

while(count($a)<200){$c="";for(;++$y&3;)$c.=chr(rand(65,90));$c.=-rand(100,999);preg_match("/ASS|666|69|KKK|SHT/",$c)||$a[$c]=1;}print_r($a);

is only 141 chars but doesn't shuffle chars and numbers. Any suggestions wellcome :)


1

F#, 264 chars

Not really a language designed for golfing, but I'm sure this could be improved. Using Seq.exists with a lambda is pretty annoying, as are the many parens and lack of implicit conversion.

Uses recursion, keeps going forever.

let g=System.Random()
let c()=char(g.Next(65,90))
let k(i:string)l=Seq.exists(fun e->i.Contains(e))l
let rec p d:unit=
 let l=sprintf"%i-%c%c%c"(g.Next(100,999))(c())(c())(c())
 if k l d||k l ["ASS";"666";"69";"KKK";"SHT"]then p d else
  printfn"%s"l
  p(l::d)
p[]

Can be run in FSI.


1

Python 203

I'm not sure if this technically counts, but I liked it so I'm posting it anyway. While I do generate the answers pseudo-randomly, as pretty much everyone else did, I strategically picked the random seed such that invalid answers wouldn't end up in the output. So, my answer isn't actually capable of generating the entire set of valid answers, without also generating the invalid ones.

from random import*;seed(1);L='ABCDEFGHIJKLMNOPQRSTUVWXYZ';D='0123456789';C=choice
for i in 'x'*200:s=randint(0,1);a=''.join(C(L)for _ in'000');b=''.join(C(D)for _ in'000');i=[a,b];print i[s-1]+'-'+i[s]

1

Perl - 123 Characters

while(@p<200){$l=(AAA..ZZZ)[int rand 999]."-".(100+int rand 899);@p=grep!/ASS|666|69|KKK|SHT|$l/,@p;push@p,$l}$,=$/;print@p

Ungolfed:

while(@p < 200){ # Repeat until we get 200 plates
    $l = (AAA..ZZZ)[int rand 999]."-".(100+int rand 899); # generate the license plate
    @p = grep !/ASS|666|69|KKK|SHT|$l/, @p; # remove disallowed license ones and duplicates
    push @p, $l # add a license plate
}
$,=$/; # so they print with newlines
print @p # print the plates

If anyone has ideas to golf it further, let me know, I am interested. If you want further explanation of part of the code, leave a comment and I'd be happy to explain more too.


1

Javascript - 283 327 Characters

Edit:

After implementing the suggestions from Alconja, here's my new version:

m=Math.random;function y(v){return "ASS|KKK|SHT|666".indexOf(v)<0&&v.indexOf("69")<0?0:!0}function c(){return String.fromCharCode(m()*26+65)}for(i=0;i<200;i++){do {do {n=(m()+"").slice(2,5)}while(y(n));do {l=c()+c()+c()}while(y(l));r=l+"-"+n}while(o.indexOf(r)>=0);o+=r+"\n"}alert(o)
/* 1 line - 283 Characters */

1) Remove Variable:s and use literal:"\n" [-4][323]
2) Remove "var o="",i,r,n,l," [-17][306]
3) Remove Variable:t and use literal:"ASS|KKK|SHT|666" [-4][302]
4) Set m=Math.random and use "m" instead [-7][296]
5) Use (m()+"") rather than m().toString() [-6][290]
6) Remove unneeded ";" [-7][283]



Old-version: Javascript - 327 Characters

I'm sure there's some room for improving... I'm pretty inexperienced at Code-golfing:

var o="",s="\n",i,r,n,l,t="ASS|KKK|SHT|666";function y(v){return t.indexOf(v)<0&&v.indexOf("69")<0?0:!0;}function c(){return String.fromCharCode(Math.random()*26+65);}for(i=0;i<200;i++){do {do {n=Math.random().toString().slice(2,5);}while(y(n));do {l=c()+c()+c();}while(y(l));r=l+"-"+n;}while(o.indexOf(r)>=0);o+=r+s;}alert(o);    
/* 1 line - 327 Characters */


Here is a formatted, "Ungolfed" version with "un-minified" variable/function names:

var outp="",lsep="\n",ndx,res,nbr,ltr,tbl="ASS|KKK|SHT|666";
function fnvfy(vinp){
  return tbl.indexOf(vinp)<0&&vinp.indexOf("69")<0?0:!0;
}
function fnchr(){
  return String.fromCharCode(Math.random()*26+65);
}
for(ndx=0;ndx<200;ndx++){
  do {
    do {
      nbr=Math.random().toString().slice(2,5);
    }
    while(fnvfy(nbr));
    do {
      ltr=fnchr()+fnchr()+fnchr();
    }
    while(fnvfy(ltr));
    res=ltr+"-"+nbr;
  }
  while(outp.indexOf(res)>=0);
  outp+=res+lsep;
}
alert(outp);



Here is a "debug" version that can be pasted into URL of browser favorite/bookmark. Output is placed in a "TEXTAREA" on a new "window" instead of "alert()":

javascript:(function(){var outp="",lsep="\n",ndx,res,nbr,ltr,tbl="ASS|KKK|SHT|666";function fnvfy(vinp){return tbl.indexOf(vinp)<0&&vinp.indexOf("69")<0?0:!0;}function fnchr(){return String.fromCharCode(Math.random()*26+65);}for(ndx=0;ndx<200;ndx++){do {do {nbr=Math.random().toString().slice(2,5);}while(fnvfy(nbr));do {ltr=fnchr()+fnchr()+fnchr();}while(fnvfy(ltr));res=ltr+"-"+nbr;}while(outp.indexOf(res)>=0);outp+=res+lsep;}var x=window.open();x.document.write('<head>\n</head>\n<body>\n<form name=sa><textarea name=t rows=25 cols=80 wrap>'+outp+'</textarea><br />\n</body>\n');x.document.close();})()
/* */

Here is the "debug" version, formatted:

javascript:
(function(){
  var outp="",lsep="\n",ndx,res,nbr,ltr,tbl="ASS|KKK|SHT|666";
  function fnvfy(vinp){
    return tbl.indexOf(vinp)<0&&vinp.indexOf("69")<0?0:!0;
  }
  function fnchr(){
    return String.fromCharCode(Math.random()*26+65);
  }
  for(ndx=0;ndx<200;ndx++){
    do {
      do {
        nbr=Math.random().toString().slice(2,5);
      }
      while(fnvfy(nbr));
      do {
        ltr=fnchr()+fnchr()+fnchr();
      }
      while(fnvfy(ltr));
      res=ltr+"-"+nbr;
    }
    while(outp.indexOf(res)>=0);
    outp+=res+lsep;
  }
  var x=window.open();
  x.document.write('<head>\n</head>\n<body>\n<form name=sa><textarea name=t rows=25 cols=80 wrap>'+outp+'</textarea><br />\n</body>\n');
  x.document.close();
}
)()

2
Without touching your actual algorithm, here's a few generic javascript golfing tips: Javascript is inherently forgiving with bad syntax, so you don't need to use var (just assigning will do, 323), you don't need ;s unless there's another statement following (eg. the last character in the line, or before a }, 316), watch for anything that takes more space to declare/use than just inline (eg. your s variable, 312), ditto for the reverse if something is used more than once (eg. Math.random(...) to r=Math.random ... r(...), 307, (x+"") is shorter than x.toString(), 300
Alconja

@Alconja - Thanks for the help. I was able to reduce the size by 44 characters.
Kevin Fegan
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.