Τετάρτη 10 Μαΐου 2017

Αναθεώρηση 19 (Έκδοση 8.7)

Σε αυτήν την αναθεώρηση έφτιαξα την Κατάσταση (Inventory) να μπορεί να αλλάζει σε νέα (αλλάζουμε δείκτη προς κατάσταση), με χρήση της εντολής Κατάσταση σε υπάρχουσα κατάσταση.
Επίσης την Καθαρό (Clear) η οποία σε Καταστάσεις καθαρίζει την λίστα, και μπορεί να αφαιρεί αναφορές από το συλλέκτη σκουπιδιών.


Παρακάτω είναι ένα πρόγραμμα με Αγγλικές εντολές (θα γραφτεί και στα Ελληνικά, σε άλλη ανάρτηση).
Έχουμε έναν αριθμό εργαζομένων και μια ομάδα ζευγών που μας λένει ποιός δεν μπορεί να είναι μαζί με κάποιον άλλο. Ζητάμε να επιστραφεί ένα μέρος της τελικής λίστα. Το πρόγραμμα είναι προχωρημένο.

Ανανέωσα το παράδειγμα (500 υπάλληλοι, 150 επιλεγμένοι) τρέχει σε 2.2 δευτερόλεπτα (ο χρόνος στην οθόνη είναι σε χιλιοστά)






Form 80,50
\\ this is module A
\\ Use of stack and inventory objects
\\ (also we make inventories lists inside inventory, for rules
report 2, {
            problem: We have codes from employees 1,2,3,4..
            we need to send a number of employees in festival but some of them can't wend together
            So we have to find a solution
            }
Ανανέωση
\\ We say that
Employees=500
to_send=150
\\ Rules (1,3) means 1 and 3 can't be in final list together
b=((1,3),(7,4),(6,3),(3,2),(6,1),(4,7),(1,7),(4,6),(1,4),(3,7), (8,9),(7,8),(5,2),(10,3))
\\ we have pairs of employes that aren

\\ About commands
\\ Data push to tail of stack, Push push to head of stack
\\ Shift get to head from a position in stack (1 for head)

Module Suffle (X) {
      If len(X)>1 Then {
            Stack X {
                  For K=1 to Random(1, Len(X) div 2) {
                        Shift Random(2, len(X))
                  }
            }
      }
}
\\ set a high resolution timer (using Profiler)
Profiler
\\ make an empty stack, and pass a reference to A
A=Stack
Stack A {
      \\ Make A as current stack for this block
      \\ and just pushing values to it
      \\ aytomatic downstep if Employees>1
      For i=Employees to 1 {
            Push i
      }
}
Dim K()
Inventory bb
b1=each(b)
Suffle A \\ A is a pointer to Stack, so passing by copy is same as passing by reference
\\Pen 15 { Print A }
While b1 {
                 K()=Array(b1)
                 If K(0)>K(1) Then Swap K(0), K(1)
                 If exist(bb,K(0)) then {
                             \\ eval(bb) is same as bb(K(0)). An internal index mark the value of key K(0).
                            If not exist(eval(bb), K(1)) then {
                              Append eval(bb), K(1)
                       }
                 } else {
                              Inventory bb1
                              Append bb1, K(1)
                              Append bb, K(0):=bb1
                              \\ so now at key k(0) we have an inventory.
                }
                \\ make other direction
                If K(0)<K(1) Then Swap K(0), K(1)
                 If exist(bb,K(0)) then {
                            If not exist(eval(bb), K(1)) then {
                              Append eval(bb), K(1)
                       }
                 } else {
                              Inventory bb1
                              Append bb1, K(1)
                              Append bb, K(0):=bb1
                }
}
Inventory bb1 \\ change object
\\MM=1
A1=Each(A,1, -2) \\ iterator for stack A
\\ A1^ is a cursor (zero based),  stack is one based so we use it as A1^+1,
\\ so +1 is A1^+2
\\ continue jump to end of block so we get next A1 or exit from while.
While A1 {
      If StackItem(A1)=0 Then Continue
      A2=Each(A, A1^+2)
      If exist(bb, StackItem(A1)) Then {
      While A2 {
      If StackItem(A2)=0 Then Continue
\\            Print MM, StackItem(A1), StackItem(A2)
\\            MM++

                      If exist(eval(bb), Stackitem(A2)) Then {
                                  Return A, A2^+1:=0
                            }
                }
          
      }
}
\\ Pen 11 { Print A }
Inventory Answer
Stack A {
      For i=1 to len(A) {
            Read num
            If Num>0 Then Append Answer, Num : to_send-- : If to_send=0 then exit
      }
}
Sort Answer as Number
goto passthis
M=Each(b)
While M {
      Print Array(M)
}
Print
passthis:
Print "Result:"
Print Answer
Print Timecount
Print "Rules:"
sort bb as number
bbc=each(bb)
While bbc {
      Pen 14 {
      Print val(eval$(bbc, bbc^)),~(11), eval(bbc)
      }
}
Clear bb \\ clear references from garbage collector
Clear bb1

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.