Block 'Max/Min' and list

1010 views
Skip to first unread message

ahs...@gmail.com

unread,
Aug 2, 2014, 5:47:52 PM8/2/14
to mitappinv...@googlegroups.com
Hi guys,

I was trying to calculate max value within a list of numbers.
Can you make it without using a loop?
What I get using 'Max' block is : "The operation max cannot accept the arguments: 81797 18527 13846"

SteveJG

unread,
Aug 2, 2014, 6:06:59 PM8/2/14
to mitappinv...@googlegroups.com
This solution results in a max  81797



What do your blocks look like?   Can you post a png or jpg image please?     Are you trying to put the list of numbers in one of the slots in the max block... you can not do that.



Regards,
Steve

ahs...@gmail.com

unread,
Aug 3, 2014, 7:43:27 AM8/3/14
to mitappinv...@googlegroups.com
That was exactly what I was trying to do: put a list block into max block.
It looks like loop is the only way to do it if list is dynamically changed by user and have a few hundreds of items?

SteveJG

unread,
Aug 3, 2014, 8:32:22 AM8/3/14
to
Here is a tutorial that demonstrates how to sort a list.  It might be possible to find the max using this technique using either a descending or ascending sort.   http://www.appinventorblocks.com/appinventor-tutorials-tips/appinventor-list-sorting-tutorial

Let us know if it works in your application.  Good luck.

Regards,
Steve

Ghica

unread,
Aug 3, 2014, 8:46:08 AM8/3/14
to mitappinv...@googlegroups.com
If you just want to find the max and do not need to have your numbers sorted, it is easiest to go through the items in your list and compare with the largest number found so far, which which you keep in a variable. When at the end, you have found your max. If you want, I can post some blocks later.

ahs...@gmail.com

unread,
Aug 3, 2014, 9:05:46 AM8/3/14
to mitappinv...@googlegroups.com
I don't need to sort the list, only wan't to get max value.
Still waiting for someone who knows no-loop solution :)

spider pig

unread,
Aug 3, 2014, 9:19:43 AM8/3/14
to mitappinv...@googlegroups.com
You can't do it without a loop. Try this

Ghica

unread,
Aug 3, 2014, 9:22:47 AM8/3/14
to mitappinv...@googlegroups.com
A no-loop solution is impossible, because you need to look at every element in the list to see if it is the largest. You can only do that in a loop. A no loop solution means that there would be a built-in block that does the loop for you. Maybe in some future.
Cheers, Ghica.

ahs...@gmail.com

unread,
Aug 3, 2014, 9:35:38 AM8/3/14
to mitappinv...@googlegroups.com
Thanks for explanation Ghica. It's not good news, but at least I know where I am :)

Taifun

unread,
Aug 3, 2014, 12:14:29 PM8/3/14
to mitappinv...@googlegroups.com
as Ghica said
the only alternative solution could be to use some JavaScript, see the following JavaScript solution How to sort a list using the webviewer(!)
Taifun

ahs...@gmail.com

unread,
Aug 3, 2014, 4:03:25 PM8/3/14
to mitappinv...@googlegroups.com
Would JavaScript be more efficient than regular loop? (just to find max value, no sorting needed)

Taifun

unread,
Aug 3, 2014, 4:17:57 PM8/3/14
to mitappinv...@googlegroups.com
the JavaScript solution will be faster for large lists compared to the plain App Inventor solution
Taifun

SteveJG

unread,
Aug 3, 2014, 4:21:15 PM8/3/14
to
All sorting routines use 'loops'.  Replacement sort, bubble sort, Quick sort, Binary Tree sort,  Selection sort, Merge sort whatever algorithm.   Unless you are using an SQL database, you must use loops to sort.  Some 'looping' is more 'efficient' than others.

You may be interested in this explanation of different sorting algorithms in Wikipedia:  http://en.wikipedia.org/wiki/Sorting_algorithm   

JavaScript is a programming language .. it too has loops.

What is faster?   It depends on where the sorting takes place; on the Android, with its minimal cpu or on a server somewhere.

JavaScript is not a sorting algorithm.     Is a Javascript sorting algorithm faster than AI2 blocks?    Selection and Insertion sorts are slow ...what someone recommended here; however a bubble sort in AI2 will be faster.

Try the methods, and report back?    You want a fast sort, don't sort on an Android but use it to access a server.

Regards,
Steve


Taifun

unread,
Aug 3, 2014, 5:26:15 PM8/3/14
to mitappinv...@googlegroups.com
the OP is interested in finding the max value within a list of numbers...
but let's talk about SteveJG's input, which is about sorting:

 Is a Javascript sorting algorithm faster than AI2 blocks?
