![]() |
generating strings
I searched with my problem but with no results :(
My question is: how can I generate string, having only simple pattern, like, [0-3]mid[4-7]end For example tyis pattern should reproduce strings like: 1. 0min4end 2. 0min5end 3. 0min6end 4. 0min7end 5. 1min4end 6. 1min5end 7. 1min6end 8. 1min7end 9. 2min4end 10. 2min5end 11. 2min6end 12. 2min7end 13. 3min4end 14. 3min5end 15. 3min6end 16. 3min7end Is there a alghoritm for it, how can I begin with making this? |
Re: generating strings
Eddie wrote:
> I searched with my problem but with no results :( > > My question is: how can I generate string, having only simple pattern, > like, [0-3]mid[4-7]end Usually with the help of 'sprintf' function. Your "pattern" is just the format string you need to create. > For example tyis pattern should reproduce strings like: > > 1. 0min4end > 2. 0min5end > 3. 0min6end > 4. 0min7end > > 5. 1min4end > 6. 1min5end > 7. 1min6end > 8. 1min7end > > 9. 2min4end > 10. 2min5end > 11. 2min6end > 12. 2min7end > > 13. 3min4end > 14. 3min5end > 15. 3min6end > 16. 3min7end > > Is there a alghoritm for it, how can I begin with making this? One may say that there is an algorithm for everything, all you need to do is to find it. Read about 'for' loops and 'sprintf' function. Also, read the FAQ. Especially section 5. Victor |
Re: generating strings
Eddie wrote:
> > I searched with my problem but with no results :( > > My question is: how can I generate string, having only simple pattern, > like, [0-3]mid[4-7]end > For example tyis pattern should reproduce strings like: > > 1. 0min4end > 2. 0min5end > 3. 0min6end > 4. 0min7end > > 5. 1min4end > 6. 1min5end > 7. 1min6end > 8. 1min7end > > 9. 2min4end > 10. 2min5end > 11. 2min6end > 12. 2min7end > > 13. 3min4end > 14. 3min5end > 15. 3min6end > 16. 3min7end > > Is there a alghoritm for it, how can I begin with making this? -- Karl Heinz Buchegger, GASCAD GmbH Teichstrasse 2 A-4595 Waldneukirchen Tel ++43/7258/7545-0 Fax ++43/7258/7545-99 email: kbuchegg@gascad.at Web: www.gascad.com Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5 |
Re: generating strings
Eddie wrote:
> > I searched with my problem but with no results :( > > My question is: how can I generate string, having only simple pattern, > like, [0-3]mid[4-7]end > For example tyis pattern should reproduce strings like: > > 1. 0min4end > 2. 0min5end > 3. 0min6end > 4. 0min7end > > 5. 1min4end > 6. 1min5end > 7. 1min6end > 8. 1min7end > > 9. 2min4end > 10. 2min5end > 11. 2min6end > 12. 2min7end > > 13. 3min4end > 14. 3min5end > 15. 3min6end > 16. 3min7end > > Is there a alghoritm for it, how can I begin with making this? I would start with dividing the pattern into 3 parts a prefix the repeat part the rest up to the right end eg. your given pattern [0-3]mid[4-7]end divides into Prefix: empty repeat part: [0-3] rest: mid[4-7]end function PrintPattern( string prefix, string pattern ) divide pattern into prefix_now, repeat part and rest catanate the prefix_now to the passed prefix if the repeat part is empty print catanate( prefix, rest ) else do a loop over the range of the repeat part create a temporary string which consists of the prefix and add the next character from the range PrintPattern( temporary, rest ) end function So this becomes a recursive algorithm. You might need to tweak the above a little bit but basically that should be it. -- Karl Heinz Buchegger kbuchegg@gascad.at |
Re: generating strings
Karl Heinz Buchegger wrote:
>Eddie wrote: > > >>I searched with my problem but with no results :( >> >>My question is: how can I generate string, having only simple pattern, >>like, [0-3]mid[4-7]end >>For example tyis pattern should reproduce strings like: >> >>1. 0min4end >>2. 0min5end >>3. 0min6end >>4. 0min7end >> >>5. 1min4end >>6. 1min5end >>7. 1min6end >>8. 1min7end >> >>9. 2min4end >>10. 2min5end >>11. 2min6end >>12. 2min7end >> >>13. 3min4end >>14. 3min5end >>15. 3min6end >>16. 3min7end >> >>Is there a alghoritm for it, how can I begin with making this? >> >> > >I would start with dividing the pattern into 3 parts >a prefix >the repeat part >the rest up to the right end > >eg. your given pattern [0-3]mid[4-7]end divides into > > Prefix: empty > repeat part: [0-3] > rest: mid[4-7]end > > >function PrintPattern( string prefix, string pattern ) > > divide pattern into prefix_now, repeat part and rest > catanate the prefix_now to the passed prefix > > if the repeat part is empty > print catanate( prefix, rest ) > else > do a loop over the range of the repeat part > create a temporary string which consists of > the prefix and add the next character from the range > > PrintPattern( temporary, rest ) > >end function > >So this becomes a recursive algorithm. You might need to >tweak the above a little bit but basically that should be it. > > > Thanks its good for a start, I will try to write this, it seems loking easy now. |
Re: generating strings
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
> Eddie wrote: > > My question is: how can I generate string, having only simple pattern, > > like, [0-3]mid[4-7]end > I would start with dividing the pattern into 3 parts > a prefix > the repeat part > the rest up to the right end > > eg. your given pattern [0-3]mid[4-7]end divides into > > Prefix: empty > repeat part: [0-3] > rest: mid[4-7]end > > > function PrintPattern( string prefix, string pattern ) > > divide pattern into prefix_now, repeat part and rest > catanate the prefix_now to the passed prefix > > if the repeat part is empty > print catanate( prefix, rest ) > else > do a loop over the range of the repeat part > create a temporary string which consists of > the prefix and add the next character from the range > > PrintPattern( temporary, rest ) > > end function > > So this becomes a recursive algorithm. You might need to > tweak the above a little bit but basically that should be it. This is good for the general case, especially when you want the user to be able to enter a string like "[0-3]mid[4-7]end" or anything else with any number of repeating parts, then you generate the strings. But there is a simpler algorithm: for (int i=0; i<=3; ++i) { for (int j=4; j<=7; ++j) { char buffer[some big number]; sprintf("%dmid%d", i, j); or instead of the above 2 lines use ostringstream and <<; } } |
Re: generating strings
"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote...
> "Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message > > Eddie wrote: > > > > My question is: how can I generate string, having only simple pattern, > > > like, [0-3]mid[4-7]end > > I would start with dividing the pattern into 3 parts [...] > > This is good for the general case, [...] > But there is a simpler algorithm: > [...] I see somebody is just itching to do somebody else's homework, eh? Not enough problems to solve at work, I take it... Or has it been a long documentation-writing week with almost no coding? <g> |
Re: generating strings
Eddie wrote:
> I searched with my problem but with no results :( > > My question is: how can I generate string, having only simple pattern, > like, [0-3]mid[4-7]end > For example tyis pattern should reproduce strings like: > > 1. 0min4end > 2. 0min5end > 3. 0min6end > 4. 0min7end > > 5. 1min4end > 6. 1min5end > 7. 1min6end > 8. 1min7end > > 9. 2min4end > 10. 2min5end > 11. 2min6end > 12. 2min7end > > 13. 3min4end > 14. 3min5end > 15. 3min6end > 16. 3min7end > > Is there a alghoritm for it, how can I begin with making this? To put things in perspective ( or to make things complicated a little bit) google for 'regular expressions'. You will have a better idea of implementing it. ( or even a considerable superset of the given problem) -- Karthik |
| All times are GMT. The time now is 07:01 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.