Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test task #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions spec/test_sceleton_spec.rb.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# frozen_string_literal: true

require_relative '../test_skeleton.rb'

RSpec.describe TestSkeleton do
let(:suite) { described_class.new }

describe '#even_or_odd' do
context 'when number is given' do
it 'should return word "even" or "odd"' do
expect(suite.even_or_odd(0)).to eq('even')
expect(suite.even_or_odd(121)).to eq('odd')
expect(suite.even_or_odd(-5)).to eq('odd')
expect(suite.even_or_odd(44)).to eq('even')
end
end
end

describe '#reverse_array' do
context 'when number is given' do
it 'should return reversed array of digits' do
expect(suite.reverse_array(12)).to eq([2, 1])
expect(suite.reverse_array(5896)).to eq([6, 9, 8, 5])
expect(suite.reverse_array(0)).to eq([0])
expect(suite.reverse_array(62854)).to eq([4, 5, 8, 2, 6])
end
end
end

describe '#high_and_low' do
context 'when string with numbers is given' do
it 'should return highest and lowest numbers' do
expect(suite.high_and_low('0 1 2 3 4 5')).to eq('5 0')
expect(suite.high_and_low('-3 -2 -1 0 1 2 3')).to eq('3 -3')
expect(suite.high_and_low('0')).to eq('0 0')
expect(suite.high_and_low('-3 -2 -1')).to eq('-1 -3')
end
end
end

describe '#my_languages' do
context 'when hash with string keys is given' do
it 'should return array of keys where value more than 60' do
#expect(suite.my_languages('Hindi' => 65, 'Greek' => 70, 'German' => 20)).to eq(%w[Greek Hindi])
expect(suite.my_languages('Japanese' => 68, 'Malasian' => 62, 'Chinese' => 61, 'Arabic' => 79)).to eq(%w[Arabic Japanese Malasian Chinese])
expect(suite.my_languages({})).to eq([])
expect(suite.my_languages('A' => 30, 'B' => 50, 'C' => 25)).to eq([])
end
end
end

describe '#remove_array_elements' do
context 'when two arrays is given' do
it 'should return array of numbers that not in second array' do
expect(suite.remove_array_elements([1, 2], [1])).to eq([2])
expect(suite.remove_array_elements([1, 1, 2, 5, 3, 3], [1, 3])).to eq([2, 5])
expect(suite.remove_array_elements([1, 2, 3, 4, nil, nil], [1, 2, nil])).to eq([3, 4])
end
end
end

describe '#consecutive_duplicates' do
context 'when string with words is given' do
it 'should return string where words not consecutive repeated' do
expect(suite.consecutive_duplicates('aa bb bb cc dd')).to eq('aa bb cc dd')
expect(suite.consecutive_duplicates('apple banana cherry')).to eq('apple banana cherry')
expect(suite.consecutive_duplicates('aaa aaa aaa bbb bbb bbb aaa aaa ccc ccc ddd')).to eq('aaa bbb aaa ccc ddd')
end
end
end

describe '#middle_chars' do
context 'when string is given' do
it 'should return middle character(s) of string' do
expect(suite.middle_chars('A')).to eq('A')
expect(suite.middle_chars('Cherry')).to eq('er')
expect(suite.middle_chars('push')).to eq('us')
expect(suite.middle_chars('Do Androids Dream of Electric Sheep?')).to eq(' o')
end
end
end

end
35 changes: 27 additions & 8 deletions test_skeleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ class TestSkeleton
# TestSkeleton.new.even_or_odd(0) should return "even"
# TestSkeleton.new.even_or_odd(-42) should return "even"
def even_or_odd(number)
# Your solution should be here
if number % 2 == 0
"even"
else
"odd"
end
end

