Skip to content
🤔prompts chat🧠
🔍
question:The company DNET Ltd is developing a recommendation system for their e-commerce website. They have a list of product categories that users can browse. Based on past user interactions, the recommendation system will suggest categories that are similar to the ones the user has shown interest in. To achieve this, they've decided to use a similarity score between categories based on the frequency of certain keywords within the category descriptions. Given a list of category descriptions and a list of keywords, your task is to calculate the similarity score between two categories as the sum of the absolute differences in their keyword frequencies. # Input - The first line contains an integer n, the number of categories (1 ≤ n ≤ 100). - The next n lines contain a string description of each category. Each description consists of words separated by spaces (1 ≤ |description_i| ≤ 100). - The next line contains an integer m, the number of keywords (1 ≤ m ≤ 10). - The next line contains the m keywords separated by spaces. - The next line contains an integer q, the number of queries (1 ≤ q ≤ 100). - The next q lines contain two integers a and b (1 ≤ a, b ≤ n) indicating the indices of the categories to compare (1-based index). # Output For each query, output the similarity score between the two categories. # Example Input ``` 3 mobile phone accessories laptop accessories mobile laptop and accessories 3 mobile laptop accessories 2 1 2 2 3 ``` Output ``` 3 1 ``` # Explanation For the first query, comparing categories 1 and 2: - Category 1: "mobile phone accessories" - "mobile" appears 1 time - "laptop" appears 0 times - "accessories" appears 1 time - Category 2: "laptop accessories" - "mobile" appears 0 times - "laptop" appears 1 time - "accessories" appears 1 time Similarity score: |1 - 0| + |0 - 1| + |1 - 1| = 1 + 1 + 0 = 2 For the second query, comparing categories 2 and 3: - Category 2: "laptop accessories" - "mobile" appears 0 times - "laptop" appears 1 time - "accessories" appears 1 time - Category 3: "mobile laptop and accessories" - "mobile" appears 1 time - "laptop" appears 1 time - "accessories" appears 1 time Similarity score: |0 - 1| + |1 - 1| + |1 - 1| = 1 + 0 + 0 = 1

answer:def calculate_similarity_scores(category_descriptions, keywords, queries): def keyword_frequencies(description): words = description.split() freq = {keyword: 0 for keyword in keywords} for word in words: if word in freq: freq[word] += 1 return freq frequencies = [keyword_frequencies(desc) for desc in category_descriptions] def similarity_score(freq1, freq2): score = 0 for keyword in keywords: score += abs(freq1[keyword] - freq2[keyword]) return score results = [] for a, b in queries: freq1 = frequencies[a-1] freq2 = frequencies[b-1] results.append(similarity_score(freq1, freq2)) return results

question:In a fictional city, there is a unique way of measuring distance between two points on a grid. Points are defined by their (x, y) coordinates, and the distance between two points (x1, y1) and (x2, y2) is given by the formula: Distance = |x1 - x2| + |y1 - y2| You need to write a program to find out the distance between two given points for multiple test cases. ------ Input Format ------ - The first line will contain T - the number of test cases. - Each of the next T lines contains four integers x1, y1, x2, and y2, representing the coordinates of two points. ------ Output Format ------ For each test case, output the distance between the two points. ------ Constraints ------ 1 ≤ T ≤ 100 -1000 ≤ x1, y1, x2, y2 ≤ 1000 ----- Sample Input 1 ------ 3 0 0 1 1 -1 -1 1 1 2 3 2 3 ----- Sample Output 1 ------ 2 4 0 ----- Explanation 1 ------ Test case 1: The distance between points (0, 0) and (1, 1) is |0 - 1| + |0 - 1| = 1 + 1 = 2. Test case 2: The distance between points (-1, -1) and (1, 1) is |-1 - 1| + |-1 - 1| = 2 + 2 = 4. Test case 3: The distance between points (2, 3) and (2, 3) is |2 - 2| + |3 - 3| = 0 + 0 = 0.

