2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,3 +1,14 @@
Take a string and reverse it. For example, "asdf" becomes "fdsa".
;Task:
Take a string and reverse it.
For extra credit, preserve Unicode combining characters. For example, "as⃝df̅" becomes "f̅ds⃝a", not "̅fd⃝sa".
For example, "asdf" becomes "fdsa".
;Extra credit:
Preserve Unicode combining characters.
For example, "as⃝df̅" becomes "f̅ds⃝a", not "̅fd⃝sa".
{{Template:Strings}}
<br><br>

View file

@ -0,0 +1,30 @@
* Reverse a string 21/05/2016
REVERSE CSECT
USING REVERSE,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
STM R14,R12,12(R13) prolog
ST R13,4(R15) "
ST R15,8(R13) "
LR R13,R15 "
MVC TMP(L'C),C tmp=c
LA R8,C @c[1]
LA R9,TMP+L'C-1 @tmp[n-1]
LA R6,1 i=1
LA R7,L'C n=length(c)
LOOPI CR R6,R7 do i=1 to n
BH ELOOPI leave i
MVC 0(1,R8),0(R9) substr(c,i,1)=substr(tmp,n-i+1,1)
LA R8,1(R8) @c=@c+1
BCTR R9,0 @tmp=@tmp-1
LA R6,1(R6) i=i+1
B LOOPI next i
ELOOPI XPRNT C,L'C print c
L R13,4(0,R13) epilog
LM R14,R12,12(R13) "
XR R15,R15 "
BR R14 exit
C DC CL12'edoC attesoR'
TMP DS CL12
YREGS
END REVERSE

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

@ -1,5 +1,5 @@
reverseString("Hello World!")
on reverseString(str)
reverse of characters of str as string
reverse of characters of str as string
end reverseString

View file

@ -0,0 +1,82 @@
-- Using either a generic foldr(f, a, xs)
-- reverse1 :: [a] -> [a]
on reverse1(xs)
script rev
on lambda(a, x)
a & x
end lambda
end script
if class of xs is text then
foldr(rev, {}, xs) as text
else
foldr(rev, {}, xs)
end if
end reverse1
-- or the built-in reverse method for lists
-- reverse2 :: [a] -> [a]
on reverse2(xs)
if class of xs is text then
(reverse of characters of xs) as text
else
reverse of xs
end if
end reverse2
-- TESTING reverse1 and reverse2 with same string and list
on run
script test
on lambda(f)
map(f, ["Hello there !", {1, 2, 3, 4, 5}])
end lambda
end script
map(test, [reverse1, reverse2])
end run
-- GENERIC LIBRARY FUNCTIONS
-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
set v to lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldr
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn

View file

@ -0,0 +1,2 @@
{{"! ereht olleH", {5, 4, 3, 2, 1}},
{"! ereht olleH", {5, 4, 3, 2, 1}}}

View file

@ -0,0 +1,7 @@
reverse_string str = rev len str
where len = length str
rev 0 str = ""
rev n str = toString (str : nn) +> rev nn str
where nn = n - 1
reverse_string "Hello"

View file

@ -0,0 +1,2 @@
open string
fromList <| reverse <| toList "Hello" ::: String

View file

@ -0,0 +1,14 @@
#import system.
#import system'routines.
#import extensions.
#class(extension) extension
{
#method reversedLiteral
= self toArray reverse summarize:(String new) literal.
}
#symbol program =
[
console writeLine:("Hello World" reversedLiteral).
].

View file

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

View file

@ -0,0 +1,9 @@
: xreverse {: c-addr u -- c-addr2 u :}
u allocate throw u + c-addr swap over u + >r begin ( from to r:end)
over r@ u< while
over r@ over - x-size dup >r - 2dup r@ cmove
swap r> + swap repeat
r> drop nip u ;
\ example use
s" ώщыē" xreverse type \ outputs "ēыщώ"

View file

@ -0,0 +1,15 @@
def string = "as⃝df̅"
List combiningBlocks = [
Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS,
Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS_SUPPLEMENT,
Character.UnicodeBlock.COMBINING_HALF_MARKS,
Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS
]
List chars = string as List
chars[1..-1].eachWithIndex { ch, i ->
if (Character.UnicodeBlock.of((char)ch) in combiningBlocks) {
chars[i..(i+1)] = chars[(i+1)..i]
}
}
println chars.reverse().join()

View file

@ -0,0 +1,18 @@
(() => {
// .reduceRight() can be useful when reversals
// are composed with some other process
let reverse1 = s => Array.from(s)
.reduceRight((a, x) => a + (x !== ' ' ? x : ' <- '), ''),
// but ( join . reverse . split ) is faster for
// simple string reversals in isolation
reverse2 = s => s.split('').reverse().join('');
return [reverse1, reverse2]
.map(f => f("Some string to be reversed"));
})();

View file

@ -0,0 +1 @@
["desrever <- eb <- ot <- gnirts <- emoS", "desrever eb ot gnirts emoS"]

View file

@ -0,0 +1,3 @@
fun main(args: Array<String>) {
println("asdf".reversed())
}

View file

@ -0,0 +1,67 @@
# First, it gets the length of the original string
# Then, it allocates memory from the copy
# Then it copies the pointer to the original string, and adds the strlen
# subtract 1, then that new pointer is at the last char.
# while(strlen)
# copy char
# decrement strlen
# decrement source pointer
# increment target pointer
.data
ex_msg_og: .asciiz "Original string:\n"
ex_msg_cpy: .asciiz "\nCopied string:\n"
string: .asciiz "Wow, what a string!"
.text
main:
la $v1,string #load addr of string into $v0
la $t1,($v1) #copy addr into $t0 for later access
lb $a1,($v1) #load byte from string addr
strlen_loop:
beqz $a1,alloc_mem
addi $a0,$a0,1 #increment strlen_counter
addi $v1,$v1,1 #increment ptr
lb $a1,($v1) #load the byte
j strlen_loop
alloc_mem:
li $v0,9 #alloc memory, $a0 is arg for how many bytes to allocate
#result is stored in $v0
syscall
la $t0,($v0) #$v0 is static, $t0 is the moving ptr
la $v1,($t1) #get a copy we can increment
add $t1,$t1,$a0 #add strlen to our original, static addr to equal last char
subi $t1,$t1,1 #previous operation is on NULL byte, i.e. off-by-one error.
#this corrects.
copy_str:
lb $a1,($t1) #copy first byte from source
strcopy_loop:
beq $a0,0,exit_procedure
sb $a1,($t0) #store the byte at the target pointer
addi $t0,$t0,1 #increment target ptr
subi $t1,$t1,1
subi $a0,$a0,1
lb $a1,($t1) #load next byte from source ptr
j strcopy_loop
exit_procedure:
la $a1,($v0) #store our string at $v0 so it doesn't get overwritten
li $v0,4 #set syscall to PRINT
la $a0,ex_msg_og #PRINT("original string:")
syscall
la $a0,($v1) #PRINT(original string)
syscall
la $a0,ex_msg_cpy #PRINT("copied string:")
syscall
la $a0,($a1) #PRINT(strcopy)
syscall
li $v0,10 #EXIT(0)
syscall

View file

@ -0,0 +1,6 @@
let string_rev s =
let len = String.length s in
String.init len (fun i -> s.[len - 1 - i])
let () =
print_endline (string_rev "Hello world!")

View file

@ -0,0 +1,12 @@
\\ Return reversed string str.
\\ 3/3/2016 aev
sreverse(str)={return(Strchr(Vecrev(Vecsmall(str))))}
{
\\ TEST1
print(" *** Testing sreverse from Version #2:");
print(sreverse("ABCDEF"));
my(s,sr,n=10000000);
s="ABCDEFGHIJKL";
for(i=1,n, sr=sreverse(s));
}

View file

@ -0,0 +1,11 @@
\\ Version #1 upgraded to complete function. Practically the same.
reverse(str)={return(concat(Vecrev(str)))}
{
\\ TEST2
print(" *** Testing reverse from Version #1:");
print(reverse("ABCDEF"));
my(s,sr,n=10000000);
s="ABCDEFGHIJKL";
for(i=1,n, sr=reverse(s));
}

View file

@ -1,4 +1,2 @@
# Not transformative.
my $reverse = flip $string;
# or
say "hello world".flip;
say "asdf̅".flip

View file

@ -0,0 +1 @@
[Regex]::Matches('abc','.','RightToLeft').Value -join ''

View file

@ -1,36 +1 @@
'''
Reverse a Unicode string with proper handling of combining characters
'''
import unicodedata
def ureverse(ustring):
'''
Reverse a string including unicode combining characters
Example:
>>> ucode = ''.join( chr(int(n, 16))
for n in ['61', '73', '20dd', '64', '66', '305'] )
>>> ucoderev = ureverse(ucode)
>>> ['%x' % ord(char) for char in ucoderev]
['66', '305', '64', '73', '20dd', '61']
>>>
'''
groupedchars = []
uchar = list(ustring)
while uchar:
if 'COMBINING' in unicodedata.name(uchar[0], ''):
groupedchars[-1] += uchar.pop(0)
else:
groupedchars.append(uchar.pop(0))
# Grouped reversal
groupedchars = groupedchars[::-1]
return ''.join(groupedchars)
if __name__ == '__main__':
ucode = ''.join( chr(int(n, 16))
for n in ['61', '73', '20dd', '64', '66', '305'] )
ucoderev = ureverse(ucode)
print (ucode)
print (ucoderev)
''.join(reversed(string))

View file

@ -0,0 +1,36 @@
'''
Reverse a Unicode string with proper handling of combining characters
'''
import unicodedata
def ureverse(ustring):
'''
Reverse a string including unicode combining characters
Example:
>>> ucode = ''.join( chr(int(n, 16))
for n in ['61', '73', '20dd', '64', '66', '305'] )
>>> ucoderev = ureverse(ucode)
>>> ['%x' % ord(char) for char in ucoderev]
['66', '305', '64', '73', '20dd', '61']
>>>
'''
groupedchars = []
uchar = list(ustring)
while uchar:
if 'COMBINING' in unicodedata.name(uchar[0], ''):
groupedchars[-1] += uchar.pop(0)
else:
groupedchars.append(uchar.pop(0))
# Grouped reversal
groupedchars = groupedchars[::-1]
return ''.join(groupedchars)
if __name__ == '__main__':
ucode = ''.join( chr(int(n, 16))
for n in ['61', '73', '20dd', '64', '66', '305'] )
ucoderev = ureverse(ucode)
print (ucode)
print (ucoderev)

View file

@ -1,3 +1,3 @@
string2 = substr(string1,j,1) || string2
/*───── or ─────*/
string2=substr(string1,j,1)||string2
string2=substr(string1,j,1)string2

View file

@ -0,0 +1,10 @@
variable sa = "Hello, World", aa = Char_Type[strlen(sa)+1];
init_char_array(aa, sa);
array_reverse(aa);
% print(aa);
% Unfortunately, strjoin() only joins strings, so we map char()
% [sadly named: actually converts char into single-length string]
% onto the array:
print( strjoin(array_map(String_Type, &char, aa), "") );

View file

@ -0,0 +1,17 @@
define init_unicode_array(a, buf)
{
variable len = strbytelen(buf), ch, p0 = 0, p1 = 0;
while (p1 < len) {
(p1, ch) = strskipchar(buf, p1, 1);
if (ch < 0) print("oops.");
a[p0] = ch;
p0++;
}
}
variable su = "Σὲ γνωρίζω ἀπὸ τὴν κόψη";
variable au = Int_Type[strlen(su)+1];
init_unicode_array(au, su);
array_reverse(au);
% print(au);
print(strjoin(array_map(String_Type, &char, au), "") );

View file

@ -0,0 +1,12 @@
int main (string[] args) {
if (args.length < 2) {
stdout.printf ("Please, input a string.\n");
return 0;
}
var str = new StringBuilder ();
for (var i = 1; i < args.length; i++) {
str.append (args[i] + " ");
}
stdout.printf ("%s\n", str.str.strip ().reverse ());
return 0;
}