PC Ekspert Forum

PC Ekspert Forum (https://forum.pcekspert.com/index.php)
-   Web dizajn, programiranje i ostalo (https://forum.pcekspert.com/forumdisplay.php?f=39)
-   -   [HITNO] Prebacivanje Pythona u PHP (https://forum.pcekspert.com/showthread.php?t=210110)

svebee 19.02.2011. 16:33

[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) :D. Unaprijed zahvaljujem :beer: 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 20.02.2011. 11:13

Pa zar nitko? :/

Bubba 20.02.2011. 13:18

Citiraj:

Autor svebee (Post 1853454)
Pa zar nitko? :/

Zasto s konja na magarca? :D ;)

BTW, vidim da su ti "zahtijevi" sve veci, pa ces definitivno morati ili izbudjetirati programere, ili naci vise vremena. ;)


Sva vremena su GMT +2. Sada je 23:48.

Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© 1999-2024 PC Ekspert - Sva prava pridržana ISSN 1334-2940
Ad Management by RedTyger