Starting from:

$39.99

PHYS2160 Lab 2: Strings, Lists, and Formatted Output Solution


Exercise 1: Operations on a Line of Text via String Manipulation
AIM:
Write a Python program that prompts the user for one line of text without punctuation marks and then asks the user to choose one of the following operations to be done on the line of text:
(a) printing the line of text with all its blank spaces removed,
(b) printing the line of text with each word in reversed order,
(c) printing the number of words in the line of text, by using string manipulation. Your program should check if the user input of the choice of operation is valid. You can assume that the entered line of text has no punctuation marks. Here are the sample input and output of this program:
Enter a line of text without punctuation mark:
I am very very happy
Type a number 1 to 3 to do one of the following operations
1 - print the line of text with all its blank spaces removed
2 - print the line of text with each word in reversed order
3 - print the number of words in the line of text
Enter your choice (1-3): 1
The line of text with all its blank spaces removed:
Iamveryveryhappy
Enter a line of text without punctuation mark:
I am very very happy
Type a number 1 to 3 to do one of the following operations
1 - print the line of text with all its blank spaces removed
2 - print the line of text with each word in reversed order
3 - print the number of words in the line of text Enter your choice (1-3): 2
The line of text with each word in reversed order:
I ma yrev yrev yppah
Enter a line of text without punctuation mark:
I am very very happy
Type a number 1 to 3 to do one of the following operations
1 - print the line of text with all its blank spaces removed
2 - print the line of text with each word in reversed order
3 - print the number of words in the line of text
Enter your choice (1-3): 3
The number of words in the line of text: 5
Enter a line of text without punctuation mark:
I am very very happy
Type a number 1 to 3 to do one of the following operations
1 - print the line of text with all its blank spaces removed
2 - print the line of text with each word in reversed order
3 - print the number of words in the line of text
Enter your choice (1-3): 5
Invalid input. You must type 1 or 2 or 3!
ALGORITHM:
- Ask for the line to perform string manipulation on from the user
- Ask the user for their choice of string manip.
- Validify the choice, end program if invalid choice
- Start Control Flow depending on choice
1. Remove whitespaces from the string & return it
2. Separate all the words and reverse them
3. Separate the words, count them and return count

Exercise 2: Caesar Cipher
AIM:
A Caesar cipher is a simple substitution cipher that produces a cipher text by shifting each plain-text letter in a message a fixed number of places down the alphabet in a circular fashion so that the next character after "z" and “Z” are "a" and ”A”, respectively. The plain text is your original message while the cipher text is the encrypted message. For example, if the key value (i.e. the shift length) is
Write a Python program that prompts for a key value in the closed interval [1, 25] and the plain text to encode from the user and then outputs the cipher text as well as the decrypted cipher text. You can assume that the input plain text consists only of letters and spaces. And your program should ask the user to input the key value again if the input value is invalid. If your program is correct, then the decrypted cipher text should match the original plain text.
(Hint: Construct two strings where one contains all the plain-text letters and one contains all the corresponding cipher-text letters. Notice that the letter to substitute is at the same index in both strings.)
ALGORITHM:
- Obtain the key and plain text from the user (validifying the key)
- Iterate over the characters in the plain text
- If the character is not a letter, do nothing
- Else if it is an uppercase letter shift its position using the ord(), chr() function by key units
(‘A’ = 65)
- Else if it is a lowercase letter do the shift it by key units in the alphabet (‘a’ = 97)

Exercise 3: Vector Manipulation
AIM:
Write a Python program that prompts the user for two vectors of the same length, computes their sum, difference, and dot product by manipulating the lists storing these vectors, and finally outputs the results. You can assume that each vector is input as a comma-space-separated list of integers enclosed by square brackets. And your program should ask the user to input the vectors again if they have different length. Here are the sample input and output of this program:
Enter the vector A: [2, 4, 6, 8]
Enter the vector B: [1, 2, 3]
Invalid input! Vectors A and B must have the same length.
Enter the vector A: [2, 4, 6, 8] Enter the vector B: [1, 2, 3, 4]
A+B = [3, 6, 9, 12]
A-B = [1, 2, 3, 4]
A.B = 60
ALGORITHM:
- Obtain the text for the two vectors from the user
- Enforce same length of the vectors
- Define a class to represent vectors and their methods
- Print out the sum, difference and dot product of the two vectors



Exercise 4: Bubble Sort
AIM:
Bubble sort is a simple algorithm that sorts a collection of data by ‘bubbling’ values to the end of the list over successive passes like air bubbles rising in water. Here are the procedures for sorting a list of numbers in ascending order using bubble sort:
(a) Starting from the first element, compare each adjacent pair of elements of the list in turn and swap the elements if the former element has a larger value. After all the elements in the list have been compared, the largest element will be at the end of the list. This completes the first pass of the algorithm.
(b) Repeat the process in (a) from the first to the second last element of the list. At the end of the second pass, the second largest element will be at the second last position of the list.
(c) Repeat the process in (a) again in a similar manner as in (b) until no more swapping occurs in a pass.
Write a Python program that generates a list composed of 10 random integers in the range of 1 to 100, prints out the list, and finally uses bubble sort to sort the integers in the list in ascending order with the updated list printed out after each swapping. Here is the sample output of this program:
Sorting Process of a List of Integers by Bubble Sort:
[83, 74, 89, 26, 18, 69, 75, 98, 47, 77]
[74, 83, 89, 26, 18, 69, 75, 98, 47, 77]
[74, 83, 26, 89, 18, 69, 75, 98, 47, 77]
[74, 83, 26, 18, 89, 69, 75, 98, 47, 77]
[74, 83, 26, 18, 69, 89, 75, 98, 47, 77]
[74, 83, 26, 18, 69, 75, 89, 98, 47, 77]
[74, 83, 26, 18, 69, 75, 89, 47, 98, 77]
[74, 83, 26, 18, 69, 75, 89, 47, 77, 98]
[74, 26, 83, 18, 69, 75, 89, 47, 77, 98]
[74, 26, 18, 83, 69, 75, 89, 47, 77, 98]
[74, 26, 18, 69, 83, 75, 89, 47, 77, 98]
[74, 26, 18, 69, 75, 83, 89, 47, 77, 98]
[74, 26, 18, 69, 75, 83, 47, 89, 77, 98]
[74, 26, 18, 69, 75, 83, 47, 77, 89, 98]
[26, 74, 18, 69, 75, 83, 47, 77, 89, 98]
[26, 18, 74, 69, 75, 83, 47, 77, 89, 98]
[26, 18, 69, 74, 75, 83, 47, 77, 89, 98]
[26, 18, 69, 74, 75, 47, 83, 77, 89, 98]
[26, 18, 69, 74, 75, 47, 77, 83, 89, 98]
[18, 26, 69, 74, 75, 47, 77, 83, 89, 98]
[18, 26, 69, 74, 47, 75, 77, 83, 89, 98]
[18, 26, 69, 47, 74, 75, 77, 83, 89, 98] [18, 26, 47, 69, 74, 75, 77, 83, 89, 98] ALGORITHM:
- Define a function to perform Bubble Sort, where each step is printed out to the user
- Iterate over the length of the input array
- Then in each iteration, iterate from the first element to the (n-i-1)th element, where n is the number of elements in the array (because the last ith elements will be in order)
- Perform swapping if the “left” element is larger than the “right” element at that index
Use numpy to obtain an array of 10 random integers in the range [ 1, 100 ) and perform bubble sort on that array.


Exercise 5: Printing a Histogram
AIM:
District Numbers of Students (nearest hundreds) District Numbers of Students (nearest hundreds)
Central & Western 7 Sai Kung 7
Wan Chai 25 Sha Tin 8
Eastern 22 Tai Po 8
Southern 8 North 8
Yau Tsim Mong 14 Yuen Long 27
Sham Shui Po 22 Tuen Mun 7
Kowloon City 29 Tsuen Wan 18
Wong Tai Sin 6 Kwai Tsing 0
Kwun Tong 15 Islands 0
Write a Python program to show the statistics of the data in this table by printing a histogram in the following format:
(in nearest hundreds)
Central & Western | ******* (7)
Wan Chai | ************************* (25)

Islands | (0)
Your program should define some variables to store the given data and then print the variables using print with the format specifier.
ALGORITHM:
- Iterate over the data in the table
-

For each line, print the area right-aligned, followed by | and the number of asterisks corresponding to the number of students in the area shown in brackets after the *s

More products