This module contains six assignments aimed at practicing Python's fundamental operators.
- Solution
- Description:
- Run the project by clicking on the debug icon (bug) to the right of the project run button.
- Observe the result of the program in the console.
- Use Step Over to proceed to the next step of the program execution.
- Observe the console and the flow of the program execution.
- Attach a screenshot of the written code.
- Solution
- Description: Task "Are All Equal?"
- The program receives 3 integers as input and assigns them to variables
first
,second
, andthird
respectively. - Write a conditional construct (using if, elif, else) that outputs the number of equal numbers among the three entered.
- If all numbers are equal, output 3.
- If at least 2 out of the 3 numbers are equal, output 2.
- If there are no equal numbers, output 0.
- The program receives 3 integers as input and assigns them to variables
- Solution
- Description:
- Given a list of numbers:
[42, 69, 322, 13, 0, 99, -5, 9, 8, 7, -6, 5]
, you need to write only positive numbers from this list until you encounter a negative number or the list ends. - Steps:
- Assign the initial list to the variable
my_list
. - Write a while loop with conditions corresponding to the task.
- Use break/continue operators in the loop according to the task conditions.
- Assign the initial list to the variable
- Given a list of numbers:
- Solution
- Description: Task "It's Not That Simple":
- Given a list of numbers:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- Create a second list
primes
containing only prime numbers from this list. - Create a third list
not_primes
containing all non-prime numbers. - Output the lists
primes
andnot_primes
to the console. - Steps:
- Create empty lists
primes
andnot_primes
. - Iterate over the
numbers
list using a for loop. - Use another nested for loop to find divisors for the number from the first loop.
- Mark the number as prime using a variable
is_prime
, setting it to True before the check. - During the check, append the numbers from
numbers
toprimes
ornot_primes
lists based on the value ofis_prime
after the check (True - inprimes
, False - innot_primes
). - Output the lists
primes
andnot_primes
to the console.
- Create empty lists
- Given a list of numbers:
- Solution
- Description: Task "Matrix in Reality":
- Write a function
get_matrix
with three parametersn
,m
, andvalue
that creates a matrix (nested list) of sizen
rows andm
columns, filled with valuesvalue
, and returns this matrix as a result. - Steps:
- Declare the function
get_matrix
with parametersn
,m
, andvalue
. - Create an empty list
matrix
inside the functionget_matrix
. - Write the first (outer) for loop for the number of rows in the matrix,
n
repetitions. - In the first loop, add an empty list to the
matrix
. - Write the second (inner) for loop for the number of columns in the matrix,
m
repetitions. - In the second loop, fill the previously added empty list with values
value
. - After all loops, return the value of the variable
matrix
. - Output the result of the
get_matrix
function to the console.
- Declare the function
- Write a function
- Solution
- Description:
Task "Ancient Cipher Too Complex":
- You went on a journey to an uninhabited island and, of course, on one of your forays into the jungle, you fell into a trap set by a local tribe. To your surprise, the tribe had good mathematicians who were also creative. You realized this when, after wandering for a long time, you came across gates (the way out of the trap) with two stone inserts for numbers.
- In the first insert, the stones with the number constantly changed randomly from 3 to 20, while the second insert was always empty.
- Fortunately for you, next to the less successful and now silent travelers, there was a papyrus that had the rules for solving this "puzzle" written on it (too bad they figured it out too late).
- You need to write pairs of numbers in the second insert so that the number from the first insert is divisible by the sum of their values without a remainder.
- Example of divisibility (no remainder): 1 + 2 = 3 (sum of the pair) 9 / 3 = 3 (exactly 3 without a remainder) 9 is divisible by 3 without a remainder (9 is a multiple of 3)
- Example 1: 9 - the number from the first insert 1218273645 - the required password (1 and 2, 1 and 8, 2 and 7, 3 and 6, 4 and 5 - pairs; the number 9 is divisible by the sum of each pair)
- Example 2: 11 - the number from the first insert 11029384756 - the required password (1 and 10, 2 and 9, 3 and 8, 4 and 7, 5 and 6 - pairs; the number 11 is divisible by the sum of each pair)