aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/spadacciniweb
diff options
context:
space:
mode:
authorATSchneider <atschneider@temple.edu>2024-06-16 07:37:39 -0400
committerGitHub <noreply@github.com>2024-06-16 07:37:39 -0400
commitbd67c7d68d54058bc6bc6b41ae8bbc8de725314f (patch)
treeb19053d19cd2c6952b1677e099c20e03ca63f19d /challenge-273/spadacciniweb
parent86f02124512baeea87ccccea67dc73bab9db71ca (diff)
parente7c6e4694cbc20e39268624c74ec9d5134bd66b7 (diff)
downloadperlweeklychallenge-club-bd67c7d68d54058bc6bc6b41ae8bbc8de725314f.tar.gz
perlweeklychallenge-club-bd67c7d68d54058bc6bc6b41ae8bbc8de725314f.tar.bz2
perlweeklychallenge-club-bd67c7d68d54058bc6bc6b41ae8bbc8de725314f.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-273/spadacciniweb')
-rw-r--r--challenge-273/spadacciniweb/elixir/ch-1.exs65
-rw-r--r--challenge-273/spadacciniweb/elixir/ch-2.exs60
-rw-r--r--challenge-273/spadacciniweb/go/ch-1.go74
-rw-r--r--challenge-273/spadacciniweb/go/ch-2.go60
-rw-r--r--challenge-273/spadacciniweb/perl/ch-2.pl12
-rw-r--r--challenge-273/spadacciniweb/python/ch-1.py61
-rw-r--r--challenge-273/spadacciniweb/python/ch-2.py46
7 files changed, 370 insertions, 8 deletions
diff --git a/challenge-273/spadacciniweb/elixir/ch-1.exs b/challenge-273/spadacciniweb/elixir/ch-1.exs
new file mode 100644
index 0000000000..3fe074e791
--- /dev/null
+++ b/challenge-273/spadacciniweb/elixir/ch-1.exs
@@ -0,0 +1,65 @@
+# Task 1: Percentage of Character
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str and a character $char.
+# Write a script to return the percentage, nearest whole, of given character in the given string.
+#
+# Example 1
+# Input: $str = "perl", $char = "e"
+# Output: 25
+#
+# Example 2
+# Input: $str = "java", $char = "a"
+# Output: 50
+#
+# Example 3
+# Input: $str = "python", $char = "m"
+# Output: 0
+#
+# Example 4
+# Input: $str = "ada", $char = "a"
+# Output: 67
+#
+# Example 5
+# Input: $str = "ballerina", $char = "l"
+# Output: 22
+#
+# Example 6
+# Input: $str = "analitik", $char = "k"
+# Output: 13
+
+defmodule Percentage do
+
+ def freq(str, char) do
+ String.split(str, "", trim: true)
+ |> Enum.frequencies
+ |> Map.get(char)
+ end
+
+ def out(str, char) do
+ IO.write( str <> " " <> char <> " -> ")
+ case freq(str, char) do
+ nil -> IO.puts "0"
+ x -> IO.puts( x / String.length(str)*100 |> round )
+ end
+ end
+
+end
+
+str = "perl"; char = "e";
+Percentage.out(str, char)
+
+str = "java"; char = "a";
+Percentage.out(str, char)
+
+str = "python"; char = "m";
+Percentage.out(str, char)
+
+str = "ada"; char = "a";
+Percentage.out(str, char)
+
+str = "ballerina"; char = "l";
+Percentage.out(str, char)
+
+str = "analitik"; char = "k";
+Percentage.out(str, char)
diff --git a/challenge-273/spadacciniweb/elixir/ch-2.exs b/challenge-273/spadacciniweb/elixir/ch-2.exs
new file mode 100644
index 0000000000..ca07864984
--- /dev/null
+++ b/challenge-273/spadacciniweb/elixir/ch-2.exs
@@ -0,0 +1,60 @@
+# Task 2: B After A
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str.
+# Write a script to return true if there is at least one b, and no a appears after the first b.
+#
+# Example 1
+# Input: $str = "aabb"
+# Output: true
+#
+# Example 2
+# Input: $str = "abab"
+# Output: false
+#
+# Example 3
+# Input: $str = "aaa"
+# Output: false
+#
+# Example 4
+# Input: $str = "bbb"
+# Output: true
+
+defmodule Position do
+
+ def first_b(str) do
+ case Regex.run(~r/b/, str, return: :index) do
+ nil -> nil
+ out -> out |> Enum.fetch!(0) |> elem(0)
+ end
+ end
+
+ def last_a(str, pos) do
+ case Regex.run(~r/a/, str, offset: pos, return: :index) do
+ nil -> IO.puts( "true" )
+ _ -> IO.puts( "false" )
+ end
+ end
+
+ def out(str) do
+ IO.write( str <> " -> " )
+ #IO.inspect( first_b(str) )
+ case first_b(str) do
+ nil -> IO.puts( "false" )
+ out -> last_a(str, out)
+ end
+ end
+
+end
+
+str = "aabb"
+Position.out(str)
+
+str = "abab"
+Position.out(str)
+
+str = "aaa"
+Position.out(str)
+
+str = "bbb"
+Position.out(str)
diff --git a/challenge-273/spadacciniweb/go/ch-1.go b/challenge-273/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..c98cffe354
--- /dev/null
+++ b/challenge-273/spadacciniweb/go/ch-1.go
@@ -0,0 +1,74 @@
+/*
+Task 1: Percentage of Character
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str and a character $char.
+Write a script to return the percentage, nearest whole, of given character in the given string.
+
+Example 1
+Input: $str = "perl", $char = "e"
+Output: 25
+
+Example 2
+Input: $str = "java", $char = "a"
+Output: 50
+
+Example 3
+Input: $str = "python", $char = "m"
+Output: 0
+
+Example 4
+Input: $str = "ada", $char = "a"
+Output: 67
+
+Example 5
+Input: $str = "ballerina", $char = "l"
+Output: 22
+
+Example 6
+Input: $str = "analitik", $char = "k"
+Output: 13
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+func percentage(str string, char string) {
+ frequency := make(map[rune]int)
+
+ for _, val := range str {
+ frequency[val]++ //assign frequencies
+ }
+
+ fmt.Printf("%s %s -> %.0f\n", str, char, float64(frequency[ rune(char[0]) ]*100)/float64( len(str) ) )
+}
+
+func main() {
+ str := "perl"
+ char := "e"
+ percentage(str, char)
+
+ str = "java"
+ char = "a"
+ percentage(str, char)
+
+ str = "python"
+ char = "m"
+ percentage(str, char)
+
+ str = "ada"
+ char = "a"
+ percentage(str, char);
+
+ str = "ballerina"
+ char = "l"
+ percentage(str, char);
+
+ str = "analitik"
+ char = "k";
+ percentage(str, char);
+}
+
diff --git a/challenge-273/spadacciniweb/go/ch-2.go b/challenge-273/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..509565840e
--- /dev/null
+++ b/challenge-273/spadacciniweb/go/ch-2.go
@@ -0,0 +1,60 @@
+/*
+Task 2: B After A
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str.
+Write a script to return true if there is at least one b, and no a appears after the first b.
+
+Example 1
+Input: $str = "aabb"
+Output: true
+
+Example 2
+Input: $str = "abab"
+Output: false
+
+Example 3
+Input: $str = "aaa"
+Output: false
+
+Example 4
+Input: $str = "bbb"
+Output: true
+*/
+
+package main
+
+import (
+ "fmt"
+ s "strings"
+)
+
+func position (str string) {
+ offset := s.Index(str, "b")
+ res := 0
+
+ if (offset >=0) {
+ res = s.Index( str[offset+1:], "a");
+ }
+
+ if (res == -1) {
+ fmt.Printf("%s -> %s\n", str, "true")
+ } else {
+ fmt.Printf("%s -> %s\n", str, "false")
+ }
+}
+
+func main() {
+ str := "aabb"
+ position(str)
+
+ str = "abab";
+ position(str)
+
+ str = "aaa"
+ position(str)
+
+ str = "bbb"
+ position(str)
+}
+
diff --git a/challenge-273/spadacciniweb/perl/ch-2.pl b/challenge-273/spadacciniweb/perl/ch-2.pl
index a7c968b0fd..9f7cc330ca 100644
--- a/challenge-273/spadacciniweb/perl/ch-2.pl
+++ b/challenge-273/spadacciniweb/perl/ch-2.pl
@@ -42,14 +42,10 @@ exit 0;
sub position {
my $str = shift;
- my $first_b = index($str, 'b');
- my $last_a = index((scalar reverse $str), 'a');
- $last_a = length($str) - $last_a - 1
- if $last_a >= 0;
-
+ my $offset = index($str, 'b');
printf "%s -> %s\n",
$str,
- ($last_a > $first_b)
- ? 'false'
- : 'true';
+ ($offset >= 0 and index($str, 'a', $offset) == -1)
+ ? 'true'
+ : 'false';
}
diff --git a/challenge-273/spadacciniweb/python/ch-1.py b/challenge-273/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..bfbc5cbc57
--- /dev/null
+++ b/challenge-273/spadacciniweb/python/ch-1.py
@@ -0,0 +1,61 @@
+# Task 1: Percentage of Character
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str and a character $char.
+# Write a script to return the percentage, nearest whole, of given character in the given string.
+#
+# Example 1
+# Input: $str = "perl", $char = "e"
+# Output: 25
+#
+# Example 2
+# Input: $str = "java", $char = "a"
+# Output: 50
+#
+# Example 3
+# Input: $str = "python", $char = "m"
+# Output: 0
+#
+# Example 4
+# Input: $str = "ada", $char = "a"
+# Output: 67
+#
+# Example 5
+# Input: $str = "ballerina", $char = "l"
+# Output: 22
+#
+# Example 6
+# Input: $str = "analitik", $char = "k"
+# Output: 13
+
+def percentage(str, char):
+ print("%s -> %d" %
+ ( str,
+ round( str.count(char)*100/len(str) )
+ )
+ )
+
+if __name__ == "__main__":
+ str = "perl"
+ char = "e"
+ percentage(str, char)
+
+ str = "java"
+ char = "a"
+ percentage(str, char)
+
+ str = "python"
+ char = "m"
+ percentage(str, char)
+
+ str = "ada"
+ char = "a"
+ percentage(str, char)
+
+ str = "ballerina"
+ char = "l"
+ percentage(str, char)
+
+ str = "analitik"
+ char = "k"
+ percentage(str, char)
diff --git a/challenge-273/spadacciniweb/python/ch-2.py b/challenge-273/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..dce0bca454
--- /dev/null
+++ b/challenge-273/spadacciniweb/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# Task 2: B After A
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str.
+# Write a script to return true if there is at least one b, and no a appears after the first b.
+#
+# Example 1
+# Input: $str = "aabb"
+# Output: true
+#
+# Example 2
+# Input: $str = "abab"
+# Output: false
+#
+# Example 3
+# Input: $str = "aaa"
+# Output: false
+#
+# Example 4
+# Input: $str = "bbb"
+# Output: true
+
+def out(str):
+ offset = str.find('b')
+ if offset >= 0 and str.find('a', offset) == -1:
+ res = "true"
+ else:
+ res = "false"
+ print("%s -> %s" %
+ ( str, res )
+ )
+
+if __name__ == "__main__":
+ str = "aabb"
+ out(str)
+
+ str = "abab"
+ out(str)
+
+ str = "aaa"
+ out(str)
+
+ str = "bbb"
+ out(str)