Question

In: Computer Science

Python. Tuples and strings can also be sliced and concatenated.       Let x = ['F','l','o','r','i','d','a']. How...

Python. Tuples and strings can also be sliced and concatenated.

      Let x = ['F','l','o','r','i','d','a']. How does cotDel(x, 3) compare to del x[3]?

      Let y = ('F','l','o','r','i','d','a'). How does cotDel(y, 3) compare to del y[3]?

      Let z = 'Florida'. How does cotDel(z, 3) compare to del z[3]?

      Give an explanation about what you observed. This question refers to the following statement.

Define function cotDel(s,i) that deletes the ith item of list s, s[i]. In other words this function should do what the statement del s[i] does. Your function should use slicing and concatenation. Test your function with the list x of problem 1, deleting x[2]. Furthermore, your code should be written so that your program can be imported by other programs to make the function cotDel(s,i) available to them.

Solutions

Expert Solution

Hi,

Hope you are doing fine. I have coded the above question in python keeping all the conditions in mind. Now, have a look at the code of cotDel(s,i) first and then have a look at other explanations. The code snippets have also been attached to get a clear picture regarding indentation. The code has been clearly explained using comments that have been highlighted in bold.

Program:

def cotDel(s,i):
   #if i is the index of the last element then
if i==len(s)-1:
   #we slice the list s upto i and store in new
new=s[:i]
#new is returned
return new
#if i is between 0 and less than length of list s then,
elif i>=0 and i<len(s):
   #we slice s from start to i, and from i+1 to end and concatenate them to store in new
new=s[:i]+s[i+1:]
#return new
return new
#if i is an invalid index
else:
print("Invalid index")

Code snippet from editor:

  • We write another file test.py to test it. Inorder to make this function available to other files we must import this using the "import" command.

Test file code:

from cotDel import cotDel

#declaring a list,tuple and a string
x = ['F','l','o','r','i','d','a']
y = ('F','l','o','r','i','d','a')
z = 'Florida'

#using cotDel
x=cotDel(x,3)
y=cotDel(y,3)
z=cotDel(z,3)

#printing results
print(x)
print(y)
print(z)

Code snippet:

Output:

Also, as asked in the question we test using only list x by deleting x[2] using cotDel.

Code:

from cotDel import cotDel

#declaring a list,tuple and a string
x = ['F','l','o','r','i','d','a']
print("Before deleting: ",x)
#calling cotDel
x=cotDel(x,2)
print("After deleting: ",x)

Code snippet:

Output:

As for the questions asked:

  • Let x = ['F','l','o','r','i','d','a']. How does cotDel(x, 3) compare to del x[3]?
  • Let y = ('F','l','o','r','i','d','a'). How does cotDel(y, 3) compare to del y[3]?
  • Let z = 'Florida'. How does cotDel(z, 3) compare to del z[3]?

Explaining this using code in jupyter notebook as it is visually more clear to understand errors and there is line by line output:

  • For list x, both the operation provide the same output.

  • For tuple y, we get an error as tuple does not support the function "del". It works fine with cotDel

  • For string z, again we get an error as even it does not support item deletion using del. It works fine with cotDel.

The above errors for y and z occur because del is only supported for list items and not string or tuples.


Related Solutions

Let (X,d) be a metric space. The graph of f : X → R is the...
Let (X,d) be a metric space. The graph of f : X → R is the set {(x, y) E X X Rly = f(x)}. If X is connected and f is continuous, prove that the graph of f is also connected.
6. (a) let f : R → R be a function defined by f(x) = x...
6. (a) let f : R → R be a function defined by f(x) = x + 4 if x ≤ 1 ax + b if 1 < x ≤ 3 3x x 8 if x > 3 Find the values of a and b that makes f(x) continuous on R. [10 marks] (b) Find the derivative of f(x) = tann 1 1 ∞x 1 + x . [15 marks] (c) Find f 0 (x) using logarithmic differentiation, where f(x)...
5. (a) Let f : R \ {−1} → R, f(x) = x+1. Show that f...
5. (a) Let f : R \ {−1} → R, f(x) = x+1. Show that f is injective, but not surjective. (b) Suppose g : R\{−1} → R\{a} is a function such that g(x) = x−1, where a ∈ R. Determine x+1 a, show that g is bijective and determine its inverse function.
2 Let F be a field and let R = F[x, y] be the ring of...
2 Let F be a field and let R = F[x, y] be the ring of polynomials in two variables with coefficients in F. (a) Prove that ev(0,0) : F[x, y] → F p(x, y) → p(0, 0) is a surjective ring homomorphism. (b) Prove that ker ev(0,0) is equal to the ideal (x, y) = {xr(x, y) + ys(x, y) | r,s ∈ F[x, y]} (c) Use the first isomorphism theorem to prove that (x, y) ⊆ F[x, y]...
5). Let f : [a,b] to R be bounded and f(x) > a > 0, for...
5). Let f : [a,b] to R be bounded and f(x) > a > 0, for all x in [a,b]. Show that if f is Riemann integrable on [a,b] then 1/f : [a,b] to R, (1/f) (x) = 1/f(x) is also Riemann integrable on [a,b].
Let R be a commutative ring with unity. Prove that f(x) is R[x] is a unit...
Let R be a commutative ring with unity. Prove that f(x) is R[x] is a unit in R[x] iff f(x)=a is of degree 0 and is a unit in R.
let R be a ring; X a non-empty set and (F(X, R), +, *) the ring...
let R be a ring; X a non-empty set and (F(X, R), +, *) the ring of the functions from X to R. Show directly the associativity of the multiplication of F(X, R). Assume that R is unital and commutative. show that F(X, R) is also unital and commutative.
Let L = {x = a r b s c t | r + s =...
Let L = {x = a r b s c t | r + s = t, r, s, t ≥ 0}. Give the simplest proof you can that L is not regular using the pumping lemma.
let i be an interval in r, when is f said to be concave on i
let i be an interval in r, when is f said to be concave on i
A probability density function on R is a function f :R -> R satisfying (i) f(x)≥0...
A probability density function on R is a function f :R -> R satisfying (i) f(x)≥0 or all x e R and (ii) \int_(-\infty )^(\infty ) f(x)dx = 1. For which value(s) of k e R is the function f(x)= e^(-x^(2))\root(3)(k^(5)) a probability density function? Explain.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT