Introduction to LINQ Queries
Element-wise Operations, Aggregation, and Sequence Generation
LinqBegin1°. An integer sequence is given, containing both positive and negative numbers. Output its first positive element and its last negative element.
LinqBegin2°. A digit D (a single-digit integer) and an integer sequence A are given. Output the first positive element of sequence A ending with the digit D. If there are no required elements in sequence A, output 0.
LinqBegin3°. An integer L (> 0) and a string sequence A are given. Output the last string from A that starts with a digit and has length L. If there are no required strings in sequence A, output the string "Not found". Note. Use the ?? operation to handle the situation related to the absence of required strings.
LinqBegin4°. A character C and a string sequence A are given. If A contains a single element ending with the character C, then output this element; if there are no required strings in A, output an empty string; if there are more than one required strings, output the string "Error". Note. Use a try block to catch a possible exception.
LinqBegin5°. A character C and a string sequence A are given. Find the number of elements of A that contain more than one character and at the same time start and end with the character C.
LinqBegin6°. A string sequence is given. Find the sum of the lengths of all strings included in this sequence.
LinqBegin7°. An integer sequence is given. Find the number of its negative elements, as well as their sum. If negative elements are absent, output 0 twice.
LinqBegin8°. An integer sequence is given. Find the number of its positive two-digit elements, as well as their arithmetic mean (as a real number). If the required elements are absent, output 0 twice (the first time as an integer, the second — as a real number).
LinqBegin9°. An integer sequence is given. Output its minimal positive element or the number 0 if the sequence does not contain positive elements.
LinqBegin10°. An integer L (> 0) and a string sequence A are given. The strings of sequence A contain only uppercase Latin letters. Among all strings from A having length L, find the largest one (in terms of lexicographical order). Output this string or an empty string if the sequence does not contain strings of length L.
LinqBegin11°. A sequence of non-empty strings is given. Using the Aggregate method, obtain a string consisting of the initial characters of all strings of the source sequence.
LinqBegin12°. An integer sequence is given. Using the Aggregate method, find the product of the last digits of all elements of the sequence. To avoid integer overflow, use a real numeric type when calculating the product.
LinqBegin13°. An integer N (> 0) is given. Using the Range and Sum methods, find the sum 1 + (1/2) + … + (1/N) (as a real number).
LinqBegin14°. Integers A and B (A < B) are given. Using the Range and Average methods, find the arithmetic mean of the squares of all integers from A to B inclusive: (A2 + (A+1)2 + … + B2)/(B − A + 1) (as a real number).
LinqBegin15°. An integer N (0 ≤ N ≤ 15) is given. Using the Range and Aggregate methods, find the factorial of the number N: N! = 1·2·…·N for N ≥ 1; 0! = 1. To avoid integer overflow, use a real numeric type when calculating the factorial.
Filtering, Sorting, Set-Theoretic Operations
LinqBegin16°. An integer sequence is given. Extract all positive numbers from it, preserving their original order.
LinqBegin17°. An integer sequence is given. Extract all odd numbers from it, preserving their original order and removing all occurrences of duplicate elements except the first ones.
LinqBegin18°. An integer sequence is given. Extract all even negative numbers from it, reversing the order of the extracted numbers.
LinqBegin19°. A digit D (a single-digit integer) and an integer sequence A are given. Extract from A all distinct positive numbers ending with the digit D (in the original order). If there are duplicate elements, remove all their occurrences except the last ones. Note. Apply the Reverse, Distinct, Reverse methods sequentially.
LinqBegin20°. An integer sequence is given. Extract all positive two-digit numbers from it, sorting them in ascending order.
LinqBegin21°. A string sequence is given. The strings of the sequence contain only uppercase Latin letters. Sort the sequence in ascending order of string lengths, and strings of the same length — in descending lexicographical order.
LinqBegin22°. An integer K (> 0) and a string sequence A are given. The strings of the sequence contain only digits and uppercase Latin letters. Extract from A all strings of length K ending with a digit, sorting them in lexicographical order.
LinqBegin23°. An integer K (> 0) and an integer sequence A are given. Starting from the element of A with ordinal number K, extract from A all odd two-digit numbers, sorting them in descending order.
LinqBegin24°. An integer K (> 0) and a string sequence A are given. From the elements of A preceding the element with ordinal number K, extract those strings that have an odd length and start with an uppercase Latin letter, reversing the order of the extracted strings.
LinqBegin25°. Integers K1 and K2 and an integer sequence A are given; 1 ≤ K1 < K2 ≤ N, where N is the size of sequence A. Find the sum of positive elements of the sequence with ordinal numbers from K1 to K2 inclusive.
LinqBegin26°. Integers K1 and K2 and a sequence of non-empty strings A are given; 1 < K1 < K2 ≤ N, where N is the size of sequence A. Find the arithmetic mean of the lengths of all elements of the sequence, except for elements with ordinal numbers from K1 to K2 inclusive, and output it as a real number.
LinqBegin27°. An integer D and an integer sequence A are given. Starting from the first element of A greater than D, extract from A all odd positive numbers, reversing the order of the extracted numbers.
LinqBegin28°. An integer L (> 0) and a sequence of non-empty strings A are given. The strings of the sequence contain only digits and uppercase Latin letters. From the elements of A preceding the first element whose length exceeds L, extract strings ending with a letter. Sort the obtained sequence in descending order of string lengths, and strings of the same length — in lexicographical order.
LinqBegin29°. Integers D and K (K > 0) and an integer sequence A are given. Find the set-theoretic union of two fragments of A: the first contains all elements before the first element greater than D (not including it), and the second — all elements starting from the element with ordinal number K. Sort the obtained sequence (which does not contain identical elements) in descending order.
LinqBegin30°. An integer K (> 0) and an integer sequence A are given. Find the set-theoretic difference of two fragments of A: the first contains all even numbers, and the second — all numbers with ordinal numbers greater than K. In the obtained sequence (which does not contain identical elements) reverse the order of the elements.
LinqBegin31°. An integer K (> 0) and a sequence of non-empty strings A are given. The strings of the sequence contain only digits and uppercase Latin letters. Find the set-theoretic intersection of two fragments of A: the first contains the first K elements, and the second — all elements located after the last element ending with a digit. Sort the obtained sequence (which does not contain identical elements) in ascending order of string lengths, and strings of the same length — in lexicographical order.
Projection
LinqBegin32°. A sequence of non-empty strings A is given. Obtain a sequence of characters, each element of which is the initial character of the corresponding string from A. The order of the characters must be reverse relative to the order of the elements of the source sequence.
LinqBegin33°. An integer sequence is given. Processing only positive numbers, obtain a sequence of their last digits and remove all occurrences of identical digits in the obtained sequence except the first. The order of the obtained digits must correspond to the order of the source numbers.
LinqBegin34°. A sequence of positive integers is given. Processing only odd numbers, obtain a sequence of their string representations and sort it in lexicographical order.
LinqBegin35°. An integer sequence is given. Obtain a sequence of numbers, each element of which is equal to the product of the corresponding element of the source sequence and its ordinal number (1, 2, …). In the obtained sequence, remove all elements that are not two-digit, and reverse the order of the remaining elements.
LinqBegin36°. A sequence of non-empty strings is given. Obtain a sequence of characters, which is defined as follows: if the corresponding string of the source sequence has an odd length, then the first character of this string is taken as the character; otherwise, the last character of the string is taken. Sort the obtained characters in descending order of their codes.
LinqBegin37°. A string sequence A is given. The strings of the sequence contain only uppercase Latin letters. Obtain a new sequence of strings, whose elements are determined by the corresponding elements of A as follows: empty strings are not included in the new sequence, and to non-empty strings the ordinal number of this string in the source sequence is appended (for example, if the fifth element of A looks like "ABC", then in the obtained sequence it will look like "ABC5"). When numbering, empty strings of sequence A must also be taken into account. Sort the obtained sequence in lexicographical order.
LinqBegin38°. An integer sequence A is given. Obtain a new sequence of numbers, whose elements are determined by the corresponding elements of sequence A as follows: if the ordinal number of the element of A is divisible by 3 (3, 6, …), then this element is not included in the new sequence; if the remainder of dividing the ordinal number by 3 is 1 (1, 4, …), then the doubled value of this element is added to the new sequence; otherwise (for elements of A with numbers 2, 5, …) the element is added to the new sequence without changes. In the obtained sequence, preserve the original order of elements.
LinqBegin39°. A string sequence A is given. Obtain a sequence of digit characters included in the strings of sequence A (characters may repeat). The order of the characters must correspond to the order of the strings A and the order of characters in each string. Note. Use the SelectMany method considering that a string can be interpreted as a sequence of characters.
LinqBegin40°. A number K (> 0) and a string sequence A are given. Obtain a sequence of characters containing the characters of all strings from A that have a length greater than or equal to K (characters may repeat). In the obtained sequence, reverse the order of the elements.
LinqBegin41°. An integer K (> 0) and a string sequence A are given. Each element of the sequence represents several words from uppercase Latin letters, separated by '.' (dot) characters. Obtain a sequence of strings containing all words of length K from the elements of A in lexicographical order (words may repeat).
LinqBegin42°. A sequence of non-empty strings is given. Obtain a sequence of characters, which is defined as follows: for strings with odd ordinal numbers (1, 3, …), all uppercase Latin letters contained in these strings are included in the character sequence, and for strings with even numbers (2, 4, …) — all their lowercase Latin letters. In the obtained character sequence, preserve their original order.
LinqBegin43°. An integer K (> 0) and a sequence of non-empty strings A are given. Obtain a sequence of characters, which is defined as follows: for the first K elements of sequence A, the characters at odd positions in the given string (1, 3, …) are added to the new sequence, and for the remaining elements of A — the characters at even positions (2, 4, …). In the obtained sequence, reverse the order of the elements.
Joining and Grouping
LinqBegin44°. Integers K1 and K2 and integer sequences A and B are given. Obtain a sequence containing all numbers from A greater than K1 and all numbers from B less than K2. Sort the obtained sequence in ascending order.
LinqBegin45°. Positive integers L1 and L2 and string sequences A and B are given. The strings of the sequences contain only digits and uppercase Latin letters. Obtain a sequence containing all strings from A of length L1 and all strings from B of length L2. Sort the obtained sequence in descending lexicographical order.
LinqBegin46°. Sequences of positive integers A and B are given; all numbers in each sequence are distinct. Find the sequence of all pairs of numbers satisfying the following conditions: the first element of the pair belongs to sequence A, the second belongs to B, and both elements end with the same digit. The resulting sequence is called the inner join of sequences A and B by the key defined by the last digits of the source numbers. Present the found join as a sequence of strings containing the first and second elements of the pair, separated by a hyphen, for example, "49-129". The order of the pairs must be determined by the original order of the elements of sequence A, and for equal first elements — by the order of the elements of sequence B.
LinqBegin47°. Sequences of positive integers A and B are given; all numbers in each sequence are distinct. Find the inner join of A and B (see LinqBegin46), where the pairs must satisfy the following condition: the last digit of the first element of the pair (from A) must match the first digit of the second element of the pair (from B). Present the found join as a sequence of strings containing the first and second elements of the pair, separated by a colon, for example, "49:921". The order of the pairs must be determined by the original order of the elements of sequence A, and for equal first elements of the pairs — by the lexicographical order of the string representations of the second elements.
LinqBegin48°. String sequences A and B are given; all strings in each sequence are distinct, have non-zero length, and contain only digits and uppercase Latin letters. Find the inner join of A and B (see LinqBegin46), each pair of which must contain strings of the same length. Present the found join as a sequence of strings containing the first and second elements of the pair, separated by a colon, for example, "AB:CD". The order of the pairs must be determined by the lexicographical order of the first elements of the pairs, and for equal first elements — by the descending lexicographical order of the second elements of the pairs.
LinqBegin49°. String sequences A, B, and C are given; all strings in each sequence are distinct, have non-zero length, and contain only digits and uppercase Latin letters. Find the inner join of A, B, and C (see LinqBegin46), each triple of which must contain strings starting with the same character. Present the found join as a sequence of strings of the form "EA=EB=EC", where EA, EB, EC are elements from A, B, C respectively. For different elements EA, their original order is preserved; for equal elements EA, the order of the triples is determined by the lexicographical order of the elements EB, and for equal elements EA and EB — by the descending lexicographical order of the elements EC.
LinqBegin50°. String sequences A and B are given; all strings in each sequence are distinct and have non-zero length. Obtain a sequence of strings of the form "E:N", where E denotes one of the elements of sequence A, and N denotes the number of elements from B that start with the same character as the element E (for example, "abc:4"); the number N can be equal to 0. The order of the elements of the obtained sequence must be determined by the original order of the elements of sequence A. Note. Use the GroupJoin method.
LinqBegin51°. Sequences of positive integers A and B are given; all numbers in sequence A are distinct. Obtain a sequence of strings of the form "S:E", where S denotes the sum of those numbers from B that end with the same digit as the number E — one of the elements of sequence A (for example, "74:23"); if for the number E no suitable number from sequence B is found, then specify 0 as S. Arrange the elements of the obtained sequence in ascending order of the found sums, and for equal sums — in descending order of the values of the elements A.
LinqBegin52°. String sequences A and B are given; all strings in each sequence are distinct, have non-zero length, and contain only digits and uppercase Latin letters. Obtain a sequence of all possible combinations of the form "EA=EB", where EA is some element from A, EB is some element from B, and both elements end with a digit (for example, "AF3=D78"). Order the obtained sequence in lexicographical order of the elements EA, and for identical elements EA — in descending lexicographical order of the elements EB. Note. Use the SelectMany and Select methods to enumerate combinations.
LinqBegin53°. Integer sequences A and B are given. Obtain a sequence of all distinct sums where the first term is taken from A and the second from B. Order the obtained sequence in ascending order.
LinqBegin54°. String sequences A and B are given; all strings in each sequence are distinct, have non-zero length, and contain only digits and uppercase Latin letters. Find the sequence of all pairs of strings satisfying the following conditions: the first element of the pair belongs to sequence A, and the second either is one of the elements of sequence B starting with the same character as the first element of the pair, or is an empty string (if B does not contain any suitable string). The resulting sequence is called the left outer join of sequences A and B by the key defined by the first characters of the source strings. Present the found join as a sequence of strings of the form "EA.EB", where EA is an element from A, and EB is either one of the corresponding elements from B, or an empty string. Arrange the elements of the obtained string sequence in lexicographical order. Note. Use the GroupJoin, DefaultIfEmpty, Select, and SelectMany methods.
LinqBegin55°. Sequences of positive integers A and B are given; all numbers in each sequence are distinct. Find the left outer join of A and B (see LinqBegin54), where the pairs must satisfy the following condition: both elements of the pair end with the same digit. Present the found join as a sequence of strings of the form "EA:EB", where EA is a number from A, and EB is either one of the corresponding numbers from B, or 0 (if B does not contain numbers corresponding to EA). Arrange the elements of the obtained sequence in descending order of the numbers EA, and for equal numbers EA — in ascending order of the numbers EB.
LinqBegin56°. An integer sequence A is given. Group the elements of sequence A that end with the same digit, and based on this grouping, obtain a sequence of strings of the form "D:S", where D is the grouping key (i.e., some digit that ends at least one number of sequence A), and S is the sum of all numbers from A that end with the digit D. Order the obtained sequence by ascending keys. Note. Use the GroupBy method.
LinqBegin57°. An integer sequence is given. Among all elements of the sequence ending with the same digit, select the maximum one. Order the obtained sequence of maximum elements by ascending their last digits.
LinqBegin58°. A sequence of non-empty strings is given. Among all strings starting with the same character, select the longest one. If there are several such strings, select the first one in the order of their sequence in the source sequence. Order the obtained sequence of strings by ascending the codes of their initial characters.
LinqBegin59°. A sequence of non-empty strings containing only uppercase Latin letters is given. Among all strings of the same length, select the first one in lexicographical order. Order the obtained sequence of strings by descending their lengths.
LinqBegin60°. A sequence of non-empty strings A containing only uppercase Latin letters is given. For all strings starting with the same letter, determine their total length and obtain a sequence of strings of the form "S-C", where S is the total length of all strings from A that start with the letter C. Order the obtained sequence by descending the numerical values of the sums, and for equal sum values — by ascending the codes of the characters C.
|