tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,3 @@
Take a string and repeat it some number of times. Example: repeat("ha", 5) => "hahahahaha"
If there is a simpler/more efficient way to repeat a single “character” (i.e. creating a string filled with a certain character), you might want to show that as well (i.e. repeat-char("*", 5) => "*****").

View file

@ -0,0 +1,2 @@
---
note: String manipulation

View file

@ -0,0 +1,10 @@
gosub repeat ha 5
echo %@repeat[*,5]
quit
:Repeat [String Times]
do %Times%
echos %String%
enddo
echo.
return

View file

@ -0,0 +1 @@
print (5 * "ha")

View file

@ -0,0 +1,10 @@
function repeat( str, n, rep, i )
{
for( ; i<n; i++ )
rep = rep str
return rep
}
BEGIN {
print repeat( "ha", 5 )
}

View file

@ -0,0 +1,7 @@
function repeatString(string:String, numTimes:uint):String
{
var output:String = "";
for(var i:uint = 0; i < numTimes; i++)
output += string;
return output;
}

View file

@ -0,0 +1,7 @@
function repeatRecursive(string:String, numTimes:uint):String
{
if(numTimes == 0) return "";
if(numTimes & 1) return string + repeatRecursive(string, numTimes - 1);
var tmp:String = repeatRecursive(string, numTimes/2);
return tmp + tmp;
}

View file

@ -0,0 +1,2 @@
import mx.utils.StringUtil;
trace(StringUtil.repeat("ha", 5));

View file

@ -0,0 +1,7 @@
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
procedure String_Multiplication is
begin
Put_Line (5 * "ha");
end String_Multiplication;

View file

@ -0,0 +1,5 @@
set str to "ha"
set final_string to ""
repeat 5 times
set final_string to final_string & str
end repeat

View file

@ -0,0 +1,8 @@
MsgBox % Repeat("ha",5)
Repeat(String,Times)
{
Loop, %Times%
Output .= String
Return Output
}

View file

@ -0,0 +1 @@
PRINT STRING$(5, "ha")

View file

@ -0,0 +1,3 @@
main: { "ha" 5 print_repeat }
print_repeat!: { <- { dup << } -> times }

View file

@ -0,0 +1 @@
hahahahaha

View file

@ -0,0 +1,8 @@
@echo off
if "%2" equ "" goto fail
setlocal enabledelayedexpansion
set char=%1
set num=%2
for /l %%i in (1,1,%num%) do set res=!res!%char%
echo %res%
:fail

View file

@ -0,0 +1,10 @@
@echo off
set /p a=Enter string to repeat :
set /p b=Enter how many times to repeat :
set "c=1"
set "d=%b%"
:a
echo %a%
set "c=%c%+=1"
if /i _"%c%"==_"%d%" (exit /b)
goto :a

View file

@ -0,0 +1,13 @@
@echo off
@FOR /L %%i in (0,1,9) DO @CALL :REPEAT %%i
@echo That's it!
@FOR /L %%i in (0,1,9) DO @CALL :REPEAT %%i
@echo.
@echo And that!
@GOTO END
:REPEAT
@echo|set /p="*"
@GOTO:EOF
:END

View file

@ -0,0 +1,8 @@
(repeat=
string N rep
. !arg:(?string.?N)
& !string:?rep
& whl
' (!N+-1:>0:?N&!string !rep:?rep)
& str$!rep
);

View file

@ -0,0 +1,9 @@
+++++ +++++ init first as 10 counter
[-> +++++ +++++<] we add 10 to second each loopround
Now we want to loop 5 times to follow std
+++++
[-> ++++ . ----- -- . +++<] print h and a each loop
and a newline because I'm kind and it looks good
+++++ +++++ +++ . --- .

View file

@ -0,0 +1 @@
p "ha" * 5 #Prints "hahahahaha"

View file

