20
\$\begingroup\$

We've had a lot of alphabet challenges. For this challenge, you are passed the output of an alphabet challenge, and you need to output the pattern scaled to size N.

For example, if N=5 and you were passed the L-phabet:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
BBCDEFGHIJKLMNOPQRSTUVWXYZ
CCCDEFGHIJKLMNOPQRSTUVWXYZ
DDDDEFGHIJKLMNOPQRSTUVWXYZ
EEEEEFGHIJKLMNOPQRSTUVWXYZ
FFFFFFGHIJKLMNOPQRSTUVWXYZ
GGGGGGGHIJKLMNOPQRSTUVWXYZ
HHHHHHHHIJKLMNOPQRSTUVWXYZ
IIIIIIIIIJKLMNOPQRSTUVWXYZ
JJJJJJJJJJKLMNOPQRSTUVWXYZ
KKKKKKKKKKKLMNOPQRSTUVWXYZ
LLLLLLLLLLLLMNOPQRSTUVWXYZ
MMMMMMMMMMMMMNOPQRSTUVWXYZ
NNNNNNNNNNNNNNOPQRSTUVWXYZ
OOOOOOOOOOOOOOOPQRSTUVWXYZ
PPPPPPPPPPPPPPPPQRSTUVWXYZ
QQQQQQQQQQQQQQQQQRSTUVWXYZ
RRRRRRRRRRRRRRRRRRSTUVWXYZ
SSSSSSSSSSSSSSSSSSSTUVWXYZ
TTTTTTTTTTTTTTTTTTTTUVWXYZ
UUUUUUUUUUUUUUUUUUUUUVWXYZ
VVVVVVVVVVVVVVVVVVVVVVWXYZ
WWWWWWWWWWWWWWWWWWWWWWWXYZ
XXXXXXXXXXXXXXXXXXXXXXXXYZ
YYYYYYYYYYYYYYYYYYYYYYYYYZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZ

You would need to output:

ABCDE
BBCDE
CCCDE
DDDDE
EEEEE

For the purposes of explanation, I'll be using only ABCD, instead of the full alphabet. You need to be able to match the L-phabet (above), as well as the following patterns:

The single line:

ABCD    or     A
               B
               C
               D

The single line repeated N times

ABCD    or    AAAA
ABCD          BBBB
ABCD          CCCC
ABCD          DDDD

The Tabula Recta:

ABCD
BCDA
CDAB
DABC

This alphabet triangle:

A      or   AAAAAAA
AB           BBBBB
ABC           CCC
ABCD           D
ABC
AB
A

We also have half triangles in lots of varieties:

A           AAAA      A        ABCD
BB          BBB       AB       ABC
CCC         CC        ABC      AB
DDDD        D         ABCD     A

Finally, the square:

AAAAAAA
ABBBBBA
ABCCCBA
ABCDCBA
ABCCCBA
ABBBBBA
AAAAAAA

All of the above patterns are of size 4. However, you will be passed a pattern of size 26, as well as an N between 1 and 26, and you need to scale the pattern. You do not need to handle any other patterns.

  • The output for 1 will always be the single character A
  • The output for 26 will always be the same, full-sized pattern passed in.
  • Trailing spaces are allowed at the end of each line, as well as a trailing newline at the end
  • You can find all patterns of size 26 here

This is a , so do it in as few bytes as possible!

\$\endgroup\$
2
  • \$\begingroup\$ You could provide the full-sized patterns somewhere. \$\endgroup\$
    – Karl Napf
    Oct 25, 2016 at 21:32
  • 1
    \$\begingroup\$ @KarlNapf done: pastebin.com/Kt8NX5MF \$\endgroup\$ Oct 25, 2016 at 22:06

3 Answers 3

2
\$\begingroup\$

PHP, 502 Bytes

