Unit 2 - Additional exercises

angelo.spognardi (4640 points)
37 60 92
asked Nov 7, 2021 in Course info by angelo.spognardi (4,640 points)
edited Nov 27, 2021 by angelo.spognardi

26 November 2021

  • Complete the follow_the_line function defining the neighborhood function
  • Write a function that takes as input a filename with a PNG image with a black background and some not-intersecting white lines (e.g. test1.png and test2.png), and returns a tuple with the two endpoints of the longest white line in the image. Use the Pixel class defined during the lecture.
  • Write a function that takes as input a filename with a PNG image with a black background and some not-intersecting colored lines (e.g. test3.png), and returns the color of the shortest line in the image.
  • Complete the Matrix class definition, implementing the __prod__ method (matrix multiplication).

19 November 2021

  • Write a function that takes as input an image, a row, a column, a color, a length, and a height and draws a rectangle with the upper-left corner in row-column, of size lengthxheight and filled with color.
  • Write a function that takes as input an image with a black background and several, possibly overlapping, shapes of different colors. The function returns the number of squares that are fully visible (namely, not covered by any other shape).
  • Write a function that takes as input an image (of size nxn), two colors a and b and a value m. It draws m squares with side n-m+1, along the diagonal of the image, alternating the two colors a and b.

5 November 2021

  • Write a function that can be used as a key function for sorting a list of integers so that: all the odd integers appear sorted in increasing order before the even ones, sorted in decreasing order.
  • Write a function that gets two filenames as input and writes the content of the first file in the second file. Every line is reversed, and the lines are sorted according to the first letter and their length. The function returns the number of lines written.
  • Write a function that gets a filename as input and returns a list. The filename is the name of a file containing several strings on several lines, separated by spaces. The list includes an integer for each string in the file, summing up the Unicode values of the characters that compose the string (use the 'ord' function).
  • Write a function that gets two filenames as input. The first file contains several strings of English alphabetical characters on several lines, separated by non-alpha symbols. For every line of the first input filename, the function has to write another line in a file with the name of the second input filename. For every line, the function writes only the first string and those strings with a larger number of characters than the previous one. The lines in the generated file have to be sorted in increasing order with respect to their string with the larger number of characters and, in case of a tie, in lexicographical order.
  • Write a function that gets a dictionary, a list, and a filename as input and writes in a file with the input filename only the values of the dictionary corresponding to the keys contained in the input list. Keys and values of the dictionary are strings. The values have to be written sorted in increasing order with respect to the number of characters of the key and, in case of a tie, the number of characters of the value and, in case of a tie, in lexicographical order. The values have to be written separated by whitespaces.

27 October 2021

  • Write a function that gets a list of float numbers as input and returns a dictionary with two keys 'positive' and 'negative', where the values are the lists of the positive and negative numbers, respectively.
  • Write a function that gets as input two lists with an equal number of elements and generates (returns) a dictionary where the keys are the values of the first list and the values are the element in the same position of the second list.
  • Write a function that gets as input a string with some words separated by commas and generates (returns) a list. The list has to contain all the words of the input string, increasingly ordered by the number of vocals in the word. In the case of a draw, use the lengths of the word, in the case of a further draw, use the lexicographic order.
  • Write a function that gets a list with integers as input and returns another list of integers. The output list is a list of the prime numbers needed to generate all the numbers of the input list, ordered by the number of times they need to be multiplied to generate them. In the case of a draw, the order is the natural order.
  • Write a function that gets a dictionary and generates (returns) another dictionary. The keys of the input dictionary are integers, the values are integer lists. The output dictionary has the same number and type of elements, where: every integer, key of the first dictionary, has to be "reduced" to a product of unique prime numbers, dividing it as many times as possible and removing the repeated primes. Every time a division is done, all the elements of the corresponding value list have to be multiplied by the same number. Ex: 12->2*2*3 can be "reduced" to 6->2*3 removing the repetition of 2.

8 October 2021

  • Write a function that gets a float number as an input, calculates its cube root, prints it on screen, and returns it
  • Write a function that gets three floats a, b, c, as an input and calculates and prints the two x roots of the equation:CodeCogsEqn1.gif and returns the greatest of the two
  • Write a function that gets five integers as an input and returns the sum of all even numbers minus the sum of all odd numbers
  • Write a function that asks the user for three grades as input (using the input function) and returns the average of the grades only if all the grades are legitimate (i.e. 0<=grade<=30), returns False otherwise
  • Write a function that gets three integers as an input d, m, y (it is assumed that a is always an odd number to avoid leap years) and returns True or False depending on whether the three numbers form a valid date in the "d/m/y" format. Ex: 30/2/2017 False, 1/1/1111 True
568 views

Please log in or register to answer this question.