Skip to content

Instantly share code, notes, and snippets.

@bhserna
Created June 24, 2017 17:14
Show Gist options
  • Save bhserna/5a82132f9172eb30f292e087c63104f4 to your computer and use it in GitHub Desktop.
Save bhserna/5a82132f9172eb30f292e087c63104f4 to your computer and use it in GitHub Desktop.
clase3
# Crear un arreglo de hashes, cada hash contiene los datos de la persona:
# - Nombre Completo
# - País
# - Continente
# - Género
#
# Poblar con muchos datos
# 2.- Contar la cantidad de personas de la lista
# 3.- Generar un array con cada continente, eliminar repeticiones, considerar
# que pueden haber nombres escritos con mayúscula y minúscula
# 4.- Armar un hash, donde cada Key sea un continente y el value un array con
# los países de cada continente
# 5.- Generar una lista con todas las personas llamadas Emmanuel
# 6.- Hacer dos listas de personas, una por cada genero
persons = [
{ name: "Benito", country: "México", continent: "América", gender: :male },
{ name: "Emmanuel", country: "México", continent: "américa", gender: :male },
{ name: "Daniela", country: "Belice", continent: "américa", gender: :female },
{ name: "Salas", country: "China", continent: "asia", gender: :male },
{ name: "Verónica", country: "México", continent: "América", gender: :female },
{ name: "Maripaz", country: "Australia", continent: "Oceanía", gender: :female }
]
persons2 = [
{ name: "Benito", country: "México", continent: "América", gender: :male },
{ name: "Emmanuel", country: "México", continent: "américa", gender: :male },
{ name: "Maripaz", country: "Australia", continent: "Oceanía", gender: :female }
]
def get_continents(persons)
persons.map{|person| person[:continent].capitalize}.uniq
end
def get_countries_by_continent(persons)
persons
.group_by{|person| person[:continent].capitalize }
.map{|continent, persons| [continent, persons.map{|person| person[:country]}.uniq] }
.to_h
end
def get_persons_by_name(name, persons)
persons
.select { |person| person[:name] == name }
.map{|person| person[:name]}
end
def get_persons_by_gender(gender, persons)
persons
.select { |person| person[:gender] == gender }
.map { |person| person[:name] }
end
# --------Resultados--------
# Cantidad de personas: 6
puts persons.count
# Continentes: ["América", "Asia", "Oceanía"]
puts get_continents(persons).inspect
# Paises por continente: {"América"=>["México", "Belice"], "Asia"=>["China"],
# "Oceanía"=>["Australia"]}
puts get_countries_by_continent(persons).inspect
# Personas llamadas Benito: ["Benito"]
puts get_persons_by_name("Benito", persons).inspect
# Hombres: ["Benito", "Emmanuel", "Salas"]
# Mujeres: ["Daniela", "Verónica", "Maripaz"]
puts get_persons_by_gender(:male, persons).inspect
puts get_persons_by_gender(:female, persons).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment