2017년 6월 8일 목요일

Sorting list of lists


I have been looking on the web for a way to sort a list of lists in ai2. I have seen an effective way of using javascript to sort a list

http://puravidaapps.com/snippets.php#2sort

but I wondered if anyone knew how to extend this to list of lists?

i.e.
Number  title          age
1           hello          12
2           goodbye    15
3           something  6

this list of lists could be sorted by age in descending

Number  title          age
2           goodbye    15
1           hello          12
3           something  6
--
using the mentioned JavaScript method, you also can sort list of lists
Taifun

Thank you both for your reply. Taifun, this is exactly what I was trying to work out, many thanks. Your website has been very useful to me and I will be donating some money next time I use the code.:)

--

Help Sorting a List of Numbers


I have 4 scores(group of numbers with no commas or decimals) in a list.  I need to sort the list from highest number to lowest number.

Can I sort the list like this?  If so then How?

--
phantomfoot provided links to two great solutions.  Both solutions are in AI Classic code.   If someone translates these sorting algorithms to AI2, PLEASE return here and consider sharing your code.  The AI2 team has a category for Tips and Tricks.  An AI2 version of these sorting routines would be great to post there.   Or, consider writing a tutorial..we are looking for tutorials too.

--
Here ya go...
This is the same sorting method (optimized bubble sort) that I use in my Flood fill project.
It was faster than the insertion sort that I tested.
Why is this faster than the standard bubble sort?
With bubble sort, the entire list is scanned each time until there are no swaps.
This optimized bubble sort keeps track of were a swap last occurred and sets that as the new end of the list!
So if the last swap was in the middle of the list, only one-half of the list  will be scanned after that.
--
Very well done Scott! Genius

--
Thanks, but I cannot take credit for the algorithm optimization.
I found it on the Internet because some nice person was willing to share it :)
I don't remember where I found it tho....

--
It is possible to do these sorting algorithms in the same screen?

I would like to do the calculate in the same screen!

In other words using a "call procedure"

--
Sure. Just put the sort procedure on the same screen and call it with the list attached.

--
Thank you Scott...

The apk is working very very well!! and in the same screen!!!!  :)

Genius!

--
Great Romulo... and nice work Scott ;)!

--

Trying to create flipbook


I have created a modified Paint Pot app with a TinyDB component.  My goal is to be able to make a flipbook out of the pictures I create and save, where the drawings that have been created (that I have labeled based on a "pages" variable) can play in order.  I am stuck trying to save the pictures sequentially, and then playing them.  

--
what have you tried? when you say that you are stuck, is there any particular step that is not working as you thought it would?

--
one way is to number the database tags sequentially and call the images by number?   

If you need to sort the images before displaying, there was an algorithm showing how to sort numbers   called Help Sorting a List of Numbers  posted a few days ago.  Help Sorting a List of Numbers        The code might help you 
sort your page numbers, but only if they are numbers    and not  in the form Page 1 , Page2 etc.     Even in that instance, they could be sorted by using a Text block to remove the word Page.

Is this the hint you need to continue with your project?

--
Doesn't the Save Canvas block accept an input string with the file name where you want the save?
You can build up a file name with a number at the end, if you remember to pad the number with leading zeroes.
Use the Join block.

To pad with leading zeroes, add 1000000 to your number, assign it into a string variable, and cut off the leading "1"
using a segment block.


--

Calling specific variables from specific columns in an CSV file and displaying


I am creating a multiplication game for my students. I have a CSV file (see attachment) with all of the multiplication facts that I want my students to practice. Column One of the file has the first multiple. Column Two contains the second multiple. Column Three contains the product. I would like the game to select a random row from the CSV list then display an image in a specific place on the screen for each variable in the row (see screenshot of game screen). The students will fling the multiplication sign at the multiples (when both are hit then the multiplication sign disappears) then fling the equals sign at the product.

I have the programming for the 'game' done. (see screenshot)


