Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,6 @@
{{basic data operation}}In this task display a substring:
{{basic data operation}}
[[Category:String manipulation]] [[Category:Simple]]
In this task display a substring:
* starting from <tt>n</tt> characters in and of <tt>m</tt> length;
* starting from <tt>n</tt> characters in, up to the end of the string;
@ -6,4 +8,6 @@
* starting from a known character within the string and of <tt>m</tt> length;
* starting from a known substring within the string and of <tt>m</tt> length.
If the program uses UTF-8 or UTF-16, it must work on any valid Unicode code point, whether in the Basic Multilingual Plane or above it. The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16. Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.
If the program uses UTF-8 or UTF-16, it must work on any valid Unicode code point,
whether in the Basic Multilingual Plane or above it.
The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16. Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.

View file

@ -1,4 +1,2 @@
---
category:
- String manipulation
note: Basic language learning

View file

@ -0,0 +1,26 @@
package main
import (
"fmt"
"strings"
)
func main() {
s := "ABCDEFGH"
n, m := 2, 3
// for reference
fmt.Println("Index: ", "01234567")
fmt.Println("String:", s)
// starting from n characters in and of m length
fmt.Printf("Start %d, length %d: %s\n", n, m, s[n : n+m])
// starting from n characters in, up to the end of the string
fmt.Printf("Start %d, to end: %s\n", n, s[n:])
// whole string minus last character
fmt.Printf("All but last: %s\n", s[:len(s)-1])
// starting from a known character within the string and of m length
dx := strings.IndexByte(s, 'D')
fmt.Printf("Start 'D', length %d: %s\n", m, s[dx : dx+m])
// starting from a known substring within the string and of m length
sx := strings.Index(s, "DE")
fmt.Printf(`Start "DE", length %d: %s`+"\n", m, s[sx : sx+m])
}

View file

@ -0,0 +1,29 @@
package main
import (
"fmt"
"strings"
)
func main() {
s := "αβγδεζηθ"
r := []rune(s)
n, m := 2, 3
kc := 'δ' // known character
ks := "δε" // known string
// for reference
fmt.Println("Index: ", "01234567")
fmt.Println("String:", s)
// starting from n characters in and of m length
fmt.Printf("Start %d, length %d: %s\n", n, m, string(r[n:n+m]))
// starting from n characters in, up to the end of the string
fmt.Printf("Start %d, to end: %s\n", n, string(r[n:]))
// whole string minus last character
fmt.Printf("All but last: %s\n", string(r[:len(r)-1]))
// starting from a known character within the string and of m length
dx := strings.IndexRune(s, kc)
fmt.Printf("Start %q, length %d: %s\n", kc, m, string([]rune(s[dx:])[:m]))
// starting from a known substring within the string and of m length
sx := strings.Index(s, ks)
fmt.Printf("Start %q, length %d: %s\n", ks, m, string([]rune(s[sx:])[:m]))
}

View file

@ -1,14 +0,0 @@
package main
import "fmt"
import "strings"
func main() {
s := "ABCDEFGH"
n, m := 2, 3
fmt.Println(s[n:n+m]) // "CDE"
fmt.Println(s[n:]) // "CDEFGH"
fmt.Println(s[0:len(s)-1]) // "ABCDEFG"
fmt.Println(s[strings.Index(s, "D"):strings.Index(s, "D")+m]) // "DEF"
fmt.Println(s[strings.Index(s, "DE"):strings.Index(s, "DE")+m]) // "DEF"
}

View file

@ -0,0 +1,4 @@
> n, m := 3, 5:
> s := "The Higher, The Fewer!":
> s[ n .. n + m - 1 ];
"e Hig"

View file

@ -0,0 +1,5 @@
> s[ n .. -1 ] = s[ n .. ];
"e Higher, The Fewer!" = "e Higher, The Fewer!"
> StringTools:-Drop( s, n - 1 );
"e Higher, The Fewer!"

View file

@ -0,0 +1,5 @@
> s[ 1 .. -2 ] = s[ .. -2 ];
"The Higher, The Fewer" = "The Higher, The Fewer"
> StringTools:-Chop( s );
"The Higher, The Fewer"

View file

@ -0,0 +1,7 @@
> pos := searchtext( ",", s ):
> s[ pos .. pos + m - 1 ];
", The"
> pos := searchtext( "Higher", s ):
> s[ pos .. pos + m - 1 ];
"Highe"

View file

@ -0,0 +1,17 @@
substring_task(Str, N, M, Char, SubStr) :-
sub_string(Str, N, M, _, Span),
sub_string(Str, N, _, 0, ToEnd),
sub_string(Str, 0, _, 1, MinusLast),
string_from_substring_to_m(Str, Char, M, FromCharToMth),
string_from_substring_to_m(Str, SubStr, M, FromSubToM),
maplist( writeln,
[ 'from n to m ':Span,
'from n to end ': ToEnd,
'string minus last char ': MinusLast,
'form known char to m ': FromCharToMth,
'from known substring to m ': FromSubToM ]).
string_from_substring_to_m(String, Sub, M, FromSubToM) :-
sub_string(String, Before, _, _, Sub),
sub_string(String, Before, M, _, FromSubToM).

View file

@ -0,0 +1,7 @@
?- substring_task("abcdefghijk", 2, 4, "d", "ef").
from n to m :cdef
from n to end :cdefghijk
string minus last char :abcdefghij
form known char to m :defg
from known substring to m :efgh
true

View file

@ -0,0 +1,25 @@
* 1...5....1....5....2....5..
D myString S 30 inz('Liebe bewegt das Universum!')
D output S 30 inz('')
D n S 2 0 inz(1)
D m S 2 0 inz(5)
D length S 2 0 inz(0)
D find S 2 0 inz(0)
/free
*inlr = *on;
dsply %subst(myString:n:m);
dsply %subst(myString:7:20);
length = %len(%trim(myString));
dsply %subst(myString:1:length-1);
find = %scan('U':myString);
dsply %subst(myString:find:9);
find = %scan('bewegt':myString);
dsply %subst(myString:find:%len('bewegt'));
output = ' *** end *** ';
dsply ' ' ' ' output;
/end-free

View file

@ -6,14 +6,15 @@ object Substring {
// Starting from n characters in and of m length
assert("inspired by love" == str.slice(n, n + m))
// Starting from n characters in, up to the end of the string
assert("inspired by love and guided by knowledge." == str.drop(n))
// Whole string minus last character
assert("The good life is one inspired by love and guided by knowledge" == str.init)
// Starting from a known character within the string and of m length
assert("life is one insp" == { val i = str.indexOf('l'); str.slice(i, i + m) })
// Alternatively
assert("life is one insp" == str.drop(str.indexOf('l')).take(m))
assert("life is one insp" == str.dropWhile(_ != 'l').take(m) )
// Starting from a known substring within the string and of m length
assert("good life is one" == { val i = str.indexOf("good"); str.slice(i, i + m) })