Riddle by Einstein

Let us assume that there are five houses of different colors next to each other on the same road. In each house lives a man of a different nationality. Every man has his favorite drink, his favorite brand of cigarettes, and keeps pets of a particular kind.

1. The Englishman lives in the red house.
2. The Swede keeps dogs.
3. The Dane drinks tea.
4. The green house is just to the left of the white one.
5. The owner of the green house drinks coffee.
6. The Pall Mall smoker keeps birds.
7. The owner of the yellow house smokes Dunhills.
8. The man in the center house drinks milk.
9. The Norwegian lives in the first house.
10. The Blend smoker has a neighbor who keeps cats.
11. The man who smokes Blue Masters drinks bier.
12. The man who keeps horses lives next to the Dunhill smoker.
13. The German smokes Prince.
14. The Norwegian lives next to the blue house.
15. The Blend smoker has a neighbor who drinks water.

The question to be answered is: Who keeps fish?

Einstein wrote this riddle early during the 19th century. He said 98% of the world could not solve it. Its not hard, you just need to pay attention and be patient.

class Person
 
  attr_accessor :nationality, :residing, :drinking, :smoking, :pet, :number
 
  def initialize(number, residing = nil)
    @number = number
    @residing = residing
  end
 
end
 
class Residing
 
  attr_accessor :number, :host, :color
 
  def initialize(number, color = nil, host = nil)
    @number = number
    @host = host
    @color = color
  end
 
end
 
 
residing_1 = Residing.new(1, "yellow") # making use of condition 9, 7
person_1 = Person.new(1, residing_1)
person_1.nationality = "Norway"  #making use of condition 9
person_1.smoking = "DUNHILL"  #making use of condition 7
person_1.pet = "cat"
#"cat"
 
residing_2 = Residing.new(2, "blue") # making use of condition 14
person_2 = Person.new(2, residing_2)
person_2.pet = "horse"  # condition 11
person_2.nationality = "Denmark"
person_2.drinking = "tea"
person_2.smoking = "blend cigarette"
#"Denmark" "tea" "blend"
 
residing_3 = Residing.new(3, nil) #making use of condition 8
person_3 = Person.new(3, residing_3)
person_3.nationality = "English"
person_3.drinking = "milk"             #making use of condition 8
person_3.residing.color = "red"       #making use of conditon 1
person_3.smoking = "PALL MALL"
person_3.pet = "bird"
#"PALL MALL" "bird"
 
residing_4 = Residing.new(4, "green")# making use
person_4 = Person.new(4, residing_4)
person_4.drinking = "coffee" # making use of condition 5
person_4.nationality = "German"
person_4.smoking = "PRINCE"
#"Germ" "PRINCE"
 
residing_5 = Residing.new(5, "white")#   making use of condition 9,14,4
person_5 = Person.new(5, residing_5)
person_5.nationality = "Sweden"
person_5.pet = "dog"
person_5.smoking = "BLUE MASTER"
person_5.drinking = "beer"
#"Sweden" "dog"
#"BLUE MASTER" "beer"
 
 
#after the initialization of the status,input as the followings
arr = [person_1, person_2, person_3, person_4, person_5]
arr.each do |i|
  i.pet ||= "fish"
  puts "Nationality: #{i.nationality}; house-color: #{i.residing.color}, drink #{i.drinking},
  smoke #{i.smoking}, raise the pet #{i.pet} "
end
17fav 收藏本文

Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">



收藏 & 分享

Powered by 17fav.com