Repeat a string: Difference between revisions
Content added Content deleted
(→{{header|PARI/GP}}: editing, adding another version) |
|||
Line 1,176: | Line 1,176: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
===Version #1. Based on recursion.=== |
|||
This solution is recursive and unimaginably bad. Slightly less bad versions can be designed, but that's not the point: don't use GP for text processing if you can avoid it. If you really need to, it's easy to create an efficient function in PARI (see [[#C|C]]) and pass that to GP. |
This solution is recursive and unimaginably bad. Slightly less bad versions can be designed, but that's not the point: don't use GP for text processing if you can avoid it. If you really need to, it's easy to create an efficient function in PARI (see [[#C|C]]) and pass that to GP. |
||
<lang parigp>repeat(s,n)={ |
<lang parigp>repeat(s,n)={ |
||
Line 1,193: | Line 1,195: | ||
); |
); |
||
}</lang> |
}</lang> |
||
===Version #2. Simple loop based.=== |
|||
{{Works with|PARI/GP|2.7.4 and above}} |
|||
Basic set of string functions is very handy for presentation purposes. At the same time, it is true that PARI/GP is not an appropriate tool |
|||
for the heavy text processing. |
|||
<lang PARI/GP> |
|||
\\ Repeat a string str the specified number of times ntimes and return composed string. |
|||
\\ 3/3/2016 aev |
|||
srepeat(str,ntimes)={ |
|||
my(srez=str,nt=ntimes-1); |
|||
if(ntimes<1||#str==0,return("")); |
|||
if(ntimes==1,return(str)); |
|||
for(i=1,nt, srez=concat(srez,str)); |
|||
return(srez); |
|||
} |
|||
{ |
|||
\\ TESTS |
|||
print(" *** Testing srepeat:"); |
|||
print("1.",srepeat("a",5)); |
|||
print("2.",srepeat("ab",5)); |
|||
print("3.",srepeat("c",1)); |
|||
print("4.|",srepeat("d",0),"|"); |
|||
print("5.|",srepeat("",5),"|"); |
|||
print1("6."); for(i=1,10000000, srepeat("e",10)); |
|||
} |
|||
</lang> |
|||
{{Output}} |
|||
<pre> |
|||
*** Testing srepeat: |
|||
1.aaaaa |
|||
2.ababababab |
|||
3.c |
|||
4.|| |
|||
5.|| |
|||
6. |
|||
(16:00) gp > ## |
|||
*** last result computed in 1min, 2,939 ms. |
|||
</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |