Fork me on GitHub

Introduction to Lists with SARL

Note 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 program to sum all the items in a list.

Solution

2. Exercise 2

Write a SARL program to multiply all the items in a list.

Solution

3. Exercise 3

Write a SARL program to get the largest number from a list.

Solution

4. Exercise 4

Write a SARL program to count the number of strings from a given list of strings. The string length is 2 or more and the first and last characters are the same.

Solution

5. Exercise 5

Write a SARL program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples.

Solution

6. Exercise 6

Write a SARL program to remove duplicates from a list.

Solution

7. Exercise 7

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

Solution

8. Exercise 8

Write a SARL program to clone or copy a list.

Solution

9. Exercise 9

Write a SARL program to find the list of words that are longer than n from a given list of words.

Solution

10. Exercise 10

Write a SARL function that takes two lists and returns true if they have at least one common member.

Solution

11. Exercise 11

Write a SARL program to print a specified list after removing the 0th, 4th and 5th elements.

Solution

12. Exercise 12

Write a SARL program to generate a 346 3D array whose each element is *.

Solution

13. Exercise 13

Write a SARL program to print the numbers of a specified list after removing even numbers from it.

Solution

14. Exercise 14

Write a SARL program to shuffle and print a specified list.

Solution

15. Exercise 15

Write a SARL program to generate all permutations of a list in SARL.

Solution

16. Exercise 16

Write a SARL program to calculate the difference between the two lists.

Solution

17. Exercise 17

Write a SARL program to access the index of a list.

0 5
1 15
2 35
3 8
4 98

Solution

18. Exercise 18

Write a SARL program to convert a list of characters into a string.

Solution

19. Exercise 19

Write a SARL program to find the index of an item in a specified list.

Solution

20. Exercise 20

Write a SARL program to flatten a shallow list.

Solution

21. Exercise 21

Write a SARL program to append a list to the second list.

Solution

22. Exercise 22

Write a SARL program to select an item randomly from a list.

Solution

23. Exercise 23

Write a SARL program to check whether two lists are circularly identical.

list1 = [10, 10, 0, 0, 10]
list2 = [10, 10, 10, 0, 0]
list1 = [10, 10, 0, 10, 0]
list2 = [10, 10, 10, 0, 0]

Solution

24. Exercise 24

Write a SARL program to find the second smallest number in a list.

Solution

25. Exercise 25

Write a SARL program to get unique values from a list.

Solution

26. Exercise 26

Write a SARL program to get the frequency of elements in a list.

Solution

27. Exercise 27

Write a SARL program to check whether a list contains a sublist.

Solution

28. Exercise 28

Write a SARL program that uses the Sieve of Eratosthenes method to compute prime numbers up to a specified number.

Note In mathematics, the sieve of Eratosthenes, one of a number of prime number sieves, is a simple, ancient algorithm for finding all prime numbers up to any given limit.

Solution

29. Exercise 29

Write a SARL program to create a list by concatenating a given list with a range from 1 to n.

Solution

30. Exercise 30

Write a SARL program to find common items in two lists.

Solution

31. Exercise 31

Write a SARL program to change the position of every n-th value to the (n+1)th in a list.

Solution

32. Exercise 32

Write a SARL program to convert a list of multiple integers into a single integer.

Solution

33. Exercise 33

Write a SARL program to split a list based on the first character of a word.

['be', 'have', 'do', 'say', 'get', 'make', 'go', 'know', 'take', 'see', 'come', 'think',
 'look', 'want', 'give', 'use', 'find', 'tell', 'ask', 'work', 'seem', 'feel',
 'leave', 'call']
{ 'a': ['ask'],
  'b': ['be'],
  'c': ['come', 'call'],
  'd': ['do'],
  'f': ['find', 'feel'],
  'g': ['get', 'go', 'give'],
  'h': ['have'],
  'k': ['know'],
  'l': ['look', 'leave'],
  'm': ['make'],
  's': ['say', 'see', 'seem'],
  't': ['take', 'think', 'tell'],
  'u': ['use'],
  'w': ['want', 'work']
}

Solution

34. Exercise 34

Write a SARL program to create multiple lists.

Solution

35. Exercise 35

Write a SARL program to convert a pair of values into a sorted unique array.

Solution

36. Exercise 36

Write a SARL program to select the odd items from a list.

Solution

37. Exercise 37

Write a SARL program to insert an element before each element of a list.

Solution

38. Exercise 38

Write a SARL program to print nested lists (each list on a new line) using the println() function.

assign1 assign2
final assign4
exam study

Solution

39. Exercise 39

Write a SARL program to convert a list to a list of maps.

Solution

40. Exercise 40

Write a SARL program to split a list every Nth element.

Solution

41. Exercise 41

Write a SARL program to compute the difference between two lists.

Solution

42. Exercise 42

Write a SARL program to replace the last element in a list with another list.

Solution

43. Exercise 43

Write a SARL program to check whether the n-th element exists in a given list.

Solution

44. Exercise 44

Write a SARL program to insert a given string at the beginning of all items in a list.

Solution

45. Exercise 45

Write a SARL program to iterate over two lists simultaneously.

Solution

46. Exercise 46

Write a SARL program to flatten a given nested list structure.

Solution

47. Exercise 47

Write a SARL program to remove consecutive (following each other continuously) duplicates (elements) from a given list.

Solution

48. Exercise 48

Write a SARL program to pack consecutive duplicates of a given list of elements into sublists.

Solution

49. Exercise 49

Write a SARL program to insert an element at a specified position into a given list.

Solution

50. Exercise 50

Write a SARL program to extract a given number of randomly selected elements from a given list.
4* Original list: [1, 1, 2, 3, 4, 4, 5, 1]

Solution

51. Exercise 51

Write a SARL program to generate combinations of n distinct objects taken from the elements of a given list.

Solution

52. Exercise 52

Write a SARL program to round every number in a given list of numbers and print the total sum multiplied by the length of the list.

Solution

53. Exercise 53

Write a SARL program to create a multidimensional list (lists of lists) with zeros. Multidimensional list: [[0, 0], [0, 0], [0, 0]].

Solution

54. Exercise 54

Write a SARL program to read a square matrix from the command line and print the sum of the matrix’s primary diagonal. Accept the size of the square matrix and elements for each column separated with a space (for every row) as input from the user.

2 3 4
4 5 6
3 4 7

Solution

55. Exercise 55

Write a SARL program to Zip two given lists of lists.

[[1, 3], [5, 7], [9, 11]]
[[2, 4], [6, 8], [10, 12, 14]]

Solution

56. Exercise 56

Write a SARL program to extract specified number of elements from a given list, which follows each other continuously.

Solution

57. Exercise 57

Write a SARL program to compute average of two given lists.

[1, 1, 3, 4, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 4, 5, 7, 8]

Solution

58. Exercise 58

Write a SARL program to count integers in a given mixed list.

Solution

59. Exercise 59

Write a SARL program to remove a specified column from a given nested list.

Solution

60. Exercise 60

Write a SARL program to extract a specified column from a given nested list.

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.