automated terminal push

This commit is contained in:
2025-06-05 07:33:41 -04:00
parent 19decb39cc
commit 1cb989b04d
619 changed files with 10585 additions and 0 deletions

View File

@@ -0,0 +1,394 @@
# Chapter 8.2. Exam Preparation - Part II
In the current chapter, we will review a **practical exam in Programming Basics**, conducted at SoftUni on December 18, 2016. The problems will give you a good overview of what you can expect at an admission exam in Programming at SoftUni. The exam covers the material studied in the current book.
## Exam Problems
Traditionally, the admission exam at SoftUni consists of **6 practical programming problems**:
- Simple problems (no conditions).
- A problem with a single condition.
- A problem with more complex conditions.
- A problem with a single loop.
- A problem with nested loops (drawing a figure on the console).
- A problem with nested loops and more complex logic.
Let's examine a **real exam topic**, the problems it contains, and their solutions.
## Problem: Distance
Write a program that calculates **what is the distance passed by a car (in kilometers)**, if we know **the initial speed** \(km/h\), **the initial time frame** in minutes, then the **speed is increased by 10%**, **the second time frame**, then the **speed is decreased by 5%**, and the **time until the end** of the trip. To calculate the distance, you need to **convert the minutes into hours** \(for example 70 minutes = 1.1666 hours\).
### Input Data
Read **4 lines** from the console:
* **The start speed in km/h** an integer within the range [**1 … 300**].
* **The first time in minutes** an integer within the range [**1 … 1000**].
* **The second time in minutes** an integer within the range [**1 … 1000**].
* **The third time in minutes** an integer within the range [**1 … 1000**].
### Output Data
Print a number on the console: **the kilometers passed**, formatted up to the **second digit after the decimal point**.
### Sample Input and Output
|Input|Output|Comments|
|-----|------|--------|
|90<br>60<br>70<br>80|330.90|**Distance with initial speed**: 90 km/h \* 1 hour (60 min) = **90 km**<br>**After speed increase**: 90 + 10% = 99.00 km/h \* 1.166 hours (70 min) = **115.50 km**<br>**After speed decrease**: 99 - 5% = 94.05 km/h \* 1.33 hours (80 min) = **125.40 km**<br>**Total number of km passed**: **330.9 km**|
|Input|Output|Comments|
|-----|------|--------|
|140<br>112<br>75<br>190|917.12|**Distance with initial speed**: 140 km/h \* 1.86 hours (112 min) = **261.33 km**<br>**After speed increase**: 140 + 10% = 154.00 km/h \* 1.25 hours (75 min) = **192.5 km**<br>**After speed decrease**: 154.00 - 5% = 146.29 km/h \* 3.16 hours (190 min) = **463.28 km**<br>**Total number of km passed**: **917.1166 km**|
### Hints and Guidelines
Such a description may look **misleading** and incomplete at first glance, which **adds** to the **complexity** of a relatively easy task. Let's **separate** the problem into a few **sub-problems** and try to **solve** each of them one by one, which will lead us to the final result:
* **Receiving** the input data.
* **Execution** of the main programming logic.
* **Calculation** and shaping up the final result.
**The main** part of the programming logic is to calculate what will be the **distance passed after all speed changes**. As during **execution** of the program, part of the data that we have is modified, we could **separate** the program code into a few logically separated parts:
* **Calculation** of the **distance** passed with initial speed.
* Change of **speed** and calculation of the **distance** passed.
* Last change of **speed** and **calculation**.
* **Summing up**.
On condition for **input** will be submitted **four** separate rows, for this reason, we will have to call the function **`input()`** four times:
![](/assets/chapter-8-2-images/01.Distance-01.png)
Next, we have to **convert the input data** to a suitable **type** so we can do the needed computations. We choose type **`int`** because in the description of the problem is mentioned that the input data is going to be in a **determined interval**, for which this data type is enough. We perform the data **conversion** as follows:
![](/assets/chapter-8-2-images/01.Distance-02.png)
In this way, we solved successfully the **first sub-problem** - receiving the input data.
We initially **store** one **variable** that will be used multiple times. This centralization approach gives us **flexibility** and the **possibility** to **modify** the final result of the program with minimum effort. In case we need to change the value, we must do it in **only one place in the code**, which saves us time and effort:
![](/assets/chapter-8-2-images/01.Distance-03.png)
<table>
<tr>
<td width=10%><img src="assets/alert-icon.png" style="max-width:50px" /></td>
<td><strong>Avoiding repetitive code</strong> (centralization of the program logic) in the tasks that we examine in the present book may look unnecessary at first glance, but this approach is very important upon building large applications in a real work environment, and its exercising in an initial stage of training will help you build a quality programming style.
</td>
</tr>
</table>
We calculate the **travel time** (in hours) by **dividing the time by 60** (minutes in an hour). The **travel distance** is calculated by **multiplying the starting speed by the time passed** (in hours). After that, we change the speed by increasing it by **10%**(on condition), as per the task description. Calculating the **percentage**, as well as the following **distances** passed, is done in the following way:
* **The time frame** (in hours) is calculated by **dividing** the provided time frame in minutes by the minutes that are contained in an hour (60).
* **The distance passed** is calculated by **multiplying** the time frame (in hours) by the speed that is obtained after the increase.
* The next step is to **decrease the speed** by **5%**, as per the problem description.
* We calculate the **remaining distance** in the manner described in the first two points.
![](/assets/chapter-8-2-images/01.Distance-04.png)
Up until now, we were able to **solve two** of the **most important sub-problems**, namely the **receiving data input** and **their processing**. What remains is to **calculate the final result**. As by the description, we are required to **format it** up to **2** symbols after the decimal point, we can do this in the following **manner**:
![](/assets/chapter-8-2-images/01.Distance-05.png)
In case you worked accurately and wrote the program using the input data given in the task description, you will be convinced that it works properly.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1060#0](https://judge.softuni.org/Contests/Practice/Index/1060#0).
## Problem: Change Tiles
Haralambi has some **savings** that he wants to use to **change the tiles** on the bathroom floor. The **floor is rectangular**, and the **tiles are triangular**. Write a program that **calculates if his savings will be sufficient**. **The width and length of the floor are submitted**, as well as **one of the sides of the triangle with its height towards it**. We must **calculate how many tiles are needed,** to cover the floor. The **number** of tiles **must be rounded up to the higher integer** and **5 more tiles must be added** as spare tiles. Also, **we have submitted** **the price per tile** and **the amount paid for the work** of a workman.
### Input Data
Read **7 lines** from the console:
* **The savings**.
* **The width** of the floor.
* **The length** of the floor.
* **The side** of the triangle.
* **The height** of the triangle.
* **The price** of a tile.
* **The sum** for the workman.
**All** numbers are real numbers within the range [**0.00 … 5000.00**].
### Output Data
In this problem, our currency will be **lv**, which is BGN (Bulgarian lev).
The following must be printed on the console as a **single line**:
* If the money **is sufficient**:
* “{Remaining funds} left.”
* If the money **IS NOT sufficient**:
* “You'll need {Insufficient funds} lv more.”
The result must be **formatted up to the second symbol** after the decimal point.
### Sample Input and Output
|Input|Output|Comments|
|-----|------|--------|
|500<br>3<br>2.5<br>0.5<br>0.7<br>7.80<br>100|25.60 lv left.|**Floor area** &rarr; 3 \* 2.5 = **7.5**<br>**Tile area** &rarr; 0.5 \* 0.7 / 2 = **0.175**<br>**Needed tiles** &rarr; 7.5 / 0.175 = 42.857… = **43 + 5 spare tiles** = **48**<br>**Total amount** &rarr; 48 \* 7.8 + 100 (workman) = **474.4**<br>**474.4 < 500** &rarr; **25.60 lv (BGN) left**|
|Input|Output|Comments|
|-----|------|--------|
|1000<br>5.55<br>8.95<br>0.90<br>0.85<br>13.99<br>321|You'll need 1209.65 lv more.|**Floor area** &rarr; 5.55 \* 8.95 = **49.67249**<br>**Tile area** &rarr; 0.9 \* 0.85 / 2 = **0.3825**<br>**Needed tiles** &rarr; 49.67249 / 0.3825 = 129.86… = **130 + 5 spare tiles** = **135**<br>**Total amount** &rarr; 135 \* 13.99 + 321 (workman) = **2209.65**<br>**2209.65 > 1000** &rarr; **1209.65 lv (BGN) are insufficient**|
### Hints and Guidelines
The following task requires our problem to accept more input data and to perform a larger amount of calculations, even though the solution is **identical**. Accepting the input data is done **familiarly**. Pay attention that in the **input** part of the description is said that all input data will be **real numbers** and for this reason, we will use **`float`** type.
Now that we already have everything for executing the programming logic, we can move to the following part. How can we **calculate** what is the **needed** number of tiles that will be sufficient to cover the entire floor? The requirement is that tiles have a **triangular** shape, which can confuse, but practically, the task needs just **simple calculations**. We can calculate the **common part of the floor** by the formula for finding a rectangle area, as well as the **area of a single tile** using the relevant formula for the triangle area.
To calculate the **number of tiles** that are needed, **we divide the floor area by the area of a single tile** (we should not forget to add the 5 additional tiles, that were mentioned in the requirements).
<table>
<tr>
<td width="10%"><img src="assets/alert-icon.png" style="max-width:50px" /></td>
<td>Pay attention that the requirements state that we should round up the number of tiles, obtained upon the division, up to the higher number, and then we should add 5. Find more information about the system functionality that does that: <code><strong>math.ceil(…)</strong></code>.
</td>
</tr>
</table>
We can find the final result by **calculating the total amount** that is needed to cover the entire floor, by **summing up the tile price and the price that will be paid to the workman**, that we have from the input data. We can figure out that **the total costs** for tiles can be calculated by **multiplying the number of tiles by the price per tile**. We will find out whether the amount that we have will be sufficient by comparing the savings (based on the input data) and the total costs:
![](/assets/chapter-8-2-images/02.Change-Tiles-01.png)
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1060#1](https://judge.softuni.org/Contests/Practice/Index/1060#1).
## Problem: Flowers
A flowers shop offers 3 types of flowers: **chrysanthemums**, **roses**, and **tulips**. The prices depend on the season. In this problem, our currency will be **lv**, which is BGN (Bulgarian lev).
|Season|Chrysanthemums|Roses|Tulips|
|:---:|:---:|:---:|:---:|
|spring / summer<br>autumn / winter|2.00 lv./pc.<br>3.75 lv./pc.|4.10 lv./pc.<br>4.50 lv./pc.|2.50 lv./pc.<br>4.15 lv./pc.|
On holidays, the prices of all flowers are **increased by 15%.** The following **discounts** are offered:
* For purchasing more than 7 tulips in spring **5% of the price** of the whole bouquet.
* For purchasing 10 or more roses in winter **10% of the price** of the whole bouquet.
* For purchasing more than 20 flowers in total in any season **20% of the price** of the whole bouquet.
**Discounts are made in the above-described order and can be combined! All discounts are valid after increasing the price on a holiday!**
The price for arranging a bouquet is always **2 lv** (BGN). Write a program that calculates the **price of a bouquet**.
### Input Data
The input is read from the **console** and contains **exactly 5 arguments**:
* **The number** of the purchased **chrysanthemums** integer number inside the interval of [**0 … 200**].
* **The number** of the purchased **roses** integer number inside the interval of [**0 … 200**].
* **The number** of the purchased **tulips** integer number inside the interval of [**0 … 200**].
* **The season** [**Spring, Summer, Autumn, Winter**].
* **If the day is a holiday** [**Y - yes / N - no**].
### Output Data
Print on the console 1 number **the price of flowers**, formatted up to the second digit after the decimal point.
### Sample Input and Output
|Input|Output|Comments|
|-----|------|--------|
|2<br>4<br>8<br>Spring<br>Y<br>|46.14|**Price**: 2\*2.00 + 4\*4.10 + 8\*2.50 = 40.40 BGN.<br>**Holiday**: 40.40 + 15% = 46.46 BGN.<br>**5% discount** for more than 7 tulips in spring: 44.14<br>The flowers are in total 20 or less: **no discount**<br>44.14 + 2 **for arranging** = 46.14 BGN.|
|Input|Output|Comments|
|-----|------|--------|
|3<br>10<br>9<br>Winter<br>N<br>|69.39|**Price**: 3\*3.75 + 10\*4.50 + 9\*4.15 = 93.60 BGN.<br>**No holiday**: no increase in price<br>**10% discount** for 10 or more roses in winter: 84.24<br>The flowers are in total over 20: **20% discount** = 67.392<br>67.392 + 2 **for arranging** = 69.392 BGN.|
|Input|Output|
|-----|------|
|10<br>10<br>10<br>Autumn<br>N|101.20|
### Hints and Guidelines
After carefully reading the requirements, we understand that once again we need to do **simple calculations**, however, this time we will need **additional** logical **conditions**. We need to pay more **attention** to the moment of **making changes** in the final price, to be able to properly build the logic of our program. Again, the bold text gives us sufficient **guidelines** on how to proceed. For a start, we will separate the already **defined** values in **variables**, as we did in the previous tasks:
![](/assets/chapter-8-2-images/03.Flowers-01.png)
We will also do the same for the rest of the defined values:
![](/assets/chapter-8-2-images/03.Flowers-02.png)
Our next sub-task is to **read** properly **the input** data from the console. We will do that in an already familiar manner but this way **we combine two** separate functions - one to **read** a line from the console and another to **convert** in a numeric data type:
![](/assets/chapter-8-2-images/03.Flowers-03.png)
Let's think of the most appropriate way to **structure** our programming logic. By the requirements, it becomes clear that the path of the program is divided mainly into two parts: **spring / summer** and **autumn / winter**. We can do the separation by a conditional statement, by storing variables in advance for the **prices** of the individual flowers, as well as for the **final result**:
![](/assets/chapter-8-2-images/03.Flowers-04.png)
What remains is to perform **a few checks** regarding **the discounts** of the different types of flowers, depending on the season, and to modify the final result.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1060#2](https://judge.softuni.org/Contests/Practice/Index/1060#2).
## Problem: Grades
Write a program that **calculates statistics for grades** in an exam. In the beginning, the program receives the **number of students** who attended the exam and for **each student their grade**. In the end, the program must **print the percentage of students** that have grades between 2.00 and 2.99, between 3.00 and 3.99, between 4.00 and 4.99, 5.00 or more, as well as the **average grade** of the exam.
### Input Data
On the console are being read a **sequence of numbers, each on a different row**:
* On the first line **the number of students who attended the exam** an integer within the range [**1 … 1000**].
* For **each student** on a separate line **the grade on the exam** a real number within the range [**2.00 … 6.00**].
### Output Data
Print on the console **5 lines** that hold the following information:
* "Top students: {percentage of students with a grade of 5.00 or more}%".
* "Between 4.00 and 4.99: {between 4.00 and 4.99 included}%".
* "Between 3.00 and 3.99: {between 3.00 and 3.99 included}%".
* "Fail: {less than 3.00}%".
* "Average: {average grade}".
The results must be **formatted up to the second symbol** after the decimal point.
### Sample Input and Output
|Input|Output|Comments|
|-----|------|--------|
|10<br>3.00<br>2.99<br>5.68<br>3.01<br>4<br>4<br>6.00<br>4.50<br>2.44<br>5<br>|Top students: 30.00%<br>Between 4.00 and 4.99: 30.00%<br>Between 3.00 and 3.99: 20.00%<br>Fail: 20.00%<br>Average: 4.06|5 or more - **3 students** = 30% of 10<br>Between 4.00 and 4.99 - **3 students** = 30% of 10<br>Between 3.00 and 3.99 - **2 students** = 20% of 10<br>Under 3 - **2 students** = 20% of 10<br>The average grade is: 3 + 2.99 + 5.68 + 3.01 + 4 + 4 + 6 + 4.50 + 2.44 + 5 = 40.62 / 10 = 4.062|
|Input|Output|
|-----|------|
|6<br>2<br>3<br>4<br>5<br>6<br>2.2|Top students: 33.33%<br>Between 4.00 and 4.99: 16.67%<br>Between 3.00 and 3.99: 16.67%<br>Fail: 33.33%<br>Average: 3.70|
### Hints and Guidelines
By requirements, we see that **first**, we will receive **the number** of students, and then, **their grades**. For that reason, **first**, we will obtain **the number** of students, as we will convert the value we have read to **`int`**. To process the grades themselves, we will use a **`for`** loop. Every iteration of the loop will read and process one grade:
![](/assets/chapter-8-2-images/05.Grades-01.png)
Before executing the code of the **`for`** loop, we will create variables where we will store **the number of students** for each group: poor results (up to 2.99), results from 3 to 3.99, from 4 to 4.99, and grades above 5. We will also need one more variable, where we will store **the sum of all grades**, via which we will calculate the average grade of all students:
![](/assets/chapter-8-2-images/05.Grades-02.png)
We run the **loop** and inside it, we **declare one more** variable, in which we will store the **currently** entered grade. The variable will be of the type **`float`** and on every iteration, we check **what is its value**. According to this value, **we increase** the number of students in the relevant group by **1**, as we should not forget to also increase the **total** amount of the grades, which we also track:
![](/assets/chapter-8-2-images/05.Grades-03.png)
We can calculate what **percentage** is taken by a particular **group of students** from the total number by **multiplying the number of students** in the relevant group by **100** and then dividing this by the **total number of students**.
**The final result** is formed in a well-known way **up to the second symbol** after the decimal point.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1060#3](https://judge.softuni.org/Contests/Practice/Index/1060#3).
## Problem: Christmas Hat
Write a program that reads from the console an **integer `n`** and draws a **Christmas hat** with a width of **4 \* `n` + 1 columns** and a height of **2 \* `n` + 5 rows**, as in the examples below.
### Input Data
The input is read from the console - one **integer `n`** within the range [**3 … 100**].
### Output Data
Print on the console a **Christmas hat**, exactly like in the examples.
### Sample Input and Output
|Input|Output|
|:-----:|:-----:|
|4|<code>......./&#124;\\.......</code><br><code>.......\\&#124;/.......</code><br><code>.......\*\*\*.......</code><br><code>......\*-\*-\*......</code><br><code>.....\*--\*--\*.....</code><br><code>....\*---\*---\*....</code><br><code>...\*----\*----\*...</code><br><code>..\*-----\*-----\*..</code><br><code>.\*------\*------\*.</code><br><code>\*-------\*-------\*</code></br><code>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*</code><br><code>\*.\*.\*.\*.\*.\*.\*.\*.\*</code><br><code>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*</code>|
|Input|Output|
|:-----:|:-----:|
|7|<code>............./&#124;\\.............</code><br><code>.............\\&#124;/.............</code><br><code>.............\*\*\*.............</code><br><code>............\*-\*-\*............</code><br><code>...........\*--\*--\*...........</code><br><code>..........\*---\*---\*..........</code><br><code>.........\*----\*----\*.........</code><br><code>........\*-----\*-----\*........</code><br><code>.......\*------\*------\*.......</code><br><code>......\*-------\*-------\*......</code><br><code>.....\*--------\*--------\*.....</code><br><code>....\*---------\*---------\*....</code><br><code>...\*----------\*----------\*...</code><br><code>..\*-----------\*-----------\*..</code><br><code>.\*------------\*------------\*.</code><br><code>\*-------------\*-------------\*</code><br><code>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*</code><br><code>\*.\*.\*.\*.\*.\*.\*.\*.\*.\*.\*.\*.\*.\*.\*</code><br><code>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*</code><br>|
### Hints and Guidelines
In tasks requiring **drawing** on the console, most often the user inputs **an integer** that is related to the **total size of the figure** that we need to draw. As the task requirements mention how the total length and width of the figure are calculated, we can use them as **starting points**. In the examples, it is clear that regardless of the input data, we always have the **first two rows** that are almost identical.
<code>......./\|\\.......</code><br><code>.......\\\|/.......</code>
We also notice that the **last three rows** are always present, as **two** of them are completely **the same**.
<code>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*</code><br><code>\*.\*.\*.\*.\*.\*.\*.\*.\*</code><br><code>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*</code>
By these observations, we can come up with the **formula** for the **height of the variable part** of the Christmas hat. We use the formula specified in the task to calculate the total height, by subtracting the size of the unchangeable part. We obtain **`(2 * n + 5) 5`** or **`2 * n`**.
To **draw** the **dynamic** or the variable part of the figure, we will use a **loop**. The size of the loop will be from **0** to the **width** that we have by requirements, namely **`4 * n + 1`**. Since we will use this formula in **a few places** in the code, it is a good practice to declare it in a **separate variable**. Before running the loop, we should **declare variables** for the **number** of individual symbols that participate in the dynamic part: **dots** and **dashes**. By analyzing examples, we can also prepare formulas for the **starting values** of these variables. Initially, the **dashes** are **0**, but we can calculate the number of **dots** by subtracting **3** from the **total width** (the number of symbols that are building the top of the Christmas hat) and then **dividing by 2**, as the number of dots on both sides of the hat is the same.
<code>.......\*\*\*.......</code><br><code>......\*-\*-\*......</code><br><code>.....\*--\*--\*.....</code><br><code>....\*---\*---\*....</code><br><code>...\*----\*----\*...</code><br><code>..\*-----\*-----\*..</code><br><code>.\*------\*------\*.</code><br><code>\*-------\*-------\*</code>
What remains is to execute the body of the loop, as **after each** drawing we **decrease** the number of dots by **1** and **increase the number of dashes** by **1**. Let's not forget to draw one **star** between each of them. The sequence of drawing in the body of the loop is the following:
* Symbol string of dots
* Star
* Symbol string of dashes
* Star
* Symbol string of dashes
* Star
* Symbol string of dots
In case we have worked properly, we will obtain figures identical to those in the examples.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1060#4](https://judge.softuni.org/Contests/Practice/Index/1060#4).
## Problem: Letters Combinations
Write a program that prints on the console **all combinations of 3 letters** within a specified range, by skipping the combinations **containing a certain letter**. Finally, print the number of printed combinations.
### Input Data
The input is read from the console and contains **exactly 3 lines**:
* A small letter from the English alphabet between **'a'** and **'z'**.
* A small letter from the English alphabet between the **first letter** and **'z'**.
* A small letter from the English alphabet from **'a'** to **'z'** as the combinations containing this letter are skipped.
### Output Data
Print on one line **all combinations** corresponding to the requirements, followed by **their number**, separated by a space.
### Sample Input and Output
|Input|Output|Comments|
|---|---|---|
|a<br>c<br>b|aaa aac aca acc caa cac cca ccc 8|All possible combinations with letters '**a**', '**b**' and '**c**' are:<br>aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc<br>The combinations **containing 'b' are not valid**.<br>**8** valid combinations remain.|
|Input|Output|
|---|---|
|f<br>k<br>h|fff ffg ffi ffj ffk fgf fgg fgi fgj fgk fif fig fii fij fik fjf fjg fji fjj fjk fkf fkg fki fkj fkk gff gfg gfi gfj gfk ggf ggg ggi ggj ggk gif gig gii gij gik gjf gjg gji gjj gjk gkf gkg gki gkj gkk iff ifg ifi ifj ifk igf igg igi igj igk iif iig iii iij iik ijf ijg iji ijj ijk ikf ikg iki ikj ikk jff jfg jfi jfj jfk jgf jgg jgi jgj jgk jif jig jii jij jik jjf jjg jji jjj jjk jkf jkg jki jkj jkk kff kfg kfi kfj kfk kgf kgg kgi kgj kgk kif kig kii kij kik kjf kjg kji kjj kjk kkf kkg kki kkj kkk 125|
|Input|Output|
|---|---|
|a<br>c<br>z|aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc 27|
### Hints and Guidelines
By requirements, we have input data on **3 rows**, each of which is represented by one character of the **ASCII table** ([https://www.asciitable.com/](https://www.asciitable.com/)). We could use the already defined function in Python, **`ord(…)`** through which we will receive the ASCII code of the symbol:
![](/assets/chapter-8-2-images/06.Letters-01.png)
Let's think of how we can achieve the **final result**. In case the task requirement is to print all characters, from the starting to the end one (by skipping a particular letter), what should we do?
The easiest and most efficient way is to use a **loop**, by passing through **all characters** and printing those that are **different** from the **letter** that we need to skip. In Python, we can go around all the symbols from 'a' to 'z' the following way:
![](/assets/chapter-8-2-images/06.Letters-02.png)
The function **`chr(…)`** will convert the received ASCII code into a symbol. The result of running the code is all letters from **a** to **z** included, printed on a single line and separated by spaces. Does this look like the final result of our task? We must find a **way** to print **3 characters**, as required, instead of **1**. Running such a program very much looks like a slot machine. We often win in slots, if we arrange a few identical characters in a row. Let's say that the machine has space for three characters. When we **stop** on a particular **character** in the first place, the other two places will **continue** rolling characters among all possible ones. In our case, **all possible characters** are the letters from the starting to the end one, entered by the user, and the solution of our program is identical to the way a slot machine works.
We use a **loop** that runs through **all characters** from the starting to the end letter (included). On **each iteration** of the **first** loop, we run a **second** one with the same parameters (but **only if** the letter of the first loop is valid, i.e. does not match the one that we must exclude, by requirements). In each iteration of the **second** loop, we run **one** more with the **same parameters** and the same **condition**. This way we have three nested loops and in the body of **the latter** we will add the symbols to the final result:
![](/assets/chapter-8-2-images/06.Letters-03.png)
Let's not forget that we also need to print the **total number of valid combinations** that we have found, and they must be printed on the **same line**, separated by a space. We leave this sub-task to the reader.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1060#5](https://judge.softuni.org/Contests/Practice/Index/1060#5).

View File

@@ -0,0 +1,667 @@
# Chapter 8.1. Practical Exam Preparation - Part I
In **the present chapter**, we will examine a few **problems** with a level of **difficulty** that can be expected in **the problems** of the practical **exam** in “Programming Basics”. We will **review** and **practice** all the knowledge that was gained from this book and through the "Programming Basics" course.
## The "Programming Basics" Practical Exam
The **Practical Exam** contains **6** problems included, and you will have **4 hours** to solve them. **Each** of the exam problems will **cover** one of the studied **topics** during the course. Problem topics are as follows:
- Problem with simple calculations (without conditions)
- Problem with simple condition
- Problem with more complex conditions
- Problem with a simple loop
- Problem with nested loops (drawing a figure on the console)
- Problem with nested loops and more complex logic
## The Online Evaluation System (Judge)
**All exams and problems** are automatically **tested** through the online **Judge system**: [https://judge.softuni.org](https://judge.softuni.org). For **each** of the problems, there are **visible** (zero points) tests that help you understand what is expected of the problem and fix your mistakes, as well as **competition** tests that are **hidden** and check if your solution is working properly.
**How** does the testing in the **Judge** system work? **You upload** the source code and from the menu below you choose to compile as a **Python** program. The program is being **tested** with a series of tests, giving **points** for each **successful** test.
## Problems with Simple Calculations
**The first** problem of the "Programming Basics" Practical Exam covers **simple calculations without checks and loops**. Here are a few examples:
### Problem: Triangle Area
<table>
<tr>
<td width="60%">
<b>Triangle in the plain</b> is defined by the coordinates of its three vertices. First, <b>the vertex (x1, y1)</b> is set. Then the other two vertices are set: <b>(x2, y2)</b> and <b>(x3, y3)</b> which <b>lie on a common horizontal line</b> i.e. they have the same Y coordinates). Write a program that calculates <b>the triangle area</b> by the coordinates of its three vertices.
</td>
<td>
<img src="assets/chapter-8-1-images/01.Triangle-area-01.png"/>
</td>
</tr>
</table>
#### Input Data
From the console **6 integers** are read (one per line):
**x1, y1, x2, y2, x3, y3.**
- All input numbers are in the range [**-1000 … 1000**].
- It's guaranteed that **y2 = y3**.
#### Output Data
Print on the console **the triangle area**.
#### Sample Input and Output
|Input|Output|Visualization|Comments|
|-----|------|-------------|--------|
|5<br>-2<br>6<br>1<br>1<br>1|7.5|![](/assets/chapter-8-1-images/01.Triangle-area-01.png)|The side of the triangle **a** = 6 - 1 = **5**<br>The height of the triangle **h** = 1 - (-2) = **3**<br>The area of the triangle **S** = a \* h / 2 = 5 \* 3 / 2 = **7.5**|
|Input|Output|Visualization|Comments|
|-----|------|-------------|--------|
|4<br>1<br>-1<br>-3<br>3<br>-3|8|![](/assets/chapter-8-1-images/01.Triangle-area-02.png)|The side of the triangle **a** = 3 - (-1) = **4**<br>The height of the triangle **h** = 1 - (-3) = **4**<br>The area of the triangle **S** = a \* h / 2 = 4 \* 4 / 2 = **8**|
#### Hints and Guidelines
It is extremely important in these types of tasks, in which some coordinates are given, to pay attention to the **order** in which they are submitted, as well as to correctly understand which of the coordinates we will use and in what way. In this case, the input is in order **x1, y1, x2, y2, x3, y3**. If we do not follow this sequence, the solution becomes wrong. First, we write the code that reads the input data:
![](/assets/chapter-8-1-images/01.Triangle-area-03.png)
We have to calculate **the side** and **the height** of the triangle. From the examples and the condition **`y2 = y3`** we notice that one **side** is always parallel to the horizontal axis. It means that its **length** is equal to the length of the segment between its coordinates **`x2` and `x3`**, which is equal to the difference between the larger and the smaller coordinates. Similarly, we can calculate **the height**. It will always be equal to the difference between **`y1` and `y2`**(or **`y3`**, as they are equal). Since we do not know if **`x2`** is greater than **`x3`**, or **`y1`** will be below or above the triangle side, we will use **the absolute values** of the difference to always get positive numbers because one segment cannot have a negative length.
![](/assets/chapter-8-1-images/01.Triangle-area-04.png)
We will use the formula familiar to us from school for finding the **area of a triangle** to calculate the area.
![](/assets/chapter-8-1-images/01.Triangle-area-05.png)
The only thing left is to print the area on the console.
![](/assets/chapter-8-1-images/01.Triangle-area-06.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#0](https://judge.softuni.org/Contests/Practice/Index/1059#0).
### Problem: Bricks
Construction workers have to transfer a total of **x bricks**. **The workers** are **w** and work simultaneously. They transport the bricks in trolleys, each with a **capacity of m** bricks. Write a program that reads the integers **x**, **w**, and **m**, and calculates **what is the minimum number of courses** the workers need to do to transport the bricks.
#### Input Data
From the console **3 integers** are read (one per line):
- **The number of bricks x** is read from the first line.
- **The number of workers w** is read from the second line.
- **The capacity of the trolley m** is read from the third line.
All input numbers are integers in the range [**1 … 1000**].
#### Output Data
Print on the console **the minimum number of courses** needed to transport the bricks.
#### Sample Input and Output
|Input|Output|Comments|
|-----|------|--------|
|120<br>2<br>30|2|We have **2** workers, each transporting **30** bricks per course. In total, workers are transporting **60** bricks per course. To transport **120** bricks, exactly **2** courses are needed.|
|Input|Output|Comments|
|-----|------|--------|
|355<br>3<br>10|12|We have **3** workers, each transporting **10** bricks per course. In total, workers are transporting **30** bricks per course. To transport **355** bricks, exactly **12** courses are needed: **11** complete courses carry **330** bricks and the last **12th** course carries the last **25** bricks.|
|Input|Output|Comments|
|-----|------|--------|
|5<br>12<br>30|1|We have **5** workers, each transporting **30** bricks per course. In total, workers are transporting **150** bricks per course. To transport **5** bricks, only **1** course is enough (although incomplete, with only 5 bricks).|
#### Hints and Guidelines
The input is standard, and we only have to pay attention to the sequence in which we read the data:
![](/assets/chapter-8-1-images/02.Bricks-01.png)
We calculate how many **bricks** the workers transport in a single course:
![](/assets/chapter-8-1-images/02.Bricks-02.png)
By dividing the total number of **bricks transported for 1 course**, we will obtain the number of **courses** needed to carry them. We will use the **`math.ceil (…)`** function to round the result up. **Remember** to add **`import math`** at the beginning of the working file so that the **`math.ceil (…)`** function can work. When the bricks can be transferred with **an exact number of courses**, the division will return a whole number and there will be nothing to round. Accordingly, if not, the result of the division will be **the number of exact courses** but with a decimal fraction. The decimal part will be rounded up and we will get the required **1 course** for the remaining bricks.
![](/assets/chapter-8-1-images/02.Bricks-03.png)
In the end, we print the result on the console:
![](/assets/chapter-8-1-images/02.Bricks-04.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#1](https://judge.softuni.org/Contests/Practice/Index/1059#1).
## Problems with Simple Conditions
**The second** problem of the "Programming Basics" Practical Exam covers **conditional statements and simple calculations**. Here are a few examples:
### Problem: Point on Segment
On a horizontal line, **a horizontal segment** is placed, set with the **x** coordinates at both ends: **first** and **second**. **A point** is located **on** the same horizontal line and is set with its **x coordinate**. Write a program that checks whether the point is **inside or outside of the segment** and calculates **the distance to the nearest end** of the segment.
#### Input Data
From the console **3 integers** are read (one per line):
- On the first line is the number first - **one end of the segment**.
- On the second line is the number second - **the other end of the segment**.
- On the third line is the number point - **the location of the point**.
All inputs are integers in the range [**-1000 … 1000**].
#### Output Data
Print the result on the console:
- On the first line, print "**in**" or "**out**" whether the point is inside or outside the segment.
- On the second line, print the distance from the point to the nearest end of the segment.
#### Sample Input and Output
|Input|Output|Visualization|
|-----|------|-------------|
|10<br>5<br>7|in<br>2|![](/assets/chapter-8-1-images/03.Point-on-segment-01.png)|
|Input|Output|Visualization|
|-----|------|-------------|
|8<br>10<br>5|out<br>3|![](/assets/chapter-8-1-images/03.Point-on-segment-02.png)|
|Input|Output|Visualization|
|-----|------|-------------|
|1<br>-2<br>3|out<br>2|![](/assets/chapter-8-1-images/03.Point-on-segment-03.png)|
#### Hints and Guidelines
First, we read the input:
![](/assets/chapter-8-1-images/03.Point-on-segment-04.png)
Since we do not know which **point** is on the left and which is on the right, we will create two variables to mark this. Since **the left point** is always the one with the smaller **x coordinate**, we will use the **`min(…)`** function to find it. Accordingly, **the right point** is always the one with a larger **x coordinate** and we will use the **`max(…)`** function. We will also find the distance from **the point x** to **the two points**. Since we do not know their position relative to each other, we will use the **`abs(…)`** function to get a positive result:
![](/assets/chapter-8-1-images/03.Point-on-segment-05.png)
The shorter of the two **distances** we will find by using **`min(…)`**:
![](/assets/chapter-8-1-images/03.Point-on-segment-06.png)
What remains is to find whether **the point** is on or out of the line. The point will be **on the line** always when it **matches** one of the other two points or its x coordinate lies **between them**. Otherwise, the point is **outside the line**. After checking, we display one of the two messages ("**in**" or "**out**"), depending on which condition is satisfied:
![](/assets/chapter-8-1-images/03.Point-on-segment-07.png)
Finally, we print **the distance** found before.
![](/assets/chapter-8-1-images/03.Point-on-segment-08.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#2](https://judge.softuni.org/Contests/Practice/Index/1059#2).
### Problem: Point in Figure
Write a program that checks if a point (with coordinates **x** and **y**) is **inside** or **outside** of the given figure:
![](/assets/chapter-8-1-images/04.Point-in-figure-01.png)
#### Input Data
From the console are read **two integers** (one per line): **x** and **y**.
All inputs are integers in the range **[-1000 … 1000]**.
#### Output Data
Print on the console "**in**" or "**out**" whether the point is **inside** or **outside** the figure (the outline is inside).
#### Sample Input and Output
|Input|Output|Input|Output|
|----|----|----|----|
|8<br>-5|in|6<br>-3|in|
|Input|Output|Input|Output|
|----|----|----|----|
|11<br>-5|out|11<br>2|out|
#### Hints and Guidelines
To find out if **the point** is inside of the figure, we will divide **the figure** into 2 rectangles:
![](/assets/chapter-8-1-images/04.Point-in-figure-02.png)
![](/assets/chapter-8-1-images/04.Point-in-figure-03.png)
A sufficient condition is the **point** to be located in one of them and be in the **figure**.
We read the input from the console:
![](/assets/chapter-8-1-images/04.Point-in-figure-04.png)
We will initialize two variables that will mark whether **the point** is in one of the rectangles:
![](/assets/chapter-8-1-images/04.Point-in-figure-05.png)
When printing the message, we will check whether any of the variables has accepted a value of **`True`**. It's enough **only one** of them to be **`True`** so that the point is in the figure.
![](/assets/chapter-8-1-images/04.Point-in-figure-06.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#3](https://judge.softuni.org/Contests/Practice/Index/1059#3).
## Problems with Complex Conditions
**The third** problem of the "Programming Basics" Practical Exam includes **several nested checks combined with simple calculations**. Here are a few examples:
### Problem: Date After 5 days
There are two numbers **d** (day) and **m** (month) that form **a date**. Write a program that prints the date that will be **after 5 days**. For example, 5 days after **28.03** is the date **2.04**. We assume that the months: April, June, September, and November have 30 days, February has 28 days, and the rest have 31 days. Months to be printed with **leading zero** when they are single-digit numbers (e.g. 01, 08).
#### Input Data
The input is read from the console and consists of two lines:
- On the first line, there is an integer **d** in the interval [**1… 31**] - day. The number of the day does not exceed the number of days in the respective month (e.g. 28 for February).
- On the second line, there is an integer **m** in the interval [**1… 12**] - month. Month 1 is January, month 2 is February,…, month 12 is December. The month may contain a leading zero (e.g. April may be written as 4 or 04).
#### Output Data
Print a single line containing the date after 5 days in the format **day.month** on the console. The month must be a two-digit number with a leading zero, if necessary. The day must be written without a leading zero.
#### Sample Input and Output
|Input|Output|Input|Output|
|-----|------|-----|------|
|28<br>03|2.04|27<br>12|1.01|
|Input|Output|Input|Output|
|-----|------|-----|------|
|25<br>1|30.01|26<br>02|3.03|
#### Hints and Guidelines
We read the input data from the console:
![](/assets/chapter-8-1-images/05.Date-after-5-days-01.png)
To make our checks easier, we will create a variable that will contain the **number of days** that we have in the month we set:
![](/assets/chapter-8-1-images/05.Date-after-5-days-02.png)
We increase the **day** by 5:
![](/assets/chapter-8-1-images/05.Date-after-5-days-03.png)
We check if **the day** has exceeded the number of days in **the month**. If so, we must deduct the days of the month from the obtained day to calculate which day of the next month our day corresponds to:
![](/assets/chapter-8-1-images/05.Date-after-5-days-04.png)
After we have passed to **the next month**, this should be noted by increasing the initial one by 1. We need to check if it has become greater than 12 and if it has, to adjust it. Because we cannot skip more than **one month** when we increase by 5 days, the following check is enough:
![](/assets/chapter-8-1-images/05.Date-after-5-days-05.png)
The only thing that remains is to print the result on the console. It is important to **format the output** correctly to display the leading zero in the first 9 months. This is done by adding a **formatted string** **`%02d`** for the second element:
![](/assets/chapter-8-1-images/05.Date-after-5-days-06.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#4](https://judge.softuni.org/Contests/Practice/Index/1059#4).
### Problem: Sums 3 Numbers
There are **3 integers** given. Write a program that checks if **the sum of two of the numbers is equal to the third one**. For example, if the numbers are **3**, **5**, and **2**, the sum of two of the numbers is equal to the third one: **2 + 3 = 5**.
#### Input Data
From the console are read **three integers**, one per line. The numbers are in the range [**1 … 1000**].
#### Output Data
- Print a single line on the console containing the solution of the problem in the format "**a + b = c**", where **a**, **b** and **c** are among the three input numbers and **a ≤ b**.
- If the problem has no solution, print “**No**” on the console.
#### Sample Input and Output
|Input|Output|Input|Output|
|-----|------|-----|------|
|3<br>5<br>2|2 + 3 = 5|2<br>2<br>4|2 + 2 = 4|
|Input|Output|Input|Output|
|-----|------|-----|------|
|1<br>1<br>5|No|2<br>6<br>3|No|
#### Hints and Guidelines
We take the input from the console:
![](/assets/chapter-8-1-images/06.Sums-3-numbers-01.png)
We have to check if **the sum** of a pair of numbers is equal to the third number. We have three possible cases:
* a + b = c
* a + c = b
* b + c = a
We will write **a template**, which will later be complemented by the required code. If none of the above three conditions is met, we will make our program print "**No**":
![](/assets/chapter-8-1-images/06.Sums-3-numbers-02.png)
Now it remains to understand the order in which **the two addends** will be displayed at the output of the program. For this purpose, we will make a **nested condition**, which checks which of the two numbers is the greater. In the first case, it will be as follows:
![](/assets/chapter-8-1-images/06.Sums-3-numbers-03.png)
Similarly, we will supplement the other two cases. The full code of the program will look like this:
![](/assets/chapter-8-1-images/06.Sums-3-numbers-04.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#5](https://judge.softuni.org/Contests/Practice/Index/1059#5).
## Problems with Simple Loops
**The fourth** problem of the "Programming Basics" Practical Exam includes a **simple loop with simple logic** in it. Here are a few examples:
### Problem: Sums Step 3
There are given **n** integers **a1, a2, …, an**. Calculate the sums:
- **sum1 = a1 + a4 + a7** + … (the numbers are summed, starting from the first one with step of 3).
- **sum2 = a2 + a5 + a8** + … (the numbers are summed, starting from the second one with step of 3).
- **sum3 = a3 + a6 + a9** + … (the numbers are summed, starting from the third one with step of 3).
#### Input Data
The input data is read from the console. The first line contains an integer **n (0 ≤ n ≤ 1000)**. The next **n** lines contain **n** integers in the interval [**-1000 … 1000**]: **a1, a2, …, an**.
#### Output Data
On the console, we should print 3 lines containing the 3 sums in a format such as in the example.
#### Sample Input and Output
|Input|Output|Input|Output|Input|Output|
|-----|------|-----|------|-----|------|
|2<br>3<br>5<br>|sum1 = 3<br>sum2 = 5<br>sum3 = 0|4<br>7<br>-2<br>6<br>12|sum1 = 19<br>sum2 = -2<br>sum3 = 6|5<br>3<br>5<br>2<br>7<br>8|sum1 = 10<br>sum2 = 13<br>sum3 = 2|
#### Hints and Guidelines
We will take **the count of numbers** from the console and will declare **starting values** of the three sums.
![](/assets/chapter-8-1-images/07.Sums-Step-3-01.png)
Since we do not know in advance how many numbers we will process, we will take them one at a time in **a loop** which will be repeated **n times** and we will process them in the body of the loop.
![](/assets/chapter-8-1-images/07.Sums-Step-3-02.png)
To find out which of **the three sums** we need to add the number, we will divide its **sequence number into three** and we will use **the remainder**. We'll use the variable **`i`** which tracks **the number of runs** of the loop, to find out which sequence number we are at. When the remainder of **`i/3`** is **zero**, it means we will add this number to **the first** sum, when it's **1** to **the second**, and when it's **2** to **the third**:
![](/assets/chapter-8-1-images/07.Sums-Step-3-03.png)
Finally, we will print the result on the console in the required **format**:
![](/assets/chapter-8-1-images/07.Sums-Step-3-04.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#6](https://judge.softuni.org/Contests/Practice/Index/1059#6).
### Problem: Increasing Elements
A series of **n** numbers is given: **a1**, **a2**, **…**, **an**. Calculate **the length of the longest increasing sequence** of consecutive elements in the series of numbers.
#### Input Data
The input data is read from the console. The first line contains an integer **n** (**0 ≤ n ≤ 1000**). The next **n** lines contain **n** integers in the interval [**-1000 … 1000**]: **a1**, **a2**, **…**, **an**.
#### Output Data
Print on the console one number **the length** of the longest increasing sequence.
#### Sample Input and Output
|Input|Output|Input|Output|Input|Output|Input|Output|
|-----|------|-----|------|-----|------|-----|------|
|3<br>5<br>2<br>4|2|4<br>2<br>8<br>7<br>6|2|4<br>1<br>2<br>4<br>4|3|4<br>5<br>6<br>7<br>8|4|
#### Hints and Guidelines
To solve this problem, we need to think in a bit **more algorithmic way**. A **sequence of numbers** is given to us, and we need to check whether each **subsequent** one will be **larger than the previous one** and if so, we count how long is the sequence in which this condition is fulfilled. Then we have to find **which sequence** of these is **the longest one**. To do this, let's create some variables that we will use during solving the problem:
![](/assets/chapter-8-1-images/08.Increasing-numbers-01.png)
The variable **`n`** is **the count of numbers** we get from the console. In **`count_current_longest`** we'll keep **the number of elements** in the increasing sequence we are **currently counting**. For example, in the sequence: 5, 6, 1, 2, 3 **`count_current_longest`** will be 2 when we reach **the second element** of the counting (5, **6**, 1, 2, 3) and will become 3 when we have reached **the last element** (5, 6, 1, 2, **3**) because the increasing row 1, 2, 3 has 3 elements. We will use **`count_longest`** to keep **the longest** increasing sequence. The other variables we will use are **`a`** - the number we are at **at the moment**, and **`a_prev`** - **the previous number**, which we will compare with **`a`** to find out whether the row **is growing**.
We begin to run the numbers and check if the present number **`a`** is larger than the previous **`a_prev`**. If this is true, then the row **is growing**, and we need to increase its number by **1**. This is stored in the variable that tracks the length of the sequence we are currently in **`count_current_longest`**. If the number **`a`** **isn't bigger** than the previous one, it means that **a new sequence** starts, and we have to start the count from **1**. Finally, after all the checks are done, **`a_prev`** becomes **the number** that we're **currently** using, and we start the loop from the beginning with **the next** entered **`a`**.
Here is a sample implementation of the algorithm described:
![](/assets/chapter-8-1-images/08.Increasing-numbers-02.png)
What remains is to see which of all sequences is **the longest**. We will do this by checking in the loop if **the sequence** we are **currently** in has become longer than **the longest one by now**. The whole loop will look like this:
![](/assets/chapter-8-1-images/08.Increasing-numbers-03.png)
Finally, we print the length of **the longest** sequence found:
![](/assets/chapter-8-1-images/08.Increasing-numbers-04.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#7](https://judge.softuni.org/Contests/Practice/Index/1059#7).
## Problems for Drawing Figures on The Console
**The fifth** problem of the "Programming Basics" Practical Exam requires **using one or several nested loops for drawing** a figure on the console. Logical reasoning, simple calculations, and conditional statements may be required. The problem tests the ability of students to think logically and invent simple algorithms for solving problems, i.e. to think algorithmically. Here are some examples of Exam Problems:
### Problem: Perfect Diamond
Write a function that takes as a parameter **n** and draws **a perfect diamond** with size **n** as in the examples below.
#### Input Data
The input is an integer **n** in the interval [**1… 1000**].
#### Output Data
The diamond should be printed on the console as in the examples below.
#### Sample Input and Output
|Input|Output|Input|Output|
|-----|------|-----|------|
|2|<code>&nbsp;&#42;&nbsp;</code><br><code>&#42;-&#42;</code><br><code>&nbsp;&#42;&nbsp;</code>|3|<code>&nbsp;&nbsp;&#42;&nbsp;&nbsp;</code><br><code>&nbsp;&#42;-&#42;&nbsp;</code><br><code>&#42;-&#42;-&#42;</code><br><code>&nbsp;&#42;-&#42;&nbsp;</code><br><code>&nbsp;&nbsp;&#42;&nbsp;&nbsp;</code><br>|
|Input|Output|Input|Output|
|-----|------|-----|------|
|4|<code>&nbsp;&nbsp;&nbsp;&#42;&nbsp;&nbsp;&nbsp;</code><br><code>&nbsp;&nbsp;&#42;-&#42;&nbsp;&nbsp;</code><br><code>&nbsp;&#42;-&#42;-&#42;&nbsp;</code><br><code>&#42;-&#42;-&#42;-&#42;</code><br><code>&nbsp;&#42;-&#42;-&#42;&nbsp;</code><br><code>&nbsp;&nbsp;&#42;-&#42;&nbsp;&nbsp;</code><br><code>&nbsp;&nbsp;&nbsp;&#42;&nbsp;&nbsp;&nbsp;</code><br>|5|<code>&nbsp;&nbsp;&nbsp;&nbsp;&#42;&nbsp;&nbsp;&nbsp;&nbsp;</code><br><code>&nbsp;&nbsp;&nbsp;&#42;-&#42;&nbsp;&nbsp;&nbsp;</code><br><code>&nbsp;&nbsp;&#42;-&#42;-&#42;&nbsp;&nbsp;</code><br><code>&nbsp;&#42;-&#42;-&#42;-&#42;&nbsp;</code><br><code>&#42;-&#42;-&#42;-&#42;-&#42;</code><br><code>&nbsp;&#42;-&#42;-&#42;-&#42;&nbsp;</code><br><code>&nbsp;&nbsp;&#42;-&#42;-&#42;&nbsp;&nbsp;</code><br><code>&nbsp;&nbsp;&nbsp;&#42;-&#42;&nbsp;&nbsp;&nbsp;</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&#42;&nbsp;&nbsp;&nbsp;&nbsp;</code><br>|
#### Hints and Guidelines
In tasks for drawing figures, the most important thing to consider is **the sequence** in which we will draw. Which elements are **repeated** and with what **steps**? We can see that **the top and the bottom** parts of the diamond are **the same**. The easiest way to solve the problem is by creating **a loop** that draws **the upper part**, and then **another loop** that draws **the bottom part** (opposite to the top one).
First, we read the number **`n`** from the console:
![](/assets/chapter-8-1-images/09.Perfect-diamond-01.png)
We start drawing **the upper half** of the diamond. We see that **each line** starts with some **whitespaces and <code>*</code>**. If we take a closer look, we will notice that **the whitespaces** are always equal to **`n - index of row - 1`** (the first row is n-1, the second n-2, etc.). We will start by drawing the number of **whitespaces** and **the first asterisk**. Let's not forget to use **`print(…, end='')`** instead of just **`print(…)`** to stay on **the same line**. At the end of the line, we write **`print ()`** to move to a **new line**. Notice that we start counting from **1, not 0**. Then all we have to do is add **`-*`** a few times to **complete the line**.
Here is the complete code for drawing **the upper part of the diamond**:
![](/assets/chapter-8-1-images/09.Perfect-diamond-02.png)
What remains is to **complete each line** with the required number of **`-*`** elements. On each row we have to add **`i - 1`** such **items** (on the first 1-1 -> 0, the second -> 1, etc.)
Here is the complete code for drawing **the upper part of the diamond**:
![](/assets/chapter-8-1-images/09.Perfect-diamond-03.png)
To draw **the bottom part** of the diamond, we have to reverse **the upper part**. We'll count from **`n - 2`** because if we start from **`n - 1`**, we will draw the middle row twice. Be sure to add **`reversed (…)`** to **`range (…)`** of the main loop.
Here is the code for drawing **the bottom part of the diamond**:
![](/assets/chapter-8-1-images/09.Perfect-diamond-04.png)
What remains is **to assemble the whole program** by first reading the input, printing the top part of the diamond, and then the bottom part of the diamond.
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#8](https://judge.softuni.org/Contests/Practice/Index/1059#8).
### Problem: Rectangle with Stars
Write a function that takes as a parameter an integer **n** and draws **a rectangle** with size **n with 2 asterisks in the center** as in the examples below.
#### Input Data
The parameter is an integer **n** in the range [**2 … 1000**].
#### Output Data
The rectangle should be printed on the console as in the examples below.
#### Sample Input and Output
|Input|Output|Input|Output|
|-----|------|-----|------|
|2|<code>&#37;&#37;&#37;&#37;</code><br><code>&#37;&#42;&#42;&#37;</code><br><code>&#37;&#37;&#37;&#37;</code><br>|3|<code>&#37;&#37;&#37;&#37;&#37;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&nbsp;&#42;&#42;&nbsp;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&#37;&#37;&#37;&#37;&#37;</code><br>|
|Input|Output|Input|Output|
|-----|------|-----|------|
|4|<code>&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&nbsp;&nbsp;&#42;&#42;&nbsp;&nbsp;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;</code><br>|5|<code>&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&#42;&#42;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#37;</code><br><code>&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;&#37;</code><br>|
#### Hints and Guidelines
We read the input data from the task:
![](/assets/chapter-8-1-images/10.Rectangle-with-stars-01.png)
The first thing we can easily notice is that **the first and the last rows** contain **`2 * n`** symbols **`%`**. We will start with this and then draw the middle part of the rectangle:
![](/assets/chapter-8-1-images/10.Rectangle-with-stars-02.png)
From the examples, we see that **the middle** part of the figure always has an **odd number** of rows. Note that when an **even number** is set, the number of rows is equal to **the previous odd** number (2 -> 1, 4 -> 3, etc.). We create a variable that represents the number of rows that our rectangle will have, and correct it if the number **`n` is even**. Then we will draw **a rectangle without the asterisks**. Each row has for **the beginning and at the end** the symbol **`%`** and between them **`2 * n - 2`** whitespaces (the width is **`2 * n`** and we subtract 2 for the two percent at the end). Do not forget to move the code or **the last line after the loop**:
![](/assets/chapter-8-1-images/10.Rectangle-with-stars-03.png)
We can **start and test the code so far**. Everything without the two asterisks in the middle should work correctly.
Now, **in the body** of the loop let's add the **asterisks**. We'll check if we're on the **middle row**. If we are in the middle, (we will check this by comparing **`i`** with **`floor(num_rows / 2)`**), we will draw **the row** together **with the asterisks**, if not we will draw **a normal row**. **Remember** to add **`from the math import floor`** at the beginning of the work file so that we can use the **`floor(…)`** function. The line with the asterisks has **`n-2` whitespaces** (**`n`** is half the length and we remove the asterisk and the percentage), **two asterisks**, and again **`n-2` whitespaces**. We leave out of the check the two percent at the beginning and the end of the row:
![](/assets/chapter-8-1-images/10.Rectangle-with-stars-04.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#9](https://judge.softuni.org/Contests/Practice/Index/1059#9).
## Problems with Nested Loops
**The last** (sixth) problem of the "Programming Basics" Practical Exam requires using of **several nested loops and more complex logic inside them**. The problems examine participants' ability to think algorithmically and to solve non-trivial coding problems that require nested loops. Here are some examples of exam problems.
### Problem: Increasing 4 Numbers
For a given pair of numbers, **a** and **b** generate all four numbers **n1, n2, n3, n4,** for which **a ≤ n1 < n2 < n3 < n4 ≤ b**.
#### Input Data
The input contains two integers **a** and **b** in the interval [**0… 1000**], one per line.
#### Output Data
The output contains all **numbers in batches of four**, in ascending order, one per line.
#### Sample Input and Output
|Input|Output|Input|Output|
|-----|------|-----|------|
|3<br>7|3 4 5 6<br>3 4 5 7<br>3 4 6 7<br>3 5 6 7<br>4 5 6 7|15<br>20|15 16 17 18<br>15 16 17 19<br>15 16 17 20<br>15 16 18 19<br>15 16 18 20<br>15 16 19 20<br>15 17 18 19<br>15 17 18 20<br>15 17 19 20<br>15 18 19 20<br>16 17 18 19<br>16 17 18 20<br>16 17 19 20<br>16 18 19 20<br>17 18 19 20<br>|
|Input|Output|Input|Output|
|-----|------|-----|------|
|5<br>7|No|10<br>13|10 11 12 13|
#### Hints and Guidelines
We read the input data from the console. We also create the additional variable **`count`**, which will keep track of whether **an existing number of ranges**:
![](/assets/chapter-8-1-images/11.Increasing-4-numbers-01.png)
We will most easily solve the problem if we logically divide it **into parts**. If we are required to draw all the rows from a number between **`a`** and **`b`**, we will do it using **one loop** that takes all the numbers from **`a`** to **`b`**. Let's think about how to do this with a **series of two numbers**. The answer is easy we will use **nested loops**:
![](/assets/chapter-8-1-images/11.Increasing-4-numbers-02.png)
We can test the incomplete program to see if it's accurate so far. It must print all pairs of numbers **`i`**, **`j`** for which **`i ≤ j`**.
Since each **next number** of the row must be **greater** than **the previous one**, the second loop will run around **`i + 1`** (the next greater number). Accordingly, if **there is no sequence** of two incremental numbers (**`a`** and **`b`** are equal), the second loop **will not be fulfilled**, and nothing will be printed on the console.
**Similarly**, what remains is to implement **the nested loops** for **four numbers**. We will add an **increase of the counter** (**`count`**) that we initialized to know if **there is such a sequence**:
![](/assets/chapter-8-1-images/11.Increasing-4-numbers-03.png)
Finally, we will check if **the counter** is equal to **0** and we will print "**No**" on the console accordingly, if so.
![](/assets/chapter-8-1-images/11.Increasing-4-numbers-04.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#10](https://judge.softuni.org/Contests/Practice/Index/1059#10).
### Problem: Generating Rectangles
By a given number **n** and **a minimum area m**, generate all possible rectangles with integer coordinates in the range [**-n … n**] with an area of at least **m**. The generated rectangles must be printed in the following format:
**(left, top) (right, bottom) -> area**
Rectangles are defined using the top left and bottom right corners. The following inequalities are in effect:
- **-n ≤ left < right n**
- **-n top < bottom n**
#### Input Data
Two numbers are entered from the console, one per line:
- Integer **n** in the range [**1 100**] - sets the minimum and maximum coordinate of a vertex.
- Integer **m** in the interval [**0 50 000**] - sets the minimum area of the generated rectangles.
#### Output Data
- The described rectangles should be printed on the console in a format such as in the examples below.
- If there are no rectangles for the specified **n** and **m**, then print "**No**".
- The order of rectangles in the output is not important.
#### Sample Input and Output
|Input|Output|Input|Output|
|-----|------|-----|------|
|1<br>2|(-1, -1) (0, 1) -> 2<br>(-1, -1) (1, 0) -> 2<br>(-1, -1) (1, 1) -> 4<br>(-1, 0) (1, 1) -> 2<br>(0, -1) (1, 1) -> 2|2<br>17|No|
|Input|Output|
|-----|------|
|3<br>36|(-3, -3) (3, 3) -> 36|
#### Hints and Guidelines
Let's read the input from the console. Again, we will create a **counter** in which we will store the number of rectangles found:
![](/assets/chapter-8-1-images/12.Generating-rectangles-01.png)
It is very important to be able to imagine the problem before we begin to solve it. In our case, it is required to search for rectangles in a coordinate system. The thing we know is that the **left point** will always have the coordinate **`x`, smaller** than **the right** one. Accordingly, **the upper one** will always have a smaller **`y`** coordinate than **the lower one**. To find all the rectangles, we'll have to create **a loop** similar to the previous problem, but this time, **not every next loop** will start from **the next number** because some of **the coordinates** can be equal (for example **`left`** and **`top`**):
![](/assets/chapter-8-1-images/12.Generating-rectangles-02.png)
With the variables **`left`** and **`right`** we will follow the coordinates along the **horizontal**, and with **`top`** and **`bottom`** - along the **vertical**.
The important thing here is knowing the corresponding coordinates so we can correctly calculate the sides of the rectangle. Now we have to find **the area of the rectangle** and check if it is **greater than** or **equal** to **`m`**. One **side** will be **the difference between `left` and `right`** and **the other one between `top` and `bottom`**. Since the coordinates may be eventually interchanged, we will use **absolute values**. Again, we add **the counter** in the loop, counting **only the rectangles** we write. It is important to note that the writing order is **`left`**, **`top`**, **`right`**, **`bottom`**, as it is set in the problem's description:
![](/assets/chapter-8-1-images/12.Generating-rectangles-03.png)
Finally, we print “**No**”, if there are no such rectangles.
![](/assets/chapter-8-1-images/12.Generating-rectangles-04.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1059#11](https://judge.softuni.org/Contests/Practice/Index/1059#11).