Dec-28-2021, 10:12 PM
Hi guys. I really got stuck with my homework assignment. Can you guys please help me to reduce the complexity of the code below?
but first, the task is:
Two strings are called similar up to addition by a constant if by simple adding of an integer Ci,iϵ{A−Z} to a character we can translate one string to another and vice versa (using another constant). For example, BOB is similar up to addition by a constant to POP, while we map B to P and O to O. Both of these are similar up to addition by a constant to PIP, where O is mapped to I and the rest of the letters - as for the previous example. BOT is not similar up to addition by a constant to any of these, since B and T can't map to the same symbol (without a proof). Write a code that checks if one string is similar up to addition by a constant to another. This might be easily checked for each string against the other sepparately at O(n^2) , here you may have an extra space of O(n) in order to solve the question at O(n) .
So far my code is:
but first, the task is:
Two strings are called similar up to addition by a constant if by simple adding of an integer Ci,iϵ{A−Z} to a character we can translate one string to another and vice versa (using another constant). For example, BOB is similar up to addition by a constant to POP, while we map B to P and O to O. Both of these are similar up to addition by a constant to PIP, where O is mapped to I and the rest of the letters - as for the previous example. BOT is not similar up to addition by a constant to any of these, since B and T can't map to the same symbol (without a proof). Write a code that checks if one string is similar up to addition by a constant to another. This might be easily checked for each string against the other sepparately at O(n^2) , here you may have an extra space of O(n) in order to solve the question at O(n) .
So far my code is:
def ascs(letter): sum=0 pow=1 for i in letter: sum=sum+ord(i)*(2**pow) pow=pow+1 return sum def contain_duplicates(list): return len(set(list)) != len(list) def excs4(char1,char2): empty=[] temp=[] for i in range(0,len(char1)): temp.append(char1[i]+char2[i]) empty.append(ascs(char1[i]+char2[i])) return contain_duplicates(empty) excs4("BIB","ZIP")