Soluție HackerRank pentru Even Odd Query, subdomeniul Fundamentals, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Even Odd Query
  • Domeniu: Fundamentals
  • Limbaj: Python 3

Challenge: Even Odd Query

Subdomeniu: Fundamentals (fundamentals)

Scor cont: 30.0 / 30

Submission status: Accepted

Submission score: 1.0

Submission ID: 464725277

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/even-odd-query/problem

Cerință

You are given an array *A* of size *N*. You are also given an integer *Q*. Can you figure out the answer to each of the *Q* queries?

Each query contains 2 integers x and y, and you need to find whether the value find(x,y) is Odd or Even:

find(int x,int y)
{
if(x>y) return 1;
ans = pow(A[x],find(x+1,y))
return ans
}

Note : pow(a,b) = *a<sup>b</sup>*.

Input Format
The first line of the input contains an integer *N*.
The next line contains *N* space separated non-negative integers(whole numbers less than or equal to 9).
The line after that contains a positive integer, *Q* , the denotes the number of queries to follow.
*Q* lines follow, each line contains two positive integer *x* and *y* separated by a single space.

Output Format
For each query, display 'Even' if the value returned is Even, otherwise display 'Odd'.

Constraints
2 ≤ *N* ≤ 10<sup>5</sup>
2 ≤ *Q* ≤ 10<sup>5</sup>
1 ≤ *x,y* ≤ *N*
*x* ≤ *y*

Array is 1-indexed.

*No 2 consecutive entries in the array will be zero.*

Sample Input

3
3 2 7
2
1 2
2 3

Sample Output

Odd
Even

Explanation

find(1,2) = 9, which is Odd
find(2,3) = 128, which is even

Cod sursă

#!/bin/python3

import math
import os
import random
import re
import sys

def solve(arr, queries):
    result= []
    for i,j in queries:
        if(i<len(arr) and arr[i]==0 and i!=j):
            result.append('Odd')
        else:
            if(arr[i-1]%2==0):
                result.append('Even')
            else:
                result.append('Odd')
    return result
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    arr_count = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    q = int(input().strip())

    queries = []

    for _ in range(q):
        queries.append(list(map(int, input().rstrip().split())))

    result = solve(arr, queries)

    fptr.write('\n'.join(result))
    fptr.write('\n')

    fptr.close()
HackerRank Fundamentals – Even Odd Query