[SOLVED] html python CS-UY 1114 Lab 8

$25

File Name: html_python_CS-UY_1114__Lab_8.zip
File Size: 273.18 KB

5/5 - (1 vote)

CS-UY 1114 Lab 8
24/25 Oct 2019
You may want to refer to your notes regarding strings or to the Python documentation for string methods while solving these problems: https://docs.python.org/3/library/stdtypes.html#string-methods
Problem 1
Given the following assignment statements, write Python expressions that evaluate to the specified strings:
1. For example, given the assignment statement below, the Python expression x[0] evaluates to G.
x = Grace Hopper
(a) Write an expression whose value is r.
(b) Write an expression using the function len whose value is r.
(c) Write an expression whose value is Grace. Use the slicing operator.
2. Given the assignment statement below, write expressions that evaluate to the specified strings. Use the slicing operator.
my_str = homework
(a) home (b) work
3. Assume that you are given a variable, my_str2, whose value is a string of even length. For example, it might be hose or foobar. Use the slicing operator and the len function to write the specified expressions.
(a) Write an expression whose value is the characters in the first half of the string. For example, in the string hose, the expression would have a value of ho.
(b) Write another expression whose value is the characters in the second half of the string. For example, in the string hose, the expression would have a value of se.
4. Assume that you are given a variable, my_str3, whose value is a string of odd length. For example, it might be helps or house. Use the slicing operator and the len function to write the specified expressions.
(a) Write an expression whose value is the middle character in the string. For example, in the string house, the value of the new expression would be u. The slicing operator is not required here, but you should use the len function.
(b) Write another expression whose value is the characters in the first half of the string. For example, in the string house, the value of the new expression would be ho.
(c) Write another expression whose value is the characters in the second half of the string. For example, in the string house, the value of the new expression would be se.
5. my_str4 = What is your name?
(a) What is the value of the expression my_str2[::2]?

Problem 2
(b) What is the value of the expression my_str2[8:2:-1]?
Write a function names_equal(name1, name2) that takes as argument two strings name1 and name2 containing names in different formats. The format for name1 is: Last_Name, First_Name while the format for name2 is First_Name Last_Name. Your function should return True if the two names are equal. You can assume that input to the function is in the correct format.
The function signature is given below:
def names_equal ( name1 , name2 ):
# sig : str , str -> bool
For example, a call to the function names_equal(Smith, John, John Smith) should return True. Hint: Make use of the find method and the slicing operator.
Problem 3
In computer science, it is useful to be able to quantify the difference between two strings. One approach is called the Hamming distance. To find the Hamming distance, count the number of positions at which the corresponding characters differ between two strings. Hamming distance requires that the strings be of equal length.
For example: The Hamming distance between cat and cat is 0 because the strings contain the same characters at all index positions. The Hamming distance between cat and car is 1 because there is one index position at which the strings differ (index position 2). At index positions 0 and 1, the strings contain the same characters. The Hamming distance between cat and pot is 2 because there are 2 positions where the strings differ (index positions 0 and 1). At index position 2, both strings contain the same character. The Hamming distance between cat and dog is 3. In this case, the strings contain different characters at all 3 index positions.
Write a function find_Hamming_dist(str1, str2) which returns the Hamming distance between str1 and str2. You may assume that the user enters strings of equal length.
Below is given the function definition:
def find_Hamming_dist ( str1 , str2 ):
# sig : str , str -> int
Hint: You should iterate over all the positions (starting at 0) of characters in the strings, and then compare the characters at that position in each string.
Problem 4
Write a function count_succ_digits(input_val) which takes a string input_val consisting of only the digits 1 and 0. The function should count the number of successive 1s and successive 0s in the string and print out this information as shown below.
For example, a call to count_succ_digits(1111000110000) should print: Your string has :

41 s
30 s
21 s
40 s
A call to count_succ_digits(101) should print: Your string has :
1 1 s 10 s 1 1 s
The function signature is given below:
def count_succ_digits ( input_val ):
# sig : str -> NoneType
Hint: Think about how you can compare the values at successive index positions. Make sure your solution correctly handles different scenarios at the end of the string.
Problem 5
A valid password must be at least 8 characters long and must contain the following:
at least two uppercase letters
at least one lowercase letter
at least two digits, 0 through 9
at least one of the following special characters: ! @ # $
Write a function named is_valid_password that takes a string parameter and determines whether it is a valid password. The function will return True for a valid password, and False otherwise.
Sample executions:
is_valid_password(P4Ssword1!) # True
is_valid_password(password) # False
is_valid_password($12ABcd) # False
Hint: set up counter variables for each kind of character you need to check for. As you iterate through each character in the string, check if the current character matches one of the required categories (using isupper, isdigit, islower) and update the counter accordingly.
Problem 6
Write a function hide_num_info(input_str) that takes as input a line of text and outputs a new line of text with the digits of the integer numbers replaced by the character x.
For example, a call to hide_num_info(My userID is john17 and my 4 digit pin in 1234 which is secret) would return: My userID is john17 and my x digit pin is xxxx which is secret

Notice that the digits in the username john17 were not changed to x. If the digit is part of a word, it should not be changed. You may assume that the text entered by the user will contain only letters, digits and spaces. You may also assume that there is exactly one space between each word in the line of text.
Below is given the function signature:
def hide_num_info ( input_str ):
# sig : str -> str
Hints: One approach to solving this problem is to use the slicing operator to isolate each word in the line of text. In order to do this, you will need to know the index position at which the word starts and ends. You can use the space character as an indicator of separation between words. You will need to iterate over the indices of the string and check if you encounter the space character . You will also need to store in a variable the previous space that you encountered so that when you encounter a new space, you will know the index positions of space that comes before the word and the space that comes after the word. One you are able to separate an individual word, you can use the string method isdigit() which returns True if all of the characters of the string are digits. Make sure that you appropriately handle the first and the last word in the line of text as these cases will differ from the general case.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] html python CS-UY 1114 Lab 8
$25