🐍Python/Python_알고리즘

[알고리즘] 37. Sequence Equation

728x90
반응형

Given a sequence of  integers,  where each element is distinct and satisfies . For each  where , find any integer  such that  and print the value of  on a new line.

For example, assume the sequence . Each value of  between  and , the length of the sequence, is analyzed as follows:

  1. , so 
  2. , so 
  3. , so 
  4. , so 
  5. , so 

The values for  are .

Function Description

Complete the permutationEquation function in the editor below. It should return an array of integers that represent the values of .

permutationEquation has the following parameter(s):

  • p: an array of integers

Input Format

The first line contains an integer , the number of elements in the sequence.
The second line contains  space-separated integers  where .

Constraints

  • , where .
  • Each element in the sequence is distinct.

Output Format

For each  from  to , print an integer denoting any valid  satisfying the equation  on a new line.

Sample Input 0

3
2 3 1

Sample Output 0

2
3
1

Explanation 0

Given the values of , and , we calculate and print the following values for each  from  to :

  1. , so we print the value of  on a new line.
  2. , so we print the value of  on a new line.
  3. , so we print the value of  on a new line.

Sample Input 1

5
4 3 5 1 2

Sample Output 1

1
3
5
4
2
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the permutationEquation function below.
if __name__ == '__main__':
n = int(input())

p = list(map(int, input().rstrip().split()))
num = list()
res = list()
for i in range(1,n+1):
for j in range(n):
if p[j] == i:
num.append(j+1)
for i in range(n):
for j in range(n):
if p[j] == num[i]:
res.append(j+1)
[print(int(r)) for r in res]



for문을 두 번 돌려서 불안하지만.. 인덱싱을 하기 위한 인덱스 넘버를 찾는 문제!

728x90
반응형