Programming with Python (lab classes)

Stefan Güttel, guettel.com

Content:

  1. Strings

  2. Generators

Problem 1. Write a program that loads a list of strings and prints them sorted, ignoring their character case (so called case insensitive comparison).

For example:

  • case sensitive: "John" < "alien", because all capital letters come before all lower case letters;
  • case insensitive: "John" > "alien", because the case is ignored, so it is the same as "john" > "alien").

Note: The original strings in the list should not be altered in any way!

Hint: Using str.tolower() in combination with the key argument of sort and a lambda-function might help you.

Problem 2. Write a function del_double(s, char_del, char_double) that returns a new string obtained from s by removing all the occurrences of the character char_del and doubling all the occurrences of the character char_double. If char_del is not given, nothing should be removed. Similarly, if char_double is not given, nothing should be doubled.

For example,

  • del_double("abcdbacabcd", "b", "d") should return "acddacacdd",
  • del_double("abcdbacabcd", "b") should return "acdacacd",
  • del_double("abcdbacabcd", char_double="d") should return "abcddbacabcdd",
  • del_double(s) should return a copy of s.

If char_del == char_double, then no doubling will take place because the deletions will remove all the occurences of char_del (which are the same as the occurences of char_double).

Note: In del_double, you may assume that len(char_del) == len(char_double) == 1, but make sure that this is correct when loading the data.

Hint: Write an auxiliary function that will take three characters c, c_del, and c_double, and return

  • an empty string if c == c_del,
  • double c if c == c_double,
  • c otherwise.

Then use this function in a list comprehension that traverses through the string s.

Problem 3. Write a function time_diff(t1, t2) that returns the absolute time difference between the times t1 and t2. Each of them is given as a string formated either as hh:mm:ss or as mm:ss. The returned difference should have the same format (hh part should be omitted if it equals zero), with each part taking up at least two digits.

For example,

  • time_diff("17:19:23", "13:11:7") should return "04:08:16";
  • time_diff("13:11:7", "17:19:23") should also return "04:08:16" (because the function returns the absolute time difference);
  • time_diff("17:19:23", "11:7") should return "17:08:16";
  • time_diff("17:19:23", "17:11:7") should return "08:16".

Hint: Write two auxiliary functions, str2time and time2str, that will convert the string of the described format to the number of seconds since the beginning of the day (3600*hh+60*mm+ss) and vice versa.

Problem 4. Write a function remove(s, chrs, case_sensitive) that returns a string obtained from s by removing all the characters present in chrs. The parameter case_sensitive is Boolean; if it is True, the matching is case sensitive; otherwise it is insensitive. If the function is invoked with only two arguments, the match should be case sensitive.

For example, remove(s, "0123456789") would return the copy of s without any digits it may have had. So, remove("a12b0 c", "0123") would return "ab c". Similarly, remove(s, "A") would return a copy of s without upper case letters "A" (but all lower case letters "a" would remain intact). However, remove(s, "A", True) would return a copy of s without any letters "A", lower case or upper case.

Preferably, use a generator expression to create this new string.

Program 5. Write a function load_a_list_of_ints_as_a_string() that loads a list of integers with a singe call to input. Assume that the it will be given in the form "x1,x2,...,xn". For example, "17,-19,23" (without spaces or any characters other than a single comma between each two neighbour integers).

Write a program that uses this function to load a list of integers, and print it sorted in a descended order.