Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University), 1998–2018

 

Tasks | Task groups | Func

PrevNext


Functions

This task group is intended for using in the Python and Ruby versions of Programming Taskbook. Tasks included in this group are similar to the tasks of the Proc group but they are formulated with taking into account Python and Ruby features connected with passing parameters and returning function values.

Functions with numeric parameters

Func1. Write an integer function Sign(X) that returns the following value:

−1,    if X < 0;        0,    if X = 0;        1,    if X > 0

(X is a real-valued parameter). Using this function, evaluate an expression Sign(A) + Sign(B) for given real numbers A and B.

Func2. Write an integer function RootCount(A, BC) that returns the amount of roots of the quadratic equation A·x2 + B·x + C = 0 (AB, C are real-valued parameters, A ≠ 0). Using this function, find the amount of roots for each of three quadratic equations with given coefficients. Note that the amount of roots is determined by the value of a discriminant:

D = B2 − 4·A·C.

Func3. Write a real-valued function CircleS(R) that returns the area of a circle of radius R (R is a real number). Using this function, find the areas of three circles of given radiuses. Note that the area of a circle of radius R can be found by formula S = π·R2. Use 3.14 for a value of π.

Func4. Write a real-valued function RingS(R1, R2) that returns the area of a ring bounded by a concentric circles of radiuses R1 and R2 (R1 and R2 are real numbers, R1 > R2). Using this function, find the areas of three rings with given outer and inner radiuses. Note that the area of a circle of radius R can be found by formula S = π·R2. Use 3.14 for a value of π.

Func5. Write a real-valued function TriangleP(a, h) that returns the perimeter of an isosceles triangle with given base a and altitude h (a and h are real numbers). Using this function, find the perimeters of three triangles with given bases and altitudes. Note that the leg b of an isosceles triangle can be found by the Pythagorean theorem:

b2 = (a/2)2 + h2.

Func6. Write an integer function SumRange(A, B) that returns a sum of all integers in the range A to B inclusively (A and B are integers). In the case of A > B the function returns 0. Using this function, find a sum of all integers in the range A to B and in the range B to C provided that integers A, BC are given.

Func7. Write a real-valued function Calc(A, B, Op) that performs an arithmetic operation over nonzero real numbers A and B and returns the result value. An arithmetic operation is determined by integer parameter Op as follows: 1 — subtraction, 2 — multiplication, 3 — division, and addition otherwise. Having input real numbers A, B and integers N1, N2N3 and using this function, perform over given A, B three operations determined by given N1, N2N3. Output the result value of each operation.

Func8. Write an integer function Quarter(x, y) that returns the number of a coordinate quarter containing a point with nonzero real-valued coordinates (xy). Using this function, find the numbers of coordinate quarters containing each of three points with given nonzero coordinates.

Func9. Write a logical function Even(K) that returns True, if an integer parameter K is an even number, and False otherwise. Using this function, find the amount of even numbers in a given sequence of 10 integers.

Func10. Write a logical function IsSquare(K) that returns True, if an positive integer parameter K is a square of some integer, and False otherwise. Using this function, find the amount of squares in a given sequence of 10 positive integers.

Func11. Write a logical function IsPower5(K) that returns True, if an positive integer parameter K is equal to 5 raised to some integer power, and False otherwise. Using this function, find the amount of powers of base 5 in a given sequence of 10 positive integers.

Func12. Write a logical function IsPowerN(K, N) that returns True, if an positive integer parameter K is equal to N (> 1) raised to some integer power, and False otherwise. Having input an integer N (> 1) and a sequence of 10 positive integers and using this function, find the amount of powers of base N in the given sequence.

Func13. Write a logical function IsPrime(N) that returns True, if an integer parameter N (> 1) is a prime number, and False otherwise. Using this function, find the amount of prime numbers in a given sequence of 10 integers greater than 1. Note that an integer (> 1) is called a prime number if it has not positive divisors except 1 and itself.

Func14. Write an integer function DigitCount(K) that returns the amount of digits in the decimal representation of a positive integer K. Using this function, find the amount of digits for each of five given positive integers.

Func15. Write an integer function DigitN(K, N) that returns the N-th digit in the decimal representation of a positive integer K provided that the digits are numbered from right to left. If the amount of digits is less than N then the function returns −1. Using this function, output sequentially 1st, 2nd, 3rd, 4th, 5th digit for each of five given positive integers K1, K2, …, K5.

Func16. Write a logical function IsPalindrome(K) that returns True, if the decimal representation of a positive parameter K is a palindrome (i. e., it is read equally both from left to right and from right to left), and False otherwise. Using this function, find the amount of palindromes in a given sequence of 10 positive integers.

