Appearance
question:You are tasked with implementing a function to calculate the average grade of a student based on grades received for five subjects. Each subject grade is stored as a floating-point number. Write a program to calculate if the student passes or fails. - A student passes if their average grade is 5.0 or higher. - A student fails if their average grade is below 5.0. If the average grade is exactly 5.0 or higher, print "Pass", otherwise print "Fail". -----Constraints----- - All input values are floating-point numbers. - Each grade is between 0.0 and 10.0 (inclusive). -----Input----- The input is given from Standard Input in the following format: grade1 grade2 grade3 grade4 grade5 -----Output----- Print "Pass" if the student's average grade is 5.0 or higher, otherwise print "Fail". -----Sample Input----- 4.5 6.0 5.5 7.0 8.0 -----Sample Output----- Pass The total average grade for 4.5, 6.0, 5.5, 7.0, and 8.0 is 6.2, which is above 5.0. Thus, the output should be "Pass".
answer:def calculate_pass_fail(grades): Determines if a student passes or fails based on an average of five grades. Parameters: grades (list of float): The list containing five subject grades. Returns: str: "Pass" if the average grade is 5.0 or higher, otherwise "Fail". average_grade = sum(grades) / len(grades) if average_grade >= 5.0: return "Pass" else: return "Fail" # Read grades from input if __name__ == "__main__": grades = list(map(float, input().split())) print(calculate_pass_fail(grades))
question:Write a program that determines if a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). Print "YES" if the string is a palindrome, "NO" if not. Constraints * 1 ≤ length of string ≤ 1,000 * N ≤ 1,000 Input Input consists of several data sets. In the first line, the number of data sets, N, is given. Then, N lines follow, each line corresponds to a data set. A data set consists of a single string which may contain spaces, punctuation, and mixed case letters. Output For each data set, print "YES" or "NO". Example Input 3 A man a plan a canal Panama racecar Hello World Output YES YES NO
answer:def is_palindrome(s): Determines if the given string is a palindrome ignoring spaces, punctuation, and capitalization. import re cleaned = re.sub(r'[^A-Za-z0-9]', '', s).lower() return cleaned == cleaned[::-1] def check_palindromes(n, strings): results = [] for s in strings: if is_palindrome(s): results.append("YES") else: results.append("NO") return results
question:You are organizing a treasure hunt in a grid of dimension N times M. You want to hide some treasures in the grid cells such that each cell can be accessed from another cell with a minimal cost. You may perform the following operation any number of times (possibly zero): - Move from a cell (i, j) to a cell (i', j') with a cost c(i', j'). You only move to adjacent cells, i.e., (i', j') is either (i+1, j), (i-1, j), (i, j+1), or (i, j-1). You are given a grid where each cell has a value that determines the cost to move to that cell. The task is to find the minimum cost to travel from the top-left corner of the grid (1, 1) to the bottom-right corner of the grid (N, M). -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and M, the dimensions of the grid. - The next N lines contain M space-separated integers where the j-th integer in the i-th line denotes the cost c(i, j) of cell (i, j). -----Output----- For each test case, print a single line containing one integer ― the minimum cost to travel from (1, 1) to (N, M). -----Constraints----- - 1 le T le 100 - 1 le N, M leq 1,000 - 1 le c(i, j) le 100 for each valid i, j -----Example Input----- 2 3 3 1 3 1 1 5 1 4 2 1 2 2 1 2 1 1 -----Example Output----- 7 3 -----Explanation----- Example case 1: The path with the minimum cost from (1, 1) to (3, 3) is (1, 1) rightarrow (2, 1) rightarrow (3, 1) rightarrow (3, 2) rightarrow (3, 3), with a total cost of 1 + 1 + 4 + 2 + 1 = 7. Example case 2: The path with the minimum cost from (1, 1) to (2, 2) is (1, 1) rightarrow (1, 2) rightarrow (2, 2), with a total cost of 1 + 1 + 1 = 3.
answer:import heapq def min_cost_to_travel(grid, N, M): # Using a priority queue to find the minimum cost path pq = [] heapq.heappush(pq, (grid[0][0], 0, 0)) # Directions arrays directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] # Costs array to store the minimum cost to reach each cell costs = [[float('inf')] * M for _ in range(N)] costs[0][0] = grid[0][0] while pq: current_cost, x, y = heapq.heappop(pq) # If we have reached the bottom-right corner, return the cost if x == N-1 and y == M-1: return current_cost # Visit neighbors for direction in directions: nx, ny = x + direction[0], y + direction[1] if 0 <= nx < N and 0 <= ny < M: new_cost = current_cost + grid[nx][ny] if new_cost < costs[nx][ny]: costs[nx][ny] = new_cost heapq.heappush(pq, (new_cost, nx, ny)) def solve_all_cases(T, cases): results = [] for case in cases: N, M, grid = case result = min_cost_to_travel(grid, N, M) results.append(result) return results
question:You are given an array of integers. Your task is to find out how many unordered pairs (i, j) exist such that their sum is equal to a given integer K. Note that the pair (i, j) is the same as the pair (j, i), and thus should not be counted twice. -----Input----- - The first line contains a single integer N denoting the size of the array. - The second line contains N space-separated integers representing the elements of the array. - The third line contains a single integer K denoting the target sum. -----Output----- Print a single integer, representing the number of unordered pairs (i, j) such that the sum of the pair is equal to K. -----Constraints----- - 1 le N le 10^6 - -10^6 le Array elements le 10^6 - -10^{12} le K le 10^{12} -----Example Input----- 5 1 5 7 -1 5 6 -----Example Output----- 3 -----Explanation----- The pairs are (1, 5), (7, -1), and (1, 5), so the total number of pairs is 3.
answer:def count_pairs_with_sum(arr, N, K): Count pairs in an array with a given sum. :param arr: List[int] - input array of integers :param N: int - size of the array :param K: int - target sum :return: int - count of pairs (i, j) with i < j and arr[i] + arr[j] == K if N < 2: return 0 count_map = {} count = 0 for num in arr: if K - num in count_map: count += count_map[K - num] if num in count_map: count_map[num] += 1 else: count_map[num] = 1 return count