Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to generate k+1 length strings from a list of k length strings?

Reply
Thread Tools

How to generate k+1 length strings from a list of k length strings?

 
 
Girish Sahani
Guest
Posts: n/a
 
      06-08-2006

I have a list of strings all of length k. For every pair of k length
strings which have k-1 characters in common, i want to generate a k+1
length string(the k-1 common characters + 2 not common characters).
e.g i want to join 'abcd' with bcde' to get 'abcde' but i dont want to
join 'abcd' with 'cdef'
Currently i'm joining every 2 strings, then removing duplicate characters
from every joined string and finally removing all those strings whose
length != k+1.Here's the code i've written:

for i in range(0,len(prunedK) - 1,1):
if k in range(1,len(prunedK),1) & i+k <= len(prunedK) -1:
colocn = prunedK[i] + prunedK[i+k]
prunedNew1.append(colocn)
continue
for string in prunedNew1:
stringNew = withoutDup(string)
prunedNew.append(stringNew)
continue

But this one is quite bad in the time aspect .
Thanks in advance,
girish
 
Reply With Quote
 
 
 
 
MTD
Guest
Posts: n/a
 
      06-08-2006
Try this:

def k2k1(string1, string2):
for c in string1:
string2 = string2.replace(c,"",1)

if len(string2) == 1:
string1 += string2

return string1

print k2k1("abcd", "ebcd")

 
Reply With Quote
 
 
 
 
MTD
Guest
Posts: n/a
 
      06-08-2006
actually, minor fix:

MTD wrote:
> Try this:
>
> def k2k1(string1, string2):
> for c in string1:
> string2 = string2.replace(c,"",1)
>
> if len(string2) == 1:
> string1 += string2

else:
string1 = ""

>
> return string1
>
> print k2k1("abcd", "ebcd")


 
Reply With Quote
 
MTD
Guest
Posts: n/a
 
      06-08-2006
So yeah, just to put it all together, try this. From your two Ks, it
either returns K+1 if it can or an empty string.

def k2k1(string1, string2):
for c in string1:
string2 = string2.replace(c,"",1)

if len(string2) == 1:
string1 += string2
else:
string1 = ""

return string1


Testing:

print k2k1("abcdadd", "abceadd")

gives:
abcdadde

 
Reply With Quote
 
Jon Clements
Guest
Posts: n/a
 
      06-08-2006
Are you asking the question, "Which pairs of strings have one character
different in each?", or "Which pairs of strings have a substring of
len(string) - 1 in common?".

Jon.

Girish Sahani wrote:
> I have a list of strings all of length k. For every pair of k length
> strings which have k-1 characters in common, i want to generate a k+1
> length string(the k-1 common characters + 2 not common characters).
> e.g i want to join 'abcd' with bcde' to get 'abcde' but i dont want to
> join 'abcd' with 'cdef'
> Currently i'm joining every 2 strings, then removing duplicate characters
> from every joined string and finally removing all those strings whose
> length != k+1.Here's the code i've written:
>
> for i in range(0,len(prunedK) - 1,1):
> if k in range(1,len(prunedK),1) & i+k <= len(prunedK) -1:
> colocn = prunedK[i] + prunedK[i+k]
> prunedNew1.append(colocn)
> continue
> for string in prunedNew1:
> stringNew = withoutDup(string)
> prunedNew.append(stringNew)
> continue
>
> But this one is quite bad in the time aspect .
> Thanks in advance,
> girish


 
Reply With Quote
 
Boris Borcic
Guest
Posts: n/a
 
      06-08-2006
Girish Sahani wrote:
> I have a list of strings all of length k. For every pair of k length
> strings which have k-1 characters in common, i want to generate a k+1
> length string(the k-1 common characters + 2 not common characters).
> e.g i want to join 'abcd' with bcde' to get 'abcde' but i dont want to
> join 'abcd' with 'cdef'
> Currently i'm joining every 2 strings, then removing duplicate characters
> from every joined string and finally removing all those strings whose
> length != k+1.


Hum, since your code is not syntactically correct, anything will run faster
I'd favor the following, that I find most readable

sets = map(set,list_of_strings)
res = set(''.join(sorted(s1|s2)) for s1 in sets for s2 in sets if len(s1^s2)==2)

unless performance is really an issue

Here's the code i've written:
>
> for i in range(0,len(prunedK) - 1,1):
> if k in range(1,len(prunedK),1) & i+k <= len(prunedK) -1:
> colocn = prunedK[i] + prunedK[i+k]
> prunedNew1.append(colocn)
> continue
> for string in prunedNew1:
> stringNew = withoutDup(string)
> prunedNew.append(stringNew)
> continue
>
> But this one is quite bad in the time aspect


how do you know ?

> Thanks in advance,
> girish


you should do your own homework
 
Reply With Quote
 
MTD
Guest
Posts: n/a
 
      06-08-2006

Jon Clements wrote:
> Are you asking the question, "Which pairs of strings have one character
> different in each?", or "Which pairs of strings have a substring of
> len(string) - 1 in common?".
>
> Jon.


I imagine it's the former because the latter is trivially easy, I mean
_really_ trivially easy.

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      06-08-2006
Boris Borcic:
> I'd favor the following, that I find most readable
> sets = map(set,list_of_strings)
> res = set(''.join(sorted(s1|s2)) for s1 in sets for s2 in sets if len(s1^s2)==2)


I think there can be written more readable code. For my programs I
usually prefer simpler code, that (if possible) even a children can
understand. So I can debug, modify and improve it better & faster.

Bye,
bearophile

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      06-08-2006
> I think there can be written more readable code. For my programs I
> usually prefer simpler code, that (if possible) even a children can
> understand. So I can debug, modify and improve it better & faster.


Debugged:
I think it can be written more readable code.
In this newsgroup sometimes I have tried to post 'clever' code, but for
my programs I (if possible) prefer simpler code, that even a child can
understand. So I can debug, modify and improve it faster.

Sorry, I was tired,
bearophile

 
Reply With Quote
 
Boris Borcic
Guest
Posts: n/a
 
      06-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Boris Borcic:
>> I'd favor the following, that I find most readable
>> sets = map(set,list_of_strings)
>> res = set(''.join(sorted(s1|s2)) for s1 in sets for s2 in sets if len(s1^s2)==2)

>
> I think there can be written more readable code.


readability, of course, is in the eye of the beholder... and I find this code
*much* easier to recognize as a realisation of the description made by the OP,
than the code he himself offered - if you care to take a look at both.

For my programs I
> usually prefer simpler code,


I challenge you to write simpler code to do the equivalent.

> that (if possible) even a children can
> understand.


what child ? one that is trained just like *you* think children should start, I
guess.

> So I can debug, modify and improve it better & faster.


Sure, but the case is we each were *distinct* children.

>
> Bye,
> bearophile
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Memory issues when storing as List of Strings vs List of List OW Ghim Siong Python 2 11-30-2010 12:22 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
generate all possible strings of given length given a set of characters chiara C Programming 6 10-06-2005 01:43 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM



Advertisments