16
\$\begingroup\$

The Background

Imagine the English alphabet written out in a row in capital letters -- ABCDEFGHIJKLMNOPQRSTUVWXYZ. Suppose we have a special variable-width lens such that, when placed over the row, it shows a certain "window" of the alphabet, with letters outside the lens hidden from view. Additionally, this lens has a warping characteristic, so that the first letter inside the lens, and every second letter thereafter, is lower-cased, but the remaining letters still show capitalized.

For example, we could have a lens of length 5 placed somewhere in the middle of the alphabet and we would only see . jKlMn (leading period here to ensure whitespace shows) -- note the K and M are still capitalized, while the j, l, and n are all lower-cased. As we slide this lens from left-to-right along the alphabet, different letters would be shown and different letters capitalized. The task here is to output the resultant letters as the lens slides along the alphabet.

The Challenge

Write a program or function that takes an input integer 0 < n < 27 and outputs or returns the string(s) that are visible as the lens described above slides across the alphabet.

For values of n larger than 1, the lens begins by "hanging" off the left side of the alphabet, so only the A is shown in the right-most slot of the lens. The first line of any output will thus be a single A, either capital or lowercase depending upon the length of the lens.

Similarly, the lens finishes sliding when the Z is in the left-most slot of the lens (meaning that the very last line in any output will always be 25 spaces followed by a lowercase z).

The Examples

Input: 1

Output:

a
 b
  c
   d
    e
     f
      g
       h
        i
         j
          k
           l
            m
             n
              o
               p
                q
                 r
                  s
                   t
                    u
                     v
                      w
                       x
                        y
                         z

Input: 4

Output:

A
aB
AbC
aBcD
 bCdE
  cDeF
   dEfG
    eFgH
     fGhI
      gHiJ
       hIjK
        iJkL
         jKlM
          kLmN
           lMnO
            mNoP
             nOpQ
              oPqR
               pQrS
                qRsT
                 rStU
                  sTuV
                   tUvW
                    uVwX
                     vWxY
                      wXyZ
                       xYz
                        yZ
                         z

Input: 26

Output:

A
aB
AbC
aBcD
AbCdE
aBcDeF
AbCdEfG
aBcDeFgH
AbCdEfGhI
aBcDeFgHiJ
AbCdEfGhIjK
aBcDeFgHiJkL
AbCdEfGhIjKlM
aBcDeFgHiJkLmN
AbCdEfGhIjKlMnO
aBcDeFgHiJkLmNoP
AbCdEfGhIjKlMnOpQ
aBcDeFgHiJkLmNoPqR
AbCdEfGhIjKlMnOpQrS
aBcDeFgHiJkLmNoPqRsT
AbCdEfGhIjKlMnOpQrStU
aBcDeFgHiJkLmNoPqRsTuV
AbCdEfGhIjKlMnOpQrStUvW
aBcDeFgHiJkLmNoPqRsTuVwX
AbCdEfGhIjKlMnOpQrStUvWxY
aBcDeFgHiJkLmNoPqRsTuVwXyZ
 bCdEfGhIjKlMnOpQrStUvWxYz
  cDeFgHiJkLmNoPqRsTuVwXyZ
   dEfGhIjKlMnOpQrStUvWxYz
    eFgHiJkLmNoPqRsTuVwXyZ
     fGhIjKlMnOpQrStUvWxYz
      gHiJkLmNoPqRsTuVwXyZ
       hIjKlMnOpQrStUvWxYz
        iJkLmNoPqRsTuVwXyZ
         jKlMnOpQrStUvWxYz
          kLmNoPqRsTuVwXyZ
           lMnOpQrStUvWxYz
            mNoPqRsTuVwXyZ
             nOpQrStUvWxYz
              oPqRsTuVwXyZ
               pQrStUvWxYz
                qRsTuVwXyZ
                 rStUvWxYz
                  sTuVwXyZ
                   tUvWxYz
                    uVwXyZ
                     vWxYz
                      wXyZ
                       xYz
                        yZ
                         z

The Rules

  • Standard input/output rules apply
  • Standard loopholes disallowed
  • Standard code-golf rules apply
  • The leading whitespace as shown is required, so that we know where we're at in the alphabet.
  • Each line cannot have trailing whitespace (as shown - the output is not a rectangle).
  • A single trailing newline is optional.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Related \$\endgroup\$ – Alex A. Feb 17 '16 at 20:13
2
\$\begingroup\$

Pyth, 31 bytes

Vr_tQ26s.erb%+Nk2+*Nd:GeS,0N+NQ

Try it online. Test suite.

Quick and dirty.

\$\endgroup\$
2
\$\begingroup\$

AWK, 160 bytes

{d=-$1
m=25
for(;i<m+$1;i++){f="%"(++d<0?0:d<m?d:m)"s"
c=u=65
l=97
printf f,""
for(j=++E-$1;j<E&&j<26;j++){c=c==u?l:u
if(j<0)continue
printf("%c",j+c)}print""}}

That's about as tight as I can come up with in AWK. Having 3 different prints and a continue really add to the byte-count.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 130 bytes