<?$c=count($x=explode("\n",$_GET[p]));for($t=$u=$o="",$f="substr",$n=0;$n<$g=$_GET["n"];$n++){if(2651==$l=strlen($s))$o.=$f($x[$n],0,$g).$f($x[$n],1-$g)."\n";elseif($l==1026)echo($t=$f($x[$n],0,$g)).$f(strrev($t),1)."\n";elseif($f($s,-1)=="Y")echo$f($x[$n],0,$g-$n).$f($x[$n],-$n,$n)."\n";elseif($l==376&&$f($s,-2,1)=="\n")echo$f($x[$n],0,$g-$n)."\n";elseif($l==726){$t.=$x[$n]."\n";$n+1==$g?:$u=$x[$n]."\n".$u;}else echo$f($x[$n]??"",0,$g)."\n";}if($o)echo$o.substr(strrev($o),2*$g+1);if($t)echo$t.$u;

Works with the string length of a pattern. A pattern has these conditions. Letter at begin an end. CR are removed.

Expanded

foreach($p as$s){ # all patterns in an array
    $c=count($x=explode("\n",$s));
    for($t=$u=$o="",$f="substr",$n=0;$n<$g=$_GET["n"];$n++){
    if(2651==$l=strlen($s))$o.=$f($x[$n],0,$g).$f($x[$n],1-$g)."\n";
# square pattern
    elseif($l==1026)echo($t=$f($x[$n],0,$g)).$f(strrev($t),1)."\n";
#alphabet triangle up down
    elseif($f($s,-1)=="Y")echo$f($x[$n],0,$g-$n).$f($x[$n],-$n,$n)."\n";
# Tabula recta
    elseif($l==376&&$f($s,-2,1)=="\n")echo$f($x[$n],0,$g-$n)."\n";
# two half triangle
    elseif($l==726){$t.=$x[$n]."\n";$n+1==$g?:$u=$x[$n]."\n".$u;}
#alphabet triangle left right
    else echo$f($x[$n]??"",0,$g)."\n";
# all other
    }
    if($o)echo$o.substr(strrev($o),2*$g+1);
    if($t)echo$t.$u;
}
\$\endgroup\$
1
  • \$\begingroup\$ 1) unnecessary quotes: "substr", $_GET["n"], "Y" (-6). 2) $x[$n]."\n" -> "$x[$n]\n", analogue for $u (-2). 3) I´m pretty sure you can use some sort of > for your <number>==$l comparisons (-4). 4) ($n=0;$n<...;$n++) -> ($n=-1;++$n<...;) (-1). 5) if($o) is unnecessary: if $o is empty, so is strrev($o) and any of its substrings, so nothing will be printed (-6). 6) && -> & (-1). 7) Do you really need empty string instead of null for substr? $x[$n]??"" --> $x[$n] (-4)? \$\endgroup\$
    – Titus
    Nov 5, 2016 at 13:02
2
\$\begingroup\$

R, 483 412 bytes

    x=function(n){d=1:n;a=LETTERS[d];z=rep;f=function(...)cat(...,"\n",sep="");g=c(d,n:2-1);for(i in d)f(a[z(i,i)],if(i!=n)a[(i+1):n]);f(a);for(i in d)f(a[i]);for(i in d)f(a);for(i in d)f(z(a[i],n));for(i in d)f(a[i:n],if(i>1)a[1:i-1]);for(i in g)f(a[1:i]);for(i in d)f(z(" ",i-1),z(a[i],2*(n-i)+1));for(i in d)f(z(a[i],i));for(i in d)f(z(a[i],n-i+1));for(i in g)f(if(i>1)a[2:i-1],z(a[i],2*(n-i)+1),if(i>1)a[i:2-1])}

This is my first time posting, I was told I didn't have a recent years experience of R, so just practise a little bit here.