But I do not know how to:
1. Import the CSV file 
2. Which blocks to use to call up the variables
3. Attached the variables to image files
4. Put the images in specific places on the screen (see screenshot)

I have searched the forums and online for help and found nothing for my specific needs. Any help is appreciated.




--
The answers to most of these questions could be found either in the many tutorials or by using the Search for messages block at the top of this page.  Part of the difficulty in finding appropriate material might be in not knowing the terminology to search.   Here is some help:

1. Import the CSV file   What you show is not a csv file; it is a spreadsheet with the values you want to use to create a comma separated value file.  Some spreadsheets have the ability to export these values to a csv text file.  Export the file and give it a name.   Numbers.txt or Numbers.csv; App Inventor will not care.  Here you have several possible routes.  The simplest is to just add the Numbers.csv to your Media.   This works great if Numbers.csv is the only resource file the app will ever need.  If you need to update the file occasionally, you need to use another method.  There are several other ways, all more complicated in which you would use the File control.

2. Which blocks to use to call up the variables .  Your app your option.  To effectively use the numbers.csv, the app should convert the csv to a List to make the information available to your app. Create a List of csv values.  Hint: you  need the list from csv table   or list from csv row block .  You will benefit most from this structure if you create your csv with \n control characters as the final 'character' in your csv row so your csv can be loaded as  a table.   Obviously there is a lot to do here so look in the Forum for Lists by Saj  and 

Once you have the List, you can select values from any of the columns using two 
select list item list index   
where the index in one of the blocks will be 'your column. and the other block will be the row.

3. Attached the variables to image files  .  This probably can be done lots of ways.  It depends on how you set up your list, the image files (which you will probably place and upload to Media)   and other factors.   You have several two digit numbers in your csv .. think about this a bit because you might need an image of each number shown.  That is quite a few images and that you will assign to perhaps four or more  ImageSprites that will be either the factors or the answer.


4. Put the images in specific places on the screen (see screenshot)  
describes how the Canvas is constructed and how to place/move objects if you use a Canvas. You can place ImageSprite using Canvas screen coordinadtes.  Then you perhaps place the images in ImageSprites  If you do not use a canvas you could use a Table arrangement (layout) to use the Image controls. 

Others may have other suggestions.  You will have to do all of these things.  If I were doing this, I would create several discrete mini projects .. to learn each of the four 'skills'
and to test in an uncluttered environment.  Save copies.. and perhaps aia files.    Your app is complicated and soon to become more complicated

In the arcade game Whac-a-Mole TM , a "mole" pops up at random positions on a playing field, and the user score points by hitting the mole with a mallet. This is a similar game that uses the touchscreen. This tutorial introduces: image sprites, timers, and procedures.   pOr MoleMash 2?
If you get into difficulty, ask another question.  It is very difficult to answer multiple questions or give advice to lots of different activities.

--
Thank you Steve. I have read through those materials and have also gone through the relevant tutorials. The specific information that I need regarding the CSV (I understand my pic was from excel. My actual CSV has comma separated values.) How do I call up a specific variable from a specific row and column using the app inventor 2 blocks? My head is just swimming. I have created mini projects based on the tutorials but none of the tutorials I have found pull an entire row of variables and place them in specific areas.

I am very new to App inventor and programming in general. Specific step by step would be helpful.

Also, regarding the pics. I was considering placing  "number_pic_name.png" (referencing the specific number) in for each value in my CSV. Do you think this is a good way to call up the image variables for he multiples and products. (Yes there are many number file pics that will be needed, do you think this will be a loading problem?)


--
third row second column  of the table list called numberList   yields 3

Now you do it with the csv you saved.

--
Steve....excellent! Thank you! I understand your example. I have 2 further questions.
1. Do I have to list all of my multiple/product facts in the global variable text box or is there a way to just call up my CSV file?

2. Do you think that listing my multiple/product facts with their respective .png instead of just raw numbers would be a better way to call up the images needed?