Func17. Write a real-valued function DegToRad(D) that converts the angle value D in degrees into the one in radians (D is a real number, 0 ≤ D < 360). Note that 180° = π radians and use 3.14 for a value of π. Using this function, convert five given angles from degrees into radians.

Func18. Write a real-valued function RadToDeg(R) that converts the angle value R in radians into the one in degrees (R is a real number, 0 ≤ R < 2·π). Note that 180° = π radians and use 3.14 for a value of π. Using this function, convert five given angles from radians into degrees.

Func19. Write a real-valued function Fact(N) that returns a factorial of a positive integer N: N! = 1·2·…·N (the real return type allows to avoid the integer overflow during the calculation of the factorials for large values of the parameter N). Using this function, find the factorials of five given integers.

Func20. Write a real-valued function Fact2(N) that returns a double factorial N!!:

N!! = 1·3·5·…·N,    if N is an odd number;
N!! = 2·4·6·…·N    otherwise

(N is a positive integer; the real return type allows to avoid the integer overflow during the calculation of the double factorials for large values of N). Using this function, find the double factorials of five given integers.

Func21. Write an integer function Fib(N) that returns the value of N-th term of the sequence of the Fibonacci numbers. The Fibonacci numbers FK are defined as follows:

F1 = 1,        F2 = 1,        FK = FK−2 + FK−1,    K = 3, 4, … .

Using this function, find five Fibonacci numbers with given order numbers N1, N2, …, N5.

Func22. Write a function PowerA3(A) that returns the third degree of a real number A (A is an input parameter). Using this function, find the third degree of five given real numbers.

Func23. Write a function PowerA234(A) that computes the second, the third, and the fourth degrees of a real number A and returns these degrees as three real numbers (A is an input parameter). Using this function, find the second, the third, and the fourth degrees of five given real numbers.

Func24. Write a function Mean(XY) that computes the arithmetical mean (X+Y)/2 and the geometrical mean (X·Y)1/2 of two positive real numbers X and Y and returns the result as two real numbers (X and Y are input parameters). Using this function, find the arithmetical mean and the geometrical mean of pairs (AB), (AC), (AD) provided that real numbers AB, CD are given.

Func25. Write a function TrianglePS(a) that computes the perimeter P = 3·a and the area S = a2·(3)1/2/4 of an equilateral triangle with the side a and returns the result as two real numbers (a is an input real-valued parameter). Using this function, find the perimeters and the areas of three triangles with the given lengths of the sides.

Func26. Write a function RectPS(x1y1x2y2) that computes and returns the perimeter P and the area S of a rectangle whose opposite vertices have coordinates (x1y1) and (x2y2) and sides are parallel to coordinate axes (x1, y1, x2, y2 are input real-valued parameters). Using this function, find the perimeters and the areas of three rectangles with the given opposite vertices.

Func27. Write a function DigitCS(K) that finds and returns the amount C of digits in the decimal representation of a positive integer K and also the sum S of its digits (K is an input integer parameter). Using this function, find the amount and the sum of digits for each of five given integers.

Func28. Write a function InvDigits(K) that inverts the order of digits of a positive integer K and returns the obtained integer (K is an input parameter). Using this function, invert the order of digits for each of five given integers.

Func29. Write a function AddRightDigit(D, K) that adds a digit D to the right side of the decimal representation of a positive integer K and returns the obtained number (D and K are input integer parameters, the value of D is in the range 0 to 9). Having input an integer K and two one-digit numbers D1, D2 and using two calls of this function, sequentially add the given digits D1, D2 to the right side of the given K and output the result of each adding.

Func30. Write a function AddLeftDigit(D, K) that adds a digit D to the left side of the decimal representation of a positive integer K and returns the obtained number (D and K are input integer parameters, the value of D is in the range 0 to 9). Having input an integer K and two one-digit numbers D1, D2 and using two calls of this function, sequentially add the given digits D1, D2 to the left side of the given K and output the result of each adding.

Func31. Write a function Swap(X, I, J) that exchanges the values of items XI and XJ of a list X of real numbers (I and J are input integer parameters, the function returns the None value). Having input a list of four real numbers and using three calls of this function, sequentially exchange the values of the two first, two last, and two middle items of the given list. Output the new values of the list.

Func32. Write a function Minmax(XIJ) that writes the minimal value of items XI and XJ of a list X to the item XI and writes the maximal value of these items to the item XJ (I and J are input integer parameters, the function returns the None value). Using four calls of this function, find the minimal value and the maximal value among four given real numbers.

Func33. Write a function SortInc3(X) that sorts the list X of three real-valued items in ascending order (the function returns the None value). Using this function, sort each of two given lists X and Y.

