2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -11,4 +11,6 @@ In particular the functions you need to create are:
|
|||
* Replace every occurrence of a byte (or a string) in a string with another string
|
||||
* Join strings
|
||||
|
||||
<br>
|
||||
Possible contexts of use: compression algorithms (like [[LZW compression]]), L-systems (manipulation of symbols), many more.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,2 @@
|
|||
(defun string-empty-p (string)
|
||||
(cond
|
||||
((= 0 (length string))t)
|
||||
(nil)))
|
||||
(zerop (length string)))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
s := "\x00" # strings can contain any value, even nulls
|
||||
s := "abc" # create a string
|
||||
s := &null # destroy a string (well sbsnfon it for garbage collection)
|
||||
s := &null # destroy a string (garbage collect value of s; set new value to &null)
|
||||
v := s # assignment
|
||||
s == t # expression s equals t
|
||||
s << t # expression s less than t
|
||||
|
|
|
|||
67
Task/Binary-strings/PowerShell/binary-strings.psh
Normal file
67
Task/Binary-strings/PowerShell/binary-strings.psh
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
Clear-Host
|
||||
|
||||
## String creation (which is string assignment):
|
||||
Write-Host "`nString creation (which is string assignment):" -ForegroundColor Cyan
|
||||
Write-Host '[string]$s = "Hello cruel world"' -ForegroundColor Yellow
|
||||
[string]$s = "Hello cruel world"
|
||||
|
||||
## String (or any variable) destruction:
|
||||
Write-Host "`nString (or any variable) destruction:" -ForegroundColor Cyan
|
||||
Write-Host 'Remove-Variable -Name s -Force' -ForegroundColor Yellow
|
||||
Remove-Variable -Name s -Force
|
||||
|
||||
## Now reassign the variable:
|
||||
Write-Host "`nNow reassign the variable:" -ForegroundColor Cyan
|
||||
Write-Host '[string]$s = "Hello cruel world"' -ForegroundColor Yellow
|
||||
[string]$s = "Hello cruel world"
|
||||
|
||||
Write-Host "`nString comparison -- default is case insensitive:" -ForegroundColor Cyan
|
||||
Write-Host '$s -eq "HELLO CRUEL WORLD"' -ForegroundColor Yellow
|
||||
$s -eq "HELLO CRUEL WORLD"
|
||||
Write-Host '$s -match "HELLO CRUEL WORLD"' -ForegroundColor Yellow
|
||||
$s -match "HELLO CRUEL WORLD"
|
||||
Write-Host '$s -cmatch "HELLO CRUEL WORLD"' -ForegroundColor Yellow
|
||||
$s -cmatch "HELLO CRUEL WORLD"
|
||||
|
||||
## Copy a string:
|
||||
Write-Host "`nCopy a string:" -ForegroundColor Cyan
|
||||
Write-Host '$t = $s' -ForegroundColor Yellow
|
||||
$t = $s
|
||||
|
||||
## Check if a string is empty:
|
||||
Write-Host "`nCheck if a string is empty:" -ForegroundColor Cyan
|
||||
Write-Host 'if ($s -eq "") {"String is empty."} else {"String = $s"}' -ForegroundColor Yellow
|
||||
if ($s -eq "") {"String is empty."} else {"String = $s"}
|
||||
|
||||
## Append a byte to a string:
|
||||
Write-Host "`nAppend a byte to a string:" -ForegroundColor Cyan
|
||||
Write-Host "`$s += [char]46`n`$s" -ForegroundColor Yellow
|
||||
$s += [char]46
|
||||
$s
|
||||
|
||||
## Extract (and display) substring from a string:
|
||||
Write-Host "`nExtract (and display) substring from a string:" -ForegroundColor Cyan
|
||||
Write-Host '"Is the world $($s.Substring($s.IndexOf("c"),5))?"' -ForegroundColor Yellow
|
||||
"Is the world $($s.Substring($s.IndexOf("c"),5))?"
|
||||
|
||||
## Replace every occurrence of a byte (or a string) in a string with another string:
|
||||
Write-Host "`nReplace every occurrence of a byte (or a string) in a string with another string:" -ForegroundColor Cyan
|
||||
Write-Host "`$t = `$s -replace `"cruel`", `"beautiful`"`n`$t" -ForegroundColor Yellow
|
||||
$t = $s -replace "cruel", "beautiful"
|
||||
$t
|
||||
|
||||
## Join strings:
|
||||
Write-Host "`nJoin strings [1]:" -ForegroundColor Cyan
|
||||
Write-Host '"Is the world $($s.Split()[1]) or $($t.Split()[1])?"' -ForegroundColor Yellow
|
||||
"Is the world $($s.Split()[1]) or $($t.Split()[1])?"
|
||||
Write-Host "`nJoin strings [2]:" -ForegroundColor Cyan
|
||||
Write-Host '"{0} or {1}... I don''t care." -f (Get-Culture).TextInfo.ToTitleCase($s.Split()[1]), $t.Split()[1]' -ForegroundColor Yellow
|
||||
"{0} or {1}... I don't care." -f (Get-Culture).TextInfo.ToTitleCase($s.Split()[1]), $t.Split()[1]
|
||||
Write-Host "`nJoin strings [3] (display an integer array using the -join operater):" -ForegroundColor Cyan
|
||||
Write-Host '1..12 -join ", "' -ForegroundColor Yellow
|
||||
1..12 -join ", "
|
||||
|
||||
## Display an integer array in a tablular format:
|
||||
Write-Host "`nMore string madness... display an integer array in a tablular format:" -ForegroundColor Cyan
|
||||
Write-Host '1..12 | Format-Wide {$_.ToString().PadLeft(2)}-Column 3 -Force' -NoNewline -ForegroundColor Yellow
|
||||
1..12 | Format-Wide {$_.ToString().PadLeft(2)} -Column 3 -Force
|
||||
|
|
@ -1,31 +1,18 @@
|
|||
/*REXX program shows ways to use and express binary strings. */
|
||||
|
||||
dingsta='11110101'b /*4 versions, bit str assignment.*/
|
||||
dingsta="11110101"b /*same as above. */
|
||||
dingsta='11110101'B /*same as above. */
|
||||
dingsta='1111 0101'B /*same as above. */
|
||||
|
||||
dingst2=dingsta /*clone 1 str to another (copy). */
|
||||
|
||||
other='1001 0101 1111 0111'b /*another binary (bit) string. */
|
||||
|
||||
if dingsta=other then say 'they are equal' /*compare two strings.*/
|
||||
|
||||
if other=='' then say 'OTHER is empty.' /*see if it's empty. */
|
||||
if length(other)==0 then say 'OTHER is empty.' /*another version. */
|
||||
|
||||
otherA=other || '$' /*append a dollar sign to OTHER. */
|
||||
otherB=other'$' /*same as above, with less fuss. */
|
||||
|
||||
guts=substr(c2b(other),10,3) /*get the 10th through 12th bits.*/
|
||||
/*see sub below. Some REXXes */
|
||||
/*have C2B as a built-in function*/
|
||||
|
||||
new=changestr('A',other,"Z") /*change the letter A to Z. */
|
||||
|
||||
tt=changestr('~~',other,";") /*change 2 tildes to a semicolon.*/
|
||||
|
||||
joined=dignsta || dingst2 /*join 2 strs together (concat). */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────C2B subroutine───────────────────────*/
|
||||
c2b: return x2b(c2x(arg(1))) /*return the string as a binary string. */
|
||||
/*REXX program demonstrates methods (code examples) to use and express binary strings.*/
|
||||
dingsta= '11110101'b /*four versions, bit string assignment.*/
|
||||
dingsta= "11110101"b /*this is the same assignment as above.*/
|
||||
dingsta= '11110101'B /* " " " " " " " */
|
||||
dingsta= '1111 0101'B /* " " " " " " */
|
||||
dingsta2=dingsta /*clone one string to another (a copy).*/
|
||||
other= '1001 0101 1111 0111'b /*another binary (or bit) string. */
|
||||
if dingsta=other then say 'they are equal' /*compare the two (binary) strings. */
|
||||
if other=='' then say 'OTHER is empty.' /*see if the OTHER string is empty.*/
|
||||
otherA=other || '$' /*append a dollar sign ($) to OTHER. */
|
||||
otherB=other'$' /*same as above, but with less fuss. */
|
||||
guts=substr(c2b(other), 10, 3) /*obtain the 10th through 12th bits.*/
|
||||
new=changeStr('A', other, "Z") /*change the upper letter A ──► Z. */
|
||||
tt=changeStr('~~', other, ";") /*change two tildes ──► one semicolon.*/
|
||||
joined=dignsta || dingsta2 /*join two strings together (concat). */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
c2b: return x2b( c2x( arg(1) ) ) /*return the string as a binary string.*/
|
||||
|
|
|
|||
72
Task/Binary-strings/Rust/binary-strings.rust
Normal file
72
Task/Binary-strings/Rust/binary-strings.rust
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
use std::str;
|
||||
fn main() {
|
||||
//Create new string
|
||||
let str = String::from("Hello world!");
|
||||
println!("{}", str);
|
||||
assert!(str == "Hello world!", "Incorrect string text");
|
||||
|
||||
//Create and assign value to string
|
||||
let mut assigned_str = String::new();
|
||||
assert!(assigned_str == "", "Incorrect string creation");
|
||||
assigned_str.push_str("Text has been assigned!");
|
||||
println!("{}", assigned_str);
|
||||
assert!(assigned_str == "Text has been assigned!","Incorrect string text");
|
||||
|
||||
//String comparison, compared lexicographically byte-wise
|
||||
//same as the asserts above
|
||||
if str == "Hello world!" && assigned_str == "Text has been assigned!" {
|
||||
println!("Strings are equal");
|
||||
}
|
||||
|
||||
//Cloning -> str can still be used after cloning
|
||||
let clone_str = str.clone();
|
||||
println!("String is:{} and Clone string is: {}", str, clone_str);
|
||||
assert!(clone_str == str, "Incorrect string creation");
|
||||
|
||||
//Copying, str won't be usable anymore, accessing it will cause compiler failure
|
||||
let copy_str = str;
|
||||
println!("String copied now: {}", copy_str);
|
||||
|
||||
//Check if string is empty
|
||||
let empty_str = String::new();
|
||||
assert!(empty_str.is_empty(), "Error, string should be empty");
|
||||
|
||||
//Append byte, Rust strings are a stream of UTF-8 bytes
|
||||
let byte_vec = vec![65]; //contains A
|
||||
let byte_str = str::from_utf8(&byte_vec).unwrap();
|
||||
assert!(byte_str == "A", "Incorrect byte append");
|
||||
|
||||
//Substrings can be accessed through slices
|
||||
let test_str = "Blah String";
|
||||
let mut sub_str = &test_str[0..11];
|
||||
assert!(sub_str == "Blah String", "Error in slicing");
|
||||
sub_str = &test_str[1..5];
|
||||
assert!(sub_str == "lah ", "Error in slicing");
|
||||
sub_str = &test_str[3..];
|
||||
assert!(sub_str == "h String", "Error in slicing");
|
||||
sub_str = &test_str[..2];
|
||||
assert!(sub_str == "Bl", "Error in slicing");
|
||||
|
||||
//String replace, note string is immutable
|
||||
let org_str = "Hello";
|
||||
assert!( org_str.replace("l", "a") == "Heaao", "Error in replacement");
|
||||
assert!( org_str.replace("ll", "r") == "Hero", "Error in replacement");
|
||||
|
||||
//Joining strings requires a string and an &str or a two string one of which needs an & for coercion
|
||||
let str1 = "Hi";
|
||||
let str2 = " There";
|
||||
let fin_str = str1.to_string() + str2;
|
||||
assert!( fin_str == "Hi There", "Error in concatenation");
|
||||
|
||||
//Joining strings requires a string and an &str or two strings, one of which needs an & for coercion
|
||||
let str1 = "Hi";
|
||||
let str2 = " There";
|
||||
let fin_str = str1.to_string() + str2;
|
||||
assert!( fin_str == "Hi There", "Error in concatenation");
|
||||
|
||||
//Splits -- note Rust supports passing patterns to splits
|
||||
let f_str = "Pooja and Sundar are up in Tumkur";
|
||||
let split_str: Vec<&str> = f_str.split(' ').collect();
|
||||
assert!( split_str == ["Pooja", "and", "Sundar", "are", "up", "in", "Tumkur"], "Error in string split");
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue