From fb1c8aa62a6ea258f6b0d0198652d3effd889ae5 Mon Sep 17 00:00:00 2001 From: Shawn Date: Tue, 6 Oct 2020 02:29:47 -0700 Subject: Challenge 081, both parts in tcl --- challenge-081/shawn-wagner/tcl/ch1.tcl | 27 +++++++++++++++++++++++++++ challenge-081/shawn-wagner/tcl/ch2.tcl | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100755 challenge-081/shawn-wagner/tcl/ch1.tcl create mode 100755 challenge-081/shawn-wagner/tcl/ch2.tcl 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 -- cgit