Read integer pairs using tuples in python

0

I'm new here, just like the Python language. This question that I present to you concerns a matter of faculty work. I tried every way I could solve it and I could not. I'm here for really despair. Here is my last attempt. Right after the question, is what I've been able to do so far - just the letter A.

Make a program, containing subprograms, that reads pairs of integer values, x and y, until the pair of zeros is read. Suppose each of these pairs represents a point in two-dimensional space. Keep these points as a vector of tuples (x, y). After entering all valid points, except the pair of zeros, write to the standard output:

  • The number of valid points;
  • The vector of points, writing one point per line;
  • If they exist, which two points are closest to each other. If there is a tie, write one of them;
  • If they exist, which are the two most distant points. If there is a tie, write one of them;
  • If they exist, what are the averages of the x and y coordinates.
  • Definition

    The distance between two points (xA, yA) and (xB, yB) is given by the square root of the sum of squares of the differences, (xA-xB) and (yA-yB).

    What I did:

    qtdParesLidos = 0
    linhaLida = input()  # faz a leitura da primeira linha
    x = float(linhaLida.split()[0])
    y = float(linhaLida.split()[1])
    
    while x != 0 or y != 0:  # repete até que o ponto 0 0 seja lido
        qtdParesLidos = qtdParesLidos + 1
        linhaLida = input()  # faz a leitura da próxima linha
        x = float(linhaLida.split()[0])
        y = float(linhaLida.split()[1])
    
        
    asked by anonymous 06.03.2018 / 03:14

    1 answer

    3

    The code you wrote will work fine for reading the data, and stop the input. The input must be integers, but the outputs of some of the requested items are decimal, so it's okay to use float instead of int . Except that item 2 asks for the input coordinates to be printed, so it is best to convert them to integers in the final job.

    So the points I think you need to understand to get the whole exercise done:

  • A "tuple" in Python is a sequence of values - which is usually declared in the code. It differs from the "lists" that once created can no longer be changed - it has the fixed value. They are created simply by placing the desired values separated by commas, and all this within parentheses (in some cases, parentheses are even optional - just separate values by commas that Python creates a tuple). Example: ponto = (x, y) or ponto = x, y are two lines that will create a tuple in which the first element will have the value that is in x and the second the value that is in y. To recover the element, just use ponto[0] (or 1 in brackets, for the other element). Lists and tuples work as "sequences" - and the number inside the bracket is the position of the desired element.
  • "Lists": These are another type of Python sequence. Unlike "tuples" they can have their elements and their length changed after they are created. One of the most commonly used methods is .append - it inserts a new element at the end of the sequence. Ex:
  • -

    coordenadas = []  # cria uma lista vazia
    coordenadas.append(ponto)  # acrecenta o valor referenciado na variável "ponto" à lista. 
    

    The best way to understand lists is to enter the Python interactive prompt and play around - check their content, add elements with the append (element) and insert (position, element) methods. Here's how you can retrieve elements in specific positions using the brackets. The complete documentation is here: link

    Question that if you know how to answer you will see that you are on the right path: if each element in the list is a tuple with two coordinates, how do I retrieve the "x-coordinate" of the element at position 0 from the list? It seems hard? - but let's take a break - let's suppose I have the following snippet of code:

    x = 3
    y = 4
    ponto = (x, y)
    coords = []
    coords.append(ponto)
    

    What will appear on the screen if I digir coords[0] ? What if I type ponto[0] ? (if in doubt, type the excerpt above in the Python interactive prompt until you have no doubts). And what happens if I continue this sequence of commands and type:

    consulta = coords[0]
    x1 = consulta[0]
    

    What will be the value in x1 ? Do I need the intermediate variable "query"? Or you can type direct coords[0][0] (try the interactive prompt)

  • You also need to know that there is the built-in len function that always returns the length of a sequence. Ex: len(coords) .
  • Remember that the for command in Python, as opposed to for in other languages always runs a sequence - the body of for is executed once for each element of a sequence. Ex .:
  • -

    for elemento in coords:
           print("A coordenada x nesse elemento é", elemento[0])
    

    Items 3 and 4 of the exercise are the most complicated ones even, and those that require some programming logic. I hope the concepts I described above will help you understand what you need right up to this point. To do the 3 you will need:

  • of a variable to store the closest items already found - remember that the variable can be a tuple or a list, so the two points that you will need to "remember" in the response can be in the same variable
  • a variable to store which was the smallest distance already found between two points. Initialize it with a large value;
  • a "for" to traverse all points - within it another "for" to traverse all points again. In the body of this second internal "for" you: check if the points of the first for and the second are the same. If they are, continue (see the command continue : link ). Calculate the distance between the selected point in the first for and in the second - if it is smaller than the lowest value you have ever seen, save those two points, and the distance value found. At the end of the two "for", the variable with the points will have the result that you need to print.
  • Calculating the longest distance between two points should be smooth - it's almost the same, just use logic appropriately.

    The mean: simply have variables to add all values of x, all values of y, and divide this by the total of points (you already know how to calculate). Good - I hope that you already have enough walking inside the exercise

        
    06.03.2018 / 04:41