Maximum ermitteln - (Get the Max Number from a List)

Ich habe eine Liste von Textfeldern, die aber alle numerische Werte enthalten, also z.B.
Wie kann ich hieraus die Maximalzahl ermitteln?

Try this:

I get an Upload error when trying (several times) to get the extension.

Bubble sort maybe?
Of course this works only for sorting positive numbers. Otherwise, you must set global max to the first element in your list when you start.
Actually, this is not really a bubble sort, that would bring the numbers in the list in ascending order. But you asked for the maximum, and this simple thing does that.
Cheers, Ghica,

1 Like

Since this is already a List, consider



Returns the largest value of a set of numbers. If there are unplugged sockets in the block, max will also consider 0 in its set of numbers. This block is a mutator and a dropdown


@SteveJG How do you get the list, which is in a csv file or similar, into the sockets?
Cheers, Ghica.

MIT’s documentation implies the max block can accept a List Returns the largest value of a set of numbers. . After a little experimentation it seems a List is not a set of numbers.

MIT’s documentation is not what it says exactly. It “Returns the largest value of a set of numbers.” but not directly from a List because the puzzle pieces will NOT accept a List as input. :frowning:

Here is ABG’s solution:

See the old AI2 Forum

– Steve :cry:

1 Like

Here are the draggable blocks …
max max_test

For fun, try 3 negative numbers.

1 Like

what is index=1 for?

Three more ways to do it!

  1. Using the ListUtils extension
  2. Using a webviewer and the javascript block
    window.AppInventor.setWebViewString(Math.max(...[1, 3, 5]));
  • the 3 dots are important!
  1. Using the Terminal extension running bash
    ar=(1 3 5)\nIFS=$'\n'\necho "${ar[*]}" | sort -nr | head -n1

1 Like

shell_sort2.aia (10.5 KB)

Index = 1 selects the first element in a List. ABG’s Procedure uses a temporary List to populate the blue Math max block since the Math block cannot accept either a List or a csv directly. The list is created using the list from csv row text.

Part of the work in programming is thinking ahead of cases where your code will break.
Here I was thinking ahead to the case where there is only one number in the list, or there are no numbers at all in the list.
The AI2 max() block needs at least 2 sockets (I assume), so what would I compare a single item to? I can't assume 0, because maybe the single item is negative and deserves to be the max because it has no competition. So that's why I start with the first item as the temporary winner of all the max(a,b) matches before finding my next item to match against it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.