Forumi


Povratak   PC Ekspert Forum > Računala > Software > Web dizajn, programiranje i ostalo
Ime
Lozinka

Odgovori
 
Uređivanje
Staro 19.02.2011., 16:33   #1
svebee
/
 
Datum registracije: Oct 2006
Lokacija: /
Postovi: 2,053
Arrow [HITNO] Prebacivanje Pythona u PHP

Dakle imam slijedeći kod kojeg HITNO trebam prebaciti iz Pythona u PHP. Nadam se da onome ko poznaje Python (i PHP) ovo neće biti problem (nema baš nešto puno koda) . Unaprijed zahvaljujem Plaćam ako treba, ali valjda ima još dobrih kolega

Code:
def find_best_run (first_array, *argv):
    # initialize data structures.
    this_array_best_run = {}
    for x in first_array:
        this_array_best_run[x] = (1, (1,), (x,))

    for this_array in argv:
        # find the best runs ending at each value in this_array
        last_array_best_run = this_array_best_run
        this_array_best_run = {}

        for x in this_array:
            for (y, pattern) in last_array_best_run.iteritems():
                (distinct_count, lengths, elements) = pattern
                if x == y:
                    lengths = tuple(lengths[:-1] + (lengths[-1] + 1,))
                else :
                    distinct_count += 1
                    lengths = tuple(lengths + (1,))
                    elements = tuple(elements + (x,))

                if x not in this_array_best_run:
                    this_array_best_run[x] = (distinct_count, lengths, elements)
                else:
                    (prev_count, prev_lengths, prev_elements) = this_array_best_run[x]
                    if distinct_count < prev_count or prev_lengths < lengths:
                        this_array_best_run[x] = (distinct_count, lengths, elements)

    # find the best overall run
    best_count = len(argv) + 10 # Needs to be bigger than any possible answer.
    for (distinct_count, lengths, elements) in this_array_best_run.itervalues():
        if distinct_count < best_count:
            best_count = distinct_count
            best_lengths = lengths
            best_elements = elements
        elif distinct_count == best_count and best_lengths < lengths:
            best_count = distinct_count
            best_lengths = lengths
            best_elements = elements

    # convert it into a more normal representation.                
    answer = []
    for (length, element) in zip(best_lengths, elements):
        answer.extend([element] * length)

    return answer

# example
print find_best_run(
    [1,4,8,10],
    [1,2,3,4,11,15],
    [2,4,20,21],
    [2,30]) # prints [4, 4, 4, 30]
svebee je offline   Reply With Quote
Staro 20.02.2011., 11:13   #2
svebee
/
 
Datum registracije: Oct 2006
Lokacija: /
Postovi: 2,053
Pa zar nitko? :/
svebee je offline   Reply With Quote
Oglasni prostor
Oglas
 
Oglas
Staro 20.02.2011., 13:18   #3
Bubba
E Pluribus UNIX
Moj komp
 
Bubba's Avatar
 
Datum registracije: Oct 2002
Lokacija: M82
Postovi: 6,544
Citiraj:
Autor svebee Pregled postova
Pa zar nitko? :/
Zasto s konja na magarca?

BTW, vidim da su ti "zahtijevi" sve veci, pa ces definitivno morati ili izbudjetirati programere, ili naci vise vremena.
__________________
Programer
Rok od dva mjeseca u stvari znači četiri, ali nikako ispod šest.
Bubba je offline   Reply With Quote
Oglasni prostor
Oglas
 
Oglas
Odgovori



Pravila postanja
Vi ne možete otvarati nove teme
Vi ne možete pisati odgovore
Vi ne možete uploadati priloge
Vi ne možete uređivati svoje poruke

BB code je Uključeno
Smajlići su Uključeno
[IMG] kod je Uključeno
HTML je Isključeno

Idi na