Func34. Write a function SortDec3(X) that sorts the list X of three real-valued items in descending order (the function returns the None value). Using this function, sort each of two given lists X and Y.

Func35. Write a function ShiftRight3(X) that performs a right cyclic shift of a list X of three real-valued items: the value of each item should be assigned to the next item and the value of the last item should be assigned to the first item (the function returns the None value). Using this function, perform the right cyclic shift for each of two given lists X and Y.

Func36. Write a function ShiftLeft3(X) that performs a left cyclic shift of a list X of three real-valued items: the value of each item should be assigned to the previous item and the value of the first item should be assigned to the last item (the function returns the None value). Using this function, perform the left cyclic shift for each of two given lists X and Y.

Functions: additional tasks

Func37. Write a real-valued function Power1(A, B) that returns the power AB calculated by the formula AB = exp(B·ln(A)) (A and B are real-valued parameters). In the case of zero-valued or negative parameter A the function returns 0. Having input real numbers PA, BC and using this function, find the powers AP, BP, CP.

Func38. Write a real-valued function Power2(A, N) that returns the power AN calculated by the following formulas:

A0 = 1;
AN = A·A·…·A    (N factors),    if N > 0;
AN = 1/(A·A·…·A)    (|N| factors),    if N < 0

(A is a real-valued parameter, N is an integer parameter). Having input a real number A and integers K, LM and using this function, find the powers AK, AL, AM.

Func39. Using the Power1 and Power2 functions (see Func37 and Func38), write a real-valued function Power3(A, B) that returns the power AB calculated as follows (A and B are real-valued parameters): if B has a zero-valued fractional part then the function Power2(A, N) is called (an integer variable N is equal to B), otherwise the function Power1(A, B) is called. Having input real numbers P, A, BC and using the Power3 function, find the powers AP, BP, CP.

Func40. Write a real-valued function Exp1(x, ε) (x and ε are real numbers, ε > 0) that returns an approximate value of the function exp(x) defined as follows:

exp(x) = 1 + x + x2/(2!) + x3/(3!) + … + xn/(n!) + …

(n! = 1·2·…·n). Stop adding new terms to the sum when the value of the next term will be less than ε. Using this function, find the approximate values of the function exp(x) at a given point x for six given ε.

Func41. Write a real-valued function Sin1(x, ε) (x and ε are real numbers, ε > 0) that returns an approximate value of the function sin(x) defined as follows:

sin(x) = x − x3/(3!) + x5/(5!) − … + (−1)n·xn+1/((2·n+1)!) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function sin(x) at a given point x for six given ε.

Func42. Write a real-valued function Cos1(x, ε) (x and ε are real numbers, ε > 0) that returns an approximate value of the function cos(x) defined as follows:

cos(x) = 1 − x2/(2!) + x4/(4!) − … + (−1)n·xn/((2·n)!) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function cos(x) at a given point x for six given ε.

Func43. Write a real-valued function Ln1(x, ε) (x and ε are real numbers, |x| < 1, ε > 0) that returns an approximate value of the function ln(1 + x) defined as follows:

ln(1 + x) = x − x2/2 + x3/3 − … + (−1)n·xn+1/(n+1) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function ln(1 + x) at a given point x for six given ε.

Func44. Write a real-valued function Atan1(x, ε) (x and ε are real numbers, |x| < 1, ε > 0) that returns an approximate value of the function atan(x) defined as follows:

atan(x) = x − x3/3 + x5/5 − … + (−1)n·xn+1/(2·n+1) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function atan(x) at a given point x for six given ε.

Func45. Write a real-valued function Power4(x, a, ε) (x, a, ε are real numbers, |x| < 1, a, ε > 0) that returns an approximate value of the function (1 + x)a defined as:

(1 + x)a = 1 + a·x + a·(a−1)·x2/(2!) + … + a·(a−1)·…·(an+1)·xn/(n!) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function (1 + x)a at a given point x for a given exponent a and six given ε.

Func46. Write an integer function GCD2(A, B) that returns the greatest common divisor (GCD) of two positive integers A and B. Use the Euclidean algorithm:

GCD(A, B) = GCD(B, A mod B),    if B ≠ 0;        GCD(A, 0) = A,

where "mod" denotes the operator of taking the remainder after integer division. Using this function, find the greatest common divisor for each of pairs (AB), (AC), (AD) provided that integers A, B, CD are given.

