|
| 1 | +### Bubble Sort Exercise |
| 2 | + |
| 3 | +Modify [bubble_sort function](https://github.com/codebasics/py/blob/master/Algorithms/2_BubbleSort/bubble_sort.py) such that it can sort following list of transactions happening in an electronic store, |
| 4 | +``` |
| 5 | +elements = [ |
| 6 | + { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'}, |
| 7 | + { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'}, |
| 8 | + { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'}, |
| 9 | + { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'}, |
| 10 | + ] |
| 11 | +``` |
| 12 | +bubble_sort function should take key from a transaction record and sort the list as per that key. For example, |
| 13 | +``` |
| 14 | +bubble_sort(elements, key='transaction_amount') |
| 15 | +``` |
| 16 | +This will sort elements by transaction_amount and your sorted list will look like, |
| 17 | +``` |
| 18 | +elements = [ |
| 19 | + { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'}, |
| 20 | + { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'}, |
| 21 | + { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'}, |
| 22 | + { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'}, |
| 23 | + ] |
| 24 | +``` |
| 25 | +But if you call it like this, |
| 26 | +``` |
| 27 | +bubble_sort(elements, key='name') |
| 28 | +``` |
| 29 | +output will be, |
| 30 | +``` |
| 31 | +elements = [ |
| 32 | + { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'}, |
| 33 | + { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'}, |
| 34 | + { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'}, |
| 35 | + { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'}, |
| 36 | + ] |
| 37 | +``` |
| 38 | + |
| 39 | +[Solution](https://github.com/codebasics/py/blob/master/Algorithms/2_BubbleSort/bubble_sort_exercise_solution.py) |
| 40 | + |
0 commit comments