--
1) You do not have to have to place the contents of the csv you generated from your Excel spreadsheet in a text box as shown in the example.  Using the text box is a simple way to define a csv for an example.  Consider uploading your csv into Media.  Once the csv is in Media , populate the  global csv with the csv file using the File control (instead of using a text box) ..something like shown here where the csv is called fileCSVSpreadsheet.csv .
You might want to read about the File control http://ai2.appinventor.mit.edu/reference/components/storage.html#File  

The code in button 6 calls the content of a random row in the numberList and then parses the row to produce the text.  It might be useful to you. Shown, just to show how a user can manipulate the csv data once the data is placed in a List.

2)  What you say is possible and would work.  1.png, 2.png, 3.png  etc. You might need to put that image names in "  ", like "1.png", "2.png", "3.png."  I would run a test case Project using a few images loaded to Media and an Image control. You could also just use  1,2,3 and 'add' the png suffix when you want to assign an image using a join block.

--
Finallly had a few minutes to work on this. AND I got it to work using your example. Thank you so much. I really appreciate your patience. Now to work on getting the images to be called from the CSV file and incorporate the game coding! Thank you again. I am really looking forward to using this app with my students. :)

--

How does AI handle strings?


I am trying to implement an algorithm my class came up with after learning about the ceasar cipher. It seemed so simple, but now I am bogged down in AI and can't quite figure out how to handle strings. We have a variable that holds the alphabet, we ask the user for a word to encrypt and an offset number. All we want to do is find the index of each letter in the word to be encrypted and add the offset to it, then find the letter at that index.

Does anyone have a simple explanation or sample code that finds the index of a single letter from one string in another string, then finds the letter at a particular index?

--
Make a list of the 25 letters of the alphabet in order.  Call it Alphabet.
Suppose you want to shift by 4.
Then, given a letter find the index of the letter in Alphabet.
Add 4 tindex, and find the element of Alphabet at that incremented index.

You'll ned to deal with the case where the incremented index is greater than 26. (HInt:  Use modulo).

The basic thing is that you want to use a list, not a string, to represent the alphabet.

--
OK, I can do that. Do I then also have to make the word they gave me into a list so I can process each letter?

--
You can do that -- convert the word to a list of characters -- but you don't need to.   You can just use the text Segment commnd to extract each character as you need it.

--
I think you can also do Ceasar cipher using just Text and string operators -- i.e., no need to convert to lists.  Basically you make a Text variable containing "abcdefghijklmnopqrstuvwxyz".  Then you split it at the Caesar shift and join the two segments to create the caesaralphabet.  For example, for a shift of 3, you would get    "defghijklmnopqrstuvewzy + "abc"   as the Caesar alphabet.  Here's a picture.  To encrypt, you find the index of the plaintext letter in the alphabet (using the startsat(alphabet, letter)  block) and take the letter at that index from the caesaralphabet.  
Inline image 1

--

Doubt beginner - button triggers another button


My project has 4 buttons of motion (F = FRONT, RIGHT = D, E = LEFT, Behind = T)
and 1 push button (START = S).
sasa

Every time I click on move buttons, I want it to store the button was clicked,

So just when I click the START button, clicked run the commands and send the text (F, T, D, E, F, D) commands to the Arduino by Bluetooth

who knows how to mount these Blocks?

--
I am not sure if I understood you. In case you want to send a command through bluetooth to your Arduino then you have to use the "SendText" Block. Lets assume your Arduino code says if you press the letter "F" in terminal than an LED goes on. Well, all you need is to write the letter "F" in a text and attach it to the SendText Block. Because I am not the best person in describing, I found this video for you :)

--
Concerning the bluetooth I already could communicate with the Arduino,

My doubt is to mount a variable in blocks,
what I need is:

Ex:. variavelLista []

Click Btn Front = var list updates from [] to ["F"]
Right click Btn = var list for updates. . ["F" "R"]
Btn Left Click = var list for updates. . ["F", "R", "L"]
Click Btn Front = var list for updates. . ["F", "R", "G", "F"]

