aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/spadacciniweb/elixir/ch-2.exs
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2024-06-13 12:18:46 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2024-06-13 12:18:46 +0200
commitc7797f212a2db97cae7ac808542c20df8402eea5 (patch)
treeda01a669bdb10805b27a49b43b25e37c29268a4c /challenge-273/spadacciniweb/elixir/ch-2.exs
parentdd679b917c37fbc332647940157e31e431557005 (diff)
downloadperlweeklychallenge-club-c7797f212a2db97cae7ac808542c20df8402eea5.tar.gz
perlweeklychallenge-club-c7797f212a2db97cae7ac808542c20df8402eea5.tar.bz2
perlweeklychallenge-club-c7797f212a2db97cae7ac808542c20df8402eea5.zip
Add ch-1 and ch-2 in Elixir
Diffstat (limited to 'challenge-273/spadacciniweb/elixir/ch-2.exs')
-rw-r--r--challenge-273/spadacciniweb/elixir/ch-2.exs60
1 files changed, 60 insertions, 0 deletions
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)