473,587 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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(pru nedK) - 1,1):
if k in range(1,len(pru nedK),1) & i+k <= len(prunedK) -1:
colocn = prunedK[i] + prunedK[i+k]
prunedNew1.appe nd(colocn)
continue
for string in prunedNew1:
stringNew = withoutDup(stri ng)
prunedNew.appen d(stringNew)
continue

But this one is quite bad in the time aspect :(.
Thanks in advance,
girish
Jun 8 '06 #1
17 1832
MTD
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")

Jun 8 '06 #2
MTD
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")


Jun 8 '06 #3
MTD
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

Jun 8 '06 #4
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(pru nedK) - 1,1):
if k in range(1,len(pru nedK),1) & i+k <= len(prunedK) -1:
colocn = prunedK[i] + prunedK[i+k]
prunedNew1.appe nd(colocn)
continue
for string in prunedNew1:
stringNew = withoutDup(stri ng)
prunedNew.appen d(stringNew)
continue

But this one is quite bad in the time aspect :(.
Thanks in advance,
girish


Jun 8 '06 #5
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(sor ted(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(pru nedK) - 1,1):
if k in range(1,len(pru nedK),1) & i+k <= len(prunedK) -1:
colocn = prunedK[i] + prunedK[i+k]
prunedNew1.appe nd(colocn)
continue
for string in prunedNew1:
stringNew = withoutDup(stri ng)
prunedNew.appen d(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
Jun 8 '06 #6
MTD

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.

Jun 8 '06 #7
Boris Borcic:
I'd favor the following, that I find most readable
sets = map(set,list_of _strings)
res = set(''.join(sor ted(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

Jun 8 '06 #8
> 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

Jun 8 '06 #9
be************@ lycos.com wrote:
Boris Borcic:
I'd favor the following, that I find most readable
sets = map(set,list_of _strings)
res = set(''.join(sor ted(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

Jun 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
18572
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from tablename; Here this function generate unique id for each row of the table. Regards..
6
4058
by: chiara | last post by:
Hi everybody! I am just at the beginning as a programmer, so maybe this is a stupid question...Anyway,I need to write a function in C to generate generate all possible strings of given length given a set of characters (allowing repetitions of the same character) For example given the characters 'E' and 'H' and maximum length 3 the function should generate the sequences
7
3881
by: anitha1234 | last post by:
In C++ by using rand(); i am able to generate 10 numbers, if i want to generate 4kbytes of numbers or strings what i have to do, if anyone knows pls help me. Thankyou
9
6444
by: Omatase | last post by:
I have a set of about 6 or so strings that I need to use to generate a unique hash. This hash will become the unique key in a database so the hash has to be the same each time I gen it for any 1 set of strings. Is there something out there that already does this written in javascript? I didn't find anything doing a google search.
8
3560
by: dohyohdohyoh | last post by:
I have a programming question to generate an ordered list of alphanumeric strings of length 4. two alphabets rest numberst, etc. EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9 then AAAA - ZZZZ
3
5843
by: dohyohdohyoh | last post by:
I have a programming question to generate an ordered list of alphanumeric strings of length 4. two alphabets rest numberst, etc. EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9 then AAAA - ZZZZ
8
7195
by: Marc | last post by:
Hi all, I have to generate and send to a printer many 6 digit alphanumeric strings. they have to be unique but I cannot check in a database or something like that if it have already been printed. the string has also to seem a random one and it cannot have an apparence of a sequence. My first approach is to do it with a decimal counter and find and use an encryption alghorithm that converts each 6 digit decimal number to a 6 digit...
6
13451
by: =?Utf-8?B?VGFydW4=?= | last post by:
Hi, I have to generate a fixed length text file.I have a file formats like fields details as well as its length and position.what actually i have to do that i will get the data from the databas and then i need to create a fixed length flat file as per format.so please give me a sample for this.
0
7924
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.