473,795 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generating strings based on pattern

Hi All,
I am writing a function to generate the strings based on a
pattern. For example A[1-3] will generate A1, A2 and A3. If the
pattern is A[1-3][1-2] then it will generate the strings A11, A12,
A21, A22, A31, A32. What is the best algorithm to accomplish this?

Thanks for your time.

ananihdv

Aug 20 '07 #1
4 1582
In article <11************ *********@k79g2 000hse.googlegr oups.com>,
<dh*********@ya hoo.comwrote:
I am writing a function to generate the strings based on a
pattern. For example A[1-3] will generate A1, A2 and A3. If the
pattern is A[1-3][1-2] then it will generate the strings A11, A12,
A21, A22, A31, A32. What is the best algorithm to accomplish this?
That's an algorithm question, not a C question.

*One* algorithm is:

For each position, construct a string which lists each of the
allowed characters at that position. Create an array of these
strings, say char *PosChars[NumPositions].
Allocate an integral array, PosIdx, which is as long as the number of
positions. The integral type needs to be as wide enough to
count the maximum number of different characters allowed at any
given position. If you aren't using multibyte characters,
unsigned char PosIdx[NumPositions] should do. Initialize each
element of the PosIdx array to 0.

Outer Loop

For J over all the position indices: output PosChars[PosIdx[J]]
Output the string seperator (e.g., newline)

Let J be the last position index

Inner Loop:

Increment PosIdx[J]
If PosChars[PosIdx[J]] is the nul character (0)
Reset PosIdx[J] to the first index
Decrement J
If J is less than 0, the program is finished
Otherwise, allow the next iteration of the inner loop
Otherwise, if the character was not nul, terminate the inner loop

End of Inner Loop

Allow the next iteration of the outer loop

End of Outer Loop
I don't know who first invented this single-index algorithm
(a typical algorithm for working with N different positions involves
N nested FOR loops.) As far as I *remember*, I didn't read it anywhere
before I (re-?) invented it in late 2005 or early 2006. But it's
too useful of a technique for me to believe that I got there first.
Perhaps one of the other readers will have a reference for the
technique. (As the technique is not immediately obvious, you should
be crediting -someone- for the algorithm if you use it.)
--
All is vanity. -- Ecclesiastes
Aug 20 '07 #2
In article <fa**********@c anopus.cc.umani toba.ca>
Walter Roberson <ro******@ibd.n rc-cnrc.gc.cawrote :
>*One* algorithm is:
[snipped]
>(As the technique is not immediately obvious, you should
be crediting -someone- for the algorithm if you use it.)
I like to think of it as an odometer.

An ordinary odometer of N decimal digits increments thus:

/*
* Increment an "ndigits"-digit decimal odometer whose values
* are in odo[0] through odo[ndigits-1]. Return 0 on
* success, nonzero if the odometer has rolled over to
* all-zeros again.
*/
int odo_increment(i nt *odo, size_t ndigits) {
size_t i;

for (i = ndigits - 1;; i--) {
if (++odo[i] <= 9)
break;
odo[i] = 0;
if (i == 0)
return -1; /* overflow -- odometer is all 0s again */
}
return 0; /* success */
}

It is not too difficult to adapt this to "odometers" using something
other than "decimal digits". The key observation, of course, is
that we simply increment the least-significant "digit" until it
overflows, then reset it to zero and increment next-most significant
digit, and so on -- exactly like an old-style mechanical odometer.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 20 '07 #3
In article <fa********@new s4.newsguy.com> ,
Chris Torek <no****@torek.n etwrote:
>In article <fa**********@c anopus.cc.umani toba.ca>
Walter Roberson <ro******@ibd.n rc-cnrc.gc.cawrote :
>>*One* algorithm is:
>>(As the technique is not immediately obvious, you should
be crediting -someone- for the algorithm if you use it.)
>It is not too difficult to adapt this to "odometers" using something
other than "decimal digits". The key observation, of course, is
that we simply increment the least-significant "digit" until it
overflows, then reset it to zero and increment next-most significant
digit, and so on -- exactly like an old-style mechanical odometer.
Yes, exactly, that's a very nice way of thinking about the
algorithm!

Any idea where it originated? Or is it old enough that we could
start a new urban legend that it Admiral Grace Hooper, coiner
of the term "bug" ?

http://www.sugarforge.org/users/gracie/
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Aug 20 '07 #4
On Mon, 20 Aug 2007 18:05:04 +0000 (UTC),
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
>In article <fa********@new s4.newsguy.com> ,
Chris Torek <no****@torek.n etwrote:
>>In article <fa**********@c anopus.cc.umani toba.ca>
Walter Roberson <ro******@ibd.n rc-cnrc.gc.cawrote :
>>>*One* algorithm is:
>>>(As the technique is not immediately obvious, you should
be crediting -someone- for the algorithm if you use it.)
>>It is not too difficult to adapt this to "odometers" using something
other than "decimal digits". The key observation, of course, is
that we simply increment the least-significant "digit" until it
overflows, then reset it to zero and increment next-most significant
digit, and so on -- exactly like an old-style mechanical odometer.

Yes, exactly, that's a very nice way of thinking about the
algorithm!

Any idea where it originated? Or is it old enough that we could
start a new urban legend that it Admiral Grace Hooper, coiner
of the term "bug" ?
She probably did it, but I rather imagine that it predates
computers - it's the obvious way to map cartesian products into
one dimension. I recall using it in Fortran II which was quite
some time ago.

>
http://www.sugarforge.org/users/gracie/
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Aug 20 '07 #5

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

Similar topics

2
8558
by: Muumac | last post by:
I have problem with large textfiles! When I load over 4MB xml and then try to preg_match something in this I get always FALSE! I have <File>....</File> tags in XML. Between tags is files contents BASE64 encoded! When xml contains only one big over 4MB file in it, preg_match and preg_match_all doesnt find it! When included file size is nelow 4MB, preg functions works perfectly! But PHP manual says that there is no size limitations to...
7
7292
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? STL set, map? Could you give me a little code example? Thank you.
5
1894
by: Jon Sequeira | last post by:
Does anyone know of a component or class that available for generating updategrams from custom business objects? Ideally I need something that parses a mapping schema, interrogates an object, and produces an updategram. Any suggestions are welcome. Thanks. Jon
7
1984
by: Mary | last post by:
Hi, I need some assistance with a query. To be honest, I'm not even sure it can be done. I'll try to keep the information limited to only what's relevant to what I have and what I am trying to achieve with this query. I have a table that contains around 100,000 records. For the sake of this discussion, assume just two columns: ID Data
3
343
by: Eddie | last post by:
I searched with my problem but with no results :( My question is: how can I generate string, having only simple pattern, like, midend For example tyis pattern should reproduce strings like: 1. 0min4end 2. 0min5end 3. 0min6end 4. 0min7end
10
1850
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It doesn't work OK in ANSI mode however, as it has to use MultiByteToWideChar and WideCharToMultiByte. I've discovered that the regular expression part is working fine. As far as I can tell the regular expression code is correctly parsing what it...
25
3574
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
5
2230
by: BBands | last post by:
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in "beatlemania". The application is to look though a long list of songs and return any approximate matches along with a confidence factor. I have looked at edit distance, but that isn't a good choice for finding a short string in a longer one. I have also explored difflib.SequenceMatcher and...
18
3274
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
9672
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
10438
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
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2920
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.