@ -0,0 +1,4 @@
blsq ) 'h5?*
"hhhhh"
blsq ) "ha"5.*\[
"hahahahaha"

View file

@ -0,0 +1,15 @@
#include <string>
#include <iostream>
std::string repeat( const std::string &word, int times ) {
std::string result ;
result.reserve(times*word.length()); // avoid repeated reallocation
for ( int a = 0 ; a < times ; a++ )
result += word ;
return result ;
}
int main( ) {
std::cout << repeat( "Ha" , 5 ) << std::endl ;
return 0 ;
}

View file

@ -0,0 +1,7 @@
#include <string>
#include <iostream>
int main( ) {
std::cout << std::string( 5, '*' ) << std::endl ;
return 0 ;
}

View file

@ -0,0 +1 @@
string s = "".PadLeft(5, 'X').Replace("X", "ha");

View file

@ -0,0 +1 @@
string s = new String('X', 5).Replace("X", "ha");

View file

@ -0,0 +1 @@
string s = String.Join("ha", new string[5 + 1]);

View file

@ -0,0 +1 @@
string s = String.Concat(Enumerable.Repeat("ha", 5));

View file

@ -0,0 +1 @@
string s = "".PadLeft(5, '*');

View file

@ -0,0 +1 @@
string s = new String('*', 5);

View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * string_repeat( int n, const char * s ) {
size_t slen = strlen(s);
char * dest = malloc(n*slen+1);
int i; char * p;
for ( i=0, p = dest; i < n; ++i, p += slen ) {
memcpy(p, s, slen);
}
*p = '\0';
return dest;
}
int main() {
char * result = string_repeat(5, "ha")
puts(result);
free(result);
return 0;
}

View file

@ -0,0 +1,13 @@
...
char *string_repeat(const char *str, int n)
{
char *pa, *pb;
size_t slen = strlen(str);
char *dest = malloc(n*slen+1);
pa = dest + (n-1)*slen;
strcpy(pa, str);
pb = --pa + slen;
while (pa>=dest) *pa-- = *pb--;
return dest;
}

View file

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * char_repeat( int n, char c ) {
char * dest = malloc(n+1);
memset(dest, c, n);
dest[n] = '\0';
return dest;
}
int main() {
char * result = char_repeat(5, '*');
puts(result);
free(result);
return 0;
}

View file

@ -0,0 +1 @@
(apply str (repeat 5 "ha"))

View file

@ -0,0 +1,5 @@
(defun repeat-string (n string)
(with-output-to-string (stream)
(loop repeat n do (write-string string stream))))
(princ (repeat-string 5 "hi"))

View file

@ -0,0 +1 @@
(make-string 5 :initial-element #\X)

View file

@ -0,0 +1,5 @@
import std.stdio, std.array;
void main() {
writeln("ha".replicate(5));
}

View file

@ -0,0 +1,8 @@
import std.stdio;
void main() {
char[] chars; // create the dynamic array
chars.length = 5; // set the length
chars[] = '*'; // set all characters in the string to '*'
writeln(chars);
}

View file

@ -0,0 +1 @@
PrintLn( StringOfString('abc',5) );

View file

@ -0,0 +1 @@
PrintLn( StringOfChar('a',5) );

View file

@ -0,0 +1,9 @@
function RepeatString(const s: string; count: cardinal): string;
var
i: Integer;
begin
for i := 1 to count do
Result := Result + s;
end;
Writeln(RepeatString('ha',5));

View file

@ -0,0 +1 @@
Writeln( StringOfChar('a',5) );

View file

@ -0,0 +1 @@
StrUtils.DupeString

View file

@ -0,0 +1 @@
"ha" * 5

View file

@ -0,0 +1,2 @@
repeat(X,N) ->
lists:flatten(lists:duplicate(N,X)).

View file

@ -0,0 +1,16 @@
function repeat_string(object x, integer times)
sequence out
if atom(x) then
return repeat(x,times)
else
out = ""
for n = 1 to times do
out &= x
end for
return out
end if
end function
puts(1,repeat_string("ha",5) & '\n') -- hahahahaha
puts(1,repeat_string('*',5) & '\n') -- *****

View file

@ -0,0 +1,4 @@
-- Here is an alternative method for "Repeat a string"
include std/sequence.e
printf(1,"Here is the repeated string: %s\n", {repeat_pattern("ha",5)})
printf(1,"Here is another: %s\n", {repeat_pattern("*",5)})

View file

@ -0,0 +1,3 @@
: repeat-string ( str n -- str' ) swap <repetition> concat ;
"ha" 5 repeat-string print

View file

@ -0,0 +1,6 @@
: place-n { src len dest n -- }
0 dest c!
n 0 ?do src len dest +place loop ;
s" ha" pad 5 place-n
pad count type \ hahahahaha

View file

@ -0,0 +1,7 @@
: place-n ( src len dest n -- )
swap >r 0 r@ c!
begin dup while -rot 2dup r@ +place rot 1- repeat
r> 2drop 2drop ;
s" ha" pad 5 place-n
pad count type \ hahahahaha

View file

@ -0,0 +1,2 @@
pad 10 char * fill \ repeat a single character
pad 10 type \ **********

View file

@ -0,0 +1,5 @@
program test_repeat
write (*, '(a)') repeat ('ha', 5)
end program test_repeat

View file

@ -0,0 +1 @@
println[repeat["ha", 5]]

View file

@ -0,0 +1,2 @@
Concatenation(List([1 .. 10], n -> "BOB "));
# "BOB BOB BOB BOB BOB BOB BOB BOB BOB BOB "

View file

@ -0,0 +1 @@
fmt.Println(strings.Repeat("ha", 5)) // ==> "hahahahaha"

View file

@ -0,0 +1 @@
fmt.Println(strings.Repeat(string('h'), 5)) // prints hhhhh

View file

@ -0,0 +1 @@
println 'ha' * 5

View file

@ -0,0 +1 @@
concat $ replicate 5 "ha"

View file

@ -0,0 +1 @@
[1..5] >> "ha"

View file

@ -0,0 +1 @@
cycle "ha"

View file

@ -0,0 +1 @@
replicate 5 '*'

View file

@ -0,0 +1,3 @@
CHARACTER out*20
EDIT(Text=out, Insert="ha", DO=5)

View file

@ -0,0 +1,3 @@
procedure main(args)
write(repl(integer(!args) | 5))
end

View file

@ -0,0 +1,4 @@
procedure repl(s, n)
every (ns := "") ||:= |s\(0 <= n)
return ns
end

View file

@ -0,0 +1,11 @@
Home is a room.
To decide which indexed text is (T - indexed text) repeated (N - number) times:
let temp be indexed text;
repeat with M running from 1 to N:
let temp be "[temp][T]";
decide on temp.
When play begins:
say "ha" repeated 5 times;
end the story.

View file

@ -0,0 +1,8 @@
5 # '*' NB. repeat each item 5 times
*****
5 # 'ha' NB. repeat each item 5 times
hhhhhaaaaa
5 ((* #) $ ]) 'ha' NB. repeat array 5 times
hahahahaha
5 ;@# < 'ha' NB. boxing is used to treat the array as a whole
hahahahaha

View file

@ -0,0 +1,9 @@
public static String repeat(String str, int times){
StringBuilder ret = new StringBuilder();
for(int i = 0;i < times;i++) ret.append(str);
return ret.toString();
}
public static void main(String[] args){
System.out.println(repeat("ha", 5));
}

View file

@ -0,0 +1,3 @@
public static String repeat(String str, int times){
return new String(new char[times]).replace("\0", str);
}

View file

@ -0,0 +1,5 @@
String.prototype.repeat = function(n) {
return new Array(1 + n).join(this);
}
alert("ha".repeat(5)); // hahahahaha

View file

@ -0,0 +1,2 @@
"ha"^5
'*'^5

View file

@ -0,0 +1,5 @@
,/5#,"ha"
"hahahahaha"
5#"*"
"*****"

View file

@ -0,0 +1,12 @@
a$ ="ha "
print StringRepeat$( a$, 5)
end
function StringRepeat$( in$, n)
o$ =""
for i =1 to n
o$ =o$ +in$
next i
StringRepeat$ =o$
end function

View file

@ -0,0 +1,4 @@
to copies :n :thing [:acc "||]
if :n = 0 [output :acc]
output (copies :n-1 :thing combine :acc :thing)
end

View file

@ -0,0 +1 @@
show cascade 5 [combine "ha ?] "|| ; hahahahaha

View file

@ -0,0 +1,6 @@
to copies :n :thing :acc
if :n = 0 [output :acc]
output (copies :n-1 :thing combine :acc :thing)
end
print copies 5 "ha "||

View file

@ -0,0 +1 @@
function repeats(s, n) return n > 0 and s .. repeat(s, n-1) or "" end

View file

@ -0,0 +1 @@
string.rep(s,n)

View file

@ -0,0 +1,3 @@
function S = repeat(s , n)
S = repmat(s , [1,n]) ;
return

View file

@ -0,0 +1,9 @@
RPTSTR(S,N)
;Repeat a string S for N times
NEW I
FOR I=1:1:N WRITE S
KILL I
QUIT
RPTSTR1(S,N) ;Functionally equivalent, but denser to read
F I=1:1:N W S
Q

View file

@ -0,0 +1,3 @@
;Even better (more terse)
S x="",$P(x,"-",10)="-"
W x

View file

@ -0,0 +1,7 @@
> use StringTools in
> Repeat( "abc", 10 ); # repeat an arbitrary string
> Fill( "x", 20 ) # repeat a character
> end use;
"abcabcabcabcabcabcabcabcabcabc"
"xxxxxxxxxxxxxxxxxxxx"

View file

@ -0,0 +1,5 @@
> cat( "abc" $ 10 );
"abcabcabcabcabcabcabcabcabcabc"
> cat( seq( "abc", i = 1 .. 10 ) );
"abcabcabcabcabcabcabcabcabcabc"

View file

@ -0,0 +1,3 @@
> s := "":
> to 10 do s := cat( s, "abc" ) end: s;
"abcabcabcabcabcabcabcabcabcabc"

View file

@ -0,0 +1,8 @@
(* solution 1 *)
rep[n_Integer,s_String]:=Apply[StringJoin,ConstantArray[s,{n}]]
(* solution 2 -- @@ is the infix form of Apply[] *)
rep[n_Integer,s_String]:=StringJoin@@Table[s,{n}]
(* solution 3 -- demonstrating another of the large number of looping constructs available *)
rep[n_Integer,s_String]:=Nest[StringJoin[s, #] &,s,n-1]

View file

@ -0,0 +1,5 @@
"$*"(s, n) := apply(sconcat, makelist(s, n))$
infix("$*")$
"abc" $* 5;
/* "abcabcabcabcabc" */

View file

@ -0,0 +1,26 @@
:- module repeat.
:- interface.
:- import_module string, char, int.
:- func repeat_char(char, int) = string.
:- func repeat(string, int) = string.
:- implementation.
:- import_module stream, stream.string_writer, string.builder.
repeat_char(C, N) = string.duplicate_char(C, N).
repeat(String, Count) = Repeated :-
S0 = string.builder.init,
Repeated = string.builder.to_string(S),
printn(string.builder.handle, Count, String, S0, S).
:- pred printn(Stream, int, string, State, State)
<= (stream.writer(Stream, string, State),
stream.writer(Stream, character, State)).
:- mode printn(in, in, in, di, uo) is det.
printn(Stream, N, String, !S) :-
( N > 0 ->
print(Stream, String, !S),
printn(Stream, N - 1, String, !S)
; true ).

View file

@ -0,0 +1,7 @@
x = StringBuilder.new
5.times do
x.append "ha"
end
puts x # ==> "hahahahaha"

View file

@ -0,0 +1,3 @@
/* NetRexx */
ha5 = 'ha'.copies(5)

View file

@ -0,0 +1,12 @@
/* NetRexx */
sampleStr = 'ha' -- string to duplicate
say ' COPIES:' sampleStr.copies(5)
say 'CHANGESTR:' '.....'.changestr('.', sampleStr)
sampleChr = '*' -- character to duplicate
say ' LEFT:' sampleChr.left(5, sampleChr)
say ' RIGHT:' sampleChr.right(5, sampleChr)
say ' CENTRE:' sampleChr.centre(5, sampleChr)
say ' OVERLAY:' sampleChr.overlay(sampleChr, 1, 5, sampleChr)
say ' SUBSTR:' ''.substr(1, 5, sampleChr)
say 'TRANSLATE:' '.....'.translate(sampleChr, '.')

View file

@ -0,0 +1 @@
(dup "ha" 5)

View file

@ -0,0 +1,8 @@
let string_repeat s n =
let len = String.length s in
let res = String.create(n * len) in
for i = 0 to pred n do
String.blit s 0 res (i * len) len;
done;
(res)
;;

View file

@ -0,0 +1,2 @@
# string_repeat "Hiuoa" 3 ;;
- : string = "HiuoaHiuoaHiuoa"

View file

@ -0,0 +1,3 @@
let string_repeat s n =
String.concat "" (Array.to_list (Array.make n s))
;;

View file

@ -0,0 +1,3 @@
let string_repeat s n =
Array.fold_left (^) "" (Array.make n s)
;;

View file

@ -0,0 +1 @@
String.make 5 '*'

View file

@ -0,0 +1,16 @@
bundle Default {
class Repeat {
function : Main(args : String[]) ~ Nil {
Repeat("ha", 5)->PrintLine();
}
function : Repeat(string : String, max : Int) ~ String {
repeat : String := String->New();
for(i := 0; i < max; i += 1;) {
repeat->Append(string);
};
return repeat;
}
}
}

View file

@ -0,0 +1,9 @@
@interface NSString (RosettaCodeAddition)
- (NSString *) repeatStringByNumberOfTimes: (NSUInteger) times;
@end
@implementation NSString (RosettaCodeAddition)
- (NSString *) repeatStringByNumberOfTimes: (NSUInteger) times {
return [@"" stringByPaddingToLength:[self length]*times withString:self startingAtIndex:0];
}
@end

View file

@ -0,0 +1,6 @@
// Instantiate an NSString by sending an NSString literal our new
// -repeatByNumberOfTimes: selector.
NSString *aString = [@"ha" repeatStringByNumberOfTimes:5];
// Display the NSString.
NSLog(@"%@", aString);

View file

@ -0,0 +1 @@
MESSAGE FILL( "ha", 5 ) VIEW-AS ALERT-BOX.

View file

@ -0,0 +1,18 @@
'REPEATING A CHARACTER
print string 10,"A" 'result AAAAAAAAAA
'REPEATING A STRING
function RepeatString(string s,sys n) as string
sys i, le=len s
if le=0 then exit function
n*=le
function=nuls n
'
for i=1 to n step le
mid function,i,s
next
end function
print RepeatString "ABC",3 'result ABCABCABC

View file

@ -0,0 +1,10 @@
declare
fun {Repeat Xs N}
if N > 0 then
{Append Xs {Repeat Xs N-1}}
else
nil
end
end
in
{System.showInfo {Repeat "Ha" 5}}

Some files were not shown because too many files have changed in this diff Show more