stuck on Google python challenge

im working on a challenge that requires a function(input) and outputs a list of outcomes. Adult with some kind of math aut

​

Directions

All of the solar panels must be squares. Fortunately, you have one very large and flat area of solar material and you can 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 3x3 square panel (with a total area of 9). That would leave 3 square yards, so you can turn those into three 1x1 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].

My Logic

Why cant the squares be all of the numbers instead of being cut up ? the example was 12 square areas why cant that be a ssingle big quare ?...... seems arbitrary

what ive come up with is something either with dividing the input number or using the `%` and doing something with the remaining numbers or import math and use math.sqrt

​

import math

def myfunc(x):

mylist = []

fnum = x / (9 ?)

rnum = x % (idk)

for i in fnum, rnum:

mylist.append(i)

return mylist

​

​

UPDATE:

This is what i came up with. thank you for the feedback

def solution(area):

tnum = area

mylist = []

while tnum > 0:

mylist.append(int(math.sqrt(tnum))**2)

tnum = tnum - int(math.sqrt(tnum))**2

return mylist

this is what I came up with. thank you for the feedback and code examples.

8 comments · 100% upvoted · other discussions

comments

ChurchHatesTucker

the example was 12 square areas why cant that be a ssingle big quare ?

Because 12 squares can't make a square. 9 or 16 can. That's a clue for your logic.

Vaphell

why cant that be a ssingle big quare? seems arbitrary.

Because there would be no exercise in the first place. For any given N you'd just answer sqrt(N) and call it a day.

They want a list of squared integers to sum up to the given number.

toastedstapler

the example was 12 square areas why cant that be a ssingle big quare ?

120.5 is not an integer value. the program is asking for you to produce square solar panels with integer length sizes. a 9m2 panel is a 3m square

if you were given 13 as an input. the question would want [9, 4] as a result as those are the largest square numbers that fit into 13

RedditGood123

I decided to try the problem, but I did it in JavaScript and the answer is sort of convoluted. Nonetheless, I put a ton of comments explaining what I did, so the code might not make sense, but the solution should

Here’s my solution (you might want to view it on PC)

Vaphell

is there a reason why you excluded 1s from the main loop?
while n>0 should work just fine.

RedditGood123

I thought it would be easier to separate those parts, but I guess you’re right about that. I updated the code to adjust for that since it’s much simpler your way

Vaphell

your while loop only handles x2 from 4 up, and 1, 2, 3 translating to individual 1s are handled by the for loop. But that second loop is an unnecessary special case.

If you let the while loop spin for any positive integer, it will handle 1s too automatically.

while num > 0:
    ...

sqrt for 1 2 and 3 floors to 1, so you are guaranteed to subtract 12 s until you go down to 0, which is when the loop stops.

Bphag [OP]

Stop letting data brokers profit from your old posts. I used Redact to wipe mine from Reddit. Also supports Twitter, Facebook, Discord, instagram and more in one batch.

aspiring crown unite one sip sophisticated long direction hat soft