Expanded

    x=function(n){
    # array of 1 to n
    d=1:n
    # first n capital letters 
    a=LETTERS[d]
    # use z to represent the repeat function
    z=rep
    # use f to represent concatenate, with new line and close the gap
    f=function(...)cat(...,"\n",sep="");
    # use g to represent 1 to n then n to 1
    g=c(d,n:2-1)
    
    # L-phabet 
    # start and repeat the first letter to i, combine the i+1 to the end
    for(i in d) f(a[z(i,i)],if(i!=n)a[(i+1):n])
    
    # single line - horizontal
    f(a)
    
    # single line - vertical
    for(i in d)f(a[i])
    
    # single line - repeated by row
    for(i in d)f(a)
    
    # single line - repeated by column
    for(i in d)f(z(a[i],n))

    # Tabula Recta
    # start from i, combine the 1 to i-1
    for(i in d) f(a[i:n],if(i>1)a[2:i-1])

    # alphabet triangle
    for(i in g)f(a[1:i])
    
    # alphabet triangle - upside down
    for(i in d)f(z(" ",i-1),z(a[i],2*(n-i)+1))
    
    # half triangles 
    for(i in d)f(z(a[i],i))
    for(i in d)f(z(a[i],n-i+1))

    # the square
    # combine the first part from i to i-1 letters, repeat the i in the middle, combine the last part from i-1 to 1 letters
    for(i in g) f(if(i>1)a[2:i-1],z(a[i],2*(n-i)+1),if(i>1)a[i:2-1])
    } 
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 382 380 370 bytes

f=(a,N)=>{X=Y=0
if(a[1]){Y=a[1].trim()[0]=='B'
X=z=a[0][1]==a[1][0]?1:!Y
l="ABCDEFGHIJKLMNOPQRSTUVWXYZ"[s='slice'](0,N)
if(z){if(a[0][1]=='A'){for(a=[],i=0,L=l;i<N;i++)a[N-i]=a[N+i]=L[s](0,-1)+l[N-i-1].repeat(i*2)+[...L].reverse().join``
L=L[s](0,-1)
return a}if(a[1][1]=='C')for(i=0;a[i++]=l,i<N;)l=l[s](1)+l[0]}}else X=1
a=X?a.map(l=>l[s](0,N)):a
return Y?a[s](0,N):a}

Pass an array of strings to the function f(), like this:

f(
`AAAAAAA
ABBBBBA
ABCCCBA
ABCDCBA
ABCCCBA
ABBBBBA
AAAAAAA`.split('\n'), 3)

Less golfed version with comments:

f=(a,N)=>{
    // Whether to truncate array horizontally to width N.
    X=0

    // Whether to truncate array vertically to height N.
    Y=0

    // If a second row exists...
    if(I=a[1]){
        // If the first non-whitespace character in the second row == 'B', truncate vertically.
        if(I.trim()[0]=='B')Y=1

        // Truncate horizontally if 2nd character in row 1 == 1st character in row 2; otherwise, if not truncating vertically.
        X=z=a[0][1]==I[0]?1:!Y

        // If 2nd character in row 1 == 1st character in row 2
        if(z){
            // Make an alphabet.
            l="ABCDEFGHIJKLMNOPQRSTUVWXYZ".slice(0,N)

            // If 2nd character in row 1 == 'A', forget everything we just did. Make a new array, generate a Square pattern, then return it.
            if(a[0][1]=='A'){
                for(a=[],i=0,L=l;i<N;i++)
                    a[N-i]=a[N+i]=L.slice(0,-1)+l[N-i-1].repeat(i*2)+[...L].reverse().join``,
                    L=L.slice(0,-1)
                return a
            }

            // If 2nd character in row 2 == 'C', fill array with a Tabula Recta.
            if(I[1]=='C')
                for(i=0;a[i++]=l,i<N;)
                    l=l.slice(1)+l[0]
        }
    }else{
        // If a second row doesn't exist, it's a horizontal line; truncate horizontally.
        X=1
    }

    // Truncate array horizontally if necessary.
    a=X?a.map(l=>l.slice(0,N)):a

    // Truncate array vertically if necessary.
    return Y?a.slice(0,N):a
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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