Advertisement
3vo

Is a List Sorted

3vo
Nov 21st, 2022 (edited)
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Is a List Sorted?
  3. Write a program that checks if a list is already sorted. For a list to be
  4. sorted, the next element must NOT be smaller than the previous one.
  5.  
  6. Input
  7. On the first line - you will receive a number N.
  8. On the next N lines, you will receive a list of numbers, separated by a comma
  9. Output
  10. There are N lines of output
  11. for each list you receive, print 'true' if sorted or 'false' otherwise.
  12. Constraints
  13. 1 <= N <= 10
  14. 1 <= list.length <= 20
  15. Input
  16. 3
  17. 1,2,3,4,5
  18. 1,2,8,9,9
  19. 1,2,2,3,2
  20. Output
  21. true
  22. true
  23. false
  24.  */
  25. let input = ['3',
  26.     '1,2,3,4,5',
  27.     '1,2,8,9,9',
  28.     '1,2,2,3,2'
  29. ];
  30. let print = this.print || console.log;
  31. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  32.  
  33. let arrNum = +gets();
  34. // console.log(arrNum);
  35. let arr = [];
  36. let result = '';
  37.  
  38. for (let i = 1; i <= arrNum; i++) {
  39.     arr = gets().split(',').map(Number);
  40.  
  41.     for (let j = 0; j <= arr.length - 2; j++) {
  42.         if (arr[j] <= arr[j + 1]) {
  43.             result = "true";
  44.         } else {
  45.             result = "false";
  46.             break;
  47.         }
  48.  
  49.     }
  50.     console.log(result);
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement