Sorting numbers by mathematicals properties

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Sorting numbers by mathematicals properties

Post by matgarage » Sat Apr 20, 2013 11:59 am

Hello everyone,

So I have the project to make a stack that can sort a list of products quantities by their properties to make groups by mathematical similarity (equal, multiple, common divider,...)
I get a list of products in a datagrid
1 2500
2 3500
3 5000
4 7500
5 4000
6 3000
7 4000
8 3500

I would like to obtain :
EQUAL
into field Group1 : line1
into field Group2 : line6
into field Group3 : line2, line8
into field Group4 : line7
....
Common dividor
into field Group500 : line1,line2,line3,line4,line5,line6,line7,line8
into field Group1000 : line3,line5,line6,line7
into field Group1500 : line4,line6
....
Multiple
into field Group2500 : line1,line3,line4
into field GroupNone : Line2,line5,line6 .....

I'm not asking for an all code done answer but I really need to be helped to find stategy do to that.

Coudl someone help ?

Thank You

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jmburnod » Sat Apr 20, 2013 4:56 pm

Hi matgarage,
I suggest you write a function for each category to make lists and work around it
I'm not asking for an all code done answer but I really need to be helped to find stategy do to that
Sorry a small script is easier for me than english explanations
Something like this one to get the list of divisors (Number,div1,div2...)

Code: Select all

on mouseUp
   put the milliseconds into tDepTime
   put empty into tListDivisorNumber
   put "2500,3500,5000,7500,4000,3000,4000,3500" into tMynumber
   repeat for each items tOneNumber in  tMynumber
      put tOneNumber & "," &MyDivisor(tOneNumber) & cr after tListDivisorNumber
   end repeat
   delete char -1 of tListDivisorNumber
   put  the milliseconds - tDepTime & cr & tListDivisorNumber
end mouseUp

function MyDivisor pNumber
   put empty into tMyDivisor
   repeat with i = 1 to 2500
      put pNumber/ i into tResDiv
      if "." is in tResDiv then next repeat
      put i & "," after tMyDivisor
   end repeat
   delete char - 1 of tMyDivisor
   return tMyDivisor
end MyDivisor
Best regards
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting numbers by mathematicals properties

Post by dunbarx » Sat Apr 20, 2013 7:31 pm

Hi.

What Jean-Marc said.

This is a great way to both learn LiveCode, and to strengthen your coding skills. These are not the same. For example, what would your strategy be to find the terms that are "equal"? I first think of sorting them numerically. That puts the ones that are the same together in a list. Then can you think of a way to parse them out? How about a repeat loop that compares one line to its neighbor(s)?

That sort of thing. If you are good, you will make such a gadget, then throw it away because you made a better one.

Fun.

Craig Newman

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Sun Apr 21, 2013 11:26 am

Hi Jean-Marc and Craig,
jmburnod wrote: Sorry a small script is easier for me than english explanations
Something like this one to get the list of divisors (Number,div1,div2...)

Code: Select all

      put tOneNumber & "," &MyDivisor(tOneNumber) & cr after tListDivisorNumber
  
Thank you for the code. Your right, a piece of code is more clear than words for me too.

But I get problem to understand the line quoted right up.
What is the mean of "MyDividor(tOneNumber)"
The function is MyDividor but what about the variable into () ?

We could speak in french by PM if it's possible...

Thanks again.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jmburnod » Sun Apr 21, 2013 12:10 pm

Hi matgarage,
Sorry I'm wrong :oops: , this one is better

Code: Select all

on mouseUp
   put the milliseconds into tDepTime
   put empty into tListDivisorNumber
   put "2500,3500,5000,7500,4000,3000,4000,3500" into tMynumber
   repeat for each items tOneNumber in  tMynumber
      put tOneNumber & "," &MyDivisor(tOneNumber) & cr after tListDivisorNumber
   end repeat
   delete char -1 of tListDivisorNumber
   put  the milliseconds - tDepTime & cr & tListDivisorNumber
end mouseUp

function MyDivisor pNumber
   put empty into tMyDivisor
   repeat with i = 1 to pNumber
      put pNumber/ i into tResDiv
      if "." is in tResDiv then next repeat
      put i & "," after tMyDivisor
   end repeat
   delete char - 1 of tMyDivisor
   return tMyDivisor
end MyDivisor
We could speak in french by PM if it's possible...
Only if that is necessary. I prefer english in this forum.
What is the mean of "MyDividor(tOneNumber)"
tOneNumber is the target number
Here is the result of scripts
targetnumber, div1,div2,div3 etc….
put MyDivisor(2500) = "2500,1,2,4,5,10,20,25,50,100,125,250,500,625,1250,2500"
Best regards
Jean-Marc
https://alternatic.ch

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Sun Apr 21, 2013 5:30 pm

dunbarx wrote:Hi.
For example, what would your strategy be to find the terms that are "equal"? I first think of sorting them numerically. That puts the ones that are the same together in a list. Then can you think of a way to parse them out? How about a repeat loop that compares one line to its neighbor(s)?

Craig Newman
Hi Craig,
Thank you for your interest in my post.

I've did exactly what you describe. Create a datagrid with my products quantities and a button with the sort function .
Then I think I need to ask if quantity of line 1 col 2 of my datagrid is equal to line 2 col 2
then put in equalList1
and repeat with the others lines by creating new equalList for each value.

My first problem is to write this with the correct syntax for code. I'm not already familiar with that and I'm trying to learn with existing example in revrun ressources, tutorials... and this fist post on the forum.

When I said that I'm not asking for an all done code, it was because I realy want to learn how to do.

I think coding require logicals approaches of the problem. For that I need to make link between my goal, the way to reach that point, ressources and tools avaible in livecode and the wau to put in together.

So I need help to give me tips and shortcuts more targeted than what I can find by myself.

Thank you again

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Sun Apr 21, 2013 7:37 pm

jmburnod wrote:Hi matgarage,

tOneNumber is the target number
Here is the result of scripts
targetnumber, div1,div2,div3 etc….
put MyDivisor(2500) = "2500,1,2,4,5,10,20,25,50,100,125,250,500,625,1250,2500"
Best regards
Jean-Marc
Ok it's more clear for me now.

But what about the use of milliseconds function ?
I think i need already an explanation of loop funtion like in:

function MyDivisor pNumber
put empty into tMyDivisor
repeat with i = 1 to pNumber
put pNumber/ i into tResDiv

As I understand you create a function MyDivisor and a variable (asociated?) pNumper
You put empty into the tMydivisor new variable
Here you create a loop (array?) where the variable i = 1 to pNumber
and you put pNumber divided by the variable i into the variable tResDiv

So I can't understand what is the value of pNumber.
Did I make a mistake? Could you explain me that ?

Thank you it's fun

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting numbers by mathematicals properties

Post by dunbarx » Mon Apr 22, 2013 1:50 am

You are well on your way. Can you find what is just a little wrong with this script to find the lines of equal value?

Code: Select all

on mouseUp
   sort yourData numeric
   repeat with y = 1 to the number of lines of yourData
      if line y of yourData = line (y + 1) of yourData then put y & "," & (y + 1) & "," after equalList
   end repeat
   answer equalList
end mouseUp
Assumes, of course, you have your data in "yourData".

Craig Newman

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Mon Apr 22, 2013 12:15 pm

dunbarx wrote:You are well on your way. Can you find what is just a little wrong with this script to find the lines of equal value?

Code: Select all

on mouseUp
   sort yourData numeric
   repeat with y = 1 to the number of lines of yourData
      if line y of yourData = line (y + 1) of yourData then put y & "," & (y + 1) & "," after equalList
   end repeat
   answer equalList
end mouseUp
Assumes, of course, you have your data in "yourData".

Craig Newman
Ok.
A good sleep this night give me solution. If I well understand in your first script, pNumber is the value associated with the function Mydivisor when it's called in the on mouseUp part of the script, into () so it's tOneNumber. Right ?

But for your second script, it look good, I can't find what is wrong.
Let me some time, I will search tonight...

Thank you :)

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jmburnod » Mon Apr 22, 2013 12:52 pm

Hi matgarage,
A good sleep this night give me solution. If I well understand in your first script, pNumber is the value associated with the function Mydivisor when it's called in the on mouseUp part of the script, into () so it's tOneNumber. Right ?
YES
Best
https://alternatic.ch

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Mon Apr 22, 2013 9:29 pm

dunbarx wrote:

Code: Select all

on mouseUp
   sort yourData numeric
   repeat with y = 1 to the number of lines of yourData
      if line y of yourData = line (y + 1) of yourData then put y & "," & (y + 1) & "," after equalList
   end repeat
   answer equalList
end mouseUp
Craig Newman
Hi,

This code sort the line yourDatat by their value, but for the equality it answer the numeric name of the equal lines , without sorting by equal unique quantity.
I'm right ?
I need to create an equalList by equal values ? equalList(n) with n=1 to the number of unique equal values ( + 1 for the non equal) ?

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Tue Apr 23, 2013 6:31 pm

Hi
I think I make a mistake in my first analysis.
First the script sort item by the numeric function in yourData.
Does it mean that each item (line)is just numbered by their order in the field? Or sorted and numbered ?
Now for the loop, I need to understand this concept.
y = 1 to the number of item in the field
y + 1 : does it mean item1+1=item2, item2+1 = item3, ,...
If so, the code look only for the neighbors numbers, not for all the list.

Wrong, right ?

Thank you

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jmburnod » Tue Apr 23, 2013 7:31 pm

Hi matgarage,
Sorry I haven't time to answer to your ask about Craig's script
I found an other way to get a list of equal numbers
Here is:

Code: Select all

on mouseUp
   put "2500,3500,5000,7500,4000,3000,4000,3500" into yourData
   replace "," with cr in yourData
   repeat for each line tNumber in yourData
      put tNumber & "," after line tNumber of equalList
      end repeat
   filter equalList without empty
    put equalList
end mouseUp
Best regards
Jean-Marc
Last edited by jmburnod on Tue Apr 23, 2013 7:50 pm, edited 1 time in total.
https://alternatic.ch

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Tue Apr 23, 2013 7:38 pm

Hi Jean-Marc,

I will take a look at your new script shortly.

Thank you for your time spent on my question. Your help is usefull for me.

Best

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Tue Apr 23, 2013 9:14 pm

Ok Jean-Marc,

When tNumber don't find equal tNumber it return empty and then the value ?

"Filter without empty" mean "remove empty" ?

I realy appreciate your help

Best

Matthieu

Post Reply