#๐Ÿ”’ help with code

52 messages ยท Page 1 of 1 (latest)

plush marsh
#

pls give me hints of where im going wrong in my logic

lucid irisBOT
#

@plush marsh

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

plush marsh
#
def largest_cube(n):
    '''
    Assume n is an integer > 1.
    Return the largest value of k such that k**3 is strictly
    less than n. For example, if n is 30, the answer would be
    3, since 3**3 = 27, which is the largest perfect cube 
    less than 30.
    '''
    number = 0
    for i in range(n):
        while n**3 < n:
            if n**3 < n:
                number += n

    return number
print(largest_cube(30))  
#

^same code in photo

acoustic onyx
plush marsh
#

but shouldnt this work also

acoustic onyx
#

No, it doesn't quite make sense what you're doing with this code

#

all you need to do is increase number, check it's cube, and stop when the cube is greater than n

#

so if the number is 30 like in the example

#

we would check
1 ** 3
2 ** 3
3 ** 3

#

4 ** 3 is greater than 30, so the loop would stop

plush marsh
acoustic onyx
#

I would use a while loop here instead of a for loop

#

if you do for num in range(n)

#

that's going to give you 0, 1, 2, 3, 4, 5....30

#

you don't need to check all 30 numbers

#

you only need to check numbers whose cube is smaller than 30

plush marsh
acoustic onyx
#

be careful with the difference of = and ==

plush marsh
#

oh it works

acoustic onyx
#

and why do you think you need num and number? How are they different?

#

Just because it works for one example, doesn't mean you got it correct

#

run more tests

plush marsh
#

yea nvm

acoustic onyx
#

You're overthinking it a bit

#

Your while loop condition is good

#

except is should be <= instead of <

#

Oh nevermind it says less than n

#

< is correct

plush marsh
#

idk how to increment num in a while loop

acoustic onyx
#
def largest_cube(n):
    number = 1
    while number ** 3 <= n:
        number += 1
#

Look at this logic here

#

This logic will check 1 ** 3 to see if it is below 30 (if n is 30)

#

if it's still less than 30, then it will check 2 ** 3

#

if it's still less than 30, it will check 3 ** 3

#

do you see how this is working?

plush marsh
#

yea

acoustic onyx
#

We're going to keep increasing number by 1 until number ** 3 is greater than (or equal to) n

plush marsh
#

much simpler

acoustic onyx
#

There's one slight flaw though

#

What do you think it might be?

plush marsh
#

hmm

#

i think if i test 3 it doesnt work

#

actually idk

acoustic onyx
#

The issue is that number has to go one too far to figure out when the cube is too large

#

when n is 30, number will be 4 after the loop, because when testing 4 ** 3, we can see that 64 is larger than 30

#

So for this, we want to return 3, not 4

plush marsh
#

would subtracting 1 help?

acoustic onyx
#

exactly ๐Ÿ™‚

plush marsh
#

!close

lucid irisBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.