yes, of course

SteveJG said:
You want a fast sort, don't sort on an Android but use it to access a server.
it's not necessary to access a server
an App Inventor solution which uses some embedded JavaScript (without internet!) will be much faster compared to a plain App Inventor solution

Taifun
PS: I'm ready for a challenge...

Shuan Wallashi

unread,
Aug 3, 2014, 6:55:49 PM8/3/14
to mitappinv...@googlegroups.com
If all the numbers have the same amount of digit and if you also know how many list of numbers you have then it should be possible like in you case. Maybe its just coincidence. However, I wanna share my example...  

I used Clock1, but you can also put the blocks into Screen1.Initialize

Is using a counter inside the Clock1 counted as Loop? If you doesn't mind to have a variable as counter then I think I would be able to make it without knowing the amount of list of numbers and I think i could make it even if all these numbers don't have the same amount of digits... 


SteveJG

unread,
Aug 3, 2014, 7:36:29 PM8/3/14
to
@Taifun

Please do not misconstrue what I said.

 "  Is a Javascript sorting algorithm faster than AI2 blocks?"    That is a question, I do not know the answer?   If you are comparing apple sort with AI2 blocks and an apple sort using  Javascript, one might be faster or slower.   I do not know.    I do know that AI2 number crunching is slow on my tablet.


'I do know that selection and Insertion sorts are slow "' (no matter what the programming language because each element of the list must be accessed)     Someone recommended an insertion or selection sort using AI2 blocks ..".what someone recommended here; however a bubble sort in AI2 will be faster". ...than the Insertion sort.   You got to be careful with sorting, if a list is partially sorted (and not essentially random data), the sort algorithm that would normally win a timing race, will lose), so all sorts are governed by the type of data being "sorted." However, Bubble sorts or Quick sorts are always faster than Insertion/Selection sorts.

"You want a fast sort, don't sort on an Android but use it (the Android) to access a server."   I stand by this statement.  No Android phone is ever going to sort as fast as a Bubble sort or other recognized fast sorting algorithm running on an Intel 7 server.    It is inherently impractical to do number crunching on a phone.  Can a phone do number crunching?  Sure,  Have a large list, then take a nap.   Use an Android phone to 'talk' to a computer; let the computer do the heavy lifting.

:)  

@ ahs .... at the moment I would be tempted to try Shuan's recommendation if loops bother you.

--Steve

Abraham Getzler

unread,
Aug 3, 2014, 9:42:40 PM8/3/14
to mitappinv...@googlegroups.com
Screenshot 2014-08-03 21.34.31.png
max_no_loop.aia
Screenshot 2014-08-03 21.40.12.png

Taifun

unread,
Aug 3, 2014, 9:51:11 PM8/3/14
to
coming back on the solution presented by Shuan: 
this "unusual" solution is a nice example how we could improve it and learn How to work with Lists...
as Shuan already realized, the solution is a special solution which works for 3 numbers all having the same amount of digits, also the Clock component is not necessary, you just could put the different if statements in the Screen.Initialize event to get the same result

Shuan's algorithm is: take a look at the first 2 items of the list and take the maximum, then take a look of that result and compare it with the third item and take the maximum again

so how could we use list blocks instead? see screenshot...:


the solution of spider pig is a good solution, which by the way uses a for each loop and also works for any number of items in the list
great recursive solution by ABG!
Taifun

ahs...@gmail.com

unread,
Aug 4, 2014, 5:07:30 AM8/4/14
to mitappinv...@googlegroups.com
Interesting discussion is going on here :)