n=>[...Array(n+25)].map(_=>[...Array(26)].map(_=>String.fromCharCode(j++<i|i+n<j?32:j+(j-i)%2*32),++i,j=64).join``,i=64-n).join`\n`

Where \n represents the literal newline character. Works by looping over the output rectangle and outputting spaces outside the diagonal while adding 32 to the character code to lower case it in alternate squares. Using replace appears to be 1 byte longer:

n=>[...Array(n+25)].map(_=>' '.repeat(26).replace(/./g,s=>j++<i|i+n<j?s:String.fromCharCode(j+(j-i)%2*32),++i,j=64),i=64-n).join`\n`
\$\endgroup\$
1
\$\begingroup\$

Jelly, 21 bytes

ØAżØaU¡ṁ€µJR;"z⁶Ḋḣ"JY

Try it online!

How it works

ØAżØaU¡ṁ€µJR;"z⁶Ḋḣ"JY - Main link. Takes an integer n on the left
ØA                    - Uppercase alphabet
   Øa                 - Lowercase alphabet
  ż                   - Zip; [['A', 'a'], ['B', 'b'], ...]
      ¡               - Do n times:
     U                -   Reverse each pair
                        This means we have a leading capital for even n,
                        or a trailing capital for odd n
        €             - Over each pair:
       ṁ              -   Mold it to length n
         µ            - Begin a new link with this list of characters C
          J           - Yield [1, 2, ..., 26]
           R          - Yield [[1], [1, 2], ..., [1, 2, ..., 26]]
            ;"        - Concatenate each of these ranges with the lists in C
              z⁶      - Transpose, filling with spaces
                Ḋ     - Remove the first line
                 ḣ"J  - Remove the trailing integers from each line
                    Y - Join with newlines
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 22 bytes

₂Iи2.ýžpA«RŽR‘S.Λ„ 
¶:

Try it online.

Or alternatively:

₂2‚Iи¨ARÂu«ŽR‘S.Λ„ 
¶:

Try it online.

Explanation:

This program uses the Canvas builtin Λ to draw the output. This builtin takes three arguments:

  1. Length of the lines we'll draw: the list [26,2,26,2,...,26] in this case:
        #(alternative 1)
₂       # Push 26
 Iи     # Repeat it the input amount of times as list
   2.ý  # Intersperse it with 2
        #(alternative 2)
₂2‚     # Pair 26 with 2: [26,2]
   Iи   # Repeat this list the input amount of times
     ¨  # And remove the trailing 2
  1. Character/string to draw: the string "zyxwvutsrqponmlkjihgfedcbaABCDEFGHIJKLMNOPQRSTUVWXYZ" in this case:
        #(alternative 1)
žp      # Push builtin string "ZYXWVUTSRQPONMLKJIHGFEDCBA"
  A«    # Append builtin string "abcdefghijklmnopqrstuvwxyz"
    R   # Reverse this string to "zyxwvutsrqponmlkjihgfedcbaABCDEFGHIJKLMNOPQRSTUVWXYZ"
        #(alternative 2)
A       # Push builtin "abcdefghijklmnopqrstuvwxyz"
 R      # Reverse it: "zyxwvutsrqponmlkjihgfedcba"
  Â     # Bifurcate it (short for Duplicate & Reverse copy)
   u    # Uppercase the reversed copy "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    «   # Append it: "zyxwvutsrqponmlkjihgfedcbaABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1. The direction to draw in: [7,0,3,0]. The digits in the range [0,7] each represent a certain direction:
7   0   1
  ↖ ↑ ↗
6 ← X → 2
  ↙ ↓ ↘
5   4   3

So the [7,0,3,0] in this case translates to the directions \$[↖,↑,↘,↑]\$:

ŽR‘     # Push compressed integer 7030
   S    # Convert it to a list of digits: [7,0,3,0]

     Λ  # Use the Canvas builtin with the three pushed arguments,
        # after which the result is implicitly output immediately afterwards

Unfortunately, the Canvas builtin adds trailing spaces to make it a rectangle by default, so we'll have to remove those to comply to the challenge rules:

   .Λ   # Use the modifiable Canvas builtin instead
„ \n    # Push the string " \n"
    ¶   # Push the builtin string "\n"
     :  # And keep repeating " \n" with "\n" until there are no longer any left
        # (after which the result is output implicitly)

Here a step-by-step explanation of the output for input \$n=3\$:

Step 1: Draw 26 characters ("zyxwvutsrqponmlkjihgfedcba") in direction 7↖:

a                         
 b                        
  c                       
   d                      
    e                     
     f                    
      g                   
       h                  
        i                 
         j                
          k               
           l              
            m             
             n            
              o           
               p          
                q         
                 r        
                  s       
                   t      
                    u     
                     v    
                      w   
                       x  
                        y 
                         z

Step 2: Draw 2-1 characters (A) in direction 0↑:

A                         
a                         
 b                        
  c                       
   d                      
    e                     
     f                    
      g                   
       h                  
        i                 
         j                
          k               
           l              
            m             
             n            
              o           
               p          
                q         
                 r        
                  s       
                   t      
                    u     
                     v    
                      w   
                       x  
                        y 
                         z

