5 kyu

Simple Fun #309: Python Snake

35 of 44myjinxin2015

Description:

Task

Amirhossein is a painter who also loves pets. He has just bought a python snake, and would love to draw a picture of it. Now the snake is lying curled up, and Amirhossen wants to draw it using the smallest canvas possible.

Write a function that draws the python according to the following rules:

- The snake's picture should be returned as a rectangular matrix, 
  where 'H' is its head, 'T' is its tail, and 'x' is a part of its
  body. Other elements of the matrix should be filled with ' '.
- The snake's body consists of several parts. Their lengths are 
  stored in the array body. The first element of the array starts 
  with the snakes head and goes from left to right (snake's head to
  the left), the second part goes from right to left, the third one
  from left to right again, and so on.
- The drawing shouldn't contain empty lines or empty columns 
  (i.e. rows or columns that consist only of ' ').

Input/Output

  • [input] integer array body

    The length of each part of pet's body.

    2 <= body.length <= 100

    1 <= body[i] <= 100

  • [output] a string array

    Picture of the pet as a matrix of chars.

Example

For body = [1, 2, 3], the output should be:

[
[' ', 'H', ' '],
['x', 'x', ' '],
['x', 'x', 'T']
]

Here's how a professionally drawn python would look like:

Puzzles

Stats:

CreatedMay 26, 2017
PublishedMay 26, 2017
Warriors Trained325
Total Skips86
Total Code Submissions248
Total Times Completed44
JavaScript Completions35
Python Completions13
Total Stars10
% of votes with a positive feedback rating90% of 15
Total "Very Satisfied" Votes13
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes1
Total Rank Assessments4
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • kazk Avatar
  • Voile Avatar
  • dfhwze Avatar
  • saudiGuy Avatar
Ad