Advertisement
Radeen10-_

Drink the Water after Separating the Substance

Aug 13th, 2021
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. Given a two-dimensional array representation of a glass of mixed liquids, sort the array such that the liquids appear in the glass based on their density. (Lower density floats to the top). If there are disproportinate number of substance then some of substances mixed together but a very low amount. The width of the glass will not change from top to bottom.
  2.  
  3. ======================
  4. |   Density Chart    |
  5. ======================
  6. | Honey   | H | 1.36 |
  7. | Water   | W | 1.00 |
  8. | Alcohol | A | 0.87 |
  9. | Oil     | O | 0.80 |
  10. ----------------------
  11. Such as:
  12.  
  13. [['W', 'A', 'A', 'W', 'W', 'W', 'O', 'W', 'W', 'A'],              
  14.  ['A', 'H', 'O', 'O', 'H', 'O', 'H', 'A', 'H', 'W'],
  15.  ['A', 'H', 'H', 'H', 'H', 'O', 'W', 'W', 'A', 'W'],
  16.  ['H', 'A', 'A', 'H', 'O', 'A', 'H', 'W', 'O', 'A'],    
  17.  ['H', 'W', 'H', 'A', 'W', 'W', 'A', 'H', 'A', 'O'],
  18.  ['A', 'W', 'H', 'W', 'H', 'O', 'A', 'A', 'A', 'W'],
  19.  ['A', 'H', 'O', 'H', 'H', 'H', 'O', 'W', 'A', 'H'],
  20.  ['A', 'W', 'W', 'H', 'H', 'W', 'A', 'A', 'A', 'A']]
  21. =====================================================
  22.                    ||
  23.                    ||
  24.                    \/
  25. ===================================================
  26.  [['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],
  27.  ['O', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'],
  28.  ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'],
  29.  ['A', 'A', 'A', 'A', 'A', 'A', 'W', 'W', 'W', 'W'],
  30.  ['W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'],
  31.  ['W', 'W', 'W', 'W', 'W', 'W', 'W', 'H', 'H', 'H'],
  32.  ['H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H'],
  33.  ['H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H']]
  34.  
  35.  
  36. #Solution using bubble sort
  37. def separate_liquids(glass):
  38.     if not glass: #if empty glass is given, return empty array
  39.         return []
  40.     glass2=glass
  41.     glass2=sum(glass2,[])
  42.     #replace the list item with int for sorting
  43.     for n,i in enumerate(glass2):
  44.         if i=='A':
  45.             glass2[n]=0.87
  46.         elif i=='H':
  47.             glass2[n]=1.36
  48.         elif i=='W':
  49.             glass2[n]=1.00
  50.         else:
  51.             glass2[n]=0.80
  52.      #bubble sort
  53.     for i in range(len(glass2)-1,0,-1):
  54.         for j in range(i):
  55.             if glass2[j] > glass2[j + 1]:
  56.                 temp = glass2[j]
  57.                 glass2[j] = glass2[j + 1]
  58.                 glass2[j + 1] = temp
  59.  
  60.     char={1.36:'H',1.00:'W',0.87:'A',0.80:'O'}
  61.     glass3=[char.get(x,x) for x in glass2]
  62.     result = [glass3[x:x + len(glass[0])] for x in range(0, len(glass3), len(glass[0]))][:len(glass)]
  63.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement