Fork me on GitHub

Introduction to Maps 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

Write a SARL script to sort (ascending and descending) a map by value.

Solution

2. Exercise 2

Write a SARL script to add a key to a map.

Solution

3. Exercise 3

Write a SARL script to concatenate the following maps to create a new one.

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

Solution

4. Exercise 4

Write a SARL script to check whether a given key already exists in a map.

Solution

5. Exercise 5

Write a SARL program to iterate over maps using for loops.

Solution

6. Exercise 6

Write a SARL script to generate and print a map that contains a number (between 1 and n) in the form (x, x*x).

Solution

7. Exercise 7

Write a SARL script to print a map where the keys are numbers between 1 and 15 (both included) and the values are the square of the keys.

Solution

8. Exercise 8

Write a SARL script to merge two SARL maps.

Solution

9. Exercise 9

Write a SARL program to sum all the values in a maps.

Solution

10. Exercise 10

Write a SARL program to multiply all the values in a map.

Solution

11. Exercise 11

Write a SARL program to remove a key from a map.

Solution

12. Exercise 12

Write a SARL program to map two lists into a map.

Solution

13. Exercise 13

Write a SARL program to sort a given map by key.

Solution

14. Exercise 14

Write a SARL program to get the maximum and minimum values of a map.

Solution

15. Exercise 15

Write a SARL program to remove duplicated values from the map.

Solution

16. Exercise 16

Write a SARL program to check if a map is empty or not.

Solution

17. Exercise 17

Write a SARL program to combine two maps by adding values for common keys.

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}

Solution

18. Exercise 18

Write a SARL program to print all distinct values in a map.

Solution

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.

ac
ad
bc
bd

Solution

20. Exercise 20

Write a SARL program to find the highest 3 values of corresponding keys in a map.

Solution

21. Exercise 21

Write a SARL program to combine values in a list of maps.

Solution

22. Exercise 22

Write a SARL program to create a map from a string. Track the count of the letters from the string.

Solution

23. Exercise 23

Write a SARL program to print a map in table format.

{1: ["Samuel", 21, 'Data Structures'],
 2: ["Richie", 20, 'Machine Learning'],
 3: ["Lauren", 21, 'OOPS with java'],
}
Samuel	21	Data Structures
Richie	20	Machine Learning
Lauren	21	OOPS with java

Solution

24. Exercise 24

Write a SARL program to sort a list alphabetically in a map.

Solution

25. Exercise 25

Write a SARL program to remove spaces from map keys.

Solution

26. Exercise 26

Write a SARL program to get the top three items in a shop.

item4 55
item1 45.5
item3 41.3

Solution

27. Exercise 27

Write a SARL program to get the key, value and item in a map.

Solution

28. Exercise 28

Write a SARL program to print a map line by line.

Solution

29. Exercise 29

Write a SARL program to count the number of items in a map value that is a list.

Solution

30. Exercise 30

Write a SARL program to sort items by value in reverse order.

Solution

31. Exercise 31

Write a SARL program to create a map from two lists without losing duplicate values.

Solution

32. Exercise 32

Write a SARL program to match key values in two dictionaries.

Solution

33. Exercise 33

Write a SARL program to store map data in a JSON file.

{'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]}
{'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]}

Solution

34. Exercise 34

Write a SARL program to create a map of keys x, y, and z where each key has as value a list from 11-20, 21-30, and 31-40 respectively. Access the fifth value of each key from the map.

{'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]

Solution

35. Exercise 35

Write a SARL program to drop empty items from a given map.

Solution

36. Exercise 36

Write a SARL program to filter a map based on values.

Solution

37. Exercise 37

Write a SARL program to convert more than one list to a nested map.

Solution

38. Exercise 38

Write a SARL program to filter the height and width of students, which are stored in a map.

Solution

39. Exercise 39

Write a SARL program to verify that all values in a map are the same.

Solution

40. Exercise 40

Write a SARL program to create a map grouping a sequence of key-value pairs into a map of lists.

Solution

41. Exercise 41

Write a SARL program to split a given map of lists into lists of maps.

Solution

42. Exercise 42

Write a SARL program to remove a specified map from a given list.

Solution

43. Exercise 43

Write a SARL program to convert string values of a given map into integer/float datatypes.

Solution

44. Exercise 44

A SARL map contains List as a value. Write a SARL program to clear the list values in the said map.

Solution

45. Exercise 45

A SARL map contains List as a value. Write a SARL program to update the list values in the said map by adding 1 to the scores in Math and substracting 2 to the scores in Physics

Solution

46. Exercise 46

Write a SARL program to extract a list of values from a given list of maps.

Solution

47. Exercise 47

Write a SARL program to find the length of a map of values.

Solution

48. Exercise 48

Write a SARL program to get the depth of a map.

Solution

49. Exercise 49

Write a SARL program to access map key’s element by index.

0 = physics
1 = math
2 = chemistry

Solution

50. Exercise 50

Write a SARL program to convert a map into a list of lists.

Solution

51. Exercise 51

Write a SARL program to filter even numbers from a map of values.

Solution

52. Exercise 52

Write a SARL program to get all combinations of key-value pairs in a given map.

Solution

53. Exercise 53

Write a SARL program to find the specified number of maximum values in a given map.

Solution

54. Exercise 54

Write a SARL program to find the shortest list of values for the keys in a given map.

Solution

55. Exercise 55

Write a SARL program to extract values from a given map and create a list of lists from those values.

Solution

56. Exercise 56

Write a SARL program to convert a given list of lists to a map.

Solution

57. Exercise 57

Write a SARL program that creates key-value list pairings within a map.

Solution

58. Exercise 58

Write a SARL program to get the total length of all values in a given map with string values.

Solution

59. Exercise 59

Write a SARL program to check if a specific key and a value exist in a map.

Solution

60. Exercise 60

Write a SARL program to invert a given map with non-unique hashable values.

Solution

Copyright © 2014-2024 SARL.io, the Original Authors and Main Authors.

Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.

Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator docs.generator 0.14.0.