and

When I click on the Btn Start, take the "Variable List" = ["F", "R", "G", "F"] and send it by bluetooth.

I made this way, but is not correct



--
Sorry, as much as i want to help you, i dnt know how. But at least I think I understand your Problem now. I assume you dont want to send the variable like "FRGF". Since its for Arduino, you want to send them separate. In case you can also send it as text "FRGF" than you can use the JOIN block to join the letters together into a label and then send the label.text to Arduino, I dont explain it in detail since I am sure you want to send it separate, don't you?

In your code you have this IF condtion, if "btn.enable". Aren't they already enabled? If not, how can you use them? and I think it should be "if btn.enable = true" etc. However, In your code there is a missing part. seems you want to call your List. Look my picture to know how to finish this part. Then again I was wondering why do you want to call the List if you dont use it at all as we can see in your if conditions.

I would call the list and start from the first Index number of the list. The BluetoothClient1 should send the content of the first index number. I would use a clock to tell the app to send after half a second the content of the second index number etc. You do not need If conditions at all.

I hope I could at least help you a little bit or maybe I gave you a hint else just wait for the Admins, they are perfect in understanding problems and helping :)


--
You want to do this?

Ex:. variavelLista []

Click Btn Front = var list updates from [] to ["F"]
Right click Btn = var list for updates. . ["F" "R"]
Btn Left Click = var list for updates. . ["F", "R", "L"]
Click Btn Front = var list for updates. . ["F", "R", "G", "F"] 

The simple way is to have four separate lists.
Ex:. variavelLista []

List #1      [] to ["F"]
List#2      ["F" "R"]
List#3 . ["F", "R", "L"]
List#4   ["F", "R", "G", "F"] 

You are going to have to set the lists to empty,
the set List1 to      set List2 to   

and select the lists with the four buttons   OR    use a ListPicker  .
This is a suggestion of an easier way to manage your lists...I did not look at your code..but hope this hint may help you.

And, you might be able to do this without using lists at all since these characters do not change so each can be a global variable.

That is part of it.   What you are attempting to do is to create a set of programmed commands, and then save them and then use the saved commands to command the robot.   You will have to have some mechanism to  program a pause between commands ... listing duration of the pause because   if you program   F R F R  probably nothing will happen...you will have to program  F  pause for so many second then do R and pause etc.   Is that true?

--
If anyone needs to use the same programming as below is the way I got it done.

When I click on any command I save the list and click START I send the commands that were clicked by bluetooth.

--
if you want to delete a list you also can use:
set global lista to create empty list

--

Multiple actions variable by text box?


So I'm tweaking an app that I've built and one of my friends pointed out that one of the buttons could do with having a variable box next to it so instead of pressing the button 20 times I could just type 20 and it would have the same effect, does anyone have any idea how to do this, I've got it all laid out on screen but can't seem to get my head around the blocks?

--
Use a TextBox.    In the Designer screen, where it indicates Properties,  find the NumbersOnly check box.   Check it, the text box will only accept numbers.

Then,   if you want the 20 in a Label ;    set Label1.text to TextBox1.Text     within the Button event handler.

Try some blocks, show a png or jpg screen capture image if you run into difficulty and ask again.

Does that work or do you want to do something different?

--
Hi There, I'll try and explain a bit more as I'm not sure I was very clear on my last post, 

My app looks something like this:

"Top Button

Bottom Button          Text Box"

The idea is that when I press the top button it sends a message but if I enter a number between 2 and 20 into the text box and press the bottom button it would complete the top buttons action multiplied by the number in the text box. The text box is set to numbers only

I also would like to restrict the number you can put into the text box to prevent people from crashing my app and there phones etc...

I cant help feeling I've almost got it but for the life of me I'm missing something!

--
The forum members can not help you more unless you share an image of your blocks so far.

Restrict numbers.?.. use a if else block      with conditionals where if number < 2 or number > 20    provide an error message else proceed.

