a For each of the tasks below write a oneline Python express
a) For each of the tasks below, write a one-line Python expression that will achieve the result. Code the Python way; the simpler, the better.
– Retrieve the n largest items from a list or a tuple of numbers lt e.g., n,lt = 3,[1,6,3,11,14,7,0,2,19,10] -> [11,14,19]
– Create a new string with the words of string s in reverse order e.g., s = \'dog bird cow ant\' -> \'ant cow bird dog\'
– Add all the comma-separated numbers contained in a string e.g., s= \"1.7, 5, 3.3\" -> 10 2 / 4
(b) Use list comprehension to realize each of the tasks below.
– Create a list of all the divisors of a given number n (excl. 1 and n) e.g., n = 99 -> [3, 9, 11, 33]
– Create a list of cubes of the numbers in list l, discarding non-numbers e.g., l = [(), 2, \'tr\', (4-7j), int(\'5\'), 22//3, (3**2), 10] -> [8, 125, 343, 729, 1000]
– Create a list of unique triples (a,b,c) such that a2+b2=c2, 0a,b,cN. e.g., N = 16 -> [(3,4,5), (5,12,13), (6,8,10), (9,12,15)]
Solution
IF (Boolean Expression 1)
THEN
Statement Sequence 1;
ELSIF (Boolean Expression 2) THEN
Statement Sequence 2;
...
ELSIF (Boolean Expression n) THEN
Statement Sequence n;
ELSE
Statement Sequence n + 1;
END;
All the ELSIFs and therefore the ELSE area unit nonmandatory, counting on the logic needed by the program. Note that the shape (Boolean Expression i) is employed here rather than a particular comparison as all told the examples up to now.
There was a short discussion of mathematician expressions in section one.7. the aim of this section is to specify additional specifically what represent valid mathematician expressions in Modula-2, for rather more elaborate ones than these will be utilized in a spread of program structures.
This brings U.S.A. to the question you will are able to raise after you browse the heading for this section and therefore the initial IF. \"What specifically is that this factor referred to as a \"Boolean expression\"? 2 connected definitions area unit useful:
An Modula-2 expression is of kind mathematician if it will be evaluated unambiguously as \"true\" or \"false\".
A Modula-2 variable of kind mathematician will have one in all the 2 values TRUE, or FALSE (all 3 words area unit normal identifiers).
Previous examples have already illustrated the utilization of the reserved symbols = and <> (or #) used as comparisons to create mathematician expressions and therefore the reserved words OR and AND as connectives to mix 2 mathematician expressions. Here could be a complete list of reserved words and symbols for such expressions, along with a translation.
Relational operators
= equals
< less than
> greater than
<= less than or adequate
>= greater than or adequate
<> or # not equal
Other operators (connectives)
AND or & both of the conditions should be true
OR at least one in all the conditions should be true
NOT or ~ reverses the worth of the subsequent logical expression
As in alternative Modula-2 expressions, any combination of those is allowed, and parentheses will be accustomed modification the cosmos of analysis. Here area unit a number of examples to review the utilization of these:
Expression mathematician worth
5 <= five TRUE
(10 < 5) AND (3 < 4) FALSE
(\'Y\' = \'y\') OR FALSE FALSE
NOT (5 = 6) TRUE
(4 = 5) AND ((3 = 7) OR TRUE) FALSE
NOTES: 1. The order of analysis is:
First NOT (~)
Second *, /, DIV, MOD, AND (&)
Third +, -, OR
Last the relative operators =, <> (or #), >, >=, <, <=
2. once analysis reaches AN AND, if the left expression is fake, the proper expression isn\'t evaluated. Likewise, if analysis reaches AN OR, if the left expression is TRUE the proper expression isn\'t evaluated. Most laptop languages can\'t be relied upon to require these shortcuts and will assess all components of each mathematician expression. Another additional formal manner of swing this is often to mention that if p and alphabetic character area unit Modula-2 mathematician expressions, then:
p AND alphabetic character means: IF p THEN alphabetic character ELSE FALSE
p OR alphabetic character means: IF p THEN TRUE ELSE alphabetic character
3. Expressions separated by AND or OR ought to be boxed in parentheses. If this is often not done, the proper order of analysis might not be followed, for the statement might not be syntactically correct. If x, y, and z area unit numbers, then
x < y AND y > z is illegitimate, but
(x < y) AND (y > z) is correct,
because the compiler would decide to use the right order of operations and decide to interpret the primary of those as x < (y AND y) > z that makes no sense in the least.
Here area unit some additional examples parenthetically these rules.
Example 1.
(6 DIV three + a pair of = 5) OR NOT (5 <= 7) & (3 < 10)
would be evaluated as:
(6 DIV three + a pair of = 5) OR FALSE & (3 < 10)
(2 + a pair of = 5) OR FALSE (& not evaluated)
(4 = 5) OR FALSE
FALSE

