diff options
| author | pme <hauptadler@gmail.com> | 2024-08-03 11:10:33 +0200 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-08-03 11:10:33 +0200 |
| commit | 52ad57985d38f248267b830e0fd68446df3455f8 (patch) | |
| tree | 31f74636c71f7e65f320d6c11b3167e39dc3f8a5 | |
| parent | 788b78b9dfb1dab102893603bd4e083edacc5be1 (diff) | |
| download | perlweeklychallenge-club-52ad57985d38f248267b830e0fd68446df3455f8.tar.gz perlweeklychallenge-club-52ad57985d38f248267b830e0fd68446df3455f8.tar.bz2 perlweeklychallenge-club-52ad57985d38f248267b830e0fd68446df3455f8.zip | |
challenge-280
| -rwxr-xr-x | challenge-280/peter-meszaros/tcl/ch-1.tcl | 56 | ||||
| -rwxr-xr-x | challenge-280/peter-meszaros/tcl/ch-2.tcl | 58 |
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-280/peter-meszaros/tcl/ch-1.tcl b/challenge-280/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..10146da1d9 --- /dev/null +++ b/challenge-280/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,56 @@ +#!/usr/bin/env tclsh +# +# Task 1: Twice Appearance +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, containing lowercase English letters only. +# +# Write a script to print the first letter that appears twice. +# +# Example 1 +# +# Input: $str = "acbddbca" +# Output: "d" +# +# Example 2 +# +# Input: $str = "abccd" +# Output: "c" +# +# Example 3 +# +# Input: $str = "abcdabbb" +# Output: "a" +# + +package require tcltest + +set cases { + {acbddbca d "Example 1"} + {abccd c "Example 2"} + {abcdabbb a "Example 3"} + {abcdxy null "Example 4"} +} + +proc twice_apperance {str} { + set d [dict create] + foreach c [split $str {}] { + if {[dict exists $d $c]} { + return $c + } else { + dict set d $c 1 + } + } + return null +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + twice_apperance [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-280/peter-meszaros/tcl/ch-2.tcl b/challenge-280/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..d4662cdd48 --- /dev/null +++ b/challenge-280/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,58 @@ +#!/usr/bin/env tclsh +# +# Task 2: Count Asterisks +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, where every two consecutive vertical bars are +# grouped into a pair. +# +# Write a script to return the number of asterisks, *, excluding any between each +# pair of vertical bars. +# +# Example 1 +# +# Input: $str = "p|*e*rl|w**e|*ekly|" +# Ouput: 2 +# +# characters we are looking here are "p" and "w**e". +# +# Example 2 +# +# Input: $str = "perl" +# Ouput: 0 +# +# Example 3 +# +# Input: $str = "th|ewe|e**|k|l***ych|alleng|e" +# Ouput: 5 +# +# The characters we are looking here are "th", "e**", "l***ych" and "e". +# + +package require tcltest + +set cases { + {"gp|*e*rl|w**e|*ekly|" 2 "Example 1"} + {"gperl" 0 "Example 2"} + {"gth|ewe|e**|k|l***ych|alleng|e" 5 "Example 3"} +} + +proc count_asterisks {str} { + + set count 0 + foreach {even odd} [split $str "|"] { + incr count [regexp -all {\*} $even] + } + return $count +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + count_asterisks [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