Numbers in my list have different number of digits.
I started with the same solution as spider pig showed, and that made me wondering if anything faster is possible - that's why I started this topic.
When 100 (and above) items are put into the list, I can see a lag in the program. I use LG L7 which has two cores I think - is that ok? Or the code which is made in 'not optimal' way could be the reason? (I'm quite a newbie to programming).

Abraham Getzler

unread,
Aug 4, 2014, 10:28:16 AM8/4/14
to mitappinv...@googlegroups.com
I hope you liked my recursion joke.

You might be able to speed things up if your list only grows,
or is rarely updated, by keeping a running maximum separately.

When a new value is added to or updated in the list, compare it to the running
maximum and update the running maximum if it exceeds the old maximum.

When a value is deleted from the list, check it against the maximum. 
If it's the current maximum, you will have to regenerate the
new running maximum value from the list the long way.

Again, this all depends on your update pattern for its speed.

ABG




Taifun

unread,
Aug 4, 2014, 12:37:47 PM8/4/14
to
you can now find my embedded HTML/JavaScript solution here
Taifun


<!doctype html>
<head>
  <meta name="author" content="puravidaapps.com">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title></title>
</head>

<body>
  <script>
    // get the list from the window.AppInventor object, remove the quotes and split at comma
    arr = window.AppInventor.getWebViewString().replace(/"/g,'').split(",");

    // find the max and print it to the title
    window.document.title = Math.max.apply(Math, arr);
  </script>
</body>
</html>

Ghica

unread,
Aug 4, 2014, 3:25:28 PM8/4/14
to mitappinv...@googlegroups.com
With the risk of beating this subject to death, I would like to make a few remarks.
First of all I think you should be careful with asking the questing of whether what way of doing something is faster. If the app just has a small list of which you need the max infrequently, the solution of spider pig is just fine. Or if you like recursion, also fine. In general the rule should be: "make it work, make it right, make it fast". In that order. And good enough is good enough.
If speed is really a problem, then the solutions by Abraham Getzler could help. Because your app becomes less simple to maintain, a JavaScript solution should be a last resort, although I think the solution by Taifun is great. If you have so much data that even JavaScript is not good enough, your data is probably already on a server, or it should be, and you can query it there.

Now, there is another question: are the numbers in you list all positive integers? If there could be negative numbers, or floating point numbers, there are some problems. For example, if the list is empty, and the numbers could be negative, the max would not be zero, but minus infinity. And if the numbers are floating point, the csv string will not work in Europe.
Cheers, Ghica

spider pig

unread,
Aug 4, 2014, 8:31:06 PM8/4/14
to mitappinv...@googlegroups.com
SteveJG wrote:
"You want a fast sort, don't sort on an Android but use it (the Android) to access a server."   I stand by this statement.  No Android phone is ever going to sort as fast as a Bubble sort or other recognized fast sorting algorithm running on an Intel 7 server.    It is inherently impractical to do number crunching on a phone.  Can a phone do number crunching?  Sure,  Have a large list, then take a nap.   Use an Android
phone to 'talk' to a computer; let the computer do the heavy lifting.
 
I can't agree with this. Let's say you have a list of 1 million integer numbers. On a fast phone you can sort that in about 1 second with javascript. Finding the max value in the list takes just a few ms.
If you want to let a server do that you first have to upload the entire list which is at least 4MB. That alone can take much longer than a second.

SteveJG

unread,
Aug 5, 2014, 8:19:20 AM8/5/14
to mitappinv...@googlegroups.com
@ spider pig

"you have a list of 1 million integer numbers. On a fast phone you can sort that in about 1 second with javascript." 

You have actually done that?  With 1 million?   and using AI2?

spider pig

unread,
Aug 6, 2014, 12:27:39 AM8/6/14
to mitappinv...@googlegroups.com
Sure, take a look at this example.
That code first creates a list with 1000000 numbers, shuffles them and then sorts them again.
You can simply copy it into Taifun's example (beween the <script> tags) to run it on your phone.
And AI2's WebViewer can sort even faster than most browsers for some reason.

Taifun

unread,
Aug 6, 2014, 6:43:23 PM8/6/14
to mitappinv...@googlegroups.com
great demonstration spider pig!
@SteveJG: any comment?
Taifun

SteveJG

unread,
Aug 6, 2014, 7:07:40 PM8/6/14
to mitappinv...@googlegroups.com


Taifun

unread,
Aug 6, 2014, 7:09:32 PM8/6/14
to mitappinv...@googlegroups.com
yes, sometimes there is no connection to jsfiddle.net, just try later again
Taifun

SteveJG

unread,
Aug 7, 2014, 8:24:04 AM8/7/14
to mitappinv...@googlegroups.com
Sorry guys, tried many times later-the link never works.

@ Spider .... clever,        "If you want to let a server do that you first have to upload the entire list which is at least 4MB. That alone can take much longer than a second."    I am in the camp with Ghica.    I can see the value in this, just expect using Taifun's solution as part of a mathematical solution in AI2 would be clunky.  Thanks for the introduction to Javascript.

spider pig

unread,
Aug 7, 2014, 10:57:06 AM8/7/14
to mitappinv...@googlegroups.com
That's strange JSFiddle seems to be unstable at the moment.
I uploaded the code to another site http://pastebin.com/5s6cHjr6
But that site has no "run" button so to test it you have to copy it into Taifun's html file. (or into JSFiddle if it works again)
And yes, it does feel a little clunky. Maybe AI2 could get better features for interacting with JavaScript. Maybe a "JavaScript procedure" block?
Reply all
Reply to author
Forward
0 new messages