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,10 @@
}:r: Start reader loop.
!~>& Push a character to the "stack".
<:a:=- Stop reading on newline.
^:r:
@> Rotate the newline to the end and enqueue a sentinel 0.
{~ Dequeue and rotate the first character into place.
}:p:
${~ Print the current character until it's 0.
^:p:
#:r: Read again.

View file

@ -0,0 +1,3 @@
echo -e "foo\nbar" | 0815 rev.0
oof
rab

View file

@ -0,0 +1,3 @@
Take a string and reverse it. For example, "asdf" becomes "fdsa".
For extra credit, preserve Unicode combining characters. For example, "as⃝df̅" becomes "f̅ds⃝a", not "̅fd⃝sa".

View file

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

View file

@ -0,0 +1 @@
(reverse "hello")

View file

@ -0,0 +1,13 @@
PROC reverse = (REF STRING s)VOID:
FOR i TO UPB s OVER 2 DO
CHAR c = s[i];
s[i] := s[UPB s - i + 1];
s[UPB s - i + 1] := c
OD;
main:
(
STRING text := "Was it a cat I saw";
reverse(text);
print((text, new line))
)

View file

@ -0,0 +1,2 @@
⌽'asdf'
fdsa

View file

@ -0,0 +1,10 @@
function reverse(s)
{
p = ""
for(i=length(s); i > 0; i--) { p = p substr(s, i, 1) }
return p
}
BEGIN {
print reverse("edoCattesoR")
}

View file

@ -0,0 +1,9 @@
function reverse(s ,l)
{
l = length(s)
return l < 2 ? s:( substr(s,l,1) reverse(substr(s,1,l-1)) )
}
BEGIN {
print reverse("edoCattesoR")
}

View file

@ -0,0 +1,12 @@
function reverseString(string:String):String
{
var reversed:String = new String();
for(var i:int = string.length -1; i >= 0; i--)
reversed += string.charAt(i);
return reversed;
}
function reverseStringCQAlternative(string:String):String
{
return string.split('').reverse().join('');
}

View file

@ -0,0 +1,14 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Reverse_String is
function Reverse_It (Item : String) return String is
Result : String (Item'Range);
begin
for I in Item'range loop
Result (Result'Last - I + Item'First) := Item (I);
end loop;
return Result;
end Reverse_It;
begin
Put_Line (Reverse_It (Get_Line));
end Reverse_String;

View file

@ -0,0 +1,7 @@
module reverse_string where
open import Data.String
open import Data.List
reverse_string : String → String
reverse_string s = fromList (reverse (toList s))

View file

@ -0,0 +1,12 @@
get reverse_string("as⃝df̅")
on reverse_string(str)
set old_delim to (get AppleScript's text item delimiters)
set AppleScript's text item delimiters to ""
set temp to (reverse of text items of str)
set temp to (text items of temp) as Unicode text
set AppleScript's text item delimiters to old_delim
return temp
end reverse_string

View file

@ -0,0 +1,8 @@
MsgBox % reverse("asdf")
reverse(string)
{
Loop, Parse, string
reversed := A_LoopField . reversed
Return reversed
}

View file

@ -0,0 +1,19 @@
Reverse(String){ ; credit to Rseding91
If (A_IsUnicode){
SLen := StrLen(String) * 2
VarSetCapacity(RString,SLen)
Loop,Parse,String
NumPut(Asc(A_LoopField),RString,SLen-(A_Index * 2),"UShort")
} Else {
SLen := StrLen(String)
VarSetCapacity(RString,SLen)
Loop,Parse,String
NumPut(Asc(A_LoopField),RString,SLen-A_Index,"UChar")
}
VarSetCapacity(RString,-1)
Return RString
}

View file

@ -0,0 +1,12 @@
#AutoIt Version: 3.2.10.0
$mystring="asdf"
$reverse_string = ""
$string_length = StringLen($mystring)
For $i = 1 to $string_length
$last_n_chrs = StringRight($mystring, $i)
$nth_chr = StringTrimRight($last_n_chrs, $i-1)
$reverse_string= $reverse_string & $nth_chr
Next
MsgBox(0, "Reversed string is:", $reverse_string)

View file

@ -0,0 +1,7 @@
function reverse$(a$)
b$ = ""
for i = 1 to len(a$)
b$ = mid$(a$, i, 1) + b$
next i
reverse$ = b$
end function

View file

@ -0,0 +1,9 @@
PRINT FNreverse("The five boxing wizards jump quickly")
END
DEF FNreverse(A$)
LOCAL B$, C%
FOR C% = LEN(A$) TO 1 STEP -1
B$ += MID$(A$,C%,1)
NEXT
= B$

View file

@ -0,0 +1 @@
strrev: { str2ar ar2ls reverse ls2lf ar2str }

View file

@ -0,0 +1,17 @@
@echo off
setlocal enabledelayedexpansion
call :reverse %1 res
echo %res%
goto :eof
:reverse
set str=%~1
set cnt=0
:loop
if "%str%" equ "" (
goto :eof
)
set chr=!str:~0,1!
set str=%str:~1%
set %2=%chr%!%2!
goto loop

View file

@ -0,0 +1,14 @@
v The string to reverse. The row to copy to.
| | The actual copying happens here.
| | | Increment column to write to.
| | | | Store column #.
v v v v v
> "reverse me" 3 10p >10g 4 p 10g1+ 10pv
^ ^ |: <
First column --| | @ ^
to write to. | ^ Get the address
All calls to 10 | to copy the next
involve saving or | character to.
reading the End when stack is empty or
column to write explicit zero is reached.
to.

View file

@ -0,0 +1,15 @@
( reverse
= L x
. :?L
& @( !arg
: ?
( %?x
& utf$!x
& !x !L:?L
& ~`
)
?
)
| str$!L
)
& out$reverse$Ελληνικά

View file

@ -0,0 +1 @@
[-]>,+[->,+]<[.<]

View file

@ -0,0 +1,5 @@
,----- ----- [+++++ +++++ > , ----- -----] If a newline is hit counter will be zero and input loop ends
<[.<] run all chars backwards and print them
just because it looks good we print CRLF
+++++ +++++ +++ . --- .

View file

@ -0,0 +1 @@
p "olleh".reverse #Prints "hello"

View file

@ -0,0 +1 @@
"Hello, world!"<-

View file

@ -0,0 +1,12 @@
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string s;
std::getline(std::cin, s);
std::reverse(s.begin(), s.end()); // modifies s
std::cout << s << std::endl;
return 0;
}

View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
const char *sa = "abcdef";
const char *su = "as⃝df̅"; /* Should be in your native locale encoding. Mine is UTF-8 */
int is_comb(wchar_t c)
{
if (c >= 0x300 && c <= 0x36f) return 1;
if (c >= 0x1dc0 && c <= 0x1dff) return 1;
if (c >= 0x20d0 && c <= 0x20ff) return 1;
if (c >= 0xfe20 && c <= 0xfe2f) return 1;
return 0;
}
wchar_t* mb_to_wchar(const char *s)
{
wchar_t *u;
size_t len = mbstowcs(0, s, 0) + 1;
if (!len) return 0;
u = malloc(sizeof(wchar_t) * len);
mbstowcs(u, s, len);
return u;
}
wchar_t* ws_reverse(const wchar_t* u)
{
size_t len, i, j;
wchar_t *out;
for (len = 0; u[len]; len++);
out = malloc(sizeof(wchar_t) * (len + 1));
out[len] = 0;
j = 0;
while (len) {
for (i = len - 1; i && is_comb(u[i]); i--);
wcsncpy(out + j, u + i, len - i);
j += len - i;
len = i;
}
return out;
}
char *mb_reverse(const char *in)
{
size_t len;
char *out;
wchar_t *u = mb_to_wchar(in);
wchar_t *r = ws_reverse(u);
len = wcstombs(0, r, 0) + 1;
out = malloc(len);
wcstombs(out, r, len);
free(u);
free(r);
return out;
}
int main(void)
{
setlocale(LC_CTYPE, "");
printf("%s => %s\n", sa, mb_reverse(sa));
printf("%s => %s\n", su, mb_reverse(su));
return 0;
}

View file

@ -0,0 +1,13 @@
#include <glib.h>
gchar *srev (const gchar *s) {
if (g_utf8_validate(s,-1,NULL)) {
return g_utf8_strreverse (s,-1);
} }
// main
int main (void) {
const gchar *t="asdf";
const gchar *u="as⃝df̅";
printf ("%s\n",srev(t));
printf ("%s\n",srev(u));
return 0;
}

View file

@ -0,0 +1 @@
FUNCTION REVERSE-STRING('QWERTY')

View file

@ -0,0 +1 @@
(defn str-reverse [s] (apply str (reverse s)))

View file

@ -0,0 +1 @@
(apply str (interpose " " (reverse (.split "the quick brown fox" " "))))

View file

@ -0,0 +1,21 @@
(defn combining? [c]
(let [type (Character/getType c)]
;; currently hardcoded to the types taken from the sample string
(or (= type 6) (= type 7))))
(defn group
"Group normal characters with their combining characters"
[chars]
(cond (empty? chars) chars
(empty? (next chars)) (list chars)
:else
(let [dres (group (next chars))]
(cond (combining? (second chars)) (cons (cons (first chars)
(first dres))
(rest dres))
:else (cons (list (first chars)) dres)))))
(defn str-reverse
"Unicode-safe string reverse"
[s]
(apply str (apply concat (reverse (group s)))))

View file

@ -0,0 +1 @@
"qwerty".split("").reverse().join ""

View file

@ -0,0 +1,2 @@
<cfset myString = "asdf" />
<cfset myString = reverse( myString ) />

View file

@ -0,0 +1 @@
(reverse my-string)

View file

@ -0,0 +1,15 @@
import std.stdio, std.range, std.conv;
void main() {
string s1 = "hello"; // UTF-8
string s1r = text(retro("hello"));
assert(s1r == "olleh");
wstring s2 = "hello"w; // UTF-16
wstring s2r = wtext(retro("hello"));
assert(s2r == "olleh");
dstring s3 = "hello"d; // UTF-32
dstring s3r = dtext(retro("hello"));
assert(s3r == "olleh");
}

View file

@ -0,0 +1,11 @@
String reverse(String s) {
StringBuffer sb=new StringBuffer();
for(int i=s.length-1;i>=0;i--) {
sb.add(s[i]);
}
return sb.toString();
}
main() {
print(reverse('a string.'));
}

View file

@ -0,0 +1,7 @@
function ReverseString(const InString: string): string;
var
i: integer;
begin
for i := Length(InString) downto 1 do
Result := Result + InString[i];
end;

View file

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

View file

@ -0,0 +1,4 @@
pragma.enable("accumulator")
def reverse(string) {
return accum "" for i in (0..!(string.size())).descending() { _ + string[i] }
}

View file

@ -0,0 +1,7 @@
function reverse( str string ) returns( string )
result string;
for ( i int from StrLib.characterLen( str ) to 1 decrement by 1 )
result ::= str[i:i];
end
return( result );
end

View file

@ -0,0 +1,15 @@
class
APPLICATION
create
make
feature
make
-- Demonstrate string reversal.
do
my_string := "Hello World!"
my_string.mirror
print (my_string)
end
my_string: STRING
-- Used for reversal
end

View file

@ -0,0 +1 @@
(concat (reverse (append "Hello World" nil)))

View file

@ -0,0 +1,2 @@
1> lists:reverse("reverse!").
"!esrever"

View file

@ -0,0 +1,4 @@
reverse(Bin) ->
Size = size(Bin)*8,
<<T:Size/integer-little>> = Bin,
<<T:Size/integer-big>>.

View file

@ -0,0 +1,3 @@
>function strrev (s) := chartostr(fliplr(strtochar(s)))
>strrev("This is a test!")
!tset a si sihT

View file

@ -0,0 +1,2 @@
include std/sequence.e
printf(1, "%s\n", {reverse("abcdef") })

View file

@ -0,0 +1,8 @@
Function StrRev1(ByVal $p1)
dim $b = ""
repeat len(p1)
b = b & right(p1,1)
p1 = left(p1,len(p1)-1)
end repeat
return b
End Function

View file

@ -0,0 +1,8 @@
Function StrRev2(ByVal $p1)
dim $b = ""
dim %i
for i = len(p1) downto 1
b = b & mid(p1,i,1)
next
return b
End Function

View file

@ -0,0 +1,7 @@
Function StrRev3( $s )
FOR DIM x = 1 TO LEN(s) \ 2
PEEK(@s + LEN - x, $1)
POKE(@s + LEN - x, s{x})(@s + x - 1, PEEK)
NEXT
RETURN s
end function

View file

@ -0,0 +1,20 @@
DynC StringRev($theString) As String
void rev(char *str)
{
int len = strlen(str);
char *HEAD = str;
char *TAIL = str + len - 1;
char temp;
int i;
for ( i = 0; i <= len / 2; i++, HEAD++, TAIL--) {
temp = *HEAD;
*HEAD = *TAIL;
*TAIL = temp;
}
}
char *main(char* theString)
{
rev(theString);
return theString;
}
End DynC

View file

@ -0,0 +1 @@
"hello" reverse

View file

@ -0,0 +1 @@
"as⃝df̅" string-reverse "f̅ds⃝a" = .

View file

@ -0,0 +1 @@
"hello world!" reverse

View file

@ -0,0 +1,9 @@
: exchange ( a1 a2 -- )
2dup c@ swap c@ rot c! swap c! ;
: reverse ( c-addr u -- )
1- bounds begin 2dup > while
2dup exchange
-1 /string
repeat 2drop ;
s" testing" 2dup reverse type \ gnitset

View file

@ -0,0 +1,16 @@
PROGRAM Example
CHARACTER(80) :: str = "This is a string"
CHARACTER :: temp
INTEGER :: i, length
WRITE (*,*) str
length = LEN_TRIM(str) ! Ignores trailing blanks. Use LEN(str) to reverse those as well
DO i = 1, length/2
temp = str(i:i)
str(i:i) = str(length+1-i:length+1-i)
str(length+1-i:length+1-i) = temp
END DO
WRITE(*,*) str
END PROGRAM Example

View file

@ -0,0 +1,25 @@
program reverse_string
implicit none
character (*), parameter :: string = 'no devil lived on'
write (*, '(a)') string
write (*, '(a)') reverse (string)
contains
recursive function reverse (string) result (res)
implicit none
character (*), intent (in) :: string
character (len (string)) :: res
if (len (string) == 0) then
res = ''
else
res = string (len (string) :) // reverse (string (: len (string) - 1))
end if
end function reverse
end program reverse_string

View file

@ -0,0 +1 @@
println[reverse["abcdef"]]

View file

@ -0,0 +1,2 @@
Reversed("abcdef");
# "fedcba"

View file

@ -0,0 +1 @@
\L<U>=@reverse{$1}

View file

@ -0,0 +1 @@
\L<U1><U>=@{$2}$1

View file

@ -0,0 +1,76 @@
package main
import (
"fmt"
"unicode"
"unicode/utf8"
)
// no encoding
func reverseBytes(s string) string {
r := make([]byte, len(s))
for i := 0; i < len(s); i++ {
r[i] = s[len(s)-1-i]
}
return string(r)
}
// reverseCodePoints interprets its argument as UTF-8 and ignores bytes
// that do not form valid UTF-8. return value is UTF-8.
func reverseCodePoints(s string) string {
r := make([]rune, len(s))
start := len(s)
for _, c := range s {
// quietly skip invalid UTF-8
if c != utf8.RuneError {
start--
r[start] = c
}
}
return string(r[start:])
}
// reversePreservingCombiningCharacters interprets its argument as UTF-8
// and ignores bytes that do not form valid UTF-8. return value is UTF-8.
func reversePreservingCombiningCharacters(s string) string {
if s == "" {
return ""
}
p := []rune(s)
r := make([]rune, len(p))
start := len(r)
for i := 0; i < len(p); {
// quietly skip invalid UTF-8
if p[i] == utf8.RuneError {
i++
continue
}
j := i + 1
for j < len(p) && (unicode.Is(unicode.Mn, p[j]) ||
unicode.Is(unicode.Me, p[j]) || unicode.Is(unicode.Mc, p[j])) {
j++
}
for k := j - 1; k >= i; k-- {
start--
r[start] = p[k]
}
i = j
}
return (string(r[start:]))
}
func main() {
test("asdf")
test("as⃝df̅")
}
func test(s string) {
fmt.Println("\noriginal: ", []byte(s), s)
r := reverseBytes(s)
fmt.Println("reversed bytes:", []byte(r), r)
fmt.Println("original code points:", []rune(s), s)
r = reverseCodePoints(s)
fmt.Println("reversed code points:", []rune(r), r)
r = reversePreservingCombiningCharacters(s)
fmt.Println("combining characters:", []rune(r), r)
}

View file

@ -0,0 +1 @@
println "Able was I, 'ere I saw Elba.".reverse()

View file

@ -0,0 +1 @@
reverse = foldl (flip (:)) []

View file

@ -0,0 +1,3 @@
import Data.Char (isMark)
import Data.List (groupBy)
myReverse = concat . reverse . groupBy (const isMark)

View file

@ -0,0 +1,10 @@
CHARACTER string = "Hello World", tmp
L = LEN( string )
DO i = 1, L/2
tmp = string(i)
string(i) = string(L-i+1)
string(L-i+1) = tmp
ENDDO
WRITE(Messagebox, Name) string

View file

@ -0,0 +1,4 @@
procedure main(arglist)
s := \arglist[1] | "asdf"
write(s," <-> ", reverse(s)) # reverse is built-in
end

View file

@ -0,0 +1 @@
"asdf" reverse

View file

@ -0,0 +1,2 @@
|.'asdf'
fdsa

View file

@ -0,0 +1,2 @@
ranges=.16b02ff 16b036f, 16b1dbf 16b1dff, 16b20cf 16b20ff, 16bfe1f 16bfe2f
iscombining=. 2 | ranges&I.

View file

@ -0,0 +1 @@
split=. (<;.1~ -.@iscombining) :. ;

View file

@ -0,0 +1,2 @@
|.&.split&.(3 u: 7&u:) 'as⃝df̅'
f̅ds⃝a

View file

@ -0,0 +1,3 @@
public static String reverseString(String s) {
return new StringBuffer(s).reverse().toString();
}

View file

@ -0,0 +1,3 @@
public static String reverseString(String s) {
return new StringBuilder(s).reverse().toString();
}

View file

@ -0,0 +1,3 @@
var a = "cat".split("");
a.reverse();
print(a.join("")); // tac

View file

@ -0,0 +1,2 @@
julia> reverse("hey")
"yeh"

View file

@ -0,0 +1,2 @@
: flip "" split reverse "" join ;
"qwer asdf" flip .

View file

@ -0,0 +1,11 @@
input$ ="abcdefgABCDEFG012345"
print input$
print ReversedStr$( input$)
end
function ReversedStr$(in$)
for i =len(in$) to 1 step -1
ReversedStr$ =ReversedStr$ +mid$( in$, i, 1)
next i
end function

View file

@ -0,0 +1 @@
print reverse "cat ; tac

View file

@ -0,0 +1 @@
theString = theString:reverse()

View file

@ -0,0 +1 @@
define(`invert',`ifelse(len(`$1'),0,,`invert(substr(`$1',1))'`'substr(`$1',0,1))')

View file

@ -0,0 +1,6 @@
>> fliplr(['She told me that she spoke English and I said great. '...
'Grabbed her hand out the club and I said let''s skate.'])
ans =
.etaks s'tel dias I dna bulc eht tuo dnah reh debbarG .taerg dias I dna hsilgnE ekops ehs taht em dlot ehS

View file

@ -0,0 +1,6 @@
fn reverseString s =
(
local reversed = ""
for i in s.count to 1 by -1 do reversed += s[i]
reversed
)

View file

@ -0,0 +1,6 @@
REVERSE
;Take in a string and reverse it using the built in function $REVERSE
NEW S
READ:30 "Enter a string: ",S
WRITE !,$REVERSE(S)
QUIT

View file

@ -0,0 +1,2 @@
> StringTools:-Reverse( "foo" );
"oof"

View file

@ -0,0 +1 @@
StringReverse["asdf"]

View file

@ -0,0 +1,3 @@
sreverse("abcdef"); /* "fedcba" */
sreverse("rats live on no evil star"); /* not a bug :o) */

View file

@ -0,0 +1,5 @@
def reverse(s:string)
StringBuilder.new(s).reverse
end
puts reverse('reversed')

View file

@ -0,0 +1,16 @@
MODULE Reverse EXPORTS Main;
IMPORT IO, Text;
PROCEDURE String(item: TEXT): TEXT =
VAR result: TEXT := "";
BEGIN
FOR i := Text.Length(item) - 1 TO 0 BY - 1 DO
result := Text.Cat(result, Text.FromChar(Text.GetChar(item, i)));
END;
RETURN result;
END String;
BEGIN
IO.Put(String("Foobarbaz") & "\n");
END Reverse.

View file

@ -0,0 +1,23 @@
using System;
using System.Globalization;
using System.Windows.Forms;
using System.Console;
using Nemerle.Utility.NString;
module StrReverse
{
UReverse(text : string) : string
{
mutable output = [];
def elements = StringInfo.GetTextElementEnumerator(text);
while (elements.MoveNext())
output ::= elements.GetTextElement().ToString();
Concat("", output.Reverse());
}
Main() : void
{
def test = "as⃝df̅";
MessageBox.Show($"$test --> $(UReverse(test))"); //for whatever reason my console didn't display Unicode properly, but a MessageBox worked
}
}

View file

@ -0,0 +1,7 @@
Reverse(text : string) : string
{
mutable output = [];
foreach (c in text.ToCharArray())
output ::= c.ToString();
Concat("", output)
}

View file

@ -0,0 +1,11 @@
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
reverseThis = 'asdf'
sihTesrever = reverseThis.reverse
say reverseThis
say sihTesrever
return

View file

@ -0,0 +1 @@
(reverse "!dlroW olleH")

View file

@ -0,0 +1,2 @@
reverse 'asdf'
=fdsa

View file

@ -0,0 +1,9 @@
var
str1 = "Reverse This!"
proc reverse(s: string): string =
result = ""
for i in countdown(high(str1), 0):
result.add str1[i]
echo "Original string: ", str1, "\nReversed: ", reverse(str1)

View file

@ -0,0 +1,9 @@
let rev_string str =
let len = String.length str in
let res = String.create len in
let last = len - 1 in
for i = 0 to last do
let j = last - i in
res.[i] <- str.[j];
done;
(res)

View file

@ -0,0 +1,8 @@
let rev_string str =
let last = String.length str - 1 in
for i = 0 to last / 2 do
let j = last - i in
let c = str.[i] in
str.[i] <- str.[j];
str.[j] <- c;
done

View file

@ -0,0 +1,4 @@
let rec revs strin list index =
if List.length list = String.length strin
then String.concat "" list
else revs strin ((String.sub strin index 1)::list) (index+1)

View file

@ -0,0 +1 @@
result := "asdf"->Reverse();

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