#Python - Wheat grains and chessboard

1 messages · Page 1 of 1 (latest)

proud cradle
#

Hi people. After many attempts, I managed to code the first part of this exercise, but I still don't know what to do in the second part.

Inst.: Calculate the number of wheat grains on a chessboard given that the number on each square doubles.
Write code that shows:

how many grains were on a given square, and
the total number of grains on the chessboard

My code:

#this is ok, I'll see what the linter has to say about it
def square(number):
    if number > 64 or number <= 0:
        raise ValueError("square must be between 1 and 64")
    amount_of_grains = 1
    for value in range(2, number+1):
        amount_of_grains = amount_of_grains * 2 
    return amount_of_grains

# What am I supposed to do with this? 
def total():
    amount_of_grains = 1
    for value in range(2, 64+1):
        amount_of_grains = amount_of_grains * 2 
    return amount_of_grains
#

Python - Wheat grains and chessboard

iron spade
#

I can't explain it well, but...

number: 1 -> grains:   1 = ?
number: 2 -> grains:   2 = 2^1
number: 3 -> grains:   4 = 2^2
number: 4 -> grains:   8 = 2^3
number: 5 -> grains:  16 = 2^4
number: 6 -> grains:  32 = 2^5
number: 7 -> grains:  64 = 2^6
number: 8 -> grains: 128 = 2^7
number: 9 -> grains: 256 = 2^8
number:10 -> grains: 512 = 2^9

(I hope the calculation is correct.)

marble yarrow
#

first one is : 2 ^ 0 = 1

weary palm
#

Try reasoning about the totals on paper: how many on squares 1 to 2, how many on squares 1 to 3. Look for a pattern with the sums

calm lily
#

Or just sum up the grains on each square. That also works.

proud cradle
#

The square() function performs what is required, it passes all the tests, my problem is what to do with the total() function, cause square() function is already giving the total amount of grains (depending what number of squares they provide). The power of 0 is not allowed since you cannot have a chessboard with 0 squares (places).

calm lily
calm lily
#

@proud cradle were you able to solve it?

proud cradle
#

Not yet, I've tryed this

def total(number):
    sum = 0
    for value in range(number+1, 1):
        sum = sum + 2**number
    return sum 
#

And got this:
TypeError: total() missing 1 required positional argument: 'number'

marble yarrow
#

Yeah, total should have not accept any argument.
This is the default stub

def square(number):
    pass


def total():
    pass

generally you shouldn't change the function interface since the test suite relies on them to work

proud cradle
#

So, how can I access the number they pass to the square() function without modifying total() ? I mean, my problem here is to figure out how to make this function work without arguments

marble yarrow
#

You can just hardcode in the range, since it is a constant value that we know : 64 spaces for the board

#

Also you could reuse the square function inside total as well

proud cradle
#

Those are two different things I guess, cause reusing the square function would mean that total() would calculate on the given number to square(), which is different from hardcode taking 64 as a constant. Does this make any sense?

marble yarrow
#

Ok, let me ask you this question.
How are you gonna calculate the total?

proud cradle
#

That's the part I don't get yet, the total for 64 squares or the total for the number given to square()?

marble yarrow
#

The total is for the whole board.
The board has 64 spots, each spot has different number of grain. Like spot 1 has 1 grain, spot 2 has 2 grains, spot 3 has 4 etc

#

The total ask you to give the sum of all of them

#

And you already have the function above to give the grain at specific spot

calm lily
#

Total is the total number of grains that would be on a complete 64 square board with grains on each square

proud cradle
#

Ok, let me figure it out the code. Thanks for the patience, I'll comment the result latter 🙂

marble yarrow
weary palm
proud cradle
#

My code:

def total():
    counter = 1
    for number in range(1, 65):
        counter =+ 2**number
    return counter

The output:
AssertionError: 18446744073709551616 != 18446744073709551615

Kill me, please 🥲

marble yarrow
#

refer back to : And you already have the function above to give the grain at specific spot

calm lily
#

Your math is off. How many grains does that count for the first square?

iron spade
#

@proud cradle Please compare with the table at the very top that I posted.

   1  +  2  +  4  +  8  +  16 +  32 +  64 + 128 + 256 + ....
= 2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + .... + 2^(64-1)

18446744073709551616 != 18446744073709551615
(But why is there only a difference of '1'? Am I wrong?)

tough sapphire
#

i dont know about any function so help me how can i write hello world here

calm lily
modest flickerBOT
#

@tough sapphire Asking questions well increases your chance of getting help. Learn how to write good support requests in this article: http://bit.ly/howto-ask

proud cradle
#

I'm a bit busy doing stuff for my university, I'll try latter or tomorrow. Thanks again people 🙂