|
| 1 | +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-313/ |
| 2 | +/*# |
| 3 | +
|
| 4 | +Task 1: Broken Keys |
| 5 | +
|
| 6 | +Submitted by: [43]Mohammad Sajid Anwar |
| 7 | + __________________________________________________________________ |
| 8 | +
|
| 9 | + You have a broken keyboard which sometimes type a character more than |
| 10 | + once. |
| 11 | +
|
| 12 | + You are given a string and actual typed string. |
| 13 | +
|
| 14 | + Write a script to find out if the actual typed string is meant for the |
| 15 | + given string. |
| 16 | +
|
| 17 | +Example 1 |
| 18 | +
|
| 19 | +Input: $name = "perl", $typed = "perrrl" |
| 20 | +Output: true |
| 21 | +
|
| 22 | +Here "r" is pressed 3 times instead of 1 time. |
| 23 | +
|
| 24 | +Example 2 |
| 25 | +
|
| 26 | +Input: $name = "raku", $typed = "rrakuuuu" |
| 27 | +Output: true |
| 28 | +
|
| 29 | +Example 3 |
| 30 | +
|
| 31 | +Input: $name = "python", $typed = "perl" |
| 32 | +Output: false |
| 33 | +
|
| 34 | +Example 4 |
| 35 | +
|
| 36 | +Input: $name = "coffeescript", $typed = "cofffeescccript" |
| 37 | +Output: true |
| 38 | +
|
| 39 | +Task 2: Reverse Letters |
| 40 | +#*/ |
| 41 | + |
| 42 | + |
| 43 | +package main |
| 44 | + |
| 45 | +import ( |
| 46 | + "io" |
| 47 | + "os" |
| 48 | + |
| 49 | + "github.com/google/go-cmp/cmp" |
| 50 | +) |
| 51 | + |
| 52 | +type charCount struct { |
| 53 | + chr rune |
| 54 | + cnt int |
| 55 | +} |
| 56 | + |
| 57 | +type charCounts []charCount |
| 58 | + |
| 59 | +func newCharCounts(str string) charCounts { |
| 60 | + chrs := []rune(str) |
| 61 | + if len(chrs) == 0 { |
| 62 | + return charCounts{} |
| 63 | + } |
| 64 | + ccs := charCounts{charCount{chrs[0], 1}} |
| 65 | + i := 0 |
| 66 | + for _, v := range chrs[1:] { |
| 67 | + if v == ccs[i].chr { |
| 68 | + ccs[i].cnt++ |
| 69 | + } else { |
| 70 | + ccs = append(ccs, charCount{v, 1}) |
| 71 | + i++ |
| 72 | + } |
| 73 | + } |
| 74 | + return ccs |
| 75 | +} |
| 76 | + |
| 77 | +type input struct { |
| 78 | + name, typed string |
| 79 | +} |
| 80 | + |
| 81 | +func (in input) process() bool { |
| 82 | + name_cc := newCharCounts(in.name) |
| 83 | + l := len(name_cc) |
| 84 | + typed_cc := newCharCounts(in.typed) |
| 85 | + if l != len(typed_cc) { |
| 86 | + return false |
| 87 | + } |
| 88 | + for i := range l { |
| 89 | + ncc := name_cc[i] |
| 90 | + tcc := typed_cc[i] |
| 91 | + if ncc.chr != tcc.chr || ncc.cnt > tcc.cnt { |
| 92 | + return false |
| 93 | + } |
| 94 | + } |
| 95 | + return true |
| 96 | +} |
| 97 | + |
| 98 | +func main() { |
| 99 | + for _, data := range []struct { |
| 100 | + input input |
| 101 | + output bool |
| 102 | + }{ |
| 103 | + {input{"perl", "perrrl"}, true}, |
| 104 | + {input{"raku", "rrakuuuu"}, true}, |
| 105 | + {input{"python", "perl"}, false}, |
| 106 | + {input{"coffeescript", "cofffeescccript"}, true}, |
| 107 | + {input{"noob", "nob"}, false}, |
| 108 | + {input{"snow", "noow"}, false}, |
| 109 | + {input{"ten", "teens"}, false}, |
| 110 | + {input{"aha", "haa"}, false}, |
| 111 | + {input{"กากมาก", "กาาากมาาาาาาก"}, true}, |
| 112 | + {input{"กาาากมาาาก", "กากมาก"}, false}, |
| 113 | + {input{"รักกัน", "รรักกันน"}, true}, |
| 114 | + {input{"บรรทัด", "บรทัด"}, false}, |
| 115 | + {input{"โรงเรียน", "โโโรรงงเรรียยนน"}, true}, |
| 116 | + {input{"เกเรียน", "เกกเรรียยนน"}, true}, |
| 117 | + {input{"แมว", "แแแมมมวววว"}, true}, |
| 118 | + {input{"มมมแมว", "มแมมมวววว"}, false}, |
| 119 | + } { |
| 120 | + //fmt.Println(data.input) |
| 121 | + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference |
| 122 | + } |
| 123 | +} |
0 commit comments