Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Input: s3+
- Output:
- ***
- ***
- ***
- Input: t5+
- Output:
- *
- ***
- *****
- Input: t3-
- Output:
- ***
- *
- JstPz_W}+zjl#m.[J**-J*}tzyd;J
- Implicit: z = input
- JstPz J = size.
- _W }+z Reverse if "+" in z
- j l# m J Join the nonempty lines in map lambda d:... over range(J)
- .[J ; Pad the following with spaces (;) to length J
- ** "*", this many times:
- -J*}tzyd J if "t" not in z,
- otherwise the correct number for a triangle.
- JsPtzj?}szm**JJ_W}-zm.[J**hyd;/hJ2
- V=Us1 n;U<'t?Vo ç*pV):0oV2 £S²pY iY'*pV-X})·z2*!Uf-
- // Implicit: U = input string, S = space
- V= // Set variable V to
- Us1 // everything after the first char of U,
- n; // converted to a number. This turns e.g. "12+" into 12.
- U<'t? // If U comes before "t" lexicographically (here, if the first char is "s"),
- Vo // make a list of V items,
- ç*pV) // and set each item to V asterisks.
- :0oV2 // Otherwise, create the range [0, V) with steps of 2 (e.g. 7 -> [0,2,4,6]),
- £ }) // and map each item X and index Y to:
- S²pY // Repeat 2 spaces Y times. This creates a string of Y*2 spaces.
- iY'*pV-X // At position Y in this string (right in the middle), insert V-X asterisks.
- · // Join with newlines.
- !Uf- // Take the logical not of U.match("-").
- // If U contains "-", this returns false; otherwise, returns true.
- 2* // Multiply by two. This converts true to 2, false to 0.
- z // Rotate the list 90° that many times.
- // Altogether, this turns the shape by 180° if necessary.
- d=raw_input()
- x=int(d[1:-1])
- o="n".join("*"*x for i in range(x))if d<"t"else"n".join(("*"*i).center(x)for i in range(x,0,-2))
- print o[::-1]if"+"in d else o
- d=raw_input()
- x=int(d[1:-1])
- if "s" in d:
- for y in range(x):
- o+="*"*x+"n"
- o=o[:-1]
- else:
- b=0
- while x+1:
- o+=" "*b+"*"*x+" "*b+"n"
- x-=2
- b+=1
- o=o[:-1]
- if d[-1]=="+":
- o=o[::-1]
- print o
- q=s=>+s.slice(1,s.length-1);f=s=>s[0]=="s"?("*".repeat(q(s))+"n").repeat(q(s)):Array.apply(0,Array(-~(q(s)/2))).map((_,n)=>(s[s.length-1]=="-"?~~(q(s)/2)-n:n)).map(n=>(" ".repeat(q(s)/2-n)+"*".repeat(n*2+1))).join("n")
- q=s=>+s.slice(1,s.length-1); Define a function, q, that takes returns the argument, without the first and last character, casted into an integer.
- f=s=> Define a function, f, that takes one argument, s. (This is the main function)
- s[0]=="s"? If the first character of s is "s" then...
- ("*".repeat(q(s)) ) Repeat the "*" character q(s) times.
- ( +"n") Append a newline to that
- .repeat(q(s)) Repeat that q(s) times.
- : Else... (the first character of s isn't "s")
- Array.apply(0,Array( )) Create an array of length...
- Array.apply(0,Array(-~(q(s)/2))) floor(q(s)/2)+1
- .map((_,n)=> ) Map each element, _ with index n to...
- .map((_,n)=>(s[s.length-1]=="-"? )) If the last element of s is "-" then...
- .map((_,n)=>(s[s.length-1]=="-"?~~(q(s)/2)-n )) floor(q(s)/2)-n
- .map((_,n)=>(s[s.length-1]=="-"? : )) Else...
- .map((_,n)=>(s[s.length-1]=="-"? n)) Just n
- .map(n=> ) Map each element into...
- .map(n=>(" ".repeat(q(s)/2-n) ) Repeat " ", q(s)/2-n times.
- .map(n=>( )+"*".repeat(n*2+1))) Append "*", repeated 2n+1 times.
- .map(n=>(" ".repeat( )+"*".repeat(n*2+1))).join("n") Join with newlines
- def f(s):
- S=int(s[1:-1])
- for n in([range(1,S+2,2),range(S,0,-2)]['-'in s],[S]*S)['s'in s]:
- print "{:^{S}}".format('*'*n,S=S)
- d+
- $0$*:¶
- ^((w)+):(:+)
- $1$2$3$2¶$0
- m`s$|:t
- )`(.+)¶-(D*)
- -$2¶$1
- m`^.
- G`.
- T`ts:` *
- ,#i's=`≈;'**@½≈";#dXdXεj' +"£n`@`≈;'**n`@Iƒ('-=WXa0WXü
- ,#i Take input, push chars separately
- 's= Iƒ IF the first char is "s":
- ` `@ run the quoted function
- ≈;'**n make list of n strings of n *'s
- ` `@ ELSE run the quoted function:
- ≈; make two copies of int n
- '** use one to make string of n *'s
- @½≈ cut the other in half (e.g. 5->2)
- " "£n run n/2 times the quoted function:
- ;# copy the string as list of chars
- dXdX discard the last 2 *'s
- εj join back into string
- ' + prepend a space
- ('-=WX 0WX IF the third character is "-":
- a invert the stack
- ü pop and print the entire stack
- s=>(p=s.match(/d+|./g),u=n=+p[1],m=n+1>>1,t=' '.repeat(n)+'*'.repeat(n),v=s<'t'?0:p[2]<'-'?(u=m,1):-1,[...Array(s<'t'?n:m)].map(_=>t.substr(u,u,u+=v)).join`
- `)
- *****
- |*****| (5)
- | ***| (4)
- | *| (3)
- ' *'jt4Y2m)U1$l't'Gm?2MQ2/:1L3$)R!P!R'+'Gm?P]]Q)
- ' *' % push string. Will be index into for to final result
- j % input string
- t % duplicate
- 4Y2 % predefined literal string '0123456789'
- m % logical index of digits in input string
- ) % index into input string to obtain substring with digits
- U % convert to number
- 1$l % generate square of ones with that size
- 't' % push character 't'
- G % push input string
- m % true if input string contains 't'
- ? % if so...
- 2M % push argument of call to function `l`, i.e. square size
- Q2/ % add 1 and divide by 2. Call result T
- : % generate vector [1, 2, ... T]
- 1L % predefined literal representing Matlab's `:` index
- 3$) % two dimensional index. Transforms square into rectangle
- R % remove (set to zero) lower-left corner
- !P! % flip horizontally
- R % remove lower-left corner. This gives inverted triangle
- '+' % push character '+'
- G % push input
- m % true if input contains '+'
- ? % if so...
- P % flip vertically
- ] % end if
- ] % end if
- Q % add 1. This gives array of values 1 and 2
- ) % index string ' *' with this array to produce char array
- % implicitly display that char array
- #define x(y);)putchar(y)
- #define m(n)for(n=0;n++<
- #define T {m(q)i x(32);m(q)s-i*2 x(42);puts("");}
- main(q,v,i,s)char**v;{s=atoi(v[1]+1);if(*v[1]=='s')m(i)s*s x(42)&&!(i%s)&&puts("");else if(strchr(v[1],'+'))for(i=s/2+1;i-->0;)T else for(i=-1;i++<s/2+1;)T}
- main(q,v,i,size)char**v; // neat way of declaring variables
- {
- size=atoi(v[1]+1);
- if(*v[1]=='s')
- {
- for(i=0;i++<size*size;)
- {
- putchar(42); // returns 42 (true)
- if(!(i%size))
- puts("");
- }
- }
- else if(strchr(v[1],'+')) // if finds plus sign
- {
- for(i=size/2+1;i-->0;) // iterate the height of the triangle
- {
- for(q=0;q++<i;)putchar(32); // conveniently i is the number os spaces before each line
- for(q=0;q++<size-i*2;) putchar(42);
- puts("");
- }
- }
- else for(i=-1;i++<size/2+1;) // does the same as above but inverted order
- {
- for(q=0;q++<i;)putchar(32);
- for(q=0;q++<size-i*2;)putchar(42);
- puts("");
- }
- }
- ->s{n=s[1,2].to_i
- n.times{|i|d=(s.ord-115)*(s[-1]<=>?,)*(n-1-i*2)
- d<1&&puts((?**(n+d)).center(n))}}
- f=->s{ #take a string as an argument
- n=s[1,2].to_i #take 2 characters starting at index 1 and convert to a number for the size
- n.times{|i| #iterate through n rows
- d= #calculate how many stars "MORE THAN" n we need on a row
- (s.ord-115)* #ascii code for 1st character of string - 115 : s-->0, t-->1
- (s[-1]<=>?,)* #compare last character of input with comma character - --> +1 + --> -1
- (n-1-i*2) #row number * 2: 0 at centre, positive above it, negative below it
- d<1&& #only output if d is nonpositive (i.e we need less than n or exactly n stars)
- puts((?**(n+d)).center(n)) #print n+d stars, centred in a field of n characters padded by whitespace
- }
- }
- f[gets.chomp]
- onFiΒ€ioSgiγ?='sn―sΒ'*―TΒ1'*?='-SZiγγ
- onFiΒ€ioSgi
- on set n to
- Fi the first entity of i (the shape identifier)
- Β set Β (beta) to
- €i the "inside" of i (in this case, the size) as a number
- oS set S to
- gi the last entity of i (the inverter)
- γ?='sn―sΒ'*―TΒ1'*
- γ set γ (gamma) to the result of the following expression
- ?='sn if n is the character s,
- ―sΒ'* then return a pattern "s" (a square) made with "*"s
- ―TΒ1'* otherwise, return a pattern "T" (triangle) that is centered and
- has a scale factor of 1, made with "*"s
- ?='-SZiγγ
- ?='-S if S is a "-"
- Ziγ return γ, inverted across its lines
- γ otherwise, return γ untouched
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement