Last active
July 12, 2018 02:49
Revisions
-
kotp revised this gist
Nov 6, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ def convert(temperature_in_F) (temperature_in_F - 32) / 1.8 # This solution can cause problems. Why? end puts "%.2f" % convert(-40) # => -40.00 -
kotp revised this gist
Apr 18, 2012 . 15 changed files with 110 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ This is a collection of exercises, and answers. Filenames will be in the form of w1e1.rb. Answers will be w1e1a1.rb with subsequent 'best answers' being numbered consecutively. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ Exercise1. Before executing the code given below, guess the results. Next, execute the code. Did you get it right? If you did not get it right, can you think of why? Discuss your first guess and what you got when running the code. Goal: Understanding operator precedence and association. ```ruby y = false z = true x = y or z puts x (x = y) or z puts x x = (y or z) puts x ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ ```ruby y = false # simply an assignment z = true # simply an assignment x = y or z # x is assigned false though true is returned for the line puts x # prints false (x = y) or z # x is again assigned false while the line returns true puts x # prints false x = (y or z) # (y or z) returns true, and that result is assigned to x puts x # prints true as the parentheses gives precedence to or ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ Read the sprintf documentation and the % documentation in the String class and figure out the output being printed by of this Ruby code. ```ruby puts "%05d" % 123 ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ sprintf format_string %[flags][width][.precision]type For %05d: flags: 0 = Pad with zeros, not spaces. width: 5 type: d = Convert argument as a decimal number. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ puts "%05d" % 123 # => Prints 00123 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ Exercise3. Write a Ruby program that displays how old I am, in years, if I am 979000000 seconds old. Display the result as a floating point (decimal) number to two decimal places ( for example, 17.23). Note: To format the output to say 2 decimal places, we can use the Kernel's format method. For example, if x = 45.5678 then format("%.2f", x) will return the string 45.57 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ def how_old(age_in_seconds) Float(age_in_seconds) / 60 / 60 / 24 / 365 end age_in_seconds = 979_000_000 puts "You are %.2f years old" % how_old(age_in_seconds) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ Exercise4. Write a Ruby program that tells you how many minutes there are in a year (do not bother right now about leap years etc.). This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ # Almost a sarcastic answer. But in reality, the simplest thing that solves # the problem is the best solution, generally, so this is an awesome start. puts 525600 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ minutes = 60 * 24 * 365 puts "There are #{minutes} minutes in a year." # => "There are 525600 minutes in a year"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ Exercise 5. The following program prints the value of the variable. Why? ```ruby my_string = 'Hello Ruby World' def my_string 'Hello World' end puts my_string ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ The puts command is pointing to the variable 'my_string', where the output is 'Hello Ruby World'. The method 'my_string' is never called. The method can be called in several ways. The simplest is my_string() with the parenthesis making it clear that it is a method call. Generally, you would carefully consider the wisdom of using the same name for a variable as a method in the same scope. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ Exercise6. Write a method called convert that takes one argument which is a temperature in degrees Fahrenheit. This method should return the temperature in degrees Celsius. To format the output to say 2 decimal places, we can use the Kernel's format method. For example, if x = 45.5678 then format("%.2f", x) will return the string "45.57". This is also to say that if x = 10 then format("$.2f", x) will return the string "10.00" Another way is to use the round function as follows: puts (x*100).round/100.0 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ def convert(temperature_in_F) (temperature_in_F - 32) / 1.8 end puts "%.2f" % convert(-40) # => -40.00 -
kotp created this gist
Apr 18, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ A collection of exercises and answers.