Python 3paiza I don't know the answer example

Asked 2 years ago, Updated 2 years ago, 33 views

[Problem]
The sequence A = {a_1,a_2,...,a_n} and the integer x are given.
Find the number of integers x in column A.In other words, find i that satisfies x=a_i.
Note that the integer x is guaranteed to contain only one in column A.

Value entered
n
a_1a_2...a_n
x

·The first line is given the number of elements n in the sequence.
· In the second line, the elements a_1, a_2,..., a_n in the sequence are given in half-width spaces.
· The third line is given the value x you want to search for.

The last line of input value has a new line at the end.
The string is passed from the standard input. Find out how to get values from standard input here

Expected Output
Please output the number of integers x in column A.
Also, add a new line at the end and do not include unnecessary characters or empty lines.

Conditions
For all test cases, the following conditions are met:

·All inputs are integers
· 2 nn 100100
· -20,000 aa_i 2020,000(1 ii nn)
· If i jj, a_i aa_j
· -20,000 xx ,20,000
· x is always included in A={a_1,a_2,...,a_n}

Input Example 1
5
4 1 3 5 2 5

Sample Output 1
4

[Answer example]

n=int(input())
a = [ int(x) for x in input().split() ]
x = int(input())

pos_of_x=-1
for i in range(n):
    if a[i] == x:
        pos_of_x = i+1
        break

print(pos_of_x)

[Question]
Why do you substitute -1 for pos_of_x and add +1 to the if statement?
If you are familiar with it, please let me know!

python python3

2022-09-29 22:54

1 Answers

Why substitute -1 for pos_of_x

The purpose of this is to declare the variable pos_of_x in advance for use in print statements after the for loop ends, and to do so, we take the means of substituting numbers.

In this case, the -1 number itself is meaningless and can be any value.

However, by initializing the array index to a value that is out of range, -1 is likely to be used as a value that can detect when a variable is not updated due to some kind of defect.

Do I do +1 in the if statement?

By the way, the if statement is not +1 and the +1 is when replacing pos_of_x.

This depends on what the 何number 目 criterion value starts with.

Python arrays begin with a leading index of 0.

On the other hand, in Japanese arithmetic, mathematics, or general counting, it is common to start with 1.

It doesn't seem like it's stated by itself, but it seems to be scattered in the problem sentence and the conditions.
In particular, is the third (1 )i /n) of the conditions?

  • How many integers x appear in sequence A...that is, find i that satisfies x=a_i...
  • A third -20,000 aa_i 2020,000(1 ii nn) of the
  • condition.
  • The sixth of the conditions...A={a_1,a_2,...,a_n}...
  • Comparison between input and output examples

In accordance with common Japanese conditions, the index number on the Python program is considered to be i+1 when replacing pos_of_x in order to display the index number as "what number" in Japanese.

For example, articles like this might be helpful.
Is it disgusting that the leading element of an array is "0th"…consider the word "Nth"


2022-09-29 22:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.