diff --git a/challenge-313/spadacciniweb/go/ch-1.go b/challenge-313/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..591cde84c0 --- /dev/null +++ b/challenge-313/spadacciniweb/go/ch-1.go @@ -0,0 +1,67 @@ +/* +Task 1: Broken Keys +Submitted by: Mohammad Sajid Anwar + +You have a broken keyboard which sometimes type a character more than once. +You are given a string and actual typed string. +Write a script to find out if the actual typed string is meant for the given string. + +Example 1 +Input: $name = "perl", $typed = "perrrl" +Output: true +Here "r" is pressed 3 times instead of 1 time. + +Example 2 +Input: $name = "raku", $typed = "rrakuuuu" +Output: true + +Example 3 +Input: $name = "python", $typed = "perl" +Output: false + +Example 4 +Input: $name = "coffeescript", $typed = "cofffeescccript" +Output: true +*/ + +package main + +import ( + "fmt" +) + +func broken_keyboard(name string, typed string) { + freq := make(map[rune]int) + for _, c := range name { + freq[c]++ + } + for _, c := range typed { + _, ok := freq[c] + if ok { + freq[c]-- + } + } + broken := false + for _, count := range freq { + if count < 0 { + broken = true + } + } + fmt.Printf("name '%s' typed '%s' -> %t\n", name, typed, broken) +} + + +func main() { + name := "perl" + typed := "perrrl" + broken_keyboard(name, typed) + + name = "raku"; typed = "rrakuuuu" + broken_keyboard(name, typed) + + name = "python"; typed = "perl" + broken_keyboard(name, typed) + + name = "coffeescript"; typed = "cofffeescccript" + broken_keyboard(name, typed) +} diff --git a/challenge-313/spadacciniweb/perl/ch-1.pl b/challenge-313/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..a9ee344889 --- /dev/null +++ b/challenge-313/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +# Task 1: Broken Keys +# Submitted by: Mohammad Sajid Anwar +# +# You have a broken keyboard which sometimes type a character more than once. +# You are given a string and actual typed string. +# Write a script to find out if the actual typed string is meant for the given string. +# +# Example 1 +# Input: $name = "perl", $typed = "perrrl" +# Output: true +# Here "r" is pressed 3 times instead of 1 time. +# +# Example 2 +# Input: $name = "raku", $typed = "rrakuuuu" +# Output: true +# +# Example 3 +# Input: $name = "python", $typed = "perl" +# Output: false +# +# Example 4 +# Input: $name = "coffeescript", $typed = "cofffeescccript" +# Output: true + +use strict; +use warnings; + +my $name = "perl"; +my $typed = "perrrl"; +broken_keyboard($name, $typed); + +$name = "raku"; $typed = "rrakuuuu"; +broken_keyboard($name, $typed); + +$name = "python"; $typed = "perl"; +broken_keyboard($name, $typed); + +$name = "coffeescript"; $typed = "cofffeescccript"; +broken_keyboard($name, $typed); + +exit 0; + +sub broken_keyboard { + my $name = shift; + my $typed = shift; + + my %cnt; + foreach (split //, $name) { + $cnt{$_}++; + } + foreach (split //, $typed) { + $cnt{$_}-- + if defined $cnt{$_}; + } + + printf "name '%s' typed '%s' -> %s\n", + $name, $typed, + ( scalar map { $_ < 0 ? $_ : () } + values %cnt + ) ? 'true' + : 'false'; +} diff --git a/challenge-313/spadacciniweb/perl/ch-2.pl b/challenge-313/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..c6cbc90db1 --- /dev/null +++ b/challenge-313/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +# Task 2: Reverse Letters +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string. +# Write a script to reverse only the alphabetic characters in the string. +# +# Example 1 +# Input: $str = "p-er?l" +# Output: "l-re?p" +# +# Example 2 +# Input: $str = "wee-k!L-y" +# Output: "yLk-e!e-w" +# +# Example 3 +# Input: $str = "_c-!h_all-en!g_e" +# Output: "_e-!g_nel-la!h_c" + +use strict; +use warnings; + +my $str = "p-er?l"; +reverse_only_characters($str); + +$str = "wee-k!L-y"; +reverse_only_characters($str); + +$str = "_c-!h_all-en!g_e"; +reverse_only_characters($str); + +exit 0; + +sub reverse_only_characters { + my $str = shift; + + my @old_str = reverse + map { /[[:alpha:]]/ ? $_ : ()} + split //, $str; + my @new_str; + foreach my $i (0..length($str)-1) { + my $char = substr $str, $i, 1; + if ($char =~ /[[:alpha:]]/) { + push @new_str, ( shift @old_str); + } else { + push @new_str, $char; + } + } + printf "'%s' -> '%s'\n", + $str, + join '', @new_str; +} diff --git a/challenge-313/spadacciniweb/python/ch-1.py b/challenge-313/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..e7cce33c5c --- /dev/null +++ b/challenge-313/spadacciniweb/python/ch-1.py @@ -0,0 +1,54 @@ +# Task 1: Broken Keys +# Submitted by: Mohammad Sajid Anwar +# +# You have a broken keyboard which sometimes type a character more than once. +# You are given a string and actual typed string. +# Write a script to find out if the actual typed string is meant for the given string. +# +# Example 1 +# Input: $name = "perl", $typed = "perrrl" +# Output: true +# Here "r" is pressed 3 times instead of 1 time. +# +# Example 2 +# Input: $name = "raku", $typed = "rrakuuuu" +# Output: true +# +# Example 3 +# Input: $name = "python", $typed = "perl" +# Output: false +# +# Example 4 +# Input: $name = "coffeescript", $typed = "cofffeescccript" +# Output: true + +def broken_keyboard(name, typed): + freq = {} + for char in name: + if char in freq: + freq[char] += 1 + else: + freq[char] = 1 + for char in typed: + if char in freq: + freq[char] -= 1 + + print("name '%s' typed '%s' -> %s" % + (name, typed, + len([v for v in freq.values() if v < 0]) > 1 + ) + ) + +if __name__ == "__main__": + name = "perl" + typed = "perrrl" + broken_keyboard(name, typed) + + name = "raku"; typed = "rrakuuuu" + broken_keyboard(name, typed) + + name = "python"; typed = "perl" + broken_keyboard(name, typed) + + name = "coffeescript"; typed = "cofffeescccript" + broken_keyboard(name, typed) diff --git a/challenge-313/spadacciniweb/python/ch-2.py b/challenge-313/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..b18f370c0e --- /dev/null +++ b/challenge-313/spadacciniweb/python/ch-2.py @@ -0,0 +1,40 @@ +# Task 2: Reverse Letters +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string. +# Write a script to reverse only the alphabetic characters in the string. +# +# Example 1 +# Input: $str = "p-er?l" +# Output: "l-re?p" +# +# Example 2 +# Input: $str = "wee-k!L-y" +# Output: "yLk-e!e-w" +# +# Example 3 +# Input: $str = "_c-!h_all-en!g_e" +# Output: "_e-!g_nel-la!h_c" + +def reverse_only_characters(str): + old_str = [c for c in str[::-1] if c.isalpha()] + + new_string = "" + for i in range(len(str)): + if str[i].isalpha(): + c = old_str.pop(0) + new_string += c + else: + new_string += str[i] + + print("'%s' -> '%s'" % (str, new_string) ) + +if __name__ == "__main__": + str = "p-er?l" + reverse_only_characters(str) + + str = "wee-k!L-y" + reverse_only_characters(str) + + str = "_c-!h_all-en!g_e" + reverse_only_characters(str)