Writing a string in a spiral

后端 未结 11 1046
别那么骄傲
别那么骄傲 2020-12-30 11:29

I had recently participated in coding competion sponsored by an company and there was this one question which I did not understood, as to what was it asking.

Here i

11条回答
  •  梦毁少年i
    2020-12-30 11:56

    Size of side of square is square root of square, where square is a length in first place.

    Answer:

    • Take length as an integer number
    • Find square root of length as float number, i.e. root
    • Check if fractional part of root is non-zero
    • If there is non-zero in fractional part of root, add 1 to integer part of root, else add 0
    • The result is an integer number
    • This number is an answer how big is each side of your square
    • Instantiate an empty square, sized as calculated
    • Fill the square by visiting every slot "spirally" starting from top left corner
    • Assert that nothing left in source string after vistiting is complete
    • Assert that remaining unfilled part of square is less that (2x side size - 1)
    • Revisit the square in left-to-right, top-to-down order to reconstruct the output
    • Assert that length of output is equal to length of input
    • Done

提交回复
热议问题