Can someone help me to solve this python question please?

This is the place for queries that don't fit in any of the other categories.

Can someone help me to solve this python question please?

Postby ltcy » Sat Feb 16, 2013 1:34 am

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'
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: Can someone help me to solve this python question please

Postby micseydel » Sat Feb 16, 2013 3:56 am

We are happy to help with homework, but you must make a reasonable effort before we do anything for you. Ask a question, don't just say what your prompt is. Post your best code attempt, even if it doesn't work. Post the traceback associated with your code, if it has one, and not just the error message.

Note to regulars: I've locked the original post, no need to quote it.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Can someone help me to solve this python question please

Postby ltcy » Sat Feb 16, 2013 5:07 am

ltcy wrote: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 tried these codes :

Code: Select all
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


AND

Code: Select all
imprTowers = int(bricks / 1024) # Impressive towers that can be built
bricks = bricks % 1024 # Remaining bricks
regTowers = int(bricks / 256) # Regular towers that can be built
bricks = bricks % 256 # Remaining bricks
cdn = (imprTowers * 10000) + (regTowers * 2000) - (bricks * 20) # Total CDN$
return cdn


either one doesn't work out...I'm using Code Write to write the program.
can someone please help me?
Last edited by ltcy on Sat Feb 16, 2013 9:17 am, edited 1 time in total.
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: Can someone help me to solve this python question please

Postby ltcy » Sat Feb 16, 2013 5:08 am

micseydel wrote:We are happy to help with homework, but you must make a reasonable effort before we do anything for you. Ask a question, don't just say what your prompt is. Post your best code attempt, even if it doesn't work. Post the traceback associated with your code, if it has one, and not just the error message.

Note to regulars: I've locked the original post, no need to quote it.


I've already post my code
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: Can someone help me to solve this python question please

Postby micseydel » Sat Feb 16, 2013 5:12 am

Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Can someone help me to solve this python question please

Postby Yoriz » Sat Feb 16, 2013 10:54 am

Just for starters, your indentation is wrong and you are using return when not using a function or method.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1178
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Can someone help me to solve this python question please

Postby ichabod801 » Sat Feb 16, 2013 2:30 pm

Yeah, that second bit of code looks like it would work if you put it in a function definition. At least it would return the right profit or loss for the company. To get the string output requested by the problem, there's lots of options. The easiest is just string addition:

Code: Select all
'Ichabod is ' + str(42) + ' years old.'
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 103
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 2 guests