This code is possibly similar to what you want to do.  Yes, it not exactly what you want but it should provide you a hint about what you should do.

Multiplication Table
The ∖n character in a label makes a line change.     You probably will not need that.   

If you want to do something different and want specific advice, the forum members need to see a screen capture of what you have so far .
Try some blocks and ask again.

--
Nick... Create a procedure to do what Button1 does, but with an input value.  Then call that procedure from button1 with "1" as the input.

In button2.click, call the same procedure with the value of the text box as the input.  In that prodecure, you can check to see if the text box value is greater than 20...

--
Ok I'm still struggling a little here but have got a screen shot of what I have so far,  Enis from what you're saying it seems to make the most sense but I'm not sure how to do that? (i.e. give an input value for the procedure)

anyway, here's what I have so far, hope you can help




--
Ok I tried what you said Enis but think I may have gotten something wrong (as it still only sends 1 message)

Here's what I've done 



--
Nick... post a screenshot please of the entire function so I can see what you're joining.

--
Actually, that's not necessary... Yes, it only sends one... In order to send multiples, you'd have to have a loop in the proc that goes x times (whatever's in the text box).  Use a for each, with the count being the x of the proc...

--
The Joins are just 2 lines of text 1 is the random message and the other the footer for the message, they could in fact be put into one box, I was just being lazy and didn't want to type the same footer over and over again so did the join to save time

--
yeah that's where I'm struggling, the message is random each time, so In theory I would like someone to be able te enter the number 4 in the text box and the proc to loop 4 times with hopefully 4 different messages going out to the same recipient. I just cant quite work it out, 

--
The message should still be random... In the beginning of the proc, start with the for each block.  Then, inside of that, do all of your other stuff... It should be random.  BUT, if you're allowing 20 and you have 16 entries, there will be replications.  That's also possible if you're trying to get 20 from 40, or even 100.  IT would take an awful lot of random thoughts to almost guarantee generating a different one for 20 tries.

--
I managed to get it to work after a couple of continuous loops and a couple of false starts so thank you very much for your help, now any idea on how I can get people to login with facebook? I have a feeling this one is going to be a lot more difficult! 

--

Changing hint box values using variables


Hello, I have read most of the chapters in the book and have learned much from it. I need to review. A lightbulb is going on in my head but flickering.
I just have not connected the dots yet.

Anyway what I am trying to to is change the text of the hint in the text box using variables.
I have a "Spinner list" with four items and a TxtBoxHint list.
What I want to happen is if I select, say item 2 from the Spinner, the hint from TxtBoxHint will update accordingly..
Thanks for any guidance you can provide
--
Here is an example of button click.
Do it individually for each sprite just like you would a button.  I like to use labels myself, as they cannot be typed into by the user, but maybe you want that.

--
Here is an example of changing a label. Attached aia file for you.


--
The only reason I did not want to do it that way is the user will be selecting spinner options more than once. I figured i could save time and hassle by having it input the correct texthint based on what spinner option they choose. 

Thanks, I'll keep it in mind if i don't  find another way

--
use the Textbox.Hint property and set it to the corresponding item from list TxtBoxHint using the Spinner.SelectionIndex

--
I'll give it a try, Thanks

--
Nice, thanks Taifun!  I just learned something new, yay!  I was thinking the hint and text were the same, but I see that they are not now.  And also there are no hints for a label I found out as I assumed there were, regardless I see the difference betwixt..

--
Ugh...Do you happen to have a photo of these blocks set up?

--
How about you let us see your aia file?  If not post your sprite blocks.

--
I'll get them up

--
The top set of blocks just puts the actual number as the hint.
The bottom set I had to use the text box to get it input what i wanted into the text hint field
Not sure how to call the variable so when I select fruit, grapes or whatever shows as the hint

Thanks for any help. Taifu apparently has a solution but am confused how to set it up


--
I figured it out .I think. Need to test but seems to be working

--