Fork me on GitHub

Introduction to Map-based or Dictionary Data Structures with SARL

Note 1 In SARL, a map is a data structure that is also known as dictionary in other programming languages. This type of data structure maps keys to values.

Note 2 If you don’t know how to solve an problem, or what is the function to be used, you could search on Internet for the answer using the API of the Java programming language. Indeed, since SARL is fully compatible with the Java API, you could use all the types or functions that are defined in this Java API.

1. Exercise 1

Answer

2. Exercise 2

Answer

3. Exercise 3

dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}

Answer

4. Exercise 4

Answer

5. Exercise 5

Answer

6. Exercise 6

Answer

7. Exercise 7

Answer

8. Exercise 8

Answer

## 9. Exercise 9 * Write a SARL program to sum all the values in a maps. Answer ## 10. Exercise 10 * Write a SARL program to multiply all the values in a map. Answer ## 11. Exercise 11 * Write a SARL program to remove a key from a map. Answer ## 12. Exercise 12 * Write a SARL program to map two lists into a map. Answer ## 13. Exercise 13 * Write a SARL program to sort a given map by key. Answer ## 14. Exercise 14 * Write a SARL program to get the maximum and minimum values of a map. Answer ## 15. Exercise 15 * Write a SARL program to remove duplicated values from the map. Answer ## 16. Exercise 16 * Write a SARL program to check if a map is empty or not. Answer ## 17. Exercise 17 * Write a SARL program to combine two maps by adding values for common keys. * Inputs: ```text d1 = {'a': 100, 'b': 200, 'c':300} d2 = {'a': 300, 'b': 200, 'd':400} ``` * Sample output: `{'a': 400, 'b': 400, 'd': 400, 'c': 300}` Answer ## 18. Exercise 18 * Write a SARL program to print all distinct values in a map. * Sample Data : `[{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {"V":"S009"},{"VIII":"S007"}]` * Expected Output : `{'S005', 'S002', 'S007', 'S001', 'S009'}` Answer ## 19. Exercise 19 * Write a SARL program to create and display all combinations of letters, selecting each letter from a different key in a map. * Sample data: `{'1':['a','b'], '2':['c','d']}` * Expected Output: ```text ac ad bc bd ``` Answer ## 20. Exercise 20 * Write a SARL program to find the highest 3 values of corresponding keys in a map. Answer ## 21. Exercise 21 * Write a SARL program to combine values in a list of maps. * Sample data: `[{'item': 'item1', 'amount': 400}, {'item': 'item2', 'amount': 300}, {'item': 'item1', 'amount': 750}]` * Expected Output: `{'item1': 1150, 'item2': 300}` Answer ## 22. Exercise 22 * Write a SARL program to create a map from a string. Track the count of the letters from the string. * Sample string: `w3resource` * Expected output: `{'w': 1, '3': 1, 'r': 2, 'e': 2, 's': 1, 'o': 1, 'u': 1, 'c': 1}` Answer ## 23. Exercise 23 * Write a SARL program to print a map in table format. * Sample Input: ```text {1: ["Samuel", 21, 'Data Structures'], 2: ["Richie", 20, 'Machine Learning'], 3: ["Lauren", 21, 'OOPS with java'], } ``` * Expected Output: ```text Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with java ``` Answer ## 24. Exercise 24 * Write a SARL program to sort a list alphabetically in a map. * Sample Input: `{'n1': [2, 3, 1], 'n2': [5, 1, 2], 'n3': [3, 2, 4]}` * Expected Output: `{'n1': [1, 2, 3], 'n2': [1, 2, 5], 'n3': [2, 3, 4]}` Answer ## 25. Exercise 25 * Write a SARL program to remove spaces from map keys. Answer ## 26. Exercise 26 * Write a SARL program to get the top three items in a shop. * Sample data: `{'item1': 45.50, 'item2':35, 'item3': 41.30, 'item4':55, 'item5': 24}` * Expected Output: ```text item4 55 item1 45.5 item3 41.3 ``` Answer ## 27. Exercise 27 *Write a SARL program to get the key, value and item in a map. Answer ## 28. Exercise 28 * Write a SARL program to print a map line by line. Answer ## 29. Exercise 29 * Write a SARL program to count the number of items in a map value that is a list. Answer ## 30. Exercise 30 * Write a SARL program to sort items by value in reverse order. * Sample data: `{'Math':81, 'Physics':83, 'Chemistry':87}` * Expected data: `[('Chemistry', 87), ('Physics', 83), ('Math', 81)]` Answer ## 31. Exercise 31 * Write a SARL program to create a map from two lists without losing duplicate values. * Sample lists: `['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII']` and `[1, 2, 2, 3]` * Expected Output: `{'Class-V': {1}, 'Class-VI': {2}, 'Class-VII': {2}, 'Class-VIII': {3}}` Answer ## 32. Exercise 32 * Write a SARL program to match key values in two dictionaries. * Sample maps: `{'key1': 1, 'key2': 3, 'key3': 2}` and `{'key1': 1, 'key2': 2}` * Expected output: `{'key1': 1}` is present in both input maps Answer ## 33. Exercise 33 * Write a SARL program to store map data in a JSON file. * Original map: ```json {'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]} ``` * Json file: ```json {'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]} ``` Answer

34. Exercise 34

{'x': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
'y': [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
'z': [31, 32, 33, 34, 35, 36, 37, 38, 39, 40]}
15
25
35
x has value [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
y has value [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
z has value [31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

Answer

35. Exercise 35

Answer

36. Exercise 36

Answer

37. Exercise 37

Answer

38. Exercise 38

Answer

39. Exercise 39

Answer

40. Exercise 40

Answer

41. Exercise 41

Answer