Simple Fun #62: Draw Rectangle
Description:
Task
You are implementing a command-line version of the Paint app. Since the command line doesn't support colors, you are using different characters to represent pixels. Your current goal is to support rectangle x1 y1 x2 y2 operation, which draws a rectangle that has an upper left corner at (x1, y1) and a lower right corner at (x2, y2). Here the x-axis points from left to right, and the y-axis points from top to bottom.
Given the initial canvas
state and the array that represents the coordinates of the two corners, return the canvas state after the operation is applied. For the details about how rectangles are painted, see the example.
Example
For
canvas = [['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'],
['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']]
and rectangle = [1, 1, 4, 3]
the output should be
[['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
['a', '*', '-', '-', '*', 'a', 'a', 'a'],
['a', '|', 'a', 'a', '|', 'a', 'a', 'a'],
['b', '*', '-', '-', '*', 'b', 'b', 'b'],
['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']]
Note that rectangle sides are depicted as -s and |s, asterisks (*) stand for its corners and all of the other "pixels" remain the same. Color in the example is used only for illustration.
Input/Output
[input]
2D string arraycanvas
A non-empty rectangular matrix of characters.
Constraints:
2 ≤ canvas.length ≤ 10, 2 ≤ canvas[0].length ≤ 10.
[input]
integer arrayrectangle
Array of four integers - [x1, y1, x2, y2].
Constraints:
0 ≤ x1 < x2 < canvas[i].length, 0 ≤ y1 < y2 < canvas.length.
[output]
2D string array
Similar Kata:
Stats:
Created | Jan 26, 2017 |
Published | Jan 26, 2017 |
Warriors Trained | 738 |
Total Skips | 14 |
Total Code Submissions | 946 |
Total Times Completed | 369 |
JavaScript Completions | 134 |
C# Completions | 58 |
Python Completions | 169 |
Ruby Completions | 35 |
Haskell Completions | 17 |
Total Stars | 8 |
% of votes with a positive feedback rating | 88% of 121 |
Total "Very Satisfied" Votes | 99 |
Total "Somewhat Satisfied" Votes | 14 |
Total "Not Satisfied" Votes | 7 |
Total Rank Assessments | 7 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |