Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
February 13th, 2013, 08:19 PM
 ltcy
Registered User

Join Date: Feb 2013
Posts: 3
Time spent in forums: 57 m 31 sec
Reputation Power: 0
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

#2
February 13th, 2013, 09:05 PM
 E-Oreo
Lost in code

Join Date: Dec 2004
Posts: 8,233
Time spent in forums: 2 Months 2 Days 20 h 11 m 10 sec
Reputation Power: 7170
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
How to program a basic, secure login system using PHP

Quote:
 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

#3
February 13th, 2013, 09:27 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
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 E-Oreo suggests, how would you convert \$1.38 to coins?
__________________
[code]Code tags[/code] are essential for python code!

#4
February 16th, 2013, 09:00 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
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))```

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Can someone help me with this python question?? URGENT!

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |