Skip to content

lucifer1004/Jl2Py.jl

Repository files navigation

Jl2Py

Stable Dev Build Status Coverage

Examples

Conversion results of LeetCode.jl - 1. Two Sum

def two_sum(nums: List[int], target: int, /) -> Union[None, Tuple[int, int]]:
    seen = {}
    for (i, n) in enumerate(nums):
        m = target - n
        if haskey(seen, m):
            return (seen[m], i)
        else:
            seen[n] = i

Conversion results of LeetCode.jl - 2. Add Two Numbers

def add_two_numbers(l1: ListNode, l2: ListNode, /) -> ListNode:
    carry = 0
    fake_head = cur = ListNode()
    while not isnothing(l1) or (not isnothing(l2) or not iszero(carry)):
        (v1, v2) = (0, 0)
        if not isnothing(l1):
            v1 = val(l1)
            l1 = next(l1)
        if not isnothing(l2):
            v2 = val(l2)
            l2 = next(l2)
        (carry, v) = divrem(v1 + v2 + carry, 10)
        next_inplace(cur, ListNode(v))
        cur = next(cur)
        val_inplace(cur, v)
    return next(fake_head)

We can see that we only need to define a few polyfill functions to make the generated Python code work.