Step 3: Draw 26-1 characters ("BCDEFGHIJKLMNOPQRSTUVWXYZ") in direction 3↘:

A                         
aB                        
 bC                       
  cD                      
   dE                     
    eF                    
     fG                   
      gH                  
       hI                 
        iJ                
         jK               
          kL              
           lM             
            mN            
             nO           
              oP          
               pQ         
                qR        
                 rS       
                  sT      
                   tU     
                    uV    
                     vW   
                      wX  
                       xY 
                        yZ
                         z

Step 4: Draw 2-1 characters ("z") in direction 0↑:

A                         
aB                        
 bC                       
  cD                      
   dE                     
    eF                    
     fG                   
      gH                  
       hI                 
        iJ                
         jK               
          kL              
           lM             
            mN            
             nO           
              oP          
               pQ         
                qR        
                 rS       
                  sT      
                   tU     
                    uV    
                     vW   
                      wX  
                       xYz
                        yZ
                         z

Step 5: Draw 26-1 characters ("yxwvutsrqponmlkjihgfedcba") in direction 7↖:

a                         
Ab                        
aBc                       
 bCd                      
  cDe                     
   dEf                    
    eFg                   
     fGh                  
      gHi                 
       hIj                
        iJk               
         jKl              
          kLm             
           lMn            
            mNo           
             nOp          
              oPq         
               pQr        
                qRs       
                 rSt      
                  sTu     
                   tUv    
                    uVw   
                     vWx  
                      wXy 
                       xYz
                        yZ
                         z

See this 05AB1E tip of mine (section How to compress large integers?) to understand why ŽR‘ is 7030.

See this 05AB1E tip of mine for an in-depth explanation of the Canvas builtin.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Doesn't the Canvas builtin pad lines to a rectangle? Unfortunately, the question says "Each line cannot have trailing whitespace (as shown - the output is not a rectangle)" (Edit: I think this also applies to your Charcoal answer as well) \$\endgroup\$ – caird coinheringaahing Nov 18 '20 at 13:09
  • \$\begingroup\$ @cairdcoinheringaahing Ugh.. Should be fixed now, thanks for noticing. \$\endgroup\$ – Kevin Cruijssen Nov 18 '20 at 13:17
  • \$\begingroup\$ @cairdcoinheringaahing My Charcoal answer should be fixed now as well. \$\endgroup\$ – Kevin Cruijssen Nov 18 '20 at 13:21
1
\$\begingroup\$

Charcoal, 14 bytes

UTFN«P↘⎇﹪ι²αβ↑

Try it online (verbose) or try it online (pure).

Explanation:

Toggle trim so the output won't contain any trailing spaces to comply to the challenge rules. Charcoal will make the output a rectangle by default:

ToggleTrim();
UT

Loop the input amount of times:

For(InputNumber()){ ... }
FN«

Print in a down-right diagonal direction without moving the cursor:

Multiprint(:DownRight, ...);
P↘

Print the uppercase alphabet if the loop index is odd, or the lowercase alphabet if it's even (b/β is a variable that contains the lowercase alphabet by default and a/α is a variable that contains the uppercase alphabet by default):

Ternary(Modulo(i, 2), a, b)
⎇﹪ι²αβ

And then move the cursor one position up for the next iteration:

Move(:Up);
↑
\$\endgroup\$
1
\$\begingroup\$

Japt -R, 21 20 bytes

;B¬£îXpv éU)iYçÃÕmx1

Try it or run all test cases

;B¬£îXpv éU)iYçÃÕmx1     :Implicit input of integer U
;B                       :Uppercase alphabet
  ¬                      :Split
   £                     :Map each X at 0-based index Y
    î                    :  Repeat and slice to length U
     Xp                  :    Append to X
       v                 :      Its lowercase
         éU              :    Rotate right U times
           )             :  End repeat
            i            :  Prepend
             Yç          :    Y spaces
               Ã         :End map
                Õ        :Transpose
                 m       :Map
                  x1     :  Trim right
                         :Implicit output joined with newlines
\$\endgroup\$
2
  • \$\begingroup\$ It looks like the question specifies "Each line cannot have trailing whitespace (as shown - the output is not a rectangle)", which I think from your interpreter link this does have trailing spaces? \$\endgroup\$ – caird coinheringaahing Nov 18 '20 at 13:12
  • \$\begingroup\$ Thanks, @cairdcoinheringaahing - fixed for now at a cost of 3 bytes. \$\endgroup\$ – Shaggy Nov 18 '20 at 14:50
0
\$\begingroup\$

Python 3, 105 bytes

lambda n:[(i-n)*' '+''.join(chr(97+j-(j^i^n)%2*32)[:-1<j<26]for j in range(i-n,i))for i in range(1,26+n)]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Unfortunately, the question specifies "Each line cannot have trailing whitespace (as shown - the output is not a rectangle)", so I think you'll need to add some way of striping trailing spaces \$\endgroup\$ – caird coinheringaahing Nov 18 '20 at 13:11
  • \$\begingroup\$ @cairdcoinheringaahing thanks, fixed \$\endgroup\$ – Jitse Nov 18 '20 at 13:34

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.