Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
38
Task/Reverse-a-string/ATS/reverse-a-string.ats
Normal file
38
Task/Reverse-a-string/ATS/reverse-a-string.ats
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
//
|
||||
// How to compile:
|
||||
// patscc -DATS_MEMALLOC_LIBC -o string_reverse string_reverse.dats
|
||||
//
|
||||
|
||||
#include
|
||||
"share/atspre_staload.hats"
|
||||
|
||||
fun
|
||||
string_reverse
|
||||
(
|
||||
x: string
|
||||
) : Strptr1 = let
|
||||
//
|
||||
val [n:int] x = g1ofg0(x)
|
||||
val y = string1_copy(x)
|
||||
val n = string1_length(x)
|
||||
val (pf, fpf | p) =
|
||||
$UNSAFE.ptr_vtake{array(char,n)}(ptrcast(y))
|
||||
val () = array_subreverse(!p, i2sz(0), n)
|
||||
prval () = fpf(pf)
|
||||
//
|
||||
in
|
||||
$UNSAFE.castvwtp0{Strptr1}(y)
|
||||
end (* end of [string_reverse] *)
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
main0 () = let
|
||||
//
|
||||
val rev = string_reverse("asdf")
|
||||
val ((*void*)) = println! ("reverse(\"asdf\") = \"", rev, "\"")
|
||||
val ((*freed*)) = strptr_free (rev)
|
||||
//
|
||||
in
|
||||
// nothing
|
||||
end // end of [main0]
|
||||
11
Task/Reverse-a-string/AWK/reverse-a-string-3.awk
Normal file
11
Task/Reverse-a-string/AWK/reverse-a-string-3.awk
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Usage: awk -f reverse.awk -v s=Rosetta
|
||||
|
||||
function rev(s, i,a,r) {
|
||||
split(s, a, "")
|
||||
for (i in a) r = a[i] r
|
||||
return r
|
||||
}
|
||||
BEGIN {
|
||||
if(!s) s = "Hello, world!"
|
||||
print s, "<-->", rev(s)
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ reverse(text s)
|
|||
i = length(s);
|
||||
while (i) {
|
||||
i -= 1;
|
||||
b_insert(b, -1, character(s, i));
|
||||
b_insert(b, -1, s[i]);
|
||||
}
|
||||
|
||||
return b_string(b);
|
||||
|
|
|
|||
1
Task/Reverse-a-string/Dart/reverse-a-string-1.dart
Normal file
1
Task/Reverse-a-string/Dart/reverse-a-string-1.dart
Normal file
|
|
@ -0,0 +1 @@
|
|||
String reverse(String s) => new String.fromCharCodes(s.runes.toList().reversed);
|
||||
17
Task/Reverse-a-string/Dart/reverse-a-string-2.dart
Normal file
17
Task/Reverse-a-string/Dart/reverse-a-string-2.dart
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import 'package:unittest/unittest.dart';
|
||||
|
||||
String reverse(String s) => new String.fromCharCodes(s.runes.toList().reversed);
|
||||
|
||||
main() {
|
||||
group("Reverse a string -", () {
|
||||
test("Strings with ASCII characters are reversed correctly.", () {
|
||||
expect(reverse("hello, world"), equals("dlrow ,olleh"));
|
||||
});
|
||||
test("Strings with non-ASCII BMP characters are reversed correctly.", () {
|
||||
expect(reverse("\u4F60\u4EEC\u597D"), equals("\u597D\u4EEC\u4F60"));
|
||||
});
|
||||
test("Strings with non-BMP characters are reversed correctly.", () {
|
||||
expect(reverse("hello, \u{1F310}"), equals("\u{1F310} ,olleh"));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
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.'));
|
||||
}
|
||||
2
Task/Reverse-a-string/Elixir/reverse-a-string.elixir
Normal file
2
Task/Reverse-a-string/Elixir/reverse-a-string.elixir
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
IO.puts (String.reverse "asdf")
|
||||
IO.puts (String.reverse "as⃝df̅")
|
||||
|
|
@ -1,3 +1,18 @@
|
|||
var a = "cat".split("");
|
||||
a.reverse();
|
||||
print(a.join("")); // tac
|
||||
//using chained methods
|
||||
function reverseStr(s) {
|
||||
return s.split('').reverse().join('');
|
||||
}
|
||||
|
||||
//fast method using for loop
|
||||
function reverseStr(s) {
|
||||
for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
|
||||
return o;
|
||||
}
|
||||
|
||||
//fast method using while loop (faster with long strings in some browsers when compared with for loop)
|
||||
|
||||
function reverseStr(s) {
|
||||
var i = s.length, o = '';
|
||||
while (i--) o += s[i];
|
||||
return o;
|
||||
}
|
||||
|
|
|
|||
2
Task/Reverse-a-string/Julia/reverse-a-string-2.julia
Normal file
2
Task/Reverse-a-string/Julia/reverse-a-string-2.julia
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
julia> join(reverse(collect(graphemes("nöel"))))
|
||||
"leön"
|
||||
3
Task/Reverse-a-string/OOC/reverse-a-string.ooc
Normal file
3
Task/Reverse-a-string/OOC/reverse-a-string.ooc
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
main: func {
|
||||
"asdf" reverse() println() // prints "fdsa"
|
||||
}
|
||||
16
Task/Reverse-a-string/Pascal/reverse-a-string-4.pascal
Normal file
16
Task/Reverse-a-string/Pascal/reverse-a-string-4.pascal
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
procedure revString(var s:string);
|
||||
var
|
||||
i,j:integer;
|
||||
tmp:char;
|
||||
begin
|
||||
i := 1;
|
||||
j := length(s);
|
||||
while i<j do
|
||||
begin
|
||||
tmp:=s[i];
|
||||
s[i]:=s[j];
|
||||
s[j]:=tmp;
|
||||
inc(i);
|
||||
dec(j)
|
||||
end;
|
||||
end;
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
output :
|
||||
L = [100,99,98,97],
|
||||
S = "dcba".
|
||||
accRev([H|T], A, R) :- accRev(T, [H|A], R).
|
||||
accRev([], A, A).
|
||||
|
||||
rev(L,R) :- accRev(L,[],R).
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
"asdf".foldRight(""){ (a,b)=> b+a }
|
||||
"asdf".foldRight("")((a,b) => b+a)
|
||||
|
|
|
|||
|
|
@ -1,30 +1,11 @@
|
|||
def reverseString(s: String) = {
|
||||
import java.lang.Character._
|
||||
|
||||
val combiningTypes = List(NON_SPACING_MARK, ENCLOSING_MARK, COMBINING_SPACING_MARK)
|
||||
def isCombiningCharacter(c: Char) = combiningTypes contains c.getType
|
||||
def isCombiningSurrogate(high: Char, low: Char) = combiningTypes contains getType(toCodePoint(high, low))
|
||||
def isCombining(l: List[Char]) = l match {
|
||||
case List(a, b) => isCombiningSurrogate(a, b)
|
||||
case List(a) => isCombiningCharacter(a)
|
||||
case Nil => true
|
||||
case _ => throw new IllegalArgumentException("isCombining expects a list of up to two characters")
|
||||
def reverse(s: String) = {
|
||||
import java.text.{Normalizer,BreakIterator}
|
||||
val norm = Normalizer.normalize(s, Normalizer.Form.NFKC) // waffle -> waffle (optional)
|
||||
val it = BreakIterator.getCharacterInstance
|
||||
it setText norm
|
||||
def break(it: BreakIterator, prev: Int, result: List[String] = Nil): List[String] = it.next match {
|
||||
case BreakIterator.DONE => result
|
||||
case cur => break(it, cur, norm.substring(prev, cur) :: result)
|
||||
}
|
||||
|
||||
def cleanSurrogate(l: List[Char]) = l match {
|
||||
case List(a, b) if a.isHighSurrogate && b.isLowSurrogate => l
|
||||
case List(a, b) if a.isLowSurrogate => Nil
|
||||
case List(a, b) => List(a)
|
||||
case _ => throw new IllegalArgumentException("cleanSurrogate expects lists of two characters, exactly")
|
||||
}
|
||||
|
||||
def splitString(string: String) = (string+" ").iterator sliding 2 map (_.toList) map cleanSurrogate toList
|
||||
|
||||
def recurse(fwd: List[List[Char]], rev: List[Char]): String = fwd match {
|
||||
case Nil => rev.mkString
|
||||
case c :: rest =>
|
||||
val (combining, remaining) = rest span isCombining
|
||||
recurse(remaining, c ::: combining.foldLeft(List[Char]())(_ ::: _) ::: rev)
|
||||
}
|
||||
recurse(splitString(s), Nil)
|
||||
break(it, it.first).mkString
|
||||
}
|
||||
|
|
|
|||
30
Task/Reverse-a-string/Scala/reverse-a-string-4.scala
Normal file
30
Task/Reverse-a-string/Scala/reverse-a-string-4.scala
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
def reverseString(s: String) = {
|
||||
import java.lang.Character._
|
||||
|
||||
val combiningTypes = List(NON_SPACING_MARK, ENCLOSING_MARK, COMBINING_SPACING_MARK)
|
||||
def isCombiningCharacter(c: Char) = combiningTypes contains c.getType
|
||||
def isCombiningSurrogate(high: Char, low: Char) = combiningTypes contains getType(toCodePoint(high, low))
|
||||
def isCombining(l: List[Char]) = l match {
|
||||
case List(a, b) => isCombiningSurrogate(a, b)
|
||||
case List(a) => isCombiningCharacter(a)
|
||||
case Nil => true
|
||||
case _ => throw new IllegalArgumentException("isCombining expects a list of up to two characters")
|
||||
}
|
||||
|
||||
def cleanSurrogate(l: List[Char]) = l match {
|
||||
case List(a, b) if a.isHighSurrogate && b.isLowSurrogate => l
|
||||
case List(a, b) if a.isLowSurrogate => Nil
|
||||
case List(a, b) => List(a)
|
||||
case _ => throw new IllegalArgumentException("cleanSurrogate expects lists of two characters, exactly")
|
||||
}
|
||||
|
||||
def splitString(string: String) = (string+" ").iterator sliding 2 map (_.toList) map cleanSurrogate toList
|
||||
|
||||
def recurse(fwd: List[List[Char]], rev: List[Char]): String = fwd match {
|
||||
case Nil => rev.mkString
|
||||
case c :: rest =>
|
||||
val (combining, remaining) = rest span isCombining
|
||||
recurse(remaining, c ::: combining.foldLeft(List[Char]())(_ ::: _) ::: rev)
|
||||
}
|
||||
recurse(splitString(s), Nil)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue