====== Computer Lab 05, Non-trivial loops ====== ===== Practical work ===== ==== Sum of digits in a string ==== Let s be a string containg several characters some of which may be digits. Write a function that returns the sum of all digits in that string. def sum_digits_in_string(s): """Return sum of all digits in a string. :param s: string containing the digits and other characters :return: numeric value, sum of all digits Examples: >>> sum_digits_in_string('1') 1 >>> sum_digits_in_string('hi 1 hello 2') 3 >>> sum_digits_in_string('Values: 1.26, 2.3, 1.76') 28 """ ==== Sum of decimal numbers in a string ==== Let s be a string containing decimal numbers separated by commas, e.g. s = '1.26,2.3,1.76'. Write a function that returns the sum of all decimal numbers in that string. def sum_decimals_in_string(s): """Return sum of decimal numbers in a string. :param s: string containing the decimal numbers separated by ',' :return: float, sum of all decimals Examples: >>> sum_decimals_in_string('1.2') 1.2 >>> sum_decimals_in_string('1.2,3.4') 4.6 >>> sum_decimals_in_string('1,2,0.000001') 3.000001 """ ==== Character triangles ==== Using nested loops, write function ''print_left_triangle(n_rows, char)'' which prints the triangle depicted below. E.g. when you call that function like this: >>> print_left_triangle(n_rows=5, char='T') T TT TTT TTTT TTTTT Again, using nested loops, write function ''print_right_triangle(n_rows, char)'' which prints the triangle depicted below. E.g. when you call that function like this: >>> print_right_triangle(n_rows=5, char='T') T TT TTT TTTT TTTTT ==== Chemical compounds ==== Write a function ''combine_elements(el1, el2)'' which takes 2 iterables (e.g. lists) of chemical elements, and returns a list of all possible compounds that can arise by combining an element from the first list with an element from the second list. def combine_elements(el1, el2): """Return a list of possible compounds each containg a single element from both the first and second list. :param el1: list of strings, symbols of chemical elements for the first place of compound :param el2: list of strings, symbols of chemical elements for the second place of compound :return: list of strings, possible chemical compounds Example: >>> metals = 'Li Na K'.split() >>> halogens = 'F Cl Br'.split() >>> print(combine_elements(metals, halogens)) ['LiF', 'LiCl', 'LiBr', 'NaF', 'NaCl', 'NaBr', 'KF', 'KCl', 'KBr'] """ ==== Production maximization ==== // Somewhat more complex task for more advanced students.// Your small company produces wooden chairs and tables. Profit from a single chair is ''profit_per_chair'', profit from a single table is ''profit_per_table''. To build a single chair, you need to invest ''wood_per_chair'' units of wood and ''manhours_per_chair'' manhours of labour, to build a single table you need to invest ''wood_per_table'' units of wood and ''manhours_per_table'' manhours of labour. How many chairs and tables (''n_chairs'', ''n_tables'') should you produce per day to maximize your profit, if you can invest ''available_manhours'' manhours a day and ''available_wood'' units of wood a day, and if you want the numbers of chairs and tables to be integers? Write a function that returns the optimal values given all the parameter values as arguments. def compute_optimal_production(profit_per_chair, profit_per_table, wood_per_chair, wood_per_table, manhours_per_chair, manhours_per_table, available_wood, available_manhours): """Return numbers of chairs and tables to be produced per day to maximize profit. :param profit_per_chair: Company profit for a single chair produced. :param profit_per_chair: Company profit for a single table produced. :param wood_per_chair: Amount of wood required to build a single chair. :param wood_per_table: Amount of wood required to build a single table. :param manhours_per_chair: Amount of work required to build a single chair. :param manhours_per_table: Amount of work required to build a single chair. :param available_wood: Total amount of wood available for production each day. :param available_manhours: Total amount of work available each day. :return: (n_chairs, n_tables) tuple containing the optimal number of chairs and tables produced each day. Example: >>> profit_per_chair, profit_per_table = 20, 30 >>> wood_per_chair, manhours_per_chair = 1, 3 >>> wood_per_table, manhours_per_table = 6, 1 >>> available_wood, available_manhours = 288, 99 >>> print(compute_optimal_production( profit_per_chair, profit_per_table, wood_per_chair, wood_per_table, manhours_per_chair, manhours_per_table, available_wood, available_manhours)) (18, 45) """ Suggestion: Proceed in smaller steps. * Create helper function to compute the total profit from x chairs and y tables. * Create a helper function telling you, whether when producing x chairs and y tables, the available amount of wood is or is not exceeded. * Create a helper function telling you, whether when producing x chairs and y tables, the available manhours are or are not exceeded. * Compute the maximal number of chairs and tables that can be produced per day, if you decided to produce only chairs/only tables. * Then combine it all to complete function ''compute_optimal_production()''. Perform exhaustive search among all feasible pairs of values for ''n_chairs'' and ''n_tables''. ===== Homework ===== * Finish all the above tasks. * Read [[http://openbookproject.net/thinkcs/python/english3e/modules.html|chapter 12]] of {[a4b99rph:Wenthworth]}.