[Python] Facebook / Meta Coding Puzzle -- Rotary Lock (Chapter 2)

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    coding challenge screenshot

    This is from Facebook/Meta Careers page practice problems here is my current solution so far 2/32 test cases passed, has anyone been able to get 32/32?

    from typing import List
    # Write any import statements here

    def getMinCodeEntryTime(N: int, M: int, C: List[int]) -> int:
    # Write your code here
    dial_1 = 1
    dial_2 = 1
    count = 0
    code = C

    if len(code) > 0:
    for number in code:
    print("before: dial_1, dial_2: ", dial_1, dial_2)

    fwd_1 = abs(number - dial_1)
    rev_1 = N - abs(number - dial_1)

    fwd_2 = abs(number - dial_2)
    rev_2 = N - abs(number - dial_2)

    # Dial 1 calc: fwd vs rev
    if fwd_1 > rev_1:
    count_1 = rev_1
    else:
    count_1 = fwd_1

    # Dial 2 calc: fwd vs rev
    if fwd_2 > rev_2:
    count_2 = rev_2
    else:
    count_2 = fwd_2

    # Dial 1 vs Dial 2 movement decision
    if count_1 > count_2:
    dial_2 = number
    count += count_2
    else:
    dial_1 = number
    count += count_1

    print("after: dial_1, dial_2: ", dial_1, dial_2, "+/-", count)

    return count

    Continue reading...

Compartilhe esta Página