Last active
January 15, 2022 08:48
-
-
Save ololx/f891f3f7d6e68e38a323c48a230535f5 to your computer and use it in GitHub Desktop.
Unitarius home tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Задание 2 | |
## Описание задания | |
Имеется массив A элементов (a(i)) длиной n. | |
Нужно переставить первые m элементов в конец массива. | |
После перестановки порядок следования элементов должен сохраниться. | |
Требуется предложить такой алгоритм перестановки элементов, чтобы общее количество действий K было пропорционально длине массива n, т.е., K = a*n, где a – некая константа. | |
В алгоритме можно пользоваться только простыми переменными (скалярными). Использование дополнительных вспомогательных массивов не допускается. | |
Число m – произвольное, т.е. куски {1, 2, .., m} и {m+1, m+2, .., n} массива имеют в общем случае разную длину. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Решение задания 2 | |
## Ссылка на решение https://onlinegdb.com/bU4Rw_hBv | |
## Пример решения | |
/****************************************************************************** | |
Online Java Compiler. | |
Code, Compile, Run and Debug java program online. | |
Write your code in this editor and press "Run" button to execute it. | |
*******************************************************************************/ | |
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
int[] a = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
a = leftShift(a, 3); | |
System.out.println("Total elements in A = " + a.length + "\nElements:"); | |
Arrays.stream(a).forEach(element -> System.out.println(element)); | |
} | |
/** | |
* Returns an array (A) according to the following rules: | |
* <ul> | |
* <li> | |
* If A is null then a new empty array will be returned. | |
* </li> | |
* <li> | |
* If A is empty or the size of the elements | |
* is greater than the size of A or less than 1, | |
* then the original A will be returned. | |
* </li> | |
* <li> | |
* In any other case, the permutation will be performed | |
* and the modified A will be returned. {@see #reverse} | |
* </li> | |
* </ul> | |
* | |
*/ | |
public static int[] leftShift(int a[], int m) { | |
if (a == null) { | |
a = new int[0]; | |
} | |
if (a.length == 0 || a.length <= m || m < 1) { | |
return a; | |
} | |
a = reverse(a, 0, m - 1); | |
a = reverse(a, m, a.length-1); | |
a = reverse(a, 0, a.length-1); | |
return a; | |
} | |
/** | |
* Returns a reversible array | |
* | |
*/ | |
private static int[] reverse(int a[], int startPos, int endPos) { | |
for (int currPos = startPos; currPos <= (startPos + endPos)/2; currPos++) { | |
int temp = a[startPos + endPos - currPos]; | |
a[startPos + endPos - currPos] = a[currPos]; | |
a[currPos] = temp; | |
} | |
return a; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Задание 3 | |
## Ссылка на задание https://www.db-fiddle.com/f/8yF6W4i7X1Rar9vAauy898/0 | |
## Описание задания | |
/* | |
-------------- | |
- Задание 1. - | |
-------------- | |
Имеется список авторов (таблица A) и список книг (таблица B). | |
Таблица A: | |
Author Birthday | |
Иванов 01.01.1950 | |
Петров 31.12.1960 | |
Сидоров 05.05.1965 | |
Синицын 01.01.1970 | |
Таблица B: | |
Author Book | |
Воробьев Книга 4 | |
Воробьев Книга 5 | |
Воробьев Книга 6 | |
Иванов Книга 1 | |
Сидоров Книга 2 | |
Сидоров Книга 3 | |
Написать SQL-запрос для получения результирующего списка с полями Author, Birthday, Book. В списке должны присутствовать все авторы, которые встречаются и в таблице A и в таблице B, а также все имеющиеся сведения о них из таблиц A и B. | |
Результат запроса: | |
Author Birthday Book | |
Воробьев Книга 4 | |
Воробьев Книга 5 | |
Воробьев Книга 6 | |
Иванов 01.01.1950 Книга 1 | |
Петров 31.12.1960 | |
Сидоров 05.05.1965 Книга 2 | |
Сидоров 05.05.1965 Книга 3 | |
Синицын 01.01.1970 | |
-------------- | |
- Задание 2. - | |
-------------- | |
Те же исходные таблицы, см. Задание 1. | |
Получить результирующий список с полями Author, Birthday, Book. В списке должны присутствовать только те авторы, которые написали более одной книги. | |
Результат запроса: | |
Author Birthday Book | |
Воробьев Книга 4 | |
Воробьев Книга 5 | |
Воробьев Книга 6 | |
Сидоров 05.05.1965 Книга 2 | |
Сидоров 05.05.1965 Книга 3 | |
*/ | |
CREATE TABLE Taba ( | |
"author" VARCHAR(20), | |
"birthday" VARCHAR(20) | |
); | |
INSERT INTO Taba | |
("author", "birthday") | |
VALUES | |
('Иванов', '01.01.1950'), | |
('Петров', '31.12.1960'), | |
('Сидоров', '05.05.1965'), | |
('Синицын', '01.01.1970'); | |
CREATE TABLE Tabb ( | |
"author" VARCHAR(20), | |
"book" VARCHAR(20) | |
); | |
INSERT INTO Tabb | |
("author", "book") | |
VALUES | |
('Воробьев', 'Книга 4'), | |
('Воробьев', 'Книга 5'), | |
('Воробьев', 'Книга 6'), | |
('Иванов', 'Книга 1'), | |
('Сидоров', 'Книга 2'), | |
('Сидоров', 'Книга 3'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Решение задания 3.1 | |
## Ссылка на решение https://www.db-fiddle.com/f/8yF6W4i7X1Rar9vAauy898/2 | |
## Пример решения | |
/* | |
* Решение задания 1 | |
*/ | |
SELECT COALESCE(BOOK.author, AUTHOR.author) author, | |
AUTHOR.birthday, | |
BOOK.book | |
FROM Taba AUTHOR | |
FULL JOIN Tabb AS BOOK | |
ON AUTHOR.author = BOOK.author | |
ORDER BY 1, 2, 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Решение задания 3.2 | |
# Вариант 1 | |
## Ссылка на решение https://www.db-fiddle.com/f/8yF6W4i7X1Rar9vAauy898/5 | |
## Пример решения | |
/* | |
* Решение задания 2 (первый вариант) | |
*/ | |
SELECT AUTHOR_PRODUCTIVE.author, | |
AUTHOR_PRODUCTIVE.birthday, | |
AUTHOR_PRODUCTIVE.book | |
FROM ( | |
SELECT BOOK.author, AUTHOR.birthday, BOOK.book, | |
count(book.book) over (partition by book.author) publications | |
from Tabb BOOK | |
full join Taba AUTHOR | |
on BOOK.author = AUTHOR.author | |
) AUTHOR_PRODUCTIVE | |
WHERE AUTHOR_PRODUCTIVE.publications > 1 | |
ORDER BY 1,2,3 | |
# Вариант 2 | |
## Ссылка на решение https://www.db-fiddle.com/f/8yF6W4i7X1Rar9vAauy898/3 | |
## Пример решения | |
/* | |
* Решение задания 2 (второй вариант) | |
*/ | |
SELECT BOOK.author, AUTHOR.birthday, BOOK.book from Tabb BOOK | |
RIGHT JOIN ( | |
SELECT BOOK.author, | |
AUTHOR.birthday, | |
count(BOOK.book) | |
FROM Taba AUTHOR | |
FULL JOIN Tabb AS BOOK | |
ON AUTHOR.author = BOOK.author | |
GROUP BY BOOK.author, AUTHOR.birthday | |
HAVING count(BOOK.book) > 1 | |
) AUTHOR | |
ON BOOK.author = AUTHOR.author | |
ORDER BY 1,2,3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment