Exercise 1.
The following program divides by the half the stringmystring
onto two strings.
mystring = 'These_are_assignment_4_practice_exercises' mystring_len = len(mystring) #slicing string onto the middle: mystring_firsthalf = mystring[:mystring_len/2] mystring_secondhalf = mystring[mystring_len/2+1:] print(mystring_firsthalf) print(mystring_secondhalf)However, the code above is not working correctly. Your task is to find and correct any error so the program will output:
These_are_assignment
_4_practice_exercises
Exercise 2.
Mr. Bugs has found a very useful piece of code that sorts in ascending order the elements of a list. However, now Mr. Bugs wants to order the list in descending order but he does not understand how the program works. Your task is to read, understand and rewrite (sort command is not allowed) the program so it orders the list in descending order.
Mr. Bugs' source code: mylist = ["rat", "cat", "dog", "mouse", "giraffe"] #length of the list length = len( mylist ) unsorted = True while unsorted : unsorted = False i = 0 while i < length -1: if mylist[i] > mylist[i+1] : temp = mylist[i] mylist[i] = mylist[i+1] mylist[i+1] = temp unsorted = True i += 1 print(mylist)
Mr. Bugs' desired output:
Output: ['rat', 'mouse', 'giraffe', 'dog', 'cat']
Exercise 3.
Given a string text
, write a program that creates 3 lists containing numbers, punctuations and letters (and spaces), respectively and without repetition.
For example:
Input: 'Pi = 3.14159265359, and g = 9.78 m/s^2' Output: list_letters = ['P','i',' ','a','n','d','g','m','s'] list_numbers = [3,1,4,5,9,2,6,7,8] list_punctuations = ['=','.',',','/','^']
Exercise 4.
Write a program that receives as input a string and replaces all white spaces by underscore.
Example:
Input: Sentence with a lot of spaces . Output: Sentence_with_a_lot_of__spaces___.
Exercise 5.
After your last sucessful task, Mr. Bugs has decided to hire you. In this occassion he needs a program that interprets mathematical sums of two numbers. The usual input is a string which has the following format: number1 + number2 = number3; your task is to return as output the three numerical values, number1, number2 and number3.
Example 1:
Input: '12 + 27 = 39' Output: number1 = 12 number2 = 27 sum = 39
Example 2:
Input: '1.2 + 12.7 = 13.9' Output: number1 = 1.2 number2 = 12.7 sum = 13.9
Exercise 6.
Write a program that given a string with animals (separated by spaces), create a list of these animals but with no repetition. As output, your program will print this list.
Example:
Input: 'cat dog horse rabbit rabbit cat mouse dog cat' Output: ['cat', 'dog', 'horse', 'rabbit', 'mouse']
Exercise 7.
Write a program that given a string mystring
and a substring mysubstring
, finds all occurences of mysubstring
into mystring
. The program will output the indices corresponding to these occurrences.
Example 1:
Input strings: mystring = 'this is a test' mysubstring = 'test' Output: [10]
Example 2:
Input strings: mystring = 'cococococococococo' mysubstring = 'coco' Output: [0, 2, 4, 6, 8, 10, 12, 14]