Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 KB | None | 0 0
  1. Input: s3+
  2. Output:
  3. ***
  4. ***
  5. ***
  6.  
  7. Input: t5+
  8.  
  9. Output:
  10. *
  11. ***
  12. *****
  13.  
  14. Input: t3-
  15. Output:
  16. ***
  17. *
  18.  
  19. JstPz_W}+zjl#m.[J**-J*}tzyd;J
  20.  
  21. Implicit: z = input
  22. JstPz J = size.
  23. _W }+z Reverse if "+" in z
  24. j l# m J Join the nonempty lines in map lambda d:... over range(J)
  25. .[J ; Pad the following with spaces (;) to length J
  26. ** "*", this many times:
  27. -J*}tzyd J if "t" not in z,
  28. otherwise the correct number for a triangle.
  29.  
  30. JsPtzj?}szm**JJ_W}-zm.[J**hyd;/hJ2
  31.  
  32. V=Us1 n;U<'t?Vo ç*pV):0oV2 £S²pY iY'*pV-X})·z2*!Uf-
  33.  
  34. // Implicit: U = input string, S = space
  35. V= // Set variable V to
  36. Us1 // everything after the first char of U,
  37. n; // converted to a number. This turns e.g. "12+" into 12.
  38.  
  39. U<'t? // If U comes before "t" lexicographically (here, if the first char is "s"),
  40. Vo // make a list of V items,
  41. ç*pV) // and set each item to V asterisks.
  42. :0oV2 // Otherwise, create the range [0, V) with steps of 2 (e.g. 7 -> [0,2,4,6]),
  43. £ }) // and map each item X and index Y to:
  44. S²pY // Repeat 2 spaces Y times. This creates a string of Y*2 spaces.
  45. iY'*pV-X // At position Y in this string (right in the middle), insert V-X asterisks.
  46. · // Join with newlines.
  47.  
  48. !Uf- // Take the logical not of U.match("-").
  49. // If U contains "-", this returns false; otherwise, returns true.
  50. 2* // Multiply by two. This converts true to 2, false to 0.
  51. z // Rotate the list 90° that many times.
  52. // Altogether, this turns the shape by 180° if necessary.
  53.  
  54. d=raw_input()
  55. x=int(d[1:-1])
  56. 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))
  57. print o[::-1]if"+"in d else o
  58.  
  59. d=raw_input()
  60. x=int(d[1:-1])
  61. if "s" in d:
  62. for y in range(x):
  63. o+="*"*x+"n"
  64. o=o[:-1]
  65. else:
  66. b=0
  67. while x+1:
  68. o+=" "*b+"*"*x+" "*b+"n"
  69. x-=2
  70. b+=1
  71. o=o[:-1]
  72. if d[-1]=="+":
  73. o=o[::-1]
  74. print o
  75.  
  76. 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")
  77.  
  78. 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.
  79. f=s=> Define a function, f, that takes one argument, s. (This is the main function)
  80. s[0]=="s"? If the first character of s is "s" then...
  81. ("*".repeat(q(s)) ) Repeat the "*" character q(s) times.
  82. ( +"n") Append a newline to that
  83. .repeat(q(s)) Repeat that q(s) times.
  84. : Else... (the first character of s isn't "s")
  85. Array.apply(0,Array( )) Create an array of length...
  86. Array.apply(0,Array(-~(q(s)/2))) floor(q(s)/2)+1
  87. .map((_,n)=> ) Map each element, _ with index n to...
  88. .map((_,n)=>(s[s.length-1]=="-"? )) If the last element of s is "-" then...
  89. .map((_,n)=>(s[s.length-1]=="-"?~~(q(s)/2)-n )) floor(q(s)/2)-n
  90. .map((_,n)=>(s[s.length-1]=="-"? : )) Else...
  91. .map((_,n)=>(s[s.length-1]=="-"? n)) Just n
  92. .map(n=> ) Map each element into...
  93. .map(n=>(" ".repeat(q(s)/2-n) ) Repeat " ", q(s)/2-n times.
  94. .map(n=>( )+"*".repeat(n*2+1))) Append "*", repeated 2n+1 times.
  95. .map(n=>(" ".repeat( )+"*".repeat(n*2+1))).join("n") Join with newlines
  96.  
  97. def f(s):
  98. S=int(s[1:-1])
  99. for n in([range(1,S+2,2),range(S,0,-2)]['-'in s],[S]*S)['s'in s]:
  100. print "{:^{S}}".format('*'*n,S=S)
  101.  
  102. d+
  103. $0$*:¶
  104. ^((w)+):(:+)
  105. $1$2$3$2¶$0
  106. m`s$|:t
  107.  
  108. )`(.+)¶-(D*)
  109. -$2¶$1
  110. m`^.
  111.  
  112. G`.
  113. T`ts:` *
  114.  
  115. ,#i's=`≈;'**@½≈";#dXdXεj' +"£n`@`≈;'**n`@Iƒ('-=WXa0WXü
  116.  
  117. ,#i Take input, push chars separately
  118. 's= Iƒ IF the first char is "s":
  119. ` `@ run the quoted function
  120. ≈;'**n make list of n strings of n *'s
  121. ` `@ ELSE run the quoted function:
  122. ≈; make two copies of int n
  123. '** use one to make string of n *'s
  124. @½≈ cut the other in half (e.g. 5->2)
  125. " "£n run n/2 times the quoted function:
  126. ;# copy the string as list of chars
  127. dXdX discard the last 2 *'s
  128. εj join back into string
  129. ' + prepend a space
  130. ('-=WX 0WX IF the third character is "-":
  131. a invert the stack
  132. ü pop and print the entire stack
  133.  
  134. 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`
  135. `)
  136.  
  137. *****
  138.  
  139. |*****| (5)
  140. | ***| (4)
  141. | *| (3)
  142.  
  143. ' *'jt4Y2m)U1$l't'Gm?2MQ2/:1L3$)R!P!R'+'Gm?P]]Q)
  144.  
  145. ' *' % push string. Will be index into for to final result
  146. j % input string
  147. t % duplicate
  148. 4Y2 % predefined literal string '0123456789'
  149. m % logical index of digits in input string
  150. ) % index into input string to obtain substring with digits
  151. U % convert to number
  152. 1$l % generate square of ones with that size
  153. 't' % push character 't'
  154. G % push input string
  155. m % true if input string contains 't'
  156. ? % if so...
  157. 2M % push argument of call to function `l`, i.e. square size
  158. Q2/ % add 1 and divide by 2. Call result T
  159. : % generate vector [1, 2, ... T]
  160. 1L % predefined literal representing Matlab's `:` index
  161. 3$) % two dimensional index. Transforms square into rectangle
  162. R % remove (set to zero) lower-left corner
  163. !P! % flip horizontally
  164. R % remove lower-left corner. This gives inverted triangle
  165. '+' % push character '+'
  166. G % push input
  167. m % true if input contains '+'
  168. ? % if so...
  169. P % flip vertically
  170. ] % end if
  171. ] % end if
  172. Q % add 1. This gives array of values 1 and 2
  173. ) % index string ' *' with this array to produce char array
  174. % implicitly display that char array
  175.  
  176. #define x(y);)putchar(y)
  177. #define m(n)for(n=0;n++<
  178. #define T {m(q)i x(32);m(q)s-i*2 x(42);puts("");}
  179. 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}
  180.  
  181. main(q,v,i,size)char**v; // neat way of declaring variables
  182. {
  183. size=atoi(v[1]+1);
  184. if(*v[1]=='s')
  185. {
  186. for(i=0;i++<size*size;)
  187. {
  188. putchar(42); // returns 42 (true)
  189. if(!(i%size))
  190. puts("");
  191. }
  192. }
  193. else if(strchr(v[1],'+')) // if finds plus sign
  194. {
  195. for(i=size/2+1;i-->0;) // iterate the height of the triangle
  196. {
  197. for(q=0;q++<i;)putchar(32); // conveniently i is the number os spaces before each line
  198. for(q=0;q++<size-i*2;) putchar(42);
  199. puts("");
  200. }
  201. }
  202. else for(i=-1;i++<size/2+1;) // does the same as above but inverted order
  203. {
  204. for(q=0;q++<i;)putchar(32);
  205. for(q=0;q++<size-i*2;)putchar(42);
  206. puts("");
  207. }
  208. }
  209.  
  210. ->s{n=s[1,2].to_i
  211. n.times{|i|d=(s.ord-115)*(s[-1]<=>?,)*(n-1-i*2)
  212. d<1&&puts((?**(n+d)).center(n))}}
  213.  
  214. f=->s{ #take a string as an argument
  215. n=s[1,2].to_i #take 2 characters starting at index 1 and convert to a number for the size
  216. n.times{|i| #iterate through n rows
  217. d= #calculate how many stars "MORE THAN" n we need on a row
  218. (s.ord-115)* #ascii code for 1st character of string - 115 : s-->0, t-->1
  219. (s[-1]<=>?,)* #compare last character of input with comma character - --> +1 + --> -1
  220. (n-1-i*2) #row number * 2: 0 at centre, positive above it, negative below it
  221. d<1&& #only output if d is nonpositive (i.e we need less than n or exactly n stars)
  222. puts((?**(n+d)).center(n)) #print n+d stars, centred in a field of n characters padded by whitespace
  223. }
  224. }
  225.  
  226. f[gets.chomp]
  227.  
  228. onFiΒ€ioSgiγ?='sn―sΒ'*―TΒ1'*?='-SZiγγ
  229.  
  230. onFiΒ€ioSgi
  231. on set n to
  232. Fi the first entity of i (the shape identifier)
  233. Β set Β (beta) to
  234. €i the "inside" of i (in this case, the size) as a number
  235. oS set S to
  236. gi the last entity of i (the inverter)
  237.  
  238. γ?='sn―sΒ'*―TΒ1'*
  239. γ set γ (gamma) to the result of the following expression
  240. ?='sn if n is the character s,
  241. ―sΒ'* then return a pattern "s" (a square) made with "*"s
  242. ―TΒ1'* otherwise, return a pattern "T" (triangle) that is centered and
  243. has a scale factor of 1, made with "*"s
  244.  
  245. ?='-SZiγγ
  246. ?='-S if S is a "-"
  247. Ziγ return γ, inverted across its lines
  248. γ otherwise, return γ untouched
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement