From de8d1b4224d82c0a8da3e7020ded07b073714da9 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Mon, 13 Jul 2015 21:31:25 -0400 Subject: [PATCH] Add Elixir American Flag --- meetings/2015-07-13/README.md | 35 +++++++++++ .../kirsche_kevin/american_flag.exs | 61 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 meetings/2015-07-13/README.md create mode 100644 meetings/2015-07-13/kirsche_kevin/american_flag.exs diff --git a/meetings/2015-07-13/README.md b/meetings/2015-07-13/README.md new file mode 100644 index 0000000..007db00 --- /dev/null +++ b/meetings/2015-07-13/README.md @@ -0,0 +1,35 @@ +# Programming Puzzle Night + +```ruby +#!/usr/bin/env ruby +# +# This goal of this RubyLoCo Puzzle is to generate an ascii +# American Flag. Try to come up with a unique way to generate +# the flag. Below is the easiest implementation. +# +# Ruby not required! Use any langauge and pair up! +# +# If you complete the task here are some more advanced ideas: +# make the flag colored +# write a DSL to generate any flag +# animate the flag +# add a flag pole +# use emoji +# +# Happy Birthday America + +puts '* * * * * * ' + '1111111111111111111111111111111111111111111' +puts ' * * * * * ' + '0000000000000000000000000000000000000000000' +puts '* * * * * * ' + '1111111111111111111111111111111111111111111' +puts ' * * * * * ' + '0000000000000000000000000000000000000000000' +puts '* * * * * * ' + '1111111111111111111111111111111111111111111' +puts ' * * * * * ' + '0000000000000000000000000000000000000000000' +puts '* * * * * * ' + '1111111111111111111111111111111111111111111' +puts '0000000000000000000000000000000000000000000000000000000' +puts '1111111111111111111111111111111111111111111111111111111' +puts '0000000000000000000000000000000000000000000000000000000' +puts '1111111111111111111111111111111111111111111111111111111' +puts '0000000000000000000000000000000000000000000000000000000' +puts '1111111111111111111111111111111111111111111111111111111' + +``` \ No newline at end of file diff --git a/meetings/2015-07-13/kirsche_kevin/american_flag.exs b/meetings/2015-07-13/kirsche_kevin/american_flag.exs new file mode 100644 index 0000000..0a3e42d --- /dev/null +++ b/meetings/2015-07-13/kirsche_kevin/american_flag.exs @@ -0,0 +1,61 @@ +defmodule Recursion do + require Integer + # Recursion for star rows + def print_star_row_pair_n_minus_1_times(n) when n <= 1 do + print_row AmericanFlag.star_row(IO.ANSI.white_background) + end + + def print_star_row_pair_n_minus_1_times(n) do + print_row AmericanFlag.star_row(IO.ANSI.white_background) + print_row AmericanFlag.star_row(IO.ANSI.red_background) + print_star_row_pair_n_minus_1_times(n-1) + end + + # Recursion for color rows + def print_color_row_n_times(n) when n <= 1 do + print_row AmericanFlag.color_row(IO.ANSI.white_background) + end + + def print_color_row_n_times(n) do + if Integer.is_even n do + print_row AmericanFlag.color_row(IO.ANSI.red_background) + else + print_row AmericanFlag.color_row(IO.ANSI.white_background) + end + print_color_row_n_times(n-1) + end + + # Allow printing of a single row N times + def print_row_n_times(msg, n) when n <= 1 do + :ok = IO.write msg <> ansi_reset + end + + def print_row_n_times(msg, n) do + :ok = IO.write msg <> ansi_reset + print_row_n_times(msg, n-1) + end + + # Helper Methods + defp ansi_reset do + "#{IO.ANSI.reset}" <> "\n" + end + + defp print_row(msg) do + :ok = IO.write msg <> ansi_reset + end +end + +defmodule AmericanFlag do + def star_row(background) do + "#{IO.ANSI.white}#{IO.ANSI.blue_background} * * * * * * " <> + "#{background}" <> String.duplicate(" ", 45) + end + + def color_row(color) do + "#{color}" <> String.duplicate(" ", 69) + end +end + +Recursion.print_star_row_pair_n_minus_1_times(5) +Recursion.print_color_row_n_times(6) +Recursion.print_row_n_times("#{IO.ANSI.white}#{IO.ANSI.black_background}| |", 12)