This labsheet is in three parts: a number of review exercises that reinforce concepts from the lectures; an important problem-solving technique, motivated by its use in solving a larger-sized problem representative of the types of problems that can be solved with computers; and some basic list operations that will be useful in the project
Assuming that the variable data refers to the string "myprogram.exe", write an expression that (a) extracts the substring "gram", (b) truncates the extension ".exe", (c) extracts the character at the middle position.
Assuming that the variable data refers to the string "Python rules!", use a string method to (a) obtain a list of the words in the string, (b) convert the string to uppercase, (c) locate the position of the string "rules", (d) replace the exclamation point with a question mark.
Write a code segment that opens a file for input and prints the number of 4-letter words in the file.
A bit shift is a procedure that takes a binary string and moves the digits either left or right, wrapping digits around as required; e.g. a left shift of 1 applied to 10011 gives 00111, and a left-shift of 2 gives 01110. Write a code segment that takes a string s, an integer k, and a Boolean b, and that prints s shifted by k, left if b if True, and right otherwise.
Write a code segment that inputs a string and replaces all occurrences of "ten" with "ten (10)". The code should work for upper-case and lower-case.
Write a code segment that prints the names of all of the items in the current working directory.
Write a script copyfile.py that prompts the user for the names of two text files, inputs the contents of the first file, and writes them to the second file.
Abstraction is the problem solving technique of examining a model of a situation instead of the situation itself. The model can be a much simplified version of the problem so that the unecessary details do not get in the way.
PROBLEM SOLVING QUESTION 4.1) Count how many people are in your lab class, or in a typical CS lab. What is the probability that two people in such a lab will share the same birthday (month and day of the month)? Use random numbers and a reasonable number of repeated simulations of a lab class full of birthdays to estimate this probability. Did you use actual dates or some abstraction of the date?
Related to Abstraction is the technique of Generalisation. This is the idea of solving a more general problem.
PROBLEM SOLVING QUESTION 4.2) In lab 3, we developed an algorithm for converting from binary to decimal. You can generalize this algorithm to work for a representation in any base. Instead of using a power of 2, this time you use a power of the new base. Also, you can use digits greater than 9, such as A… F, when they occur. Write a function with parameters string and integer base parameters and returns the decimal (base 10) value of the string. For simplicity, consider only base values upto 16.
s1 = [2,1,4,3]
s2 = ['c','a','b']
s1 + s2
3 * s1+ 2 * s2