📜 ⬆️ ⬇️

Festive biathlon

Programmers' day Happy programmer, colleagues!

I propose in honor of the holiday to brainstorm and participate in biathlon.
Sports: algorithms, SQL. For each of them will be two tasks: simpler and more difficult.
As a reward for the efforts, all participants are guaranteed an improvement in blood circulation in the left hemisphere of the brain (:

Algorithms. Task №1, warm-up


Write a code that finds the number of subsets of the number n , by which this number is divided without remainder.
')
For the number n, the sub number is the number whose record is a substring of the number n. For example, if n equals 1938, then its subnotes will be: 1, 9, 3, 8, 19, 93, 38, 193 and 938. Without a remainder, 1938 is divided into four of these subnumbers: 1, 3, 19, and 38. Accordingly, the result of the program should be the number 4.
If the numbers are repeated, each of them is considered. For example, 101 is divided without a balance by 1, 1 and 01, which means the answer is 3.

As the task is simple, in decisions the brevity or non-standard approach is appreciated.

Algorithms. Problem number 2, more difficult


The management of the financial pyramid decided to assign a numerical rating to each of the participants in the pyramid. If the participant has not yet led anyone into the pyramid, his rating is 1. If the participant already has subordinate participants, then his rating is 1 + the sum of the ratings of his direct subordinates.
Task: find the sum of the ratings of all participants in the financial pyramid.

The source data is provided in the form of an array of n lines, where each line contains n characters. Each character is either '0' or '1'.
The j-th character of the i-th line indicates whether the participant with the number j is the direct subordinate of the participant i.
The j-th character of the j-th line is always '0'. The member tree never has cycles. Each member is always a direct subordinate of at most one other member. There is always only one root member on the member tree.

Sample source data:

"0110" "0000" "0001" "0000" 

The root element in this case is participant 0. He has two subordinates: participants 1 and 2. 1 has no subordinates, 2 has a subordinate participant 3.
Rating 3rd: 1.
Rating 1: 1.
Rating 2nd: 1 + 1 = 2.
Rating 0th: 1 + 1 + 2 = 4.

The result of the code should be the sum of the ratings of all participants, in this case it is: 1 + 1 + 2 + 4 = 8.

SQL. Problem number 1, simpler


Given: two (non-normalized) tables:
Categories (Id, Name)
Books (Id, CategoryId, Name)

In the future, of course, you want to add a third Book_Category table-join, but so far there may be several entries in the Books table with the same Name field value, but different CategoryId.

Task: write a query that selects all the books that are in the category with Id = 1, but which are not in the category with Id = 2.
A small clarification: the DBMS that you use does not know what subqueries are :)

SQL. Problem number 2, more difficult


There is a table Players (Id, Name, Score), where Score is the number of player points. The more points, the higher the player's place in the test table.
Task: write a request that selects players who occupy (dividing) 3rd place.

UPD: Since the task was simpler than expected, a restriction is introduced: you cannot use subqueries. For fun, try to manage joins :)

Sample data:

Players

As you can see, Stepan and Svetlana share 1-2 places, and 3-4 places are shared by Vladimir and Valentin, which the request should return.

The script for creating the table and test data can be downloaded .

At last


Those who do not have an account can send solutions to my email: dmitrii.shevchenko@gmail.com . I do not promise invites (I don’t have them), but in the comments I’ll post the most interesting solutions.

I hope you will like it :)

Source: https://habr.com/ru/post/128289/


All Articles