wilbert wrote: Mon May 29, 2023 9:36 am
Can you explain why it can't be bigger than 10 ?
It looks to me if all 100 items of the vet1 or vet2 array would be equal, it would be > 10.
If you are absolutely sure of that limit of 10, you can count how many each value from 1 to 10 occurs and use those counts to output the result.
Yes, you really understood my procedure.
I generated vet1() and vet2() randomly in the example allowing more than "'10" occurrences to exist.
But this was the way I showed the routine working only, the generation of vet1(1) and vet2() is controlled and does not allow more than 10 equal occurrences, they represent samplings of statistical tables.
This created key "111122334" or "000011223" will enter a "hashtable" like this:
Code: Select all
Global NewMap Example.i()
...
a1=Today(@String1,18,1): Example(a1)+1
...
A lot of information is consolidated using the created key.
wilbert wrote: Mon May 29, 2023 9:36 amFor example if dr(1 to 10) contains the values 1-4-3-1-2-0-1-1-3-2, counting will result in 4x1, 2x2, 2x3 and 1x4.
Those counts can then be used to output the result 111122334 (or 000011223 if you use 0-9 characters).
That will be faster compared to sorting.
At this point you gave me the idea of not using the SortArray command and doing a different count, see:Code: Select all
i=0
!mov r13, 10
!l_loop2a:
!add qword [p.v_i], 1
dr(11+dr(i))+1: If dr(i)>j: j=dr(i): EndIf
!sub r13, 1
!jnz l_loop2a
aux="": For i=1 To j : aux+Str(dr(11+i)): Next
ProcedureReturn LSet(aux,10,"z")
The generated key remains unique and works for my system.
The "z" can be any character ... "0" or "x" .... but they need to exist to keep the answer key 10 characters long.
With this idea I doubled the speed again, see how the processing times are now:
( In 2011 ) result: 122222223 and 111122334 in 210 ms
( Today ) result: 122222223 and 111122334 in 102 ms
( New idea ) result: 171zzzzzzz and 4221zzzzzz in 48 ms
"171zzzzzzz" it is the same as "122222223" and "4221zzzzzz" it is the same as "111122334"
see the procedure test code:
Code: Select all
DisableDebugger
Global Dim vet1.i(100): Global Dim vet2.i(100)
Define.i i,j
Define.s a1,a2
RandomSeed(0)
For i=0 To 100: vet1(i)=Random(10,1): vet2(i)=Random(10,1): Next
Procedure.s Year2011(*String1,qt.i,flag.i) ; String1 from 00 to 99 with spaces | qt = 2 to 50 = len(String1+1)/3 | flag= 1 in vet1() or 0 in vet2()
Protected Dim dr.i(21)
Protected.i i,j
Protected.s aux
For i=1 To qt
aux=PeekS(*String1+(i-1)*6,2): If flag: dr(vet1(Val(aux)))+1: Else: dr(vet2(Val(aux)))+1: EndIf
Next
For i=1 To 10: If dr(i)<>0: dr(11+j)=dr(i):j+1: EndIf: Next
SortArray(dr(),#PB_Sort_Ascending,11,10+j)
aux="": For i=1 To j: aux+Str(dr(10+i)): Next
ProcedureReturn aux
EndProcedure
Structure StringElement
StructureUnion
c.c[2]
n.l
EndStructureUnion
spacing.c
EndStructure
Procedure.s Today(*cval.StringElement,qt.i,i2.i)
Protected Dim dr.i(21)
Protected.i i,j,n
Protected.s aux
!mov r11, [p.a_dr]
!mov r8, [a_vet1]
!mov r9, [a_vet2]
!mov r13, [p.v_qt]
!l_loop1:
n = (*cval\c[0] * 10 + *cval\c[1] - 528)*8
!mov r10,[p.v_n]
If i2
!mov r14, [r8 + r10]
!add qword [r11 + r14 * 8], 1
Else
!mov r14, [r9 + r10]
!add qword [r11 + r14 * 8], 1
EndIf
*cval + 6
!sub r13, 1
!jnz l_loop1
i=0
!mov r13, 10
!l_loop2:
!add qword [p.v_i], 1
If dr(i)<>0: dr(11+j)=dr(i):j+1: EndIf
!sub r13, 1
!jnz l_loop2
SortArray(dr(),#PB_Sort_Ascending,11,10+j)
aux="": For i=1 To j: aux+Str(dr(10+i)): Next
ProcedureReturn aux
EndProcedure
Procedure.s NewIdea(*cval.StringElement,qt.i,i2.i)
Protected Dim dr.i(21)
Protected.i i,j,n
Protected.s aux
!mov r11, [p.a_dr]
!mov r8, [a_vet1]
!mov r9, [a_vet2]
!mov r13, [p.v_qt]
!l_loop1a:
n = (*cval\c[0] * 10 + *cval\c[1] - 528)*8
!mov r10,[p.v_n]
If i2
!mov r14, [r8 + r10]
!add qword [r11 + r14 * 8], 1
Else
!mov r14, [r9 + r10]
!add qword [r11 + r14 * 8], 1
EndIf
*cval + 6
!sub r13, 1
!jnz l_loop1a
i=0
!mov r13, 10
!l_loop2a:
!add qword [p.v_i], 1
dr(11+dr(i))+1: If dr(i)>j: j=dr(i): EndIf
!sub r13, 1
!jnz l_loop2a
aux="": For i=1 To j : aux+Str(dr(11+i)): Next
ProcedureReturn LSet(aux,10,"z")
EndProcedure
String1.s="15 12 18 87 95 10 13 21 14 88 80 77 51 53 43 26 81 00 11"
t1.i = ElapsedMilliseconds()
!mov r15, 50000
!l_again:
a1=Year2011(@String1,18,1)
a2=Year2011(@String1,18,0)
!sub r15, 1
!jnz l_again
Result$+"( In 2011 ) result: "+a1+" and "+a2+" in "+StrF((ElapsedMilliseconds()-t1),0)+" ms"+Chr(13)
t1.i = ElapsedMilliseconds()
!mov r15, 50000
!l_again1:
a1=Today(@String1,18,1)
a2=Today(@String1,18,0)
!sub r15, 1
!jnz l_again1
Result$+"( Today ) result: "+a1+" and "+a2+" in "+StrF((ElapsedMilliseconds()-t1),0)+" ms"+Chr(13)
t1.i = ElapsedMilliseconds()
!mov r15, 50000
!l_again2:
a1=NewIdea(@String1,18,1)
a2=NewIdea(@String1,18,0)
!sub r15, 1
!jnz l_again2
Result$+"( New idea ) result: "+a1+" and "+a2+" in "+StrF((ElapsedMilliseconds()-t1),0)+" ms"+Chr(13)
EnableDebugger
Debug Result$