Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Elixir American Flag #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions meetings/2015-07-13/README.md
Original file line number Diff line number Diff line change
@@ -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'

```
61 changes: 61 additions & 0 deletions meetings/2015-07-13/kirsche_kevin/american_flag.exs
Original file line number Diff line number Diff line change
@@ -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)