Permalink
Browse files

Remove old week 3

  • Loading branch information...
1 parent 451e749 commit 9dc238fd46fe7db7dc90afb3ab36c49f2fe795cc @citizen428 committed Feb 11, 2012
View
62 coreruby/exercise_set3/answers.txt
@@ -1,62 +0,0 @@
-2. The first version calls the method "mtdarry" which runs 10 times and prints the numbers 0-9. The second version prints the return value of the method after the execution of the method. This can be illustrated by changing the first program as follows (the output will now be the same as the output of the second version):
-
-def mtdarry
- 10.times do |num|
- puts num
- end
-end
-
-x = mtdarry
-puts x
-
-Another way to illustrate the described behaviour is by changing the return value of the method "mtdarry" in the second version:
-
-def mtdarry
- 10.times do |num|
- puts num
- end
- "lala"
-end
-
-puts mtdarry
-
-5. The program is inefficient in assigment, the use of unnecessary variables and an overly complex logical check. A more efficient version could look like this (given that the variables i, x and y need to be used):
-
-i = x = y = 0
-1000000.times do
- i = x = y += 1
- puts x*y if (i == 100001)
-end
-
-An even more efficient version could do with a single variable:
-
-1000000.times do |x|
- puts x**2 if (x == 100001)
-end
-
-6. The correct answer is
-
- 5. "#{title}" is #{price} yen.
-
-since the string is enclosed in single quotes (') no interpolation is done.
-
-7. The operator "defined?" has a lower precedence than the assignment operator, so an undefined value gets assigned to the variable "expand". The idiomatic
-
-puts expand ||= true
-
-is probably the best way to do it.
-
-8. The contents of x and y get swapped because Ruby assignments are done in parallel (See "Programming Ruby" - "Expressions" - "Assigment" - "Parallel Assignment").
-
-9. The variable has been the left-hand side of an assignment, so the symbol is known (although nothing got assigned, hence the value "nil"). In this modified example
-
-if false
- x = pqr
- pqr = foo
-end
-puts x
-puts pqr
-puts foo
-
-we now get two "nil" values and an error on foo.
-
View
26 coreruby/exercise_set3/e0301splitstring.rb
@@ -1,26 +0,0 @@
-#!/usr/bin/ruby
-=begin
- Breaks a multiline string up into individual lines
- and outputs them including line numbers.
-
- Needs Ruby 1.9
-=end
-
-s="Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n"
-
-s.each_line.with_index { |line, i| puts "Line #{i+1}: #{line}" }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
15 coreruby/exercise_set3/e0303leapyearcheck.rb
@@ -1,15 +0,0 @@
-#!/usr/bin/ruby
-=begin
- Ask user to input a year and tell him if it's a leap year.
-=end
-
-def leap_year?(year)
- (year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0
-end
-
-print 'Please enter the year to be checked: '
-STDOUT.flush
-input_year = gets.to_i
-
-puts "#{input_year}:#{' not' unless leap_year?(input_year)} a leap year"
-
View
19 coreruby/exercise_set3/e0304minutesperyear.rb
@@ -1,19 +0,0 @@
-#!/usr/bin/ruby
-=begin
- Write a method leap_year?. It should accept a year value from the user,
- check whether it's a leap year, and then return true or false.
-
- With the help of this method calculate and display the number of minutes
- in a leap year or non-leap year.
-
- Note: a century year is a leap year only if it is divisible by 4.
-=end
-
-def leap_year?(year)
- (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
-end
-
-print 'Please enter a year: '
-STDOUT.flush
-year = gets.to_i
-puts "#{year} has #{24 * 60 * (leap_year?(year) ? 366 : 365)} minutes."

0 comments on commit 9dc238f

Please sign in to comment.