4/14/2009

Workshop 4-part 2

Challenge Problems:

1. Create, test and debug a Ruby program called dognames.rb or catnames.rb to accept 3 names from the keyboard and to display each name on the screen in alphabetical order WITHOUT using a data structure such as a list.

a.) Create a ruby file named dognames with the following code:
























Code Explanation:

First, create a method name dogname.

Second, print out a message to ask user for imput three dog names and store the standard input to the variable named dogA, dogB and dogC.
Third, create three varibale which declare null is used to store the order of dog name, they are named first, second and third.
Fourth, use if-elseif-else statement to sort dog name order, if the result is -1, that means the front variable is smaller, for exampe, (a <=> b)==-1.
Fifth, print out the order of the dog name and end the dogname method.
Sixth, execute dogname method.

b.) Execute the dognames.rb by issuing the command "ruby dognames.rb", and the result is showed as below screen dump.




















2. Write a Ruby program called fizzbuzz.rb that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".


a.) Create a ruby file named fizzbuzz with the following code:























Code Explanation:
First, declare a variable named num and set the value to 1,
Second, use while-loop to control the looping between 1 to 100
Third, use if-elseif-else statement to set the conditon, if multiples of three print "Fizz" instead of the number, multiples of five print "Buzz" and multiples of both three and five print "FizzBuzz".
Fourth, num will plus 1 per each looping.


b.) Execute the fizzbuzz .rb by issuing the command "ruby fizzbuzz .rb", and the result is showed as below screen dump.

























3.Compare the Ruby and Python versions of the dog years calculator:
#!/usr/bin/ruby
# The Dog year calculator program called dogyears.rb

def dogyears
# get the original age
puts “Enter your age (in human years): "
age = gets # gets is a method for input from keyboard
puts # is a method or operator for screen output

#do some range checking, then print result
if age <> 110
puts "Frankly, I don't believe you."
else
puts "That's", age*7, "in dog years."
end
dogyears

Python

#!/usr/bin/python
# The Dog year calculator program called dogyears.py

def dogyears():
# get the original age
age = input("Enter your age (in human years): ")
print # print a blank line

# do some range checking, then print result
if age <> 110:
print "Frankly, I don't believe you."
else:
print "That's", age*7, "in dog years."

### pause for Return key (so window doesn't disappear)
raw_input('press Return>')

def main():
dogyears()
main()

===========================================

There are some different codes syntax between Ruby and Pythob which are listed in the following

1.Getting standard Input Ruby uses get, but Python uses input
2. Printing out the messageRuby uses puts, but Python uses print
3.The syntax of if-statement is differenta.)Ruby does not need the symbol -> “:” after the if-conditonal, but Python needsb.) Ruby uses “elsif” in if-elseif-else statement, but Python uses elif.
4.)Ruby does not need call the method in the main(), but Python needs.

In the dogyear program, actually, both of them do the same thing that get the input from console and verify the input integer whether is smaller than zero, if yes , print out “Negative age?!? I don't think so” if no, verify it whether is smaller than 3 and older than 110, if yes, print out “Frankly, I don't believe you.”, and finally, if all condition is not true, it let the input integer times 7 and print out "That's", then print out the value of input integer which has been already timed 7 and finally print out "in dog years."

沒有留言:

發佈留言