diff options
| author | Shawn <shawnw.mobile@gmail.com> | 2020-10-06 02:29:47 -0700 |
|---|---|---|
| committer | Shawn <shawnw.mobile@gmail.com> | 2020-10-06 02:29:47 -0700 |
| commit | fb1c8aa62a6ea258f6b0d0198652d3effd889ae5 (patch) | |
| tree | b0561c0a6e5bc23ab750b4137579a3a91e95a5b6 | |
| parent | 27cd79d114db989658262ab8f98a494b83219aad (diff) | |
| download | perlweeklychallenge-club-fb1c8aa62a6ea258f6b0d0198652d3effd889ae5.tar.gz perlweeklychallenge-club-fb1c8aa62a6ea258f6b0d0198652d3effd889ae5.tar.bz2 perlweeklychallenge-club-fb1c8aa62a6ea258f6b0d0198652d3effd889ae5.zip | |
Challenge 081, both parts in tcl
| -rwxr-xr-x | challenge-081/shawn-wagner/tcl/ch1.tcl | 27 | ||||
| -rwxr-xr-x | challenge-081/shawn-wagner/tcl/ch2.tcl | 20 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-081/shawn-wagner/tcl/ch1.tcl b/challenge-081/shawn-wagner/tcl/ch1.tcl new file mode 100755 index 0000000000..bc0b95423b --- /dev/null +++ b/challenge-081/shawn-wagner/tcl/ch1.tcl @@ -0,0 +1,27 @@ +#!/usr/bin/env tclsh + +proc substrings {s} { + set len [string length $s] + for {set start 0} {$start < $len} {incr start} { + for {set end $start} {$end < $len} {incr end} { + dict set substrings [string range $s $start $end] 1 + } + } + return [dict keys $substrings] +} + +proc task1 {A B} { + foreach substr [concat [substrings $A] [substrings $B]] { + dict incr subs $substr + } + dict for {substr count} $subs { + if {$count != 2} { continue } + if {[regexp "^(?:$substr)+$" $A] && [regexp "^(?:$substr)+$" $B]} { + lappend common $substr + } + } + puts [lsort $common] +} + +task1 abcdabcd abcdabcdabcdabcd +task1 aaa aa diff --git a/challenge-081/shawn-wagner/tcl/ch2.tcl b/challenge-081/shawn-wagner/tcl/ch2.tcl new file mode 100755 index 0000000000..6bee2cb108 --- /dev/null +++ b/challenge-081/shawn-wagner/tcl/ch2.tcl @@ -0,0 +1,20 @@ +#!/usr/bin/env tclsh + +proc task2 {input} { + set fp [open $input] + while {[gets $fp line] >= 0} { + regsub -all {[.""(),]|--|'s} $line "" line + foreach word [split $line] { + dict incr words $word + } + } + close $fp + dict for {word count} $words { + dict lappend frequencies $count $word + } + foreach count [lsort -integer [dict keys $frequencies]] { + puts "$count [lsort [dict get $frequencies $count]]" + } +} + +task2 input |
