Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   generating strings (http://www.velocityreviews.com/forums/t285214-generating-strings.html)

Eddie 08-25-2004 07:24 PM

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?

Victor Bazarov 08-25-2004 07:32 PM

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

Karl Heinz Buchegger 08-26-2004 08:42 AM

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

Karl Heinz Buchegger 08-26-2004 08:53 AM

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

Eddie 08-26-2004 05:15 PM

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.

Siemel Naran 08-27-2004 04:45 AM

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 <<;
}
}



Victor Bazarov 08-27-2004 05:12 AM

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>



Karthiik Kumar 08-28-2004 05:31 AM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57