NEWTON

NEWTON


Popular tags

    How can I create an array and populate it in the hint Cairo lang/Protostar?

    Asked

    1 month ago

    17

    views


    0

    Does anyone know how I can create an array and populate it in the python hint? Basically something like

    let (my_array: felt*) = alloc() %{ ids.my_array = [1, 2, 3] %}
      protostarhintsstarknetcairo

    Newton

    asked

    1 month ago


    1 answers

    0

    Accepted answer

    There are two examples I found, that can work:

    Example 1

    Simply add a hint:

    let (local result : felt*) = alloc()
        %{ segments.write_arg(ids.result, [1, 2, 3]) %}
    

    Example 2

    This example proposed by Lord Cairo:

    %{
            IDENTIFIER_INDEX = 1
            ADJACENT_VERTICES_INDEX = 2
            for i in range(ids.graph_len):
                neighbours_len = memory[ids.adjacent_vertices_count+i]
                vertex_id = memory[ids.vertices.address_+i*ids.Vertex.SIZE+IDENTIFIER_INDEX]
                adjacent_vertices_pointer = memory[ids.vertices.address_+i*ids.Vertex.SIZE+ADJACENT_VERTICES_INDEX]
                print(f"{vertex_id} -> {{",end='')
                for j in range (neighbours_len):
                    adjacent_vertex = memory[adjacent_vertices_pointer+j*ids.AdjacentVertex.SIZE+IDENTIFIER_INDEX]
                    print(f"{adjacent_vertex} ",end='')
                print('}',end='')
                print()
        %}
    

    Newton

    answered

    1 month ago

    Your answer

    NEWTON

    NEWTON