-3
\$\begingroup\$

Objective/Restrictions:

  1. May only be answered with programming language that has function syntax (or procedure, method, lambda, macro, statement grouping and labelling) .

  2. Create a function with at least 2 argument/parameter, that generates an array containing 7 random strings, each string contains 3 characters, string may only contain alphanumeric characters [a-zA-Z0-9]. The number 7 and 3 passed into the function

  3. Create a function that sort those strings using in special alphabetical ascending order: number first, alphabet later (lower-case then upper-case), for example:

    1aa
    2ac
    aab
    abc
    Aab
    bAA
    BZ2
    
  4. Create a function to print each string in descending order from the last one to the first one without mutating the ascending ordered array.

  5. Call all three function and produce the correct output

  6. Shortest code wins!

Note: if possible, give the ideone or any other online compiler link to test the correctness.

\$\endgroup\$
  • \$\begingroup\$ care to explain why -1, and now -2? \$\endgroup\$ – Kokizzu Jan 27 '15 at 12:52
  • 2
    \$\begingroup\$ All the restrictions and requirements seem arbitrary and thrown together. Because you're saying what each step needs to do, there's little room for cleverness. Your challenge looks decent and well-specified, but I wouldn't want to work on it. I'd prefer a task that just told me what task my code needed to achieve, meaning the connection the output must have to the input, and let me figure out the best way to do it. \$\endgroup\$ – xnor Jan 27 '15 at 22:44
  • \$\begingroup\$ people here are quite mean to me.. XD -5 now.. \$\endgroup\$ – Kokizzu Jan 29 '15 at 12:31
0
\$\begingroup\$

J - 70 chars

a=:'0123456789',6}.26|.a.{~65+i.58
b=:{&a@?@$&62
c=:/:62&#.@(a&i.)
d=:\:~

Usage: d c b 7 3

\$\endgroup\$
  • \$\begingroup\$ I was looking for ways to shorten your a, but I only found a.#~(0j48 10j7 26j6 26j133)#4$1, which has the same length. \$\endgroup\$ – FUZxxl Jan 27 '15 at 20:47
  • \$\begingroup\$ @FUZxxl '0123456789' is (1":i.10), that's -3 chars \$\endgroup\$ – randomra Jan 27 '15 at 21:44
  • \$\begingroup\$ @FUZxxl 48}.a.#~(58j7 26j6 26j133)#3$1 saves 1 char on your idea \$\endgroup\$ – randomra Jan 27 '15 at 21:53
  • \$\begingroup\$ b=:a{~?@$&62, c=:/:a&i., and d=:|. are shorter. Further, this definition of a gives a flat-out wrong solution, because it sorts incorrectly. Try a=:(1":i.10),,97,.&(26$}.&a.)65. \$\endgroup\$ – algorithmshark Jan 27 '15 at 23:45
2
\$\begingroup\$

Perl 5 - 181 chars

Ungolfed

@z=(0..9,a..z,A..Z);

sub d{
    map{ tr/A-Za-z/a-zA-Z/; $_ } @_
}

# n - number of strings to generate
# l - length of strings
sub a{
    ($n,$l) = @_;
    $a .= $z[rand@z] for 1..$n*$l;
    ($a=~/(.{$l})/g)
}

sub b{
    d(sort d(@_))
}

sub c{
    for(-$#_..0){
        print "$_[-$_]\n"
    }
}

c b a 7,3

Golfed

@z=(0..9,a..z,A..Z);sub d{map{tr/A-Za-z/a-zA-Z/;$_}@_}sub a{($n,$l)=@_;$a.=$z[rand@z]for 1..$n*$l;($a=~/(.{$l})/g)}sub b{d(sort d(@_))}sub c{for(-$#_..0){print"$_[-$_]\n"}}c b a 7,3
\$\endgroup\$
1
\$\begingroup\$

Perl6   145 chars

#! /usr/bin/env perl6

my@a='a'..'z';
my@b='A'..'Z';
my@c=^10,@a,@b;

# first argument $^a is the number of strings
# second argument $^b is the length of the strings
sub a{@c.roll($^b).join xx$^a};

# sub b{@_.sort} # numbers then upper then lower
sub b{@_.sort(*.trans(@a,@b Z=>@b,@a))}; # swaps upper and lower

sub c{say @_.reverse};

c b a 7,3
my@a='a'..'z';my@b='A'..'Z';my@c=^10,@a,@b;sub a{@c.roll($^b).join xx$^a};sub b{@_.sort(*.trans(@a,@b Z=>@b,@a))};sub c{say @_.reverse};c b a 7,3

After removing the comments and all of the newlines, I get 145 characters.

If the requirements were changed so that the strings were sorted by numbers then uppercase then lowercase, I could remove 25 characters from the b subroutine.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 141

Fairly straightforward solution. Uses recursion to generate the random Strings and makes use of String#tr to make the sorting short and sweet.

O=[*?0..?9]+[*?A..?Z]+[*?a..?z] 
r=->i,s{i<1?[]:([O.sample(s)*'']+r[i-1,s])}
s=->a{a.sort_by{|e|e.tr"A-Za-z","a-zA-Z"}}
p=->a{puts a.reverse}

Usage: p[s[r[7,3]]].

\$\endgroup\$
0
\$\begingroup\$

Ruby - 339 chars

X=[*('0'..'9')]+[*('a'..'z')].map{|c|[c,c.upcase]}.flatten
Y={};v=0;X.each{|c|Y[c]=(v+=1)}
def x n;n.times.to_a.map{X[(rand()*X.length).to_i]}.join '';end
def y m,n;m.times.to_a.map{x n};end
def z r;r.sort{|a,b|v=-1;w=0;a.each_char{|c|d=b[v+=1];next if c==d;w=(Y[c]<Y[d]?-1:1);break};w};end
def a b;b.reverse_each{|c|puts c};end
a z(y 7,3)

verify here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.