February 13th, 2013, 07:19 PM

Can someone help me with this python question?? URGENT!
Question :
A company builds towers with bricks. The company can build two types of towers: impressive and regular. It takes the company 1024 bricks to build impressive towers and 256 bricks to build regular towers. The company makes the most gain by building impressive towers: CDN$10000 each. Regular towers produce less gain but still some: a regular tower provides the company CDN$ 2000 gain. On the other hand, the company loses CDN$ 20 per brick not used. Thus, the company will attempt to make as many impressive towers as possible, and then as many regular towers as possible.
This function receives as input parameter a number of bricks and it should return a string with the following information: first, the number of impressive towers to build, followed by two dots ("..") followed by the number of regular towers to build, followed again by two dots, followed by the number of unused bricks, followed by two dots and finally the money the company makes, considering the gains (becuase of the towers) minus the losses (becasue of the unused bricks).
For example,
bricksAndTowers(1027) should return '1..0..3..9940', because with 1027 bricks one impressive tower can be built (the first 1 in the string), no regular towers can be built (0) and there are 3 unused bricks. The company makes CDN$ 9940, that is, CDN$10000 because of the impressive tower minus CDN$ 60 (CDN$20 per each of the 3 unused bricks).
Notice that the company may have a net loss, when there are not enough bricks to build any tower.
For example:
bricksAndTowers(10) should return '0..0..10..200'
I have no idea how to do this question, and my code I have atm is :
if (bricks > 1024):
impressive_tower = 1
gain_loss = 10000
unused_bricks = bricks  1024
elif (bricks == 1024):
unused_bricks = 0
elif (bricks < 1024):
impressive_tower = 0
unused_bricks = bricks
elif (bricks  1024 > 256):
regular_tower = 1
elif (bricks  1024 < 256):
regular_tower = 0
gain_loss = 10000  unused_bricks * 20
result = impressive_tower + ".." + regular_tower + ".." + unused_bricks + ".." + gain_loss
return result
can someone please help me??!!
February 13th, 2013, 08:05 PM

If I asked you to calculate the result of bricksAndTowers(6435) by hand (not using Python or other code), what steps would you take to arrive at the answer? Write out those steps to explain how you got to the answer and I'll show you how that helps you figure out what Python code you need to write to accomplish this.
PHP FAQ
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
February 13th, 2013, 08:27 PM

You've written special case code for the case of a single supreme tower.
Integer division and modulus (remainder after division) will help. The divmod function returns both at once.
A similar problem to help you think along the lines EOreo suggests, how would you convert $1.38 to coins?
[code]
Code tags[/code] are essential for python code and Makefiles!
February 16th, 2013, 08:00 PM

Code:
def antibase(n,base=[10]*8):
'''
doctest. Run with bash command $ python m doctest this_file.py
By default find base 10 digits of small numbers.
>>> antibase(23456)
(0, 2, 3, 4, 5, 6)
>>>
convert $1.38 to coins.
A quarter is 2.5 times more valuable than a dime.
A dime has twice the value of a nickel.
A nickel is worth 5 pennies.
A penny is worth 1.
The output (5.0, 1.0, 0.0, 3.0, 0.0) represents
the number of quarters, dimes, nickels, pennies, and fractional pennies.
>>> US_COINS = antibase(138, [2.5, 2, 5, 1])
>>> print(US_COINS)
(5.0, 1.0, 0.0, 3.0, 0.0)
>>>
'''
cumprod = [1]
for b in reversed(base):
cumprod[:0] = [cumprod[0]*b]
result = []
# print(cumprod)
for f in cumprod[:1]:
(a,n,) = divmod(n,f,)
result.append(a)
result.append(n)
return tuple(result)
def main(bricks):
'''
report how to use the bricks.
'''
r = (impressive, regular, left_over_bricks,) = antibase(bricks,[4, 256])
profit = 10000 * impressive + 2000 * regular  20 * left_over_bricks
return ('{}..'*4).format(*(r+(profit,)))[:2]
print(main(12345))
[code]
Code tags[/code] are essential for python code and Makefiles!