From 27251356fc3074cdde903035ef1b35b7e6612502 Mon Sep 17 00:00:00 2001 From: M King Date: Mon, 12 Apr 2021 22:55:45 -0700 Subject: [PATCH] submit week4 assignments --- block_function.rb | 18 +++++++++++++++ divisible.rb | 19 ++++++++++++++++ hangman.rb | 31 ++++++++++++++++++++++++++ hash_to_array.rb | 33 +++++++++++++++++++++++++++ sort_blocks.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++ sums.rb | 33 +++++++++++++++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 block_function.rb create mode 100644 divisible.rb create mode 100644 hangman.rb create mode 100644 hash_to_array.rb create mode 100644 sort_blocks.rb create mode 100644 sums.rb diff --git a/block_function.rb b/block_function.rb new file mode 100644 index 0000000..6d38637 --- /dev/null +++ b/block_function.rb @@ -0,0 +1,18 @@ +# (6) Create a program block_function.rb. +# It should have a function do_calc that calls a block using a yield statement. +# The yield statement will pass the numbers 7 and 9 to a block, and then will print out the result. +# Call the do_calc function twice in your program. +# The first time, pass a block that adds the two numbers. +# The second time, pass a block that multiplies the two numbers. Your program should print out 16 and 63. + +def do_calc + yield 7, 9 +end + +do_calc do |a, b| + puts a + b +end + +do_calc do |a, b| + puts a * b +end \ No newline at end of file diff --git a/divisible.rb b/divisible.rb new file mode 100644 index 0000000..1cc3116 --- /dev/null +++ b/divisible.rb @@ -0,0 +1,19 @@ +# (1) Write a method that returns in an array all the numbers between 1 and 100 that are divisible by 2 or 3 or 5. +# Then call the method in your program and print out what it returns. Call the program divisible.rb. + +def divisible + array= [] + i = 1 + + while i >=1 and i <= 100 + array.push(i) if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) + i += 1 + end + + array.each do |item| + puts item + end +end + +divisible + diff --git a/hangman.rb b/hangman.rb new file mode 100644 index 0000000..61d2146 --- /dev/null +++ b/hangman.rb @@ -0,0 +1,31 @@ +#(2) Write a program hangman.rb that contains a function called hangman. +# The function's parameters are a word and an array of letters. +# It returns a string showing the letters that have been guessed. +# Call the function from within your program so that you know that it works. + +def hangman(string, array) + result = "_" * string.length + temp = string.downcase + array.each do |item| + item = item.downcase + while temp.index(item) do + num = temp.index(item) + result[num] = item + temp[num] = "_" + end + end + result +end + +puts hangman("bob", ["b"]) +puts hangman("alphabet", ["a", "h"]) +puts hangman("mingkIng", ["M", "i", "g"]) + +# Another student did it in a very efficient way +# def hangman(string, array) +# result = "" +# string.each_char do |char| +# result += array.include?(char)? char : "_" +# end +# result +# end \ No newline at end of file diff --git a/hash_to_array.rb b/hash_to_array.rb new file mode 100644 index 0000000..296abdd --- /dev/null +++ b/hash_to_array.rb @@ -0,0 +1,33 @@ +#(3) Write a program that collects 5 keys and 5 values from the user, and stores them in a hash. +# Write a function that accepts the hash as optional parameters and prints out an array of keys and an array of values. +# Call the function within your program so you know it works. +# (Question: Can you find online information on Ruby hash methods that will help with this function?) +# Call this program hash_to_array.rb. + +def hash_to_array(hash={}) + + def ask(question, kind="string") + print question + " " + answer = gets.chomp + answer = answer.to_i if kind == "number" + return answer + end + + if hash.empty? + hash = {} + 5.times do |num| + key = ask("enter key#{num+1}: ") + value = ask("enter value#{num+1}: ") + hash[key] = value + end + end + + puts "Array of keys: #{hash.keys}" + puts "Array of values: #{hash.values}" +end + +puts "Result with parameter send in: " +hash_to_array({"a" => "aaa", "b" => "bbb"}) + +puts "\nResult without parameter send in:" +hash_to_array() \ No newline at end of file diff --git a/sort_blocks.rb b/sort_blocks.rb new file mode 100644 index 0000000..b155b2d --- /dev/null +++ b/sort_blocks.rb @@ -0,0 +1,57 @@ +#(5) The ruby array sort method can use a block to sort various arrays. The block +# must compare between two elements of the array, a and b. If ab, it should evaluate to 1. +# Here is the start of a program, which you should call sort_blocks.rb. + +# Add to this program. Add additional calls to book_array.sort that pass blocks. +# For your first call to sort, pass a block so that the array is sorted in order of title, and print out the array. +# For your second call to sort, pass a block so that the array is sorted in order of copies, and print out the array. + +class Book + attr_reader :author, :title, :count + def initialize(author,title,count) + @author = author + @title = title + @count = count + end + def to_s + "author: #{author} title: #{title} count: #{count}" + end +end + +book_array = [] +book_array.push(Book.new("Beatrice Potter","Peter Rabbit",25)) +book_array.push(Book.new("Henry Fielding","Tom Jones",12)) +book_array.push(Book.new("Bob Woodward","All the President's Men",30)) + +puts "Sorting alphabetically by author" + +new_array = book_array.sort do |a,b| + author1 = a.author.downcase + author2 = b.author.downcase + author1 <=> author2 + # if author1 > author2 + # 1 + # elsif author1 < author2 + # -1 + # else + # 0 + # end +end +puts new_array + +puts "\nSorting alphabetically by titles" +sort_array_by_title = book_array.sort do |a, b| + title1 = a.title.downcase + title2 = b.title.downcase + title1 <=> title2 +end +puts sort_array_by_title + +puts "\nSorting by copies in ascending order" +sort_array_by_copies = book_array.sort do |a, b| + copies1 = a.count + copies2 = b.count + copies1 <=> copies2 +end +puts sort_array_by_copies diff --git a/sums.rb b/sums.rb new file mode 100644 index 0000000..99fbeea --- /dev/null +++ b/sums.rb @@ -0,0 +1,33 @@ +#(4) Create a program sums.rb with two classes. +#(a) a class called Sum1 with an initialize method that takes two parameters +# and sets the instance variable total to the sum of the two. Include a line at the top of the class: attr_accessor :total + +#(b) a class called Sum2 with an initialize method that takes two parameters a and b +#and sets the instance variable a to the parameter a and the instance variable b to the parameter b. +#Create a method new_total inside Sum2 that returns the sum of the instance variables a and b. + +#(c) In the mainline program, create instances of Sum1 and Sum2, passing 5 and 6 as parameters on the new statement. +# Print out the total for Sum1. Print out the new_total for Sum2. + +class Sum1 + attr_accessor :total + def initialize(param1, param2) + @total = param1 + param2 + end +end + +class Sum2 + def initialize(a, b) + @a = a + @b = b + end + def new_total + @a + @b + end +end + +sum1 = Sum1.new(5, 6) +sum2 = Sum2.new(5, 6) + +puts sum1.total +puts sum2.new_total \ No newline at end of file