Foobar: Solar Doomsday

Solar Doomsday

Who would’ve guessed? Doomsday devices take a LOT of power. Commander Lambda wants to supplement the LAMBCHOP’s quantum antimatter reactor core with solar arrays, and you’ve been tasked with setting up the solar panels.

Due to the nature of the space station’s outer paneling, all of its solar panels must be squares. Fortunately, you have one very large and flat area of solar material, a pair of industrial-strength scissors, and enough MegaCorp Solar Tape(TM) to piece together any excess panel material into more squares. For example, if you had a total area of 12 square yards of solar material, you would be able to make one 3×3 square panel (with a total area of 9). That would leave 3 square yards, so you can turn those into three 1×1 square solar panels.

Write a function solution(area) that takes as its input a single unit of measure representing the total area of solar panels you have (between 1 and 1000000 inclusive) and returns a list of the areas of the largest squares you could make out of those panels, starting with the largest squares first. So, following the example above, solution(12) would return [9, 1, 1, 1].

Test cases:
Input: 15324
Output: [15129,169,25,1]

Input: 12
Output: [9,1,1,1]

Python

<span>from</span> <span>math</span> <span>import</span> <span>sqrt</span>
<span>def</span> <span>solution</span><span>(</span><span>area</span><span>):</span>
<span>res</span> <span>=</span> <span>[]</span>
<span>while</span> <span>area</span> <span>></span> <span>0</span><span>:</span>
<span>square</span> <span>=</span> <span>int</span><span>(</span><span>sqrt</span><span>(</span><span>area</span><span>))</span><span>**</span><span>2</span>
<span>area</span> <span>-=</span> <span>square</span>
<span>res</span><span>.</span><span>append</span><span>(</span><span>square</span><span>)</span>
<span>return</span> <span>res</span>
<span>from</span> <span>math</span> <span>import</span> <span>sqrt</span>

<span>def</span> <span>solution</span><span>(</span><span>area</span><span>):</span>
    <span>res</span> <span>=</span> <span>[]</span>
    <span>while</span> <span>area</span> <span>></span> <span>0</span><span>:</span>
        <span>square</span> <span>=</span> <span>int</span><span>(</span><span>sqrt</span><span>(</span><span>area</span><span>))</span><span>**</span><span>2</span>
        <span>area</span> <span>-=</span> <span>square</span>
        <span>res</span><span>.</span><span>append</span><span>(</span><span>square</span><span>)</span>
    <span>return</span> <span>res</span>
from math import sqrt def solution(area): res = [] while area > 0: square = int(sqrt(area))**2 area -= square res.append(square) return res

Enter fullscreen mode Exit fullscreen mode

原文链接:Foobar: Solar Doomsday

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
Smash the waves would rather get in the way of the reef hill, also not willing to take a step back.
海浪宁可在挡路的礁山上撞得粉碎,也不肯后退一步
评论 抢沙发

请登录后发表评论

    暂无评论内容