# ------------------------------------------------------------
# Static lookup table; extend as needed.
# Defined at the top level so it's a proper constant.
# ------------------------------------------------------------
COUNTRY_MAP = {
"CA" => "Canada",
"CN" => "China",
"DE" => "Germany",
"FR" => "France",
"GB" => "United Kingdom",
"SK" => "South Korea",
"IN" => "India",
"JP" => "Japan",
"US" => "United States"
}.freeze
# ------------------------------------------------------------
# get_country_name
# Receives a 2‑letter ISO country code and returns the
# corresponding country name.
#
# Uses a Hash for O(1) lookups.
# Input is normalized to uppercase to ensure consistent matching.
# Returns nil if the code is not found.
# ------------------------------------------------------------
def get_country_name(alpha2)
# Normalize input
code = alpha2.strip.upcase
# Lookup
COUNTRY_MAP[code]
end
# ------------------------------------------------------------
# main
# Demonstrates the lookup function with several sample codes.
# ------------------------------------------------------------
def main
codes = ["US", "GB", "FR", "JP", "ZZ"] # ZZ is intentionally invalid
codes.each do |code|
name = get_country_name(code)
if name
puts "#{code} → #{name}"
else
puts "#{code} → (invalid code)"
end
end
end
main
=begin
run:
US → United States
GB → United Kingdom
FR → France
JP → Japan
ZZ → (invalid code)
=end