Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
16
Task/Substring/Tcl/substring-1.tcl
Normal file
16
Task/Substring/Tcl/substring-1.tcl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
set str "abcdefgh"
|
||||
set n 2
|
||||
set m 3
|
||||
|
||||
puts [string range $str $n [expr {$n+$m-1}]]
|
||||
puts [string range $str $n end]
|
||||
puts [string range $str 0 end-1]
|
||||
# Because Tcl does substrings with a pair of indices, it is easier to express
|
||||
# the last two parts of the task as a chained pair of [string range] operations.
|
||||
# A maximally efficient solution would calculate the indices in full first.
|
||||
puts [string range [string range $str [string first "d" $str] end] [expr {$m-1}]]
|
||||
puts [string range [string range $str [string first "de" $str] end] [expr {$m-1}]]
|
||||
|
||||
# From Tcl 8.5 onwards, these can be contracted somewhat.
|
||||
puts [string range [string range $str [string first "d" $str] end] $m-1]
|
||||
puts [string range [string range $str [string first "de" $str] end] $m-1]
|
||||
19
Task/Substring/Tcl/substring-2.tcl
Normal file
19
Task/Substring/Tcl/substring-2.tcl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Define the substring operation, efficiently
|
||||
proc ::substring {string start length} {
|
||||
string range $string $start [expr {$start + $length - 1}]
|
||||
}
|
||||
# Plumb it into the language
|
||||
set ops [namespace ensemble configure string -map]
|
||||
dict set ops substr ::substring
|
||||
namespace ensemble configure string -map $ops
|
||||
|
||||
# Now show off by repeating the challenge!
|
||||
set str "abcdefgh"
|
||||
set n 2
|
||||
set m 3
|
||||
|
||||
puts [string substr $str $n $m]
|
||||
puts [string range $str $n end]
|
||||
puts [string range $str 0 end-1]
|
||||
puts [string substr $str [string first "d" $str] $m]
|
||||
puts [string substr $str [string first "de" $str] $m]
|
||||
Loading…
Add table
Add a link
Reference in a new issue