|
| 1 | +# Clean up after your dog |
| 2 | +""" |
| 3 | +You have stumbled across the divine pleasure that is owning a dog and a garden. Now time to pick up all the cr@p! 😄 |
| 4 | +Given a 2D array to represent your garden, you must find and collect all of the dog cr@p - represented by `@`. |
| 5 | +
|
| 6 | +You will also be given the number of bags you have access to (bags), and the capacity of a bag (cap). If there are no |
| 7 | +bags then you can't pick anything up, so you can ignore cap. |
| 8 | +You need to find out if you have enough capacity to collect all the cr@p and make your garden clean again. |
| 9 | +
|
| 10 | +If you do, return 'Clean',else return 'Cr@p'. |
| 11 | +
|
| 12 | +Watch out though - if your dog is out there ('D'), he gets very touchy about being watched. If he is there you need to |
| 13 | +return 'Dog!!'. |
| 14 | +""" |
| 15 | + |
| 16 | +garden1 = [ |
| 17 | + ['_', '_', '_', '_', '_', '_'], |
| 18 | + ['_', '_', '_', '_', '@', '_'], |
| 19 | + ['@', '_', '_', '_', '_', '_'] |
| 20 | +] |
| 21 | + |
| 22 | +garden2 = [ |
| 23 | + ['_', '_', '_', '_'], |
| 24 | + ['_', '_', '_', '_'], |
| 25 | + ['@', '_', '_', 'D'] |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +def cleanUp(garden, bags, cap): |
| 30 | + craps = 0 |
| 31 | + for lst in garden: |
| 32 | + if 'D' in lst: |
| 33 | + print('Dog!!') |
| 34 | + return 0 |
| 35 | + craps += lst.count('@') |
| 36 | + |
| 37 | + capacity = cap * bags |
| 38 | + |
| 39 | + if capacity >= craps: |
| 40 | + print('Clean') |
| 41 | + else: |
| 42 | + print('Cr@p') |
| 43 | + return 0 |
| 44 | + |
| 45 | + |
| 46 | +cleanUp(garden1, 2, 2) # 'Clean' |
| 47 | +cleanUp(garden1, 1, 1) # 'Cr@p' |
| 48 | +cleanUp(garden2, 2, 2) # 'Dog!!' |
| 49 | + |
| 50 | +# Test cases |
| 51 | + |
| 52 | +cleanUp([['_','_','_','_'], ['_','_','_','@'], ['_','_','@','_']], 2, 2) # 'Clean' |
| 53 | +cleanUp([['_','_','_','_'], ['_','_','_','@'], ['_','_','@','_']], 1, 1) # 'Cr@p' |
| 54 | +cleanUp([['_','_'], ['D','_'], ['_','_']], 2, 2) # 'Dog!!' |
| 55 | +cleanUp([['_','_','_','_'], ['_','_','_','_'], ['_','_','_','_']], 2, 2) # 'Clean' |
| 56 | +cleanUp([['@','@'], ['@','@'], ['@','@']], 2, 2) # 'Cr@p' |
| 57 | +cleanUp([['_','_','_','_','D','_','_','_','_'], ['_','_','_','_','@','@','_','_','_'], |
| 58 | + ['_','_','_','_','_','_','D','_','_']], 9, 5) # 'Dog!!' |
| 59 | +cleanUp([['_','_','_','_','_']], 6, 9) # 'Clean' |
| 60 | +cleanUp([['_','_','D','_','_','_','_','_','D'], ['_','_','D','_','_','_','_','_','_'], |
| 61 | + ['_','_','_','_','_','@','_','_','_']] , 4, 4) # 'Dog!!' |
| 62 | +cleanUp([['_','@','@'], ['_','@','@']], 1, 2) # 'Cr@p' |
| 63 | +cleanUp([['_','_','_','_','_','_','_','@'], ['_','_','_','@','D','_','_','_'], ['_','_','_','_','_','_','D','_'], |
| 64 | + ['_','_','_','_','D','_','_','_'], ['_','_','_','_','D','@','_','_']], 1, 6) # 'Dog!!' |
| 65 | +cleanUp([['_','_','_','_','_','_','_','@','_'], ['_','_','_','_','_','_','_','@','_']], 4, 3) # 'Clean' |
| 66 | +cleanUp([['_','_','_','_','_']], 6, 0) # 'Clean' |
| 67 | +cleanUp([['_','@','@','_','_','@','_','_','_']], 4, 0) # 'Cr@p' |
| 68 | +cleanUp([['D','_','_','_'], ['_','_','_','_'], ['_','_','_','_'], ['_','_','@','_'], ['_','_','_','_']], 7, 8) # 'Dog!!' |
| 69 | +cleanUp([['_','@','_'], ['_','@','_']], 0, 8) # 'Cr@p' |
| 70 | + |
0 commit comments