From 6602d8743afc8f8f41295383e4ae7cccf349eeb2 Mon Sep 17 00:00:00 2001 From: Ajvar Date: Tue, 14 Apr 2020 00:32:39 +0200 Subject: [PATCH] tests --- lib/00_hello.rb | 4 ++-- lib/01_temperature.rb | 9 +++++++++ lib/02_calculator.rb | 29 +++++++++++++++++++++++++++++ lib/03_basics.rb | 18 ++++++++++++++++++ lib/04_simon_says.rb | 36 ++++++++++++++++++++++++++++++++++++ lib/05_timer.rb | 3 +++ lib/06_pig_latin.rb | 31 +++++++++++++++++++++++++++++++ spec/02_calculator_spec.rb | 35 ++++++++++++++++++++++++----------- 8 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 lib/01_temperature.rb create mode 100644 lib/02_calculator.rb create mode 100644 lib/03_basics.rb create mode 100644 lib/04_simon_says.rb create mode 100644 lib/05_timer.rb create mode 100644 lib/06_pig_latin.rb diff --git a/lib/00_hello.rb b/lib/00_hello.rb index a5fd1d52..5efd8628 100644 --- a/lib/00_hello.rb +++ b/lib/00_hello.rb @@ -1,7 +1,7 @@ def hello - + return "Hello!" end def greet(name) - + return "Hello, #{name}!" end diff --git a/lib/01_temperature.rb b/lib/01_temperature.rb new file mode 100644 index 00000000..7646b8bc --- /dev/null +++ b/lib/01_temperature.rb @@ -0,0 +1,9 @@ +def ftoc(number) + return ((number - 32) * 0.5556).round(1) +end + +def ctof(number) + return ((number * 1.8) + 32).round(1) +end + +puts(ctof(302)) \ No newline at end of file diff --git a/lib/02_calculator.rb b/lib/02_calculator.rb new file mode 100644 index 00000000..adaed6e2 --- /dev/null +++ b/lib/02_calculator.rb @@ -0,0 +1,29 @@ +def add(x, y) + return x + y +end + +def subtract(x, y) + return x - y +end + + def sum(arr) + return arr.sum +end + +def multiply(x, y) + return x * y +end + +def power(x, y) + y -= 1 + copy = x + y.times {x *= copy} + return (x) +end + +def factorial(x) + return 1 if x == 0 || x == 1 + return (1..x).inject(:*) +end + +puts(factorial(5)) \ No newline at end of file diff --git a/lib/03_basics.rb b/lib/03_basics.rb new file mode 100644 index 00000000..a433b74f --- /dev/null +++ b/lib/03_basics.rb @@ -0,0 +1,18 @@ +def who_is_bigger(x, y, z) + return "nil detected" if x == nil || y == nil || z == nil + return "a is bigger" if x > y && x > z + return "b is bigger" if y > x && y > z + return "c is bigger" +end + +def reverse_upcase_noLTA(s) + return s.upcase.tr('LTA', '').reverse +end + +def array_42(arr) + return arr.include?(42) +end + +def magic_array(arr) + arr = arr.flatten.uniq.sort.delete_if{|a| a % 3 == 0 && a != 0}.collect {|n| n * 2} +end \ No newline at end of file diff --git a/lib/04_simon_says.rb b/lib/04_simon_says.rb new file mode 100644 index 00000000..6275eca2 --- /dev/null +++ b/lib/04_simon_says.rb @@ -0,0 +1,36 @@ +def echo(s) + return s +end + +def shout(s) + return s.upcase +end + +def repeat(s, x=2) + copy = s + x -= 1 + x.times do + s += " " + copy + end + return s +end + +def start_of_word(s, x) + return s[0..x-1] +end + +def first_word(s) + return s.split(" ")[0] +end + +def titleize(s) + little_words = ["and", "the", "or"] + arr = s.split(/(\W)/) + arr.each do |n| + if little_words.any?(n) == false + n[0] = n[0].upcase + end + end + arr[0][0] = arr[0][0].upcase + return arr.join +end diff --git a/lib/05_timer.rb b/lib/05_timer.rb new file mode 100644 index 00000000..6fb1cf92 --- /dev/null +++ b/lib/05_timer.rb @@ -0,0 +1,3 @@ +def time_string(time) + return Time.at(time).utc.strftime("%H:%M:%S") +end \ No newline at end of file diff --git a/lib/06_pig_latin.rb b/lib/06_pig_latin.rb new file mode 100644 index 00000000..516fc1eb --- /dev/null +++ b/lib/06_pig_latin.rb @@ -0,0 +1,31 @@ +def translate(s) + arr = s.split(" ") + arr.each do |mot| + if mot[0].match(/a|e|i|o|u/i) + mot << "ay" + next + end + if mot[0..2] == "sch" + mot[0..2] = '' + mot << "schay" + next + end + if mot[0..1] == "qu" + mot[0..1] = '' + mot << "quay" + next + end + if mot[1..2] == "qu" + mot << mot[0] + "quay" + mot[0..2] = '' + next + end + cons="" + while mot[0].match(/\A[^aeiou]/i) + mot << mot[0] + mot[0] = '' + end + mot << "ay" + end + return arr.join(" ") +end \ No newline at end of file diff --git a/spec/02_calculator_spec.rb b/spec/02_calculator_spec.rb index 08ed2d7f..e54fba00 100644 --- a/spec/02_calculator_spec.rb +++ b/spec/02_calculator_spec.rb @@ -47,23 +47,36 @@ expect(multiply(3, 4)).to eq(12) end - it "multiplies two other numbers" - # one other test here, don't forget do end ;) - - it "multiplies two other numbers, one of them being 0" - # one last test, with 0 in it + it "multiplies two other numbers" do + expect(multiply(1, 2)).to eq(2) + end + it "multiplies two other numbers, one of them being 0" do + expect(multiply(0, 5)).to eq(0) + end end describe "#power" do - it "raises one number to the power of another number" + it "raises one number to the power of another number" do + expect(power(5, 3)).to eq(125) + end end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + expect(factorial(0)).to eq(1) + end + it "computes the factorial of 1" do + expect(factorial(1)).to eq(1) + end + it "computes the factorial of 2" do + expect(factorial(2)).to eq(2) + end + it "computes the factorial of 5" do + expect(factorial(5)).to eq(120) + end + it "computes the factorial of 10" do + expect(factorial(10)).to eq(3628800) + end end