Func47. Using the GCD2 function from the task Func46, write a procedure Frac1(a, b, p, q), that simplifies a fraction a/b to the irreducible form p/q (a and b are input integer parameters, p and q are output integer parameters). The sign of a resulting fraction p/q is assigned to its numerator, so q > 0. Using this procedure, find the numerator and the denominator for each of irreducible fractions a/b + c/d, a/b + e/f, a/b + g/h provided that integers a, b, c, d, e, f, gh are given.

Func48. Taking into account that the least common multiple of two positive integers A and B equals A·(B/GCD(AB)), where GCD(AB) is the greatest common divisor of A and B, and using the GCD2 function from the task Func46, write an integer function LCM2(A, B) that returns the least common multiple of A and B. Using this function, find the least common multiple for each of pairs (AB), (AC), (AD) provided that integers A, B, CD are given.

Func49. Taking into account the formula GCD(A, B, C) = GCD(GCD(A, B), C) and using the GCD2 function from the task Func46, write an integer function GCD3(ABC) that returns the greatest common divisor of three positive integers A, BC. Using this function, find the greatest common divisor for each of triples (ABC), (ACD), (BCD) provided that integers A, B, CD are given.

Func50. Write a function TimeToHMS(T) that converts a time interval T (in seconds) into the "hours H, minutes M, seconds S" format and returns the values H, M, S (TH, MS are integers). Using this function, find the amount of hours, minutes and seconds for each of five given time intervals T1, T2, …, T5.

Func51. Write a function IncTime(HMST) that increases a time interval in hours H, minutes M, seconds S on T seconds and returns new values of hours, minutes, and seconds (all numbers are positive integers). Having input hours H, minutes M, seconds S (as integers) and an integer T and using the IncTime function, increase the given time interval on T seconds and output new values of H, MS.

Func52. Write a logical function IsLeapYear(Y) that returns True if a year Y (a positive integer parameter) is a leap year, and False otherwise. Output the return values of this function for five given values of the parameter Y. Note that a year is a leap year if it is divisible by 4 except for years that are divisible by 100 and are not divisible by 400.

Func53. Using the IsLeapYear function from the task Func52, write an integer function MonthDays(M, Y) that returns the amount of days for M-th month of year Y (M and Y are integers, 1 ≤ M ≤ 12, Y > 0). Output the return value of this function for a given year Y and each of given months M1, M2M3.

Func54. Using the MonthDays function from the task Func53, write a function PrevDate(D, M, Y) that changes a correct date, represented at the "day D, month number M, year Y" format, to a previous one and returns new values of day, month, and year (all numbers are integers). Apply this function to three given dates and output resulting previous ones.

Func55. Using the MonthDays function from the task Func53, write a function NextDate(D, M, Y) that changes a correct date, represented at the "day D, month number M, year Y" format, to a next one and returns new values of day, month, and year (all numbers are integers). Apply this function to three given dates and output resulting next ones.

Func56. Write a real-valued function Leng(xA, yA, xB, yB) that returns the length of a segment AB with given coordinates of its endpoints:

|AB| = ((xA − xB)2 + (yA − yB)2)1/2

(xA, yA, xB, yB are real-valued parameters). Using this function, find the lengths of segments AB, ACAD provided that coordinates of points AB, CD are given.

Func57. Using the Leng function from the task Func56, write a real-valued function Perim(xA, yA, xB, yB, xC, yC) that returns the perimeter of a triangle ABC with given coordinates of its vertices (xA, yA, xB, yB, xC, yC are real-valued parameters). Using the Perim function, find the perimeters of triangles ABC, ABDACD provided that coordinates of points AB, CD are given.

Func58. Using the Leng and Perim functions from the tasks Func56 and Func57, write a real-valued function Area(xA, yA, xB, yB, xC, yC) that returns the area of a triangle ABC:

SABC = (p·(p−|AB|)·(p−|AC|)·(p−|BC|))1/2,

where p is the half-perimeter. Using the Area function, find the areas of triangles ABC, ABDACD provided that coordinates of points ABCD are given.

Func59. Using the Leng and Area functions from the tasks Func56 and Func58, write a real-valued function Dist(xP, yP, xA, yA, xB, yB) that returns the distance D(P, AB) between a point P and a line AB:

D(P, AB) = 2·SPAB/|AB|,

where SPAB is the area of the triangle PAB. Using this function, find the distance between a point P and each of lines AB, ACBC provided that coordinates of points PA, BC are given.

Func60. Using the Dist function from the task Func59, write a function Alts(xAyAxByBxCyC) that evaluates and returns the altitudes hA, hBhC drawn from the vertices A, BC of a triangle ABC (the coordinates of vertices are input real-valued parameters). Using this function, evaluate the altitudes of each of triangles ABC, ABDACD provided that the coordinates of points AB, CD are given.


PrevNext

 

  @Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
06.05.2018