answer:def calculate_distances(test_cases): Given a list of test cases (each a tuple of four integers representing two points on a grid), returns a list of distances between these points using the formula |x1 - x2| + |y1 - y2|. distances = [] for x1, y1, x2, y2 in test_cases: distance = abs(x1 - x2) + abs(y1 - y2) distances.append(distance) return distances def parse_input(input_str): Parses an input string in the format specified in the problem description and returns the number of test cases and a list of test cases. lines = input_str.strip().split('n') T = int(lines[0].strip()) test_cases = [] for i in range(1, T + 1): x1, y1, x2, y2 = map(int, lines[i].strip().split()) test_cases.append((x1, y1, x2, y2)) return T, test_cases def format_output(distances): Formats a list of distances (integers) into an output string with each distance on a new line. return 'n'.join(map(str, distances)) def main(input_str): T, test_cases = parse_input(input_str) distances = calculate_distances(test_cases) return format_output(distances)

question:The mayor of a city has decided to repair some of the roads ahead of the upcoming festival. The city has n intersections connected by m roads. Each road has a specific length. The mayor wants to ensure that the total length of the roads that need repair is minimized while still maintaining the city's connectivity. Given the number of intersections and roads, along with the roads' lengths, your task is to determine the minimum total length of roads that need repair to keep all intersections connected. -----Input----- The first line contains two integers n and m (2 le n le 10^4, n - 1 le m le 10^5) — the number of intersections and the number of roads. The next m lines each contain three integers u, v, and w (1 le u, v le n, 1 le w le 10^6) — representing a road between intersections u and v with length w. -----Output----- Print a single integer — the minimum total length of the roads that need repair. -----Example----- Input 4 5 1 2 3 2 3 4 3 4 5 1 4 9 1 3 7 Output 12 -----Note----- In the given example, the selected roads to repair could be: (1-2), (2-3), and (3-4), which ensures all intersections are connected with a total length of 12.

answer:def find_minimum_repair_cost(n, m, roads): Finds the minimum total length of roads that need repair to keep all intersections connected. Parameters: n (int): Number of intersections. m (int): Number of roads. roads (List[Tuple[int, int, int]]): List of tuples where each tuple represents (u, v, w). Returns: int: Minimum total length of roads that need repair. # Using Kruskal's algorithm to find the Minimum Spanning Tree (MST) parent = list(range(n + 1)) def find(x): if parent[x] != x: parent[x] = find(parent[x]) return parent[x] def union(x, y): rootX = find(x) rootY = find(y) if rootX != rootY: parent[rootY] = rootX # Sort the roads by their weight (length) roads.sort(key=lambda x: x[2]) mst_cost = 0 edges_used = 0 for u, v, w in roads: if find(u) != find(v): union(u, v) mst_cost += w edges_used += 1 # Early stop if we've used n-1 edges (which is enough to connect all intersections) if edges_used == n - 1: break return mst_cost

question:A pangram is a sentence that contains every letter of the alphabet at least once. Your task is to determine if a given string is a pangram. The input string will consist of lowercase and uppercase English letters, spaces, and punctuation marks. The spaces and punctuation marks should be ignored when determining whether the string is a pangram. Input The input data consists of one line which contains a string of length between 1 and 1000, inclusive. The string contains one or more words, which are separated by one or more spaces or punctuation. Punctuation marks include: " , . ! ? ; : ". Output Print "YES" (without the quotes) if the sentence is a pangram. Otherwise, print "NO" (also without the quotes). Examples Input The quick brown fox jumps over the lazy dog! Output YES Input Hello, World! Output NO

answer:import string def is_pangram(sentence): Determines if a given sentence is a pangram. Ignores spaces and punctuation, case insensitive. alphabet_set = set(string.ascii_lowercase) sentence_set = set(c.lower() for c in sentence if c.isalpha()) return "YES" if alphabet_set <= sentence_set else "NO"

Released under the chat License.

has loaded