https://www.hackerrank.com/challenges/circular-array-rotation/problem?isFullScreen=true
Circular Array Rotation | HackerRank
Print the elements in an array after 'k' right circular rotation operations.
www.hackerrank.com
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'circularArrayRotation' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER_ARRAY a
# 2. INTEGER k
# 3. INTEGER_ARRAY queries
#
# def circularArrayRotation(a, k, queries):
# # Write your code here
# for i in range(k):
# a = [a[-1]] + a[:-1]
# for q in queries:
# print(str(a[q]))
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
k = int(first_multiple_input[1])
q = int(first_multiple_input[2])
a = list(map(int, input().rstrip().split()))
queries = []
for _ in range(q):
queries_item = int(input().strip())
queries.append(queries_item)
if k >= n: #k가 n보다 크다면 나눈 나머지만큼만 하면 된다.
k = k%n
a = a[len(a)-k:] + a[:len(a)-k]
result = []
for q in queries:
result.append(str(a[q]))
# result = circularArrayRotation(a, k, queries)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()'파이썬 코딩테스트 > 해커랭크' 카테고리의 다른 글
| HackerRank / Picking Numbers / Python (0) | 2022.01.07 |
|---|---|
| HackerRank / Drawing Books / Python (0) | 2022.01.05 |
| HackerRank / Append and Delete / Python (0) | 2022.01.05 |
| HackerRank / Strange Counter / Python (0) | 2022.01.03 |
| HackerRank / Games of Thrones - 1 / Python (0) | 2022.01.02 |