aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/spadacciniweb/elixir/ch-2.exs
blob: ca07864984e6601da44f1cc20815c4baf4d59148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)