This tutorial shows how to find the minimum and maximum number from a set of numbers.

Numbers Stored In A List

Enumeration Method

If all the numbers are stored in a list, the basic idea is to use a loop to enumerate over the items. A variable can store the last found item. There are many variations including but not limited to: storing the last index, finding both the minimum and maximum at the same time, and finding multiple minima and maxima.

Example 1 finding the maximum

set [max v] to [-Infinity] // the result will be stored here
set [i v] to [1] // counter
repeat (length of [list v]) // enumerate over every list item
  if <(item (i) of [list v]) > (max)> then // check if the list item is larger
    set [max v] to (item (i) of [list v]) // a larger item was found so update the variable
  end
  change [i v] by [1] // increase counter to go to the next list item
end

Example 2 finding the maximum

set [i v] to [1]
set [max v] to (item (1) of [list v]) // assumes that the maximum will be at least equal to the first item
repeat (length of [list v])
  change [i v] by [1]
  if <(item (i) of [list v]) > (max)> then
    set [max v] to (item (i) of [list v])
  end
end


Note Tip: You can use a custom block set to "run without screen refresh" to make it run instantly.

Sorting Method

If a list is sorted smallest-to-largest the first value would be the minimum and the last value would be the maximum.

(item (1) of [sorted list v]) // minimum
(item (length of [sorted list v]) of [sorted list v]) // maximum
(item (join [last] []) of [sorted list v]) // maximum

Numbers Joined In A String

This section explains how to find a minimum or maximum from a string of numbers separated by semicolons such as 45;-12;6;18.5.

The simplest way conceptually would be to write a script that separates the numbers into list items. Then the method follows identically to Numbers Stored In A List.

To perform the separation, the following can be used:

split string [45;-12;6;18.5] by delimiter [;]
find maximum of list
say (max)

define split string (string) by delimiter (delimiter)
delete all of [list v]
set [substring v] to [] // empty string
set [i v] to [1]
repeat (length of (string)) // enumerate over the string's characters
  if <(letter (i) of (string)) = (delimiter)> then // check if the current character is the delimiter
    add (substring) to [list v]
    set [substring v] to [] // empty string
  else
    set [substring v] to (join (substring) (letter (i) of (string)))
  end
  change [i v] by (1)
end
add (substring) to [list v] // add whatever's left to the list

define find maximum of list
set [max v] to []
set [i v] to [1]
repeat (length of [list v])
  if <(item (i) of [list v]) > (max)> then
    set [max v] to (item (i) of [list v])
  end
change [i v] by [1]
end

Optimized

Alternatively the above can be implemented in one loop, avoiding the need to store the numbers in a list:

find maximum of [45;-12;6;18.5] separated by delimiter [;]
say (max)

define find maximum of (string) separated by delimiter (delimiter)
set [max v] to [] // the result will be stored here
set [substring v] to [] // empty string
set [i v] to [1]
repeat (length of (string)) // enumerate over the string's characters
  if <(letter (i) of (string)) = (delimiter)> then // check if the current character is the delimiter
    if <(substring) > (max)> then // check if the substring is larger
      set [max v] to (substring)
    end
    set [substring v] to [] // empty string
  else
    set [substring v] to (join (substring) (letter (i) of (string)))
  end
  change [i v] by (1)
end
if <(substring) > (max)> then
  set [max v] to (substring)
end

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.