#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep 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

    can someone please help me??!!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep 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

    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
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    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 and Makefiles!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    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!

IMN logo majestic logo threadwatch logo seochat tools logo