examples/mapcolor.rb
# when called as colorize2(map), we start from the beginning colorizing that
# map.  when recursively called as colorize2(map, countries, colorized),
# countries are the countries left to colorize, and colorized are the
# assignments made so far.
when called as colorize2(map), we start from the beginning colorizing that
map.  when recursively called as colorize2(map, countries, colorized),
countries are the countries left to colorize, and colorized are the
assignments made so far.
when called as colorize2 ( map ) we start from the beginning colorizing that map . when recursively called as colorize2 ( map countries colorized ) countries are the countries left to colorize and colorized are the assignments made so far .
def colorize map, countries=map.keys, colorized={}
  country=countries.first
  return colorized if country.nil?
  color = Ambit.choose Colors
  map[country].each {|n| Ambit.assert colorized[n] != color}
  colorized_new = colorized.clone 
  colorized_new[country] = color
  colorize map, countries.drop(1), colorized_new
end
def colorize map , countries = map . keys , colorized = { } country = countries . first return colorized if country . nil? color = Ambit . choose Colors map [ country ] . each { | n | Ambit . assert colorized [ n ] != color } colorized_new = colorized . clone colorized_new [ country ] = color colorize map , countries . drop ( 1 ) , colorized_new end
when called as colorize2(map), we start from the beginning colorizing that
map.
when called as colorize2 ( map ) we start from the beginning colorizing that map .
#puts "considering #{color} for #{country}" # fake a functional view of hash
map
<not_specific>
countries
<not_specific>
keys
<not_specific>
colorized
<not_specific>