cpinitiative / usaco-guide

A free collection of curated, high-quality resources to take you from Bronze to Platinum and beyond.
https://usaco.guide
Other
1.62k stars 495 forks source link

More Python Internal Solutions #3342

Closed bqi343 closed 10 months ago

bqi343 commented 1 year ago

Especially for Bronze and Silver (see https://usaco.guide/general/choosing-lang?lang=py#can-i-pass-every-problem-in-every-language for the note about 2022-23)

SansPapyrus683 commented 1 year ago

some of them just can't be solved with python though?

bqi343 commented 1 year ago

Well, for those that can.

Btw, ChatGPT does a good job of converting existing solutions into Python code, if we want to do that instead of writing solutions from scratch. For example,

https://usaco.guide/gold/shortest-paths?lang=py#implementation

->

import sys

INF = 1e18

def main():
    n, m, q = map(int, input().strip().split())

    mat = [[INF for j in range(n)] for i in range(n)]

    for i in range(m):
        u, v, w = map(int, input().strip().split())
        u -= 1
        v -= 1
        if w < mat[u][v]:
            mat[u][v] = mat[v][u] = w

    # floyd-warshall
    for k in range(n):
        for i in range(n):
            for j in range(i + 1, n):
                if mat[i][k] + mat[k][j] < mat[i][j]:
                    mat[i][j] = mat[j][i] = mat[i][k] + mat[k][j]

    for i in range(q):
        u, v = map(int, input().strip().split())
        u -= 1
        v -= 1
        if u == v:
            mat[u][v] = 0
        if mat[u][v] == INF:
            mat[u][v] = -1
        print(mat[u][v])

if __name__ == '__main__':
    sys.exit(main())

(which TLEs but is correct)

Prompt:

Can you convert the following code to Python?

```cpp
(add code here ...)
SansPapyrus683 commented 10 months ago

i feel like there's enough python support to warrant closing this now

SansPapyrus683 commented 10 months ago

what're yall's opinions on this

bqi343 commented 10 months ago

feel free to close