# https://www.codewars.com/kata/5583090cbe83f4fd8c000051
Expand All @@ -21,7 +25,7 @@ def even_or_odd(number)
# TestSkeleton.new.reverse_array(348597) should return [7,9,5,8,4,3]
# TestSkeleton.new.reverse_array(0) should return [0]
def reverse_array(number)
# Your solution should be here
a = number.to_s.split(//).reverse.map{|x| x.to_i}
end

# https://www.codewars.com/kata/554b4ac871d6813a03000035
Expand All @@ -36,7 +40,8 @@ def reverse_array(number)
# There will always be at least one number in the input string.
# Output string must be two numbers separated by a single space, and highest number is first.
def high_and_low(test_string)
# Your solution should be here
a = test_string.split(" ").map{|x| x.to_i}
a.max.to_s + " " + a.min.to_s
end

# https://www.codewars.com/kata/5b16490986b6d336c900007d
Expand All @@ -49,7 +54,7 @@ def high_and_low(test_string)
# TestSkeleton.new.my_languages({"Hindi" => 60, "Dutch" => 93, "Greek" => 71}) should return ["Dutch", "Greek", "Hindi"]
# TestSkeleton.new.my_languages({"C++" => 50, "ASM" => 10, "Haskell" => 20}) should return []
def my_languages(hash)
# Your solution should be here
hash.select{|k,v| v>=60}.sort_by{|k, v| v}.reverse.to_h.keys
end

# https://www.codewars.com/kata/563089b9b7be03472d00002b
Expand All @@ -62,7 +67,7 @@ def my_languages(hash)
# values_list = [1, 3, 4, 2]
# TestSkeleton.new.remove_array_elements(integer_list, values_list) should return [5, 6 ,7 ,8]
def remove_array_elements(source_array, values_array)
# Your solution should be here
source_array.delete_if {|a| values_array.include? a}
end

# https://www.codewars.com/kata/5b39e91ee7a2c103300018b3
Expand All @@ -72,7 +77,15 @@ def remove_array_elements(source_array, values_array)
# string = "alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta"
# TestSkeleton.new.consecutive_duplicates(string) should return "alpha beta gamma delta alpha beta gamma delta"
def consecutive_duplicates(string)
# Your solution should be here
s = string.split(" ")
a = Array.new
a.append(s[0])
for i in 1..s.length-1
if (s[i] != s[i-1])
a.append(s[i])
end
end
a.join(" ")
end

# https://www.codewars.com/kata/56747fd5cb988479af000028
Expand All @@ -90,6 +103,12 @@ def consecutive_duplicates(string)
# Output:
# The middle character(s) of the word represented as a string.
def middle_chars(test_string)
# Your solution should be here
m = test_string.length/2
if test_string.length%2 ==0
mid = test_string[m-1] + test_string[m]
else
mid = test_string[m]
end
end
end

end
114 changes: 114 additions & 0 deletions test_skeleton.rb.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# frozen_string_literal: true

class TestSkeleton
# https://www.codewars.com/kata/53da3dbb4a5168369a0000fe
# Create a function that takes an integer as an argument and returns "Even"
# for even numbers or "Odd" for odd numbers.
# Example:
# TestSkeleton.new.even_or_odd(1) should return "odd"
# TestSkeleton.new.even_or_odd(2) should return "even"
# TestSkeleton.new.even_or_odd(0) should return "even"
# TestSkeleton.new.even_or_odd(-42) should return "even"
def even_or_odd(number)
if number % 2 == 0
"even"
else
"odd"
end
end

# https://www.codewars.com/kata/5583090cbe83f4fd8c000051
# Convert number to reversed array of digits
# Given a random non-negative number, you have to return the digits of this
# number within an array in reverse order.
# Examples:
# TestSkeleton.new.reverse_array(348597) should return [7,9,5,8,4,3]
# TestSkeleton.new.reverse_array(0) should return [0]
def reverse_array(number)
a = number.to_s.split(//).reverse.map{|x| x.to_i}
end

# https://www.codewars.com/kata/554b4ac871d6813a03000035
# In this little assignment you are given a string of space separated numbers,
# and have to return the highest and lowest number.
# Examples:
# TestSkeleton.new.high_and_low("1 2 3 4 5") should return "5 1"
# TestSkeleton.new.high_and_low("1 2 -3 4 5") should return "5 -3"
# TestSkeleton.new.high_and_low("1 9 3 4 -5") should return "9 -5"
# Notes
# All numbers are valid Int32, no need to validate them.
# There will always be at least one number in the input string.
# Output string must be two numbers separated by a single space, and highest number is first.
def high_and_low(test_string)
a = test_string.to_s.split(" ").map{|x| x.to_i}
a.max.to_s + " " + a.min.to_s
end

# https://www.codewars.com/kata/5b16490986b6d336c900007d
# You are given a dictionary/hash/object containing some languages and your test results in the
# given languages. Return the list of languages where your test score is at least 60,
# in descending order of the results.
# Note: the scores will always be unique (so no duplicate values)
# Examples:
# TestSkeleton.new.my_languages({"Java" => 10, "Ruby" => 80, "Python" => 65}) should return ["Ruby", "Python"]
# TestSkeleton.new.my_languages({"Hindi" => 60, "Dutch" => 93, "Greek" => 71}) should return ["Dutch", "Greek", "Hindi"]
# TestSkeleton.new.my_languages({"C++" => 50, "ASM" => 10, "Haskell" => 20}) should return []
def my_languages(hash)
hash.select{|k,v| v>=60}.sort_by{|k, v| v}.reverse.to_h.keys
end

# https://www.codewars.com/kata/563089b9b7be03472d00002b
# Define a method/function that removes from a given array of integers all the values contained in a second array.
# Examples:
# integer_list = [1, 1, 2 ,3 ,1 ,2 ,3 ,4]
# values_list = [1, 3]
# TestSkeleton.new.remove_array_elements(integer_list, values_list) should return [2, 2, 4]
# integer_list = [1, 1, 2 ,3 ,1 ,2 ,3 ,4, 4, 3 ,5, 6, 7, 2, 8]
# values_list = [1, 3, 4, 2]
# TestSkeleton.new.remove_array_elements(integer_list, values_list) should return [5, 6 ,7 ,8]
def remove_array_elements(source_array, values_array)
source_array.delete_if {|a| values_array.include? a}
end

# https://www.codewars.com/kata/5b39e91ee7a2c103300018b3
# Your task is to remove all consecutive duplicate words from a string,
# leaving only first words entries. Words would be separated by space
# Examples:
# string = "alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta"
# TestSkeleton.new.consecutive_duplicates(string) should return "alpha beta gamma delta alpha beta gamma delta"
def consecutive_duplicates(string)
s = string.split(" ")
a = Array.new
a.append(s[0])
for i in 1..s.length-1
if (s[i] != s[i-1])
a.append(s[i])
end
end
a.join(" ")
end

# https://www.codewars.com/kata/56747fd5cb988479af000028
# You are going to be given a word. Your job is to return the middle character of the word.
# If the word's length is odd, return the middle character. If the word's length is even,
# return the middle 2 characters.
# Examples:
# TestSkeleton.new.middle_chars("test") should return "es"
# TestSkeleton.new.middle_chars("testing") should return "t"
# TestSkeleton.new.middle_chars("middle") should return "dd"
# TestSkeleton.new.middle_chars("A") should return "A"
# Input:
# A word (string) of length 0 < str < 1000
# You do not need to test for this.
# Output:
# The middle character(s) of the word represented as a string.
def middle_chars(test_string)
m = test_string.length/2
if test_string.length%2 ==0
mid = test_string[m-1] + test_string[m]
else
mid = test_string[m]
end
end

end