Expressions

Save all functions into the same file named expressions.py.

  1. Write function items_count_1() that takes a string (str) which represents a Python expression and returns two integers (int) corresponding respectively to the number of alphabetical characters and digits. Examples:

    >>> items_count_1('((x[i]+2)*(3*r[4]/z)))')
    (4, 3)
    >>> items_count_1('(f(x)))//(5*x[2]-y)')
    (4, 2)
    >>> items_count_1('{4:{5:[3, 4], 6:[7, 8]}, 9:[1, 2, 3, 4, 5]}')
    (0, 13)
    >>> items_count_1("d[4] = {'a':'((x+2)*(3*r/z))}'")
    (5, 3)
    
    

    Note

    More tests are provided in the items_1.txt file.

  2. Write function items_count_2() that takes a string which represents a Python expression and returns two integers corresponding to the number of items of the following types found in this expression. The first type are the following characters corresponding to some operators, '+', '-', '*', '/', '%', '=', '!', '>' and '<'. The second type are the following bracket type characters: '(', ')', '[', ']', '{' and '}'. Examples:

    >>> items_count_2('((x[i]+2)*(3*r[4]/z)))')
    (4, 11)
    >>> items_count_2('(f(x)))//(5*x[2]-y)')
    (4, 9)
    >>> items_count_2('{4:{5:[3, 4], 6:[7, 8]}, 9:[1, 2, 3, 4, 5]}')
    (0, 10)
    >>> items_count_2("d[4] = {'a':'((x+2)*(3*r/z))}'")
    (5, 10)
    
    

    Note

    More tests are provided in the items_2.txt file.

  3. Write the function well_parenthesized_1() that takes a string (str) which represents a Python expression and returns True if it is well-parenthesized or False otherwise.

    A well-parenthesized expression must fulfil the following rules:

    1. while traversing it left to right, the number of open parenthesis must be always greater than or equal to the number of closed parenthesis.

    2. at the end of the traversal, the number of open parenthesis must be equal to the number of closed parenthesis.

    See the following examples:

    >>> well_parenthesized_1('((x+2)*(3*r/z)))')
    False
    >>> well_parenthesized_1('(f(x)))(5*x-y)')
    False
    >>> well_parenthesized_1('((x+2)*(((3*r/z))')
    False
    >>> well_parenthesized_1('((x+2)*(3*r/z))')
    True
    
    

    Note

    More tests are provided in the parenthesis_1.txt file.

  4. Write the function well_parenthesized_2() that takes a string (str) which represents a Python expression and returns the string 'correct' if it is a well-parenthesized expression. If not, the function returns the string 'incorrect closed parenthesis' together with the first position (string index) (int) in which rule 1 fails. If rule 2 fails, the function returns the string 'more open than closed parenthesis'. Examples:

    >>> well_parenthesized_2('((x+2)*(3*r/z))')
    'correct'
    >>> well_parenthesized_2('(f(x)))(5*x-y)')
    ('incorrect closed parenthesis', 6)
    >>> well_parenthesized_2('((x+2)*(3*r/z)))')
    ('incorrect closed parenthesis', 15)
    >>> well_parenthesized_2('((x+2)*(((3*r/z))')
    'more open than closed parenthesis'
    
    

    Note

    More tests are provided in the parenthesis_2.txt file.

Solution

A solution of these functions is provided in the expressions.py file.