161k views
4 votes
one_diff(a?): Description: Determine if there is exactly one character different between exactly two strings. Parameters: Determine these from the description, and the provided examples. Return value: True If there is exactly one character different, False otherwise Example: one_diff("banana", "apple") → False one_diff("need", "reed") → True one_diff("goof", "good") → True one_diff("food", "feed") → False

User Maxbfuer
by
3.3k points

1 Answer

6 votes

Answer:

See explaination

Step-by-step explanation:

def one_diff(s1,s2):#mthod return true if s1 and s2 has one difference

l1=list(s1)#store characters to list

l2=list(s2)

c=0

if len(l1)!=len(l2):

return False

for i in l1:

if i in l2:#for each character if it is in l1 and l2 continue

continue

else:

c+=1

if c==1:

return True#ret True if cis 1 else False

return False

def exchange(s,*k):#method that exchanges characters

l=list(k)

if len(l)!=0:#check validity of arguments

for i in l:

if i<0 and i+(2*i)>len(s):

res=s

elif i>len(s):

res=s

return res

if len(l)==0:#if no indexes are given

c1=s[0]

c2=s[-1]

res=c2

res=res+s[1:]

res=res[0]+res[1:].replace(c2,c1)

return (res)

elif len(l)==1:#if one index is given

c1=s[l[0]]

c2=s[-1]

res=''

for i in s:

if i==c1 or i==c2:

if i==c2:

res+=c1

else:

res+=c2

else:

res+=i

return (res)

else:#if two indexes are given

c1=s[l[0]]

c2=s[l[1]]

res=''

for i in s:

if i==c1 or i==c2:

if i==c2:

res+=c1

else:

res+=c2

else:

res+=i

return (res)

def is_chain(*k):#method that checks words for one difference

l=list(k)

if len(l)==0:

return False

if len(l)==1:

return True

if len(l)>1:

for i in range(len(l)-1):#for two words in l

if one_diff(l[i],l[i+1]):#call one_diff method

continue

else:

return False

return True

def scramble(s):#method that exchanges characters

i=0

while i<len(s)-1:

s=exchange(s,i,i+1)

i+=2

print(s)

please check out the screenshots in attachment.

one_diff(a?): Description: Determine if there is exactly one character different between-example-1
one_diff(a?): Description: Determine if there is exactly one character different between-example-2
one_diff(a?): Description: Determine if there is exactly one character different between-example-3
one_diff(a?): Description: Determine if there is exactly one character different between-example-4
User Miradulo
by
3.9k points