## Question

Write a function replace (L, old obj, new_obj) that takes as its arguments a list L, an object old_obj, and another object new_obj This function should replace every occurrence of old_obj in L with new_obj. It should change the list that is passed in, but should not return a value.

For example:

my_list = [1,2,1,2,3]

replace(my_list,2, 'a')

my_list

1,'a',1,'a',3]

Fill in your solution in hw3 . py.

2. List second largest element

Write a function second largest (L) that takes as its argument a list L of ints and floats and returns the second largest element in L. You may assume that L contains only ints and floats. For example, if my L is [1,2,3,4], second_largest(my_L) should return 3.

Fill in your solution in hw3. py.

3. Strings

In this problem, we'l ll write code to perform a couple of tasks involving strings.

(a) The Hamming distance between two strings s1 and s2 of the same length is defined to be the number of indices on which they differ. For example, The Hamming distance between two identical strings is 0. The Hamming distance between the strings tick' and tock' is 1: they have different characters at index 1 and all other characters are the same.

The Hamming distance between the strings 'except' and 'accept' is 2: they have different characters at indices 0 and 1 and all other characters are the same.

The Hamming distance between the strings 'apple' and 'phone' is 4: they are only the same at index 4.

The Hamming distance between the strings 'boat' and cars' is 4: they do not have the same character at any index.

Again, note that the Hamming distance is only defined for two strings of the same length. Write a function ham_dist (s1, s2) that takes two strings s1 and s2 of the same length and returns the Hamming distance between them. You may assume that s 1 and s2 have the same length. Fill in your solution in hw3 . py.

(b) Let s be a string and p be another string that is at most as long as s. The substring of s that is closest to p is the substring (slice) of S of length len (p) of minimum Hamming distance to p.

Consider the following example: Let s be abcefgh' and let p be 'cdef' The substrings of s of length len (p), which is 4 in this case, and their corresponding Hamming distances from p are

'abce - 4

'bcef - 2

'cefg' 3

efgh' 4

so bcef' is the substring of s that is closest to p.

Write a function find_closest_substring (s, p) that finds the substring of S that is closest to p.

More specifically, your function should return a tuple of three objects:

The first should be the index in s of the first element of the closest substring

The second should be the Hamming distance of the closest substring to p

The third should be the closest substring itself.

In the above example, your function should return (1,2, bcef - ) because the closest substring is bcef', it begins at index 1 in s, and its Hamming distance to p is 2. In your function, you should use your ham dist function from part (a). If there is more than one substring with the same minimum distance to p, return any of them.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

# Problem 1: List replacedef replace(L, old_obj, new_obj):

'''

Replaces every instance of old_obj in L with new_obj

'''

for i in range(len(L)):

if L[i] == old_obj:

L[i] = new_obj

# Problem 2: List second largest element

def second_largest(L):

'''

Returns the second largest element of L

Assumes L contains only ints and floats

'''

largest = None

for i in range(len(L)):

if largest == None or L[i] > largest:

largest = L[i]

second_largest = None

for i in range(len(L)):

if second_largest == None or (L[i] > second_largest and L[i] < largest):

second_largest = L[i]

return second_largest...

By purchasing this solution you'll be able to access the following files:

Solution.PNG and Solution.py.