Reference no: EM132369322
Attempt the following tutorial questions:
Part 1:
1-) You are given a string that holds a message encoded with a Caesar cipher of unknown distance. Suggest an algorithm for cracking this message.
2-) 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.
3-) Write a function which can take an integer N as an input and display all prime numbers between 0 and N. In addition, the function is expected to return the total number of prime numbers found.
4-) Write a Python function that, given the path to a directory, lists the files in the directory (Hint: The function is actually quite simple, but it will require a function that you will find in the os module).
Part 2:
5-) Show the string that would result from each of the following string formatting operations. If the operation is not legal, explain why.
(a) "Looks like {1} and {0} for breakfast".format("eggs", "spam")
(b) "There is {0} {1} {2} {3}".format(1,"spam", 4, "you")
(c) "Hello {0}".format("Susan", "Computewell")
(d) "{0:0.2f} {0:0.2f}".format(2.3, 2.3468)
(e) "{7.5f} {7.5f}".format(2.3, 2.3468)
(f) "Time left {0:02}:{1:05.2f}".format(1, 37.374)
(g) "{1:3}".format("14")
6-) i-) Write a function that generates a list of all prime numbers upto N. The function should take N as parameter and return the list.
ii-) Write a function that returns a list of the first N prime numbers.
Hint for question 6:You can skip testing even numbers as no even number besides 2 is a prime number. Also notice that your function must "return a list".
7-) Define a Python function for the function header: intersect(r1, x1, y1, r2, x2, y2), where r1 and r2 are the radii of the two circles whose corresponding centres are at (x1, y1) and (x2, y2). The function returns True if the cicles intersect; False otherwise. Hint: Calculate the distance between the center of the circles.
8-) Write a function that takes a list as a parameter and swaps pairs of consecutive values in the list i.e. index 0 and 1, 2 and 3, 4 and 5 and so on. If there are odd number of elements in the list, the last element remains the same.
9-) Write a code segment that opens a file for input and prints the number of 4-letter words in the file.
10-) Write a script 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.
11-) Write a function that reads a text file, encrypts the text with public key encryption and writes the encrypted text to another file.
Hint: Use the public, private key pairs provided in this PDF document by DrAjmalMian.
12-) Write another function that will read the encrypted file, decypher it and print the plain text on the screen.
Part 3:
13-) As part of the solution to the SEND + MORE = MONEY cryptarithmetic problem, I suggested that you need a function to ensure that each solution only involves unique digits. Based on only using lists here is one implementation of that function:
defall_different(S, E, N, D, M, O, R, Y) :
digitlist = [S, E, N, D, M, O, R, Y]
digitlist.sort()
for i in range(len(digitlist) - 1) :
if digitlist[i] == digitlist[i+1] :
return False
return True
You will learn in your Level 2 units that sorting, which dominates this function, takes O(NlogN) time. That is, the run-time for this kind of program will rise proportional to NlogN, where N is the size of the input. What you are to do now is recode the function using a dictionary in which you record the number of times each digit has been seen. This implementation will have O(N) run time, which grows more slowly as N increases, so is better.
14-) i-) Write a function that reads the marks.txt file and prints the names of the students who obtained maximum marks in each subject. e.g. "Tom Hanks got the highest marks of 90 in CITS1401".
ii-) write a function that will calculate the average marks obtained in each subject in the above file and print them along with the subject name.
iii-) if passing marks in each subject are 50, write a function that will print the number of students failed in each subject.
v-) write a function that will calculate and print the overall total marks of each student along with the student's name.
15-) Write a function that can return the sum of the digits of the number N which is provided as an input.
16-) Write a function which takes two inputs: list of numbers and a number, and returns list of lists containing three numbers from the input lists such that the sum of three numbers equal to a given number. For instance, myfunction([1, 0, -1, 0, -2, 2], 0) returns [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]].
17-) Write a function which takes any positive integer n, if n is even, divide it by 2 to get n/2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Store the results in a list and repeat the process until it reaches 1. For instance, myfunction(12) returns [12, 6.0, 3.0, 10.0, 5.0, 16.0, 8.0, 4.0, 2.0, 1.0].
18-) Write a function to take a positive integer as an input and add the digits of the input repeatedly until the result has a single digit. For instance, myfunction(48) returns 3.
19-) Write a function that can display a triangle of asterisk (*). The height of the triangle will be provided as an input.
*
***
*****
*******
*********