Re: newb: comapring two strings

2006-05-19 Thread Peter Otten
manstey wrote: Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1[4] and the difference is ('i','e') But if there was str1=yiqtol and str2=yaqtel, I am

Re: newb: comapring two strings

2006-05-19 Thread John Machin
Use the levenshtein distance. Given the constraint that the two strings are the same length, I'm assuming (as other posters appear to have done) that vary by only one character precludes insertion and deletion operations. In that case, the problem can be solved in O(n) time by a simple loop

Re: newb: comapring two strings

2006-05-19 Thread Paul McGuire
John Machin [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] In that case, the problem can be solved in O(n) time by a simple loop which counts the number of differences and notes the position of the first (if any) difference. Any full-distance Levenshtein method that does no

Re: newb: comapring two strings

2006-05-19 Thread bearophileHUGS
I think a way to solve the problem may be: 1) create a little Python script to separate the original words in many files, each one containing only words of the same length. Every filename can contain the relative word length. 2) write a little C program with two nested loops, that scans all the

Re: newb: comapring two strings

2006-05-19 Thread bearophileHUGS
I have suggested C because if the words are all of the same length then you have 3^2 = 90 000 000 000 pairs to test. Sorry, you have (n*(n-1))/2 pairs to test (~ 45 000 000 000). bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: newb: comapring two strings

2006-05-19 Thread Paul Rubin
[EMAIL PROTECTED] writes: I have suggested C because if the words are all of the same length then you have 3^2 = 90 000 000 000 pairs to test. Sorry, you have (n*(n-1))/2 pairs to test (~ 45 000 000 000). Still terrible. Use a better algorithm! --

Re: newb: comapring two strings

2006-05-19 Thread John Machin
Paul Rubin wrote: [EMAIL PROTECTED] writes: I have suggested C because if the words are all of the same length then you have 3^2 = 90 000 000 000 pairs to test. Sorry, you have (n*(n-1))/2 pairs to test (~ 45 000 000 000). Still terrible. Use a better algorithm! To put all this in

Re: newb: comapring two strings

2006-05-19 Thread bearophileHUGS
Paul RubinStill terrible. Use a better algorithm! I agree, it's O(n^2), but if you need to run this program just 1 time, and the program is in C, and you don't want to use much time to think and code a better algorithm (or you aren't able to do it) then maybe that naive solution can be enough,

newb: comapring two strings

2006-05-18 Thread manstey
Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1[4] and the difference is ('i','e') But if there was str1=yiqtol and str2=yaqtel, I am not interested. can

Re: newb: comapring two strings

2006-05-18 Thread Diez B. Roggisch
Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1[4] and the difference is ('i','e') But if there was str1=yiqtol and str2=yaqtel, I am not interested.

Re: newb: comapring two strings

2006-05-18 Thread Justin Azoff
manstey wrote: Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1[4] and the difference is ('i','e') something like this maybe? str1='yaqtil'

Re: newb: comapring two strings

2006-05-18 Thread Serge Orlov
manstey wrote: Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1[4] and the difference is ('i','e') But if there was str1=yiqtol and str2=yaqtel, I am

Re: newb: comapring two strings

2006-05-18 Thread johnzenger
manstey wrote: Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. You want zip. def diffbyonlyone(string1, string2): diffcount = 0 for c1, c2 in zip(string1, string2): if c1 != c2: