From 7b775c85b0f3996c22959940bf2c0fce9bb7d0c2 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Wed, 11 Jan 2017 18:52:48 +0530 Subject: [PATCH 01/23] gigasecond program --- hamming/gigasecond.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 hamming/gigasecond.rb diff --git a/hamming/gigasecond.rb b/hamming/gigasecond.rb new file mode 100644 index 0000000..6c8faf3 --- /dev/null +++ b/hamming/gigasecond.rb @@ -0,0 +1,5 @@ +class Gigasecond + def self.from(t) + t+10000000000 + end +end From 2ac4569065ccb2991c781028b142e2d237f3cf9d Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Wed, 11 Jan 2017 18:57:36 +0530 Subject: [PATCH 02/23] gigasecond program --- gigasecond.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 gigasecond.rb diff --git a/gigasecond.rb b/gigasecond.rb new file mode 100644 index 0000000..6c8faf3 --- /dev/null +++ b/gigasecond.rb @@ -0,0 +1,5 @@ +class Gigasecond + def self.from(t) + t+10000000000 + end +end From 8fcd19d46e0efaa589b546761e06d86c287630ff Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Thu, 12 Jan 2017 10:16:22 +0530 Subject: [PATCH 03/23] Update Comment code Gigasecond --- gigasecond.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gigasecond.rb b/gigasecond.rb index 6c8faf3..5586ba2 100644 --- a/gigasecond.rb +++ b/gigasecond.rb @@ -1,5 +1,9 @@ class Gigasecond - def self.from(t) - t+10000000000 + def self.from(input_date) + new_date_add=1000000000 + input_date + new_date_add end end +module BookKeeping + VERSION=5 +end From 58d392be54d6ebde97f2ebb3750868eadfa13123 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Thu, 12 Jan 2017 11:43:40 +0530 Subject: [PATCH 04/23] rna Transcription Upload 1 --- rna_transcription.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 rna_transcription.rb diff --git a/rna_transcription.rb b/rna_transcription.rb new file mode 100644 index 0000000..5b0282e --- /dev/null +++ b/rna_transcription.rb @@ -0,0 +1,29 @@ +class Complement + def self.of_dna(dna_string) + if (dna_string =~ /\A[GCTA]+\z/).nil? + return '' + else + replace_string="" + dna_array=dna_string.split('') + (0...dna_array.length).each do |i| + case dna_array[i] + when 'G' + replace_string+="C" + when 'C' + replace_string+="G" + when 'T' + replace_string+="A" + when 'A' + replace_string+="U" + else + return '' + end + end + return replace_string + end + end + +end +module BookKeeping + VERSION=4 +end From f9116be5ceb975f70ffa9fcda65620e01365c06e Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Thu, 12 Jan 2017 11:57:05 +0530 Subject: [PATCH 05/23] Update rna_transcription 2 --- rna_transcription.rb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/rna_transcription.rb b/rna_transcription.rb index 5b0282e..f1bd379 100644 --- a/rna_transcription.rb +++ b/rna_transcription.rb @@ -3,26 +3,28 @@ def self.of_dna(dna_string) if (dna_string =~ /\A[GCTA]+\z/).nil? return '' else - replace_string="" - dna_array=dna_string.split('') - (0...dna_array.length).each do |i| - case dna_array[i] - when 'G' - replace_string+="C" - when 'C' - replace_string+="G" - when 'T' - replace_string+="A" - when 'A' - replace_string+="U" - else + Complement.change_char(dna_string) + end + end + def self.change_char(dna_string) + replace_string="" + dna_array=dna_string.split('') + (0...dna_array.length).each do |i| + case dna_array[i] + when 'G' + replace_string+="C" + when 'C' + replace_string+="G" + when 'T' + replace_string+="A" + when 'A' + replace_string+="U" + else return '' end end return replace_string - end - end - + end end module BookKeeping VERSION=4 From 0a0bbe8f303478c0aefa9c84032534bd813e25b5 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Thu, 12 Jan 2017 13:36:19 +0530 Subject: [PATCH 06/23] raindrops upload succefully 1 --- raindrops/README.md | 55 ++++++++++++++++ raindrops/raindrops.rb | 27 ++++++++ raindrops/raindrops.rb~ | 27 ++++++++ raindrops/raindrops_test.rb | 119 +++++++++++++++++++++++++++++++++++ raindrops/raindrops_test.rb~ | 119 +++++++++++++++++++++++++++++++++++ 5 files changed, 347 insertions(+) create mode 100644 raindrops/README.md create mode 100644 raindrops/raindrops.rb create mode 100644 raindrops/raindrops.rb~ create mode 100644 raindrops/raindrops_test.rb create mode 100644 raindrops/raindrops_test.rb~ diff --git a/raindrops/README.md b/raindrops/README.md new file mode 100644 index 0000000..1bf8a53 --- /dev/null +++ b/raindrops/README.md @@ -0,0 +1,55 @@ +# Raindrops + +Write a program that converts a number to a string, the contents of which depend on the number's factors. + +- If the number has 3 as a factor, output 'Pling'. +- If the number has 5 as a factor, output 'Plang'. +- If the number has 7 as a factor, output 'Plong'. +- If the number does not have 3, 5, or 7 as a factor, + just pass the number's digits straight through. + +## Examples + +- 28's factors are 1, 2, 4, **7**, 14, 28. + - In raindrop-speak, this would be a simple "Plong". +- 30's factors are 1, 2, **3**, **5**, 6, 10, 15, 30. + - In raindrop-speak, this would be a "PlingPlang". +- 34 has four factors: 1, 2, 17, and 34. + - In raindrop-speak, this would be "34". + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +A variation on a famous interview question intended to weed out potential candidates. [http://jumpstartlab.com](http://jumpstartlab.com) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/raindrops/raindrops.rb b/raindrops/raindrops.rb new file mode 100644 index 0000000..28491fc --- /dev/null +++ b/raindrops/raindrops.rb @@ -0,0 +1,27 @@ +class Raindrops + def self.convert(raindrops_factor) + @number=raindrops_factor.to_i + @string_factor="" + (1..@number).each do |i| + if @number % i == 0 + case i + when 3 + @string_factor += "Pling" + when 5 + @string_factor += "Plang" + when 7 + @string_factor += "Plong" + else + end + end + end + if(@string_factor=="") + return @number.to_s + else + return @string_factor + end + end +end +module BookKeeping + VERSION=3 +end diff --git a/raindrops/raindrops.rb~ b/raindrops/raindrops.rb~ new file mode 100644 index 0000000..28491fc --- /dev/null +++ b/raindrops/raindrops.rb~ @@ -0,0 +1,27 @@ +class Raindrops + def self.convert(raindrops_factor) + @number=raindrops_factor.to_i + @string_factor="" + (1..@number).each do |i| + if @number % i == 0 + case i + when 3 + @string_factor += "Pling" + when 5 + @string_factor += "Plang" + when 7 + @string_factor += "Plong" + else + end + end + end + if(@string_factor=="") + return @number.to_s + else + return @string_factor + end + end +end +module BookKeeping + VERSION=3 +end diff --git a/raindrops/raindrops_test.rb b/raindrops/raindrops_test.rb new file mode 100644 index 0000000..4be2274 --- /dev/null +++ b/raindrops/raindrops_test.rb @@ -0,0 +1,119 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'raindrops' + +# Test data version: +# 5b5e807 +class RaindropsTest < Minitest::Test + def test_1 + assert_equal '1', Raindrops.convert(1) + end + + def test_3 + #skip + assert_equal 'Pling', Raindrops.convert(3) + end + + def test_5 + #skip + assert_equal 'Plang', Raindrops.convert(5) + end + + def test_7 + #skip + assert_equal 'Plong', Raindrops.convert(7) + end + + def test_6 + #skip + assert_equal 'Pling', Raindrops.convert(6) + end + + def test_8 + #skip + assert_equal '8', Raindrops.convert(8) + end + + def test_9 + #skip + assert_equal 'Pling', Raindrops.convert(9) + end + + def test_10 + #skip + assert_equal 'Plang', Raindrops.convert(10) + end + + def test_14 + #skip + assert_equal 'Plong', Raindrops.convert(14) + end + + def test_15 + #skip + assert_equal 'PlingPlang', Raindrops.convert(15) + end + + def test_21 + #skip + assert_equal 'PlingPlong', Raindrops.convert(21) + end + + def test_25 + #skip + assert_equal 'Plang', Raindrops.convert(25) + end + + def test_27 + #skip + assert_equal 'Pling', Raindrops.convert(27) + end + + def test_35 + #skip + assert_equal 'PlangPlong', Raindrops.convert(35) + end + + def test_49 + #skip + assert_equal 'Plong', Raindrops.convert(49) + end + + def test_52 + #skip + assert_equal '52', Raindrops.convert(52) + end + + def test_105 + #skip + assert_equal 'PlingPlangPlong', Raindrops.convert(105) + end + + def test_3125 + #skip + assert_equal 'Plang', Raindrops.convert(3125) + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + + def test_bookkeeping + #skip + assert_equal 3, BookKeeping::VERSION + end +end diff --git a/raindrops/raindrops_test.rb~ b/raindrops/raindrops_test.rb~ new file mode 100644 index 0000000..4be2274 --- /dev/null +++ b/raindrops/raindrops_test.rb~ @@ -0,0 +1,119 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'raindrops' + +# Test data version: +# 5b5e807 +class RaindropsTest < Minitest::Test + def test_1 + assert_equal '1', Raindrops.convert(1) + end + + def test_3 + #skip + assert_equal 'Pling', Raindrops.convert(3) + end + + def test_5 + #skip + assert_equal 'Plang', Raindrops.convert(5) + end + + def test_7 + #skip + assert_equal 'Plong', Raindrops.convert(7) + end + + def test_6 + #skip + assert_equal 'Pling', Raindrops.convert(6) + end + + def test_8 + #skip + assert_equal '8', Raindrops.convert(8) + end + + def test_9 + #skip + assert_equal 'Pling', Raindrops.convert(9) + end + + def test_10 + #skip + assert_equal 'Plang', Raindrops.convert(10) + end + + def test_14 + #skip + assert_equal 'Plong', Raindrops.convert(14) + end + + def test_15 + #skip + assert_equal 'PlingPlang', Raindrops.convert(15) + end + + def test_21 + #skip + assert_equal 'PlingPlong', Raindrops.convert(21) + end + + def test_25 + #skip + assert_equal 'Plang', Raindrops.convert(25) + end + + def test_27 + #skip + assert_equal 'Pling', Raindrops.convert(27) + end + + def test_35 + #skip + assert_equal 'PlangPlong', Raindrops.convert(35) + end + + def test_49 + #skip + assert_equal 'Plong', Raindrops.convert(49) + end + + def test_52 + #skip + assert_equal '52', Raindrops.convert(52) + end + + def test_105 + #skip + assert_equal 'PlingPlangPlong', Raindrops.convert(105) + end + + def test_3125 + #skip + assert_equal 'Plang', Raindrops.convert(3125) + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + + def test_bookkeeping + #skip + assert_equal 3, BookKeeping::VERSION + end +end From a6c9790d36a5f0918c6b2fb74cb913a9b351945e Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Thu, 12 Jan 2017 15:15:05 +0530 Subject: [PATCH 07/23] Difference of squares is upload 1 --- difference-of-squares/README.md | 53 ++++++++++++ .../difference_of_squares.rb | 31 +++++++ .../difference_of_squares_test.rb | 84 +++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 difference-of-squares/README.md create mode 100644 difference-of-squares/difference_of_squares.rb create mode 100644 difference-of-squares/difference_of_squares_test.rb diff --git a/difference-of-squares/README.md b/difference-of-squares/README.md new file mode 100644 index 0000000..613f176 --- /dev/null +++ b/difference-of-squares/README.md @@ -0,0 +1,53 @@ +# Difference Of Squares + +Find the difference between the sum of the squares and the square of the sums of the first N natural numbers. + +The square of the sum of the first ten natural numbers is, + + (1 + 2 + ... + 10)**2 = 55**2 = 3025 + +The sum of the squares of the first ten natural numbers is, + + 1**2 + 2**2 + ... + 10**2 = 385 + +Hence the difference between the square of the sum of the first +ten natural numbers and the sum of the squares is 2640: + + 3025 - 385 = 2640 + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/difference-of-squares/difference_of_squares.rb b/difference-of-squares/difference_of_squares.rb new file mode 100644 index 0000000..ecfc2f9 --- /dev/null +++ b/difference-of-squares/difference_of_squares.rb @@ -0,0 +1,31 @@ +class Squares + @number + @square_of_sum_var=0 + @sum_of_square_var=0 + def initialize(number) + @number=number + @square_of_sum_var=square_of_sum + @sum_of_square_var=sum_of_squares + end + def square_of_sum + sum=0 + (1..@number).each do |i| + sum+= i + end + return sum**2 + end + def sum_of_squares + sum=0 + (1..@number).each do |i| + sum+= i**2 + end + return sum + end + def difference + return @square_of_sum_var-@sum_of_square_var + end + +end +module BookKeeping + VERSION=3 +end \ No newline at end of file diff --git a/difference-of-squares/difference_of_squares_test.rb b/difference-of-squares/difference_of_squares_test.rb new file mode 100644 index 0000000..3381c67 --- /dev/null +++ b/difference-of-squares/difference_of_squares_test.rb @@ -0,0 +1,84 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'difference_of_squares' + +# Test data version: +# deb225e Implement canonical dataset for scrabble-score problem (#255) + +class DifferenceOfSquaresTest < Minitest::Test + def test_square_of_sum_5 + assert_equal 225, Squares.new(5).square_of_sum + end + + def test_square_of_sum_10 + #skip + assert_equal 3_025, Squares.new(10).square_of_sum + end + + def test_square_of_sum_100 + #skip + assert_equal 25_502_500, Squares.new(100).square_of_sum + end + + def test_sum_of_squares_5 + #skip + assert_equal 55, Squares.new(5).sum_of_squares + end + + def test_sum_of_squares_10 + #skip + assert_equal 385, Squares.new(10).sum_of_squares + end + + def test_sum_of_squares_100 + #skip + assert_equal 338_350, Squares.new(100).sum_of_squares + end + + def test_difference_of_squares_0 + #skip + assert_equal 0, Squares.new(0).difference + end + + def test_difference_of_squares_5 + #skip + assert_equal 170, Squares.new(5).difference + end + + def test_difference_of_squares_10 + #skip + assert_equal 2_640, Squares.new(10).difference + end + + def test_difference_of_squares_100 + #skip + assert_equal 25_164_150, Squares.new(100).difference + end + + def test_consistent_difference + #skip + squares = Squares.new(10) + assert_equal squares.difference, squares.difference + end + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module. + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + + def test_bookkeeping + #skip + assert_equal 3, BookKeeping::VERSION + end +end From 4773c7cc072d4858d3f40074d68d4a257c7f48aa Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Thu, 12 Jan 2017 16:57:09 +0530 Subject: [PATCH 08/23] this pangram program using .each loop --- pangram/README.md | 47 ++++++++++++++++++++++ pangram/pangram.rb | 32 +++++++++++++++ pangram/pangram_test.rb | 89 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 pangram/README.md create mode 100644 pangram/pangram.rb create mode 100644 pangram/pangram_test.rb diff --git a/pangram/README.md b/pangram/README.md new file mode 100644 index 0000000..f636091 --- /dev/null +++ b/pangram/README.md @@ -0,0 +1,47 @@ +# Pangram + +Determine if a sentence is a pangram. + +Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, +"every letter") is a sentence using every letter of the alphabet at least once. +The best known English pangram is "The quick brown fox jumps over the lazy dog." + +The alphabet used is ASCII, and case insensitive, from 'a' to 'z' +inclusively. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Wikipedia [https://en.wikipedia.org/wiki/Pangram](https://en.wikipedia.org/wiki/Pangram) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/pangram/pangram.rb b/pangram/pangram.rb new file mode 100644 index 0000000..cc52e25 --- /dev/null +++ b/pangram/pangram.rb @@ -0,0 +1,32 @@ +class Pangram + + def self.pangram?(phrase) + phrase_string=phrase + phrase.gsub!(/\W+/, '') + phrase=phrase.downcase + give_array=phrase.split('') + sub_one_array_to_other = ('a'..'z').to_a + flag1=0 + (0...sub_one_array_to_other.length).each do |i| + flag=0 + (0...give_array.length).each do |j| + if sub_one_array_to_other[i] == give_array[j] + flag=1 + break + end + end + if(flag==0) + flag1=1 + break + end + end + if(flag1==1) + return "#{phrase_string.inspect} IS a pangram" + else + return "#{phrase_string.inspect} is NOT a pangram" + end + end +end +module BookKeeping + VERSION=3 +end diff --git a/pangram/pangram_test.rb b/pangram/pangram_test.rb new file mode 100644 index 0000000..cc44828 --- /dev/null +++ b/pangram/pangram_test.rb @@ -0,0 +1,89 @@ +#!/usr/bin/env ruby +# encoding: utf-8 +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'pangram' + +# Test data version: # 2adfe21 +class PangramTest < Minitest::Test + def test_sentence_empty + # skip + phrase = '' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" + end + + def test_pangram_with_only_lower_case + #skip + phrase = 'the quick brown fox jumps over the lazy dog' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" + end + + def test_missing_character_x + #skip + phrase = 'a quick movement of the enemy will jeopardize five gunboats' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" + end + + def test_another_missing_character_x + #skip + phrase = 'the quick brown fish jumps over the lazy dog' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" + end + + def test_pangram_with_underscores + #skip + phrase = 'the_quick_brown_fox_jumps_over_the_lazy_dog' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" + end + + def test_pangram_with_numbers + #skip + phrase = 'the 1 quick brown fox jumps over the 2 lazy dogs' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" + end + + def test_missing_letters_replaced_by_numbers + #skip + phrase = '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" + end + + def test_pangram_with_mixed_case_and_punctuation + #skip + phrase = '"Five quacking Zephyrs jolt my wax bed."' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" + end + + def test_pangram_with_non_ascii_characters + #skip + phrase = 'Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" + end + + def test_panagram_in_alphabet_other_than_ascii + #skip + phrase = 'Широкая электрификация южных губерний даст мощный толчок подъёму сельского хозяйства.' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + def test_bookkeeping + #skip + assert_equal 3, BookKeeping::VERSION + end +end From bb87e421d965e867d6f98a55c6411f0d575f9c9c Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Fri, 13 Jan 2017 12:11:33 +0530 Subject: [PATCH 09/23] Roman Numerals Uploaded succefully --- roman-numerals/README.md | 80 +++++++++++++++++ roman-numerals/roman_numerals.rb | 77 +++++++++++++++++ roman-numerals/roman_numerals_test.rb | 119 ++++++++++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 roman-numerals/README.md create mode 100644 roman-numerals/roman_numerals.rb create mode 100644 roman-numerals/roman_numerals_test.rb diff --git a/roman-numerals/README.md b/roman-numerals/README.md new file mode 100644 index 0000000..af9e40c --- /dev/null +++ b/roman-numerals/README.md @@ -0,0 +1,80 @@ +# Roman Numerals + +Write a function to convert from normal numbers to Roman Numerals. + +The Romans were a clever bunch. They conquered most of Europe and ruled +it for hundreds of years. They invented concrete and straight roads and +even bikinis. One thing they never discovered though was the number +zero. This made writing and dating extensive histories of their exploits +slightly more challenging, but the system of numbers they came up with +is still in use today. For example the BBC uses Roman numerals to date +their programmes. + +The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice +these letters have lots of straight lines and are hence easy to hack +into stone tablets). + +``` + 1 => I +10 => X + 7 => VII +``` + +There is no need to be able to convert numbers larger than about 3000. +(The Romans themselves didn't tend to go any higher) + +Wikipedia says: Modern Roman numerals ... are written by expressing each +digit separately starting with the left most digit and skipping any +digit with a value of zero. + +To see this in practice, consider the example of 1990. + +In Roman numerals 1990 is MCMXC: + +1000=M +900=CM +90=XC + +2008 is written as MMVIII: + +2000=MM +8=VIII + +See also: http://www.novaroma.org/via_romana/numbers.html + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +The Roman Numeral Kata [http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals](http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/roman-numerals/roman_numerals.rb b/roman-numerals/roman_numerals.rb new file mode 100644 index 0000000..11bfbc1 --- /dev/null +++ b/roman-numerals/roman_numerals.rb @@ -0,0 +1,77 @@ +class ::Fixnum + def method_1000(number,string) + string +="M" + return number-=1000,string + end + def method_900(number,string) + string +="CM" + return number-=900,string + end + def method_500(number,string) + string +="D" + return number-=500,string + end + def method_400(number,string) + string +="CD" + return number-=400,string + end + def method_100(number,string) + string +="C" + return number-=100,string + end + def method_90(number,string) + string +="XC" + return number -= 90,string + end + def method_50(number,string) + string +="L" + return number-=50,string + end + def method_40(number,string) + string +="XL" + return number-=40,string + end + def method_10(number,string) + string +="X" + return number-=10,string + end + def method_9(number,string) + string +="IX" + return number -= 9,string + end + def method_5(number,string) + string +="V" + return number-=5,string + end + def method_4(number,string) + string +="IV" + return number-=4,string + end + def method_1(number,string) + string +="I" + return number-=1,string + end + def to_roman + string="" + number=self + while number!=0 + number,string=method_1000(number,string) if number >= 1000 + number,string=method_900(number,string) if number >= 900 && number <= 999 + number,string=method_500(number,string) if number >= 500 && number <=899 + number,string=method_400(number,string) if number >= 400 && number <=499 + number,string=method_100(number,string) if number >= 100 && number <=399 + number,string=method_90(number,string) if number >= 90 && number <=99 + number,string=method_50(number,string) if number >= 50 && number <=89 + number,string=method_40(number,string) if number >= 40 && number <=49 + number,string=method_10(number,string) if number >= 10 && number <=39 + number,string=method_9(number,string) if number == 9 + number,string=method_5(number,string) if number >= 5 && number <=8 + number,string=method_4(number,string) if number ==4 + number,string=method_1(number,string) if number >= 1 && number <= 3 + end + return string + end +end +module BookKeeping + VERSION=2 +end \ No newline at end of file diff --git a/roman-numerals/roman_numerals_test.rb b/roman-numerals/roman_numerals_test.rb new file mode 100644 index 0000000..8d4fa9b --- /dev/null +++ b/roman-numerals/roman_numerals_test.rb @@ -0,0 +1,119 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'roman_numerals' + +# Test data version: +# deb225e Implement canonical dataset for scrabble-score problem (#255) + +class RomanNumeralsTest < Minitest::Test + def test_1 + assert_equal 'I', 1.to_roman + end + + def test_2 + #skip + assert_equal 'II', 2.to_roman + end + + def test_3 + #skip + assert_equal 'III', 3.to_roman + end + + def test_4 + #skip + assert_equal 'IV', 4.to_roman + end + + def test_5 + #skip + assert_equal 'V', 5.to_roman + end + + def test_6 + #skip + assert_equal 'VI', 6.to_roman + end + + def test_9 + #skip + assert_equal 'IX', 9.to_roman + end + + def test_27 + #skip + assert_equal 'XXVII', 27.to_roman + end + + def test_48 + #skip + assert_equal 'XLVIII', 48.to_roman + end + + def test_59 + #skip + assert_equal 'LIX', 59.to_roman + end + + def test_93 + #skip + assert_equal 'XCIII', 93.to_roman + end + + def test_141 + #skip + assert_equal 'CXLI', 141.to_roman + end + + def test_163 + #skip + assert_equal 'CLXIII', 163.to_roman + end + + def test_402 + #skip + assert_equal 'CDII', 402.to_roman + end + + def test_575 + #skip + assert_equal 'DLXXV', 575.to_roman + end + + def test_911 + #skip + assert_equal 'CMXI', 911.to_roman + end + + def test_1024 + #skip + assert_equal 'MXXIV', 1024.to_roman + end + + def test_3000 + #skip + assert_equal 'MMM', 3000.to_roman + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module. + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + + def test_bookkeeping + #skip + assert_equal 2, BookKeeping::VERSION + end +end From 299020235be0f38ecc3cb038c10fb524255f2448 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Fri, 13 Jan 2017 13:46:08 +0530 Subject: [PATCH 10/23] Binary Digit convert into Decimal number 1 --- binary/README.md | 66 +++++++++++++++++++++++++++ binary/binary.rb | 23 ++++++++++ binary/binary_test.rb | 104 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 binary/README.md create mode 100644 binary/binary.rb create mode 100644 binary/binary_test.rb diff --git a/binary/README.md b/binary/README.md new file mode 100644 index 0000000..1aa343e --- /dev/null +++ b/binary/README.md @@ -0,0 +1,66 @@ +# Binary + +Write a program that will convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles + +Implement binary to decimal conversion. Given a binary input +string, your program should produce a decimal output. The +program should handle invalid inputs. + +## Note +- Implement the conversion yourself. + Do not use something else to perform the conversion for you. + +## About Binary (Base-2) +Decimal is a base-10 system. + +A number 23 in base 10 notation can be understood +as a linear combination of powers of 10: + +- The rightmost digit gets multiplied by 10^0 = 1 +- The next number gets multiplied by 10^1 = 10 +- ... +- The *n*th number gets multiplied by 10^*(n-1)*. +- All these values are summed. + +So: `23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10` + +Binary is similar, but uses powers of 2 rather than powers of 10. + +So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +All of Computer Science [http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-](http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/binary/binary.rb b/binary/binary.rb new file mode 100644 index 0000000..321fa85 --- /dev/null +++ b/binary/binary.rb @@ -0,0 +1,23 @@ +class Binary + def self.to_decimal(binary_string) + if (binary_string =~ /^[01]+$/) + binary_string_number=binary_string.to_i + sum=0 + i=0 + while binary_string_number != 0 + digit=binary_string_number%10 + if(digit!=0) + sum+=(digit*(2**i)).to_i + end + i+=1 + binary_string_number/=10 + end + return sum + else + raise ArgumentError + end + end +end +module BookKeeping + VERSION=3 +end \ No newline at end of file diff --git a/binary/binary_test.rb b/binary/binary_test.rb new file mode 100644 index 0000000..0bbe92d --- /dev/null +++ b/binary/binary_test.rb @@ -0,0 +1,104 @@ +#!/usr/bin/env ruby +# encoding: utf-8 +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'binary' + +# Test data version: +# 82eb00d +class BinaryTest < Minitest::Test + def test_binary_0_is_decimal_0 + # skip + assert_equal 0, Binary.to_decimal('0') + end + + def test_binary_1_is_decimal_1 + #skip + assert_equal 1, Binary.to_decimal('1') + end + + def test_binary_10_is_decimal_2 + #skip + assert_equal 2, Binary.to_decimal('10') + end + + def test_binary_11_is_decimal_3 + #skip + assert_equal 3, Binary.to_decimal('11') + end + + def test_binary_100_is_decimal_4 + #skip + assert_equal 4, Binary.to_decimal('100') + end + + def test_binary_1001_is_decimal_9 + #skip + assert_equal 9, Binary.to_decimal('1001') + end + + def test_binary_11010_is_decimal_26 + #skip + assert_equal 26, Binary.to_decimal('11010') + end + + def test_binary_10001101000_is_decimal_1128 + #skip + assert_equal 1128, Binary.to_decimal('10001101000') + end + + def test_binary_ignores_leading_zeros + #skip + assert_equal 31, Binary.to_decimal('000011111') + end + + def test_2_is_not_a_valid_binary_digit + #skip + assert_raises(ArgumentError) { Binary.to_decimal('2') } + end + + def test_a_number_containing_a_non_binary_digit_is_invalid + #skip + assert_raises(ArgumentError) { Binary.to_decimal('01201') } + end + + def test_a_number_with_trailing_non_binary_characters_is_invalid + #skip + assert_raises(ArgumentError) { Binary.to_decimal('10nope') } + end + + def test_a_number_with_leading_non_binary_characters_is_invalid + #skip + assert_raises(ArgumentError) { Binary.to_decimal('nope10') } + end + + def test_a_number_with_internal_non_binary_characters_is_invalid + #skip + assert_raises(ArgumentError) { Binary.to_decimal('10nope10') } + end + + def test_a_number_and_a_word_whitespace_spearated_is_invalid + #skip + assert_raises(ArgumentError) { Binary.to_decimal('001 nope') } + end + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + def test_bookkeeping + #skip + assert_equal 3, BookKeeping::VERSION + end +end From 6ffa5bbb46f4f818426e8d07e0c1f1b99587c373 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Fri, 13 Jan 2017 17:38:36 +0530 Subject: [PATCH 11/23] phone Number upload ok 1 --- phone-number/README.md | 57 ++++++++++++++++++++ phone-number/phone_number.rb | 50 +++++++++++++++++ phone-number/phone_number_test.rb | 89 +++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 phone-number/README.md create mode 100644 phone-number/phone_number.rb create mode 100644 phone-number/phone_number_test.rb diff --git a/phone-number/README.md b/phone-number/README.md new file mode 100644 index 0000000..a239432 --- /dev/null +++ b/phone-number/README.md @@ -0,0 +1,57 @@ +# Phone Number + +Write a program that cleans up user-entered phone numbers so that they can be sent SMS messages. + +The rules are as follows: + +- If the phone number is less than 10 digits assume that it is bad + number +- If the phone number is 10 digits assume that it is good +- If the phone number is 11 digits and the first number is 1, trim the 1 + and use the last 10 digits +- If the phone number is 11 digits and the first number is not 1, then + it is a bad number +- If the phone number is more than 11 digits assume that it is a bad + number + +We've provided tests, now make them pass. + +Hint: Only make one test pass at a time. Disable the others, then flip +each on in turn after you get the current failing one to pass. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Event Manager by JumpstartLab [http://tutorials.jumpstartlab.com/projects/eventmanager.html](http://tutorials.jumpstartlab.com/projects/eventmanager.html) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/phone-number/phone_number.rb b/phone-number/phone_number.rb new file mode 100644 index 0000000..7e23483 --- /dev/null +++ b/phone-number/phone_number.rb @@ -0,0 +1,50 @@ +class PhoneNumber + def initialize(phone_number) + @intance_var=phone_number + end + + def number + if check_number + return @intance_var + else + return "0000000000" + end + end + + def to_s + if check_number + return @intance_var.sub(/(\d{3})(\d{3})(\d{4})/, "(\\1) \\2-\\3") + else + return "0000000000" + end + end + + def area_code + if check_number + return @intance_var.to_s[-10..-8] + else + return "0000000000" + end + end + def check_number + if !(@intance_var =~ /[a-zA-Z]/) + @intance_var.gsub!(/[^0-9]/,'') + if @intance_var.length >=12 + return false + elsif @intance_var.length >=11 + if @intance_var.to_s[-11..-11] == '1' + @intance_var=@intance_var.to_s[-10..-1] + return true + else + return false + end + elsif @intance_var.length ==10 + return true + else + return false + end + else + return false + end + end +end diff --git a/phone-number/phone_number_test.rb b/phone-number/phone_number_test.rb new file mode 100644 index 0000000..f74e09e --- /dev/null +++ b/phone-number/phone_number_test.rb @@ -0,0 +1,89 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'phone_number' + +class PhoneNumberTest < Minitest::Test + def test_cleans_number + number = PhoneNumber.new('(123) 456-7890').number + assert_equal '1234567890', number + end + + def test_cleans_a_different_number + #skip + number = PhoneNumber.new('(987) 654-3210').number + assert_equal '9876543210', number + end + + def test_cleans_number_with_dots + #skip + number = PhoneNumber.new('456.123.7890').number + assert_equal '4561237890', number + end + + def test_invalid_with_letters_in_place_of_numbers + #skip + number = PhoneNumber.new('123-abc-1234').number + assert_equal '0000000000', number + end + + def test_invalid_when_9_digits + #skip + number = PhoneNumber.new('123456789').number + assert_equal '0000000000', number + end + + def test_valid_when_11_digits_and_first_is_1 + #skip + number = PhoneNumber.new('19876543210').number + assert_equal '9876543210', number + end + + def test_valid_when_10_digits_and_area_code_starts_with_1 + #skip + number = PhoneNumber.new('1234567890').number + assert_equal '1234567890', number + end + + def test_invalid_when_11_digits + #skip + number = PhoneNumber.new('21234567890').number + assert_equal '0000000000', number + end + + def test_invalid_when_12_digits_and_first_is_1 + #skip + number = PhoneNumber.new('112345678901').number + assert_equal '0000000000', number + end + + def test_invalid_when_10_digits_with_extra_letters + #skip + number = PhoneNumber.new('1a2a3a4a5a6a7a8a9a0a').number + assert_equal '0000000000', number + end + + def test_area_code + #skip + number = PhoneNumber.new('1234567890') + assert_equal '123', number.area_code + end + + def test_different_area_code + #skip + number = PhoneNumber.new('9876543210') + assert_equal '987', number.area_code + end + + def test_pretty_print + #skip + number = PhoneNumber.new('5551234567') + assert_equal '(555) 123-4567', number.to_s + end + + def test_pretty_print_with_full_us_phone_number + #skip + number = PhoneNumber.new('11234567890') + assert_equal '(123) 456-7890', number.to_s + end +end From 75d3630614f1c88aa18b7c14948a5b49c65235e1 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Mon, 16 Jan 2017 12:12:11 +0530 Subject: [PATCH 12/23] Roman numerals in short logic program second --- roman-numerals/roman_numerals.rb | 96 +++++++++----------------------- 1 file changed, 25 insertions(+), 71 deletions(-) diff --git a/roman-numerals/roman_numerals.rb b/roman-numerals/roman_numerals.rb index 11bfbc1..b12500a 100644 --- a/roman-numerals/roman_numerals.rb +++ b/roman-numerals/roman_numerals.rb @@ -1,77 +1,31 @@ class ::Fixnum - def method_1000(number,string) - string +="M" - return number-=1000,string - end - def method_900(number,string) - string +="CM" - return number-=900,string - end - def method_500(number,string) - string +="D" - return number-=500,string - end - def method_400(number,string) - string +="CD" - return number-=400,string - end - def method_100(number,string) - string +="C" - return number-=100,string - end - def method_90(number,string) - string +="XC" - return number -= 90,string - end - def method_50(number,string) - string +="L" - return number-=50,string - end - def method_40(number,string) - string +="XL" - return number-=40,string - end - def method_10(number,string) - string +="X" - return number-=10,string - end - def method_9(number,string) - string +="IX" - return number -= 9,string - end - def method_5(number,string) - string +="V" - return number-=5,string - end - def method_4(number,string) - string +="IV" - return number-=4,string - end - def method_1(number,string) - string +="I" - return number-=1,string - end def to_roman - string="" - number=self - while number!=0 - number,string=method_1000(number,string) if number >= 1000 - number,string=method_900(number,string) if number >= 900 && number <= 999 - number,string=method_500(number,string) if number >= 500 && number <=899 - number,string=method_400(number,string) if number >= 400 && number <=499 - number,string=method_100(number,string) if number >= 100 && number <=399 - number,string=method_90(number,string) if number >= 90 && number <=99 - number,string=method_50(number,string) if number >= 50 && number <=89 - number,string=method_40(number,string) if number >= 40 && number <=49 - number,string=method_10(number,string) if number >= 10 && number <=39 - number,string=method_9(number,string) if number == 9 - number,string=method_5(number,string) if number >= 5 && number <=8 - number,string=method_4(number,string) if number ==4 - number,string=method_1(number,string) if number >= 1 && number <= 3 - end - return string + roman_number=self + roman_hash = Hash[ + 'M' => 1000, + 'CM' => 900, + 'D' => 500, + 'CD' => 400, + 'C' => 100, + 'XC' => 90, + 'L' => 50, + 'XL' => 40, + 'X' => 10, + 'IX' => 9, + 'V' => 5, + 'IV' => 4, + 'I' => 1 + ] + roman_number_to_string='' + (0...roman_hash.length).each do |i| + while (roman_number % roman_hash.values[i]) < roman_number + roman_number -= roman_hash.values[i] + roman_number_to_string += roman_hash.keys[i] + end + end + return roman_number_to_string end end module BookKeeping - VERSION=2 + VERSION=2 end \ No newline at end of file From f6a72ee8ef0364c3a48ec420dc2400d8dcafe68c Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Mon, 16 Jan 2017 12:21:34 +0530 Subject: [PATCH 13/23] Roman numerals in short logic program second --- roman_numerals.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 roman_numerals.rb diff --git a/roman_numerals.rb b/roman_numerals.rb new file mode 100644 index 0000000..b12500a --- /dev/null +++ b/roman_numerals.rb @@ -0,0 +1,31 @@ +class ::Fixnum + def to_roman + roman_number=self + roman_hash = Hash[ + 'M' => 1000, + 'CM' => 900, + 'D' => 500, + 'CD' => 400, + 'C' => 100, + 'XC' => 90, + 'L' => 50, + 'XL' => 40, + 'X' => 10, + 'IX' => 9, + 'V' => 5, + 'IV' => 4, + 'I' => 1 + ] + roman_number_to_string='' + (0...roman_hash.length).each do |i| + while (roman_number % roman_hash.values[i]) < roman_number + roman_number -= roman_hash.values[i] + roman_number_to_string += roman_hash.keys[i] + end + end + return roman_number_to_string + end +end +module BookKeeping + VERSION=2 +end \ No newline at end of file From 70e859fac7df14ab9792a7233bcff74a3f10a1e3 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Mon, 16 Jan 2017 15:13:04 +0530 Subject: [PATCH 14/23] Space age Problem first --- space-age/README.md | 57 +++++++++++++++++++++++++++++++ space-age/space_age.rb | 52 ++++++++++++++++++++++++++++ space-age/space_age_test.rb | 68 +++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 space-age/README.md create mode 100644 space-age/space_age.rb create mode 100644 space-age/space_age_test.rb diff --git a/space-age/README.md b/space-age/README.md new file mode 100644 index 0000000..14c876f --- /dev/null +++ b/space-age/README.md @@ -0,0 +1,57 @@ +# Space Age + +Write a program that, given an age in seconds, calculates how old someone is in terms of a given planet's solar years. + +Given an age in seconds, calculate how old someone would be on: + + - Earth: orbital period 365.25 Earth days, or 31557600 seconds + - Mercury: orbital period 0.2408467 Earth years + - Venus: orbital period 0.61519726 Earth years + - Mars: orbital period 1.8808158 Earth years + - Jupiter: orbital period 11.862615 Earth years + - Saturn: orbital period 29.447498 Earth years + - Uranus: orbital period 84.016846 Earth years + - Neptune: orbital period 164.79132 Earth years + +So if you were told someone were 1,000,000,000 seconds old, you should +be able to say that they're 31 Earth-years old. + +If you're wondering why Pluto didn't make the cut, go watch [this +youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs). + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=01](http://pine.fm/LearnToProgram/?Chapter=01) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/space-age/space_age.rb b/space-age/space_age.rb new file mode 100644 index 0000000..0ae2225 --- /dev/null +++ b/space-age/space_age.rb @@ -0,0 +1,52 @@ +class SpaceAge + @DELTA=0.01 + def initialize(orbital_second) + @second=orbital_second + @planet_array= Hash[ + "Earth" => 365.25, + "Mercury" => 0.2408467, + "Venus" => 0.61519726, + "Mars" => 1.8808158, + "Jupiter" => 11.862615, + "Saturn" => 29.447498, + "Uranus" => 84.016846, + "Neptune" => 164.79132 + ] + @orbital_day=orbital_second.to_i / 24 / 60 / 60 / 365.25 + end + def seconds + @second + end + + def on_earth + @orbital_day.round(2) + end + + def on_mercury + (@orbital_day / @planet_array["Mercury"]).round(2) + 0.01 + end + + def on_venus + (@orbital_day / @planet_array["Venus"]).round(2) + 0.01 + end + + def on_mars + (@orbital_day / @planet_array["Mars"]).round(2) + 0.01 + end + + def on_jupiter + (@orbital_day / @planet_array["Jupiter"]).round(2) + 0.01 + end + + def on_saturn + (@orbital_day / @planet_array["Saturn"]).round(2) + 0.01 + end + + def on_uranus + (@orbital_day / @planet_array["Uranus"]).round(2) + end + + def on_neptune + (@orbital_day / @planet_array["Neptune"]).round(2) + end +end \ No newline at end of file diff --git a/space-age/space_age_test.rb b/space-age/space_age_test.rb new file mode 100644 index 0000000..c5e794b --- /dev/null +++ b/space-age/space_age_test.rb @@ -0,0 +1,68 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'space_age' + +class SpaceAgeTest < Minitest::Test + DELTA = 0.01 + + def test_age_in_seconds + age = SpaceAge.new(1_000_000) + assert_in_delta 1_000_000, age.seconds, DELTA + end + + def test_age_in_earth_years + #skip + age = SpaceAge.new(1_000_000_000) + assert_in_delta 31.69, age.on_earth, DELTA + end + + def test_age_in_mercury_years + #skip + age = SpaceAge.new(2_134_835_688) + assert_in_delta 67.65, age.on_earth, DELTA + assert_in_delta 280.88, age.on_mercury, DELTA + end + + def test_age_in_venus_years + #skip + age = SpaceAge.new(189_839_836) + assert_in_delta 6.02, age.on_earth, DELTA + assert_in_delta 9.78, age.on_venus, DELTA + end + + def test_age_on_mars + #skip + age = SpaceAge.new(2_329_871_239) + assert_in_delta 73.83, age.on_earth, DELTA + assert_in_delta 39.25, age.on_mars, DELTA + end + + def test_age_on_jupiter + #skip + age = SpaceAge.new(901_876_382) + assert_in_delta 28.58, age.on_earth, DELTA + assert_in_delta 2.41, age.on_jupiter, DELTA + end + + def test_age_on_saturn + #skip + age = SpaceAge.new(3_000_000_000) + assert_in_delta 95.06, age.on_earth, DELTA + assert_in_delta 3.23, age.on_saturn, DELTA + end + + def test_age_on_uranus + #skip + age = SpaceAge.new(3_210_123_456) + assert_in_delta 101.72, age.on_earth, DELTA + assert_in_delta 1.21, age.on_uranus, DELTA + end + + def test_age_on_neptune + #skip + age = SpaceAge.new(8_210_123_456) + assert_in_delta 260.16, age.on_earth, DELTA + assert_in_delta 1.58, age.on_neptune, DELTA + end +end From 18d67fa47f8a858329317592acb6e1024f21d166 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Mon, 16 Jan 2017 18:14:34 +0530 Subject: [PATCH 15/23] Rail fence cipher uploaded --- rail-fence-cipher/README.md | 91 +++++++++++++++++++++ rail-fence-cipher/rail_fence_cipher.rb | 59 +++++++++++++ rail-fence-cipher/rail_fence_cipher_test.rb | 67 +++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 rail-fence-cipher/README.md create mode 100644 rail-fence-cipher/rail_fence_cipher.rb create mode 100644 rail-fence-cipher/rail_fence_cipher_test.rb diff --git a/rail-fence-cipher/README.md b/rail-fence-cipher/README.md new file mode 100644 index 0000000..a767fb1 --- /dev/null +++ b/rail-fence-cipher/README.md @@ -0,0 +1,91 @@ +# Rail Fence Cipher + +Implement encoding and decoding for the rail fence cipher. + +The Rail Fence cipher is a form of transposition cipher that gets its name from +the way in which it's encoded. It was already used by the ancient Greeks. + +In the Rail Fence cipher, the message is written downwards on successive "rails" +of an imaginary fence, then moving up when we get to the bottom (like a zig-zag). +Finally the message is then read off in rows. + +For example, using three "rails" and the message "WE ARE DISCOVERED FLEE AT ONCE", +the cipherer writes out: +``` +W . . . E . . . C . . . R . . . L . . . T . . . E +. E . R . D . S . O . E . E . F . E . A . O . C . +. . A . . . I . . . V . . . D . . . E . . . N . . +``` + +Then reads off: +``` +WECRLTEERDSOEEFEAOCAIVDEN +``` + + +To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows. +``` +? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ? +. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . +. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . +``` + +The first row has seven spots that can be filled with "WECRLTE". +``` +W . . . E . . . C . . . R . . . L . . . T . . . E +. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . +. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . +``` + +Now the 2nd row takes "ERDSOEEFEAOC". +``` +W . . . E . . . C . . . R . . . L . . . T . . . E +. E . R . D . S . O . E . E . F . E . A . O . C . +. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . +``` + +Leaving "AIVDEN" for the last row. +``` +W . . . E . . . C . . . R . . . L . . . T . . . E +. E . R . D . S . O . E . E . F . E . A . O . C . +. . A . . . I . . . V . . . D . . . E . . . N . . +``` + +If you now read along the zig-zag shape you can read the original message. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Wikipedia [https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher](https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/rail-fence-cipher/rail_fence_cipher.rb b/rail-fence-cipher/rail_fence_cipher.rb new file mode 100644 index 0000000..4bf2d3f --- /dev/null +++ b/rail-fence-cipher/rail_fence_cipher.rb @@ -0,0 +1,59 @@ +class RailFenceCipher + VERSION=1 + def self.encode(encode_string,zigzag_number) + @argument=zigzag_number + @str=encode_string + RailFenceCipher.comman_encode_decode() + end + def self.decode(encode_string,zigzag_number) + @argument=zigzag_number + @str=encode_string + RailFenceCipher.comman_encode_decode() + end + + def self.comman_encode_decode() + if @argument==1 || @str.length <=1 || @str=='' || @str.length<@argument + return @str + else + @str.gsub!(/[ ]/,'') + str_array=@str.split('') + + a=[] + str_temp='' + (0...@argument).each do |i| + (0...@str.length).each do |j| + str_temp += ' ' + end + a << str_temp.split('') + end + n=@str.length + no=-1 + m=-1 + temp=0 + while n!=0 + no += 1 + if temp==0 + m += 1 + if m==@argument-1 + temp=1 + end + a[m][no]=str_array[no] + else + m -= 1 + if m==0 + temp=0 + end + a[m][no]=str_array[no] + end + n -= 1 + end + decode_string='' + (0...a.length).each do |i| + (0...a[i].length).each do |j| + decode_string += a[i][j] if a[i][j]!=' ' + end + end + return decode_string + end + end +end \ No newline at end of file diff --git a/rail-fence-cipher/rail_fence_cipher_test.rb b/rail-fence-cipher/rail_fence_cipher_test.rb new file mode 100644 index 0000000..73fd076 --- /dev/null +++ b/rail-fence-cipher/rail_fence_cipher_test.rb @@ -0,0 +1,67 @@ +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'rail_fence_cipher' + +# rubocop:enable all +class RailFenceCipherTest < Minitest::Test + def test_encode_with_empty_string + assert_equal '', RailFenceCipher.encode('', 4) + end + + def test_encode_with_one_rail + #skip + assert_equal 'One rail, only one rail', + RailFenceCipher.encode('One rail, only one rail', 1) + end + + def test_encode_with_two_rails + #skip + assert_equal 'XXXXXXXXXOOOOOOOOO', + RailFenceCipher.encode('XOXOXOXOXOXOXOXOXO', 2) + end + + def test_encode_with_three_rails + #skip + assert_equal 'WECRLTEERDSOEEFEAOCAIVDEN', + RailFenceCipher.encode('WEAREDISCOVEREDFLEEATONCE', 3) + end + + def test_encode_with_ending_in_the_middle + #skip + assert_equal 'ESXIEECSR', RailFenceCipher.encode('EXERCISES', 4) + end + + def test_encode_with_less_letters_than_rails + #skip + assert_equal 'More rails than letters', + RailFenceCipher.encode('More rails than letters', 24) + end + + def test_decode_with_empty_string + #skip + assert_equal '', RailFenceCipher.decode('', 4) + end + + def test_decode_with_one_rail + #skip + assert_equal 'ABCDEFGHIJKLMNOP', + RailFenceCipher.decode('ABCDEFGHIJKLMNOP', 1) + end + + def test_decode_with_two_rails + #skip + assert_equal 'XOXOXOXOXOXOXOXOXO', + RailFenceCipher.decode('XXXXXXXXXOOOOOOOOO', 2) + end + + def test_decode_with_three_rails + #skip + assert_equal 'THEDEVILISINTHEDETAILS', + RailFenceCipher.decode('TEITELHDVLSNHDTISEIIEA', 3) + end + + def test_bookkeeping + #skip + assert_equal 1, RailFenceCipher::VERSION + end +end From 63823ec4fc2adfa64d409472e01af45fdb54a03a Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Tue, 17 Jan 2017 10:44:30 +0530 Subject: [PATCH 16/23] Hexadecimal Convert to Decimal number 1 --- hexadecimal/README.md | 45 +++++++++++++++++++++++++++ hexadecimal/hexadecimal.rb | 35 +++++++++++++++++++++ hexadecimal/hexadecimal_test.rb | 55 +++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 hexadecimal/README.md create mode 100644 hexadecimal/hexadecimal.rb create mode 100644 hexadecimal/hexadecimal_test.rb diff --git a/hexadecimal/README.md b/hexadecimal/README.md new file mode 100644 index 0000000..d366396 --- /dev/null +++ b/hexadecimal/README.md @@ -0,0 +1,45 @@ +# Hexadecimal + +Write a program that will convert a hexadecimal number, represented as a string (e.g. "10af8c"), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion). + +On the web we use hexadecimal to represent colors, e.g. green: 008000, +teal: 008080, navy: 000080). + +The program should handle invalid hexadecimal strings. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +All of Computer Science [http://www.wolframalpha.com/examples/NumberBases.html](http://www.wolframalpha.com/examples/NumberBases.html) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/hexadecimal/hexadecimal.rb b/hexadecimal/hexadecimal.rb new file mode 100644 index 0000000..d248530 --- /dev/null +++ b/hexadecimal/hexadecimal.rb @@ -0,0 +1,35 @@ +class Hexadecimal + def initialize(hexa_number) + @hexa_decimal=hexa_number + end + def to_decimal + if @hexa_decimal =~ /^[abcdefABCDE0-9]+$/ + hexa_temp=@hexa_decimal.split('') + var_16=0 + sum_of_all=0 + hexa_temp.reverse.each do |i| + raised_to=16**var_16 + var_16 += 1 + case i + when 'a','A' + sum_of_all += raised_to.to_i * 10 + when 'b','B' + sum_of_all += raised_to.to_i * 11 + when 'c','C' + sum_of_all += raised_to.to_i * 12 + when 'd','D' + sum_of_all += raised_to.to_i * 13 + when 'e','E' + sum_of_all += raised_to.to_i * 14 + when 'f','F' + sum_of_all += raised_to.to_i * 15 + else + sum_of_all += raised_to.to_i * i.to_i + end + end + return sum_of_all + else + return 0 + end + end +end \ No newline at end of file diff --git a/hexadecimal/hexadecimal_test.rb b/hexadecimal/hexadecimal_test.rb new file mode 100644 index 0000000..fa7636f --- /dev/null +++ b/hexadecimal/hexadecimal_test.rb @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'hexadecimal' + +class HexadecimalTest < Minitest::Test + def test_hex_1_is_decimal_1 + assert_equal 1, Hexadecimal.new('1').to_decimal + end + + def test_hex_c_is_decimal_12 + #skip + assert_equal 12, Hexadecimal.new('c').to_decimal + end + + def test_hex_10_is_decimal_16 + #skip + assert_equal 16, Hexadecimal.new('10').to_decimal + end + + def test_hex_af_is_decimal_175 + #skip + assert_equal 175, Hexadecimal.new('af').to_decimal + end + + def test_hex_100_is_decimal_256 + #skip + assert_equal 256, Hexadecimal.new('100').to_decimal + end + + def test_hex_19ace_is_decimal_105166 + #skip + assert_equal 105_166, Hexadecimal.new('19ace').to_decimal + end + + def test_invalid_hex_is_decimal_0 + #skip + assert_equal 0, Hexadecimal.new('carrot').to_decimal + end + + def test_black + #skip + assert_equal 0, Hexadecimal.new('000000').to_decimal + end + + def test_white + #skip + assert_equal 16_777_215, Hexadecimal.new('ffffff').to_decimal + end + + def test_yellow + #skip + assert_equal 16_776_960, Hexadecimal.new('ffff00').to_decimal + end +end From 8c4b22c0391861906846fe92e6cc9ded7323fa01 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Tue, 17 Jan 2017 15:01:04 +0530 Subject: [PATCH 17/23] Queen attack Check 1 --- queen-attack/README.md | 63 ++++++++++++++++++ queen-attack/queen_attack.rb | 95 +++++++++++++++++++++++++++ queen-attack/queen_attack_test.rb | 105 ++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 queen-attack/README.md create mode 100644 queen-attack/queen_attack.rb create mode 100644 queen-attack/queen_attack_test.rb diff --git a/queen-attack/README.md b/queen-attack/README.md new file mode 100644 index 0000000..9d20416 --- /dev/null +++ b/queen-attack/README.md @@ -0,0 +1,63 @@ +# Queen Attack + +Write a program that positions two queens on a chess board and indicates whether or not they are positioned so that they can attack each other. + +In the game of chess, a queen can attack pieces which are on the same +row, column, or diagonal. + +A chessboard can be represented by an 8 by 8 array. + +So if you're told the white queen is at (2, 3) and the black queen at +(5, 6), then you'd know you've got a set-up like so: + +```plain +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ +_ _ _ W _ _ _ _ +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ B _ +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ +``` + +You'd also be able to answer whether the queens can attack each other. +In this case, that answer would be yes, they can, because both pieces +share a diagonal. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +J Dalbey's Programming Practice problems [http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html](http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/queen-attack/queen_attack.rb b/queen-attack/queen_attack.rb new file mode 100644 index 0000000..c254e68 --- /dev/null +++ b/queen-attack/queen_attack.rb @@ -0,0 +1,95 @@ +class Queens + def initialize(set_queen) + @h = set_queen + @first_arr=[] + @h.each_value do |value| + @first_arr << value + if value[0] < 0 || value[1] < 0 || value[0] >= 8 || value[1] >= 8 + raise ArgumentError + end + end + end + + def attack? + if @h.length >1 + if (@first_arr[0][0] == @first_arr[1][0]) || (@first_arr[0][1] == @first_arr[1][1]) + return true + else + if left_top || left_down || right_top || right_down + return true + else + return false + end + end + else + return true + end + end + + def left_top + temp1=@first_arr[0][0] + temp2=@first_arr[0][1] + while true + temp2 -= 1 + temp1 -= 1 + if temp1 == @first_arr[1][0] && temp2 == @first_arr[1][1] + return true + end + if temp1 < 0 || temp2 < 0 + break + end + end + return false + end + + def left_down + temp1=@first_arr[0][0] + temp2=@first_arr[0][1] + while true + temp1 += 1 + temp2 -= 1 + if temp1 == @first_arr[1][0] && temp2 == @first_arr[1][1] + return true + end + if temp1 > 8 || temp2 < 0 + break + end + end + return false + end + def right_top + temp1=@first_arr[0][0] + temp2=@first_arr[0][1] + while true + temp1 += 1 + temp2 += 1 + if temp1 == @first_arr[1][0] && temp2 == @first_arr[1][1] + return true + end + if temp1 > 8 || temp2 > 8 + break + end + end + return false + end + def right_down + temp1=@first_arr[0][0] + temp2=@first_arr[0][1] + while true + temp1 -= 1 + temp2 += 1 + if temp1 == @first_arr[1][0] && temp2 == @first_arr[1][1] + return true + end + if temp1 < 0 || temp2 > 8 + break + end + end + return false + end +end +module BookKeeping + VERSION = 2 +end +# queens = Queens.new white: [1, 0], black: [5, 4] +# queens.attack? diff --git a/queen-attack/queen_attack_test.rb b/queen-attack/queen_attack_test.rb new file mode 100644 index 0000000..5220437 --- /dev/null +++ b/queen-attack/queen_attack_test.rb @@ -0,0 +1,105 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'queen_attack' + +# Test data version: +# 82eb00d +class QueenTest < Minitest::Test + + def test_queen_with_a_valid_position + # skip + Queens.new white: [2, 2] + end + + def test_queen_must_have_positive_rank + #skip + assert_raises ArgumentError do + Queens.new white: [-2, 2] + end + end + + def test_queen_must_have_rank_on_board + #skip + assert_raises ArgumentError do + Queens.new white: [8, 4] + end + end + + def test_queen_must_have_positive_file + #skip + assert_raises ArgumentError do + Queens.new white: [2, -2] + end + end + + def test_queen_must_have_file_on_board + #skip + assert_raises ArgumentError do + Queens.new white: [4, 8] + end + end + + def test_can_not_attack + # skip + queens = Queens.new white: [2, 4], black: [6, 6] + refute queens.attack? + end + + def test_can_attack_on_same_rank + #skip + queens = Queens.new white: [2, 4], black: [2, 6] + assert queens.attack? + end + + def test_can_attack_on_same_file + #skip + queens = Queens.new white: [4, 5], black: [2, 5] + assert queens.attack? + end + + def test_can_attack_on_first_diagonal + # skip + queens = Queens.new white: [2, 2], black: [0, 4] + assert queens.attack? + end + + def test_can_attack_on_second_diagonal + # skip + queens = Queens.new white: [2, 2], black: [3, 1] + assert queens.attack? + end + + def test_can_attack_on_third_diagonal + # skip + queens = Queens.new white: [2, 2], black: [1, 1] + assert queens.attack? + end + + def test_can_attack_on_fourth_diagonal + #skip + queens = Queens.new white: [2, 2], black: [5, 5] + assert queens.attack? + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + def test_bookkeeping + # skip + assert_equal 2, BookKeeping::VERSION + end +end From 8b355f51dd391921f3588da2d2fa02a806151e1d Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Tue, 17 Jan 2017 16:34:45 +0530 Subject: [PATCH 18/23] Atbash cipher is uploaded 1 --- atbash-cipher/README.md | 65 +++++++++++++++++++++++++++++ atbash-cipher/atbash_cipher.rb | 31 ++++++++++++++ atbash-cipher/atbash_cipher_test.rb | 48 +++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 atbash-cipher/README.md create mode 100644 atbash-cipher/atbash_cipher.rb create mode 100644 atbash-cipher/atbash_cipher_test.rb diff --git a/atbash-cipher/README.md b/atbash-cipher/README.md new file mode 100644 index 0000000..3f47b96 --- /dev/null +++ b/atbash-cipher/README.md @@ -0,0 +1,65 @@ +# Atbash Cipher + +Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East. + +The Atbash cipher is a simple substitution cipher that relies on +transposing all the letters in the alphabet such that the resulting +alphabet is backwards. The first letter is replaced with the last +letter, the second with the second-last, and so on. + +An Atbash cipher for the Latin alphabet would be as follows: + +```plain +Plain: abcdefghijklmnopqrstuvwxyz +Cipher: zyxwvutsrqponmlkjihgfedcba +``` + +It is a very weak cipher because it only has one possible key, and it is +a simple monoalphabetic substitution cipher. However, this may not have +been an issue in the cipher's time. + +Ciphertext is written out in groups of fixed length, the traditional group size +being 5 letters, and punctuation is excluded. This is to make it harder to guess +things based on word boundaries. + +## Examples +- Encoding `test` gives `gvhg` +- Decoding `gvhg` gives `test` +- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `The quick brown fox jumps over the lazy dog.` + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Wikipedia [http://en.wikipedia.org/wiki/Atbash](http://en.wikipedia.org/wiki/Atbash) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/atbash-cipher/atbash_cipher.rb b/atbash-cipher/atbash_cipher.rb new file mode 100644 index 0000000..0ea71a7 --- /dev/null +++ b/atbash-cipher/atbash_cipher.rb @@ -0,0 +1,31 @@ +class Atbash + def self.encode(plain_value) + plain_value.gsub!(/[ .,]/,'') + plain_value.downcase! + @array_char=('a'..'z').to_a + string_to_array=plain_value.split('') + n=0 + cipher_text='' + while n != string_to_array.length + if string_to_array[n] =~ /[a-z]/ + temp_index=@array_char.index(string_to_array[n]).to_i + cipher_text += @array_char[25 - temp_index] + else + cipher_text += string_to_array[n].to_s + end + n += 1 + end + contain_string='' + temp_div=0 + temp_array=cipher_text.split('') + (0...temp_array.length).each do |i| + if(temp_div % 5 == 0) + contain_string += ' ' + end + contain_string += temp_array[i] + temp_div += 1 + end + return contain_string.gsub!(/^[ ]+/,'') + + end +end \ No newline at end of file diff --git a/atbash-cipher/atbash_cipher_test.rb b/atbash-cipher/atbash_cipher_test.rb new file mode 100644 index 0000000..a419362 --- /dev/null +++ b/atbash-cipher/atbash_cipher_test.rb @@ -0,0 +1,48 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'atbash_cipher' + +class AtbashTest < Minitest::Test + def test_encode_no + assert_equal 'ml', Atbash.encode('no') + end + + def test_encode_yes + # skip + assert_equal 'bvh', Atbash.encode('yes') + end + + def test_encode_OMG + # skip + assert_equal 'lnt', Atbash.encode('OMG') + end + + def test_encode_O_M_G + # skip + assert_equal 'lnt', Atbash.encode('O M G') + end + + def test_encode_long_word + # skip + assert_equal 'nrmwy oldrm tob', Atbash.encode('mindblowingly') + end + + def test_encode_numbers + # skip + assert_equal('gvhgr mt123 gvhgr mt', + Atbash.encode('Testing, 1 2 3, testing.')) + end + + def test_encode_sentence + # skip + assert_equal 'gifgs rhurx grlm', Atbash.encode('Truth is fiction.') + end + + def test_encode_all_the_things + # skip + plaintext = 'The quick brown fox jumps over the lazy dog.' + cipher = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt' + assert_equal cipher, Atbash.encode(plaintext) + end +end From eabfa8b4b66d0df5f72ddb49dcff4e961119c2ff Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Wed, 18 Jan 2017 11:21:39 +0530 Subject: [PATCH 19/23] String convert into Maths equation successfull 1 --- wordy/README.md | 93 ++++++++++++++++++++++++++++++++ wordy/wordy.rb | 46 ++++++++++++++++ wordy/wordy_test.rb | 128 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 wordy/README.md create mode 100644 wordy/wordy.rb create mode 100644 wordy/wordy_test.rb diff --git a/wordy/README.md b/wordy/README.md new file mode 100644 index 0000000..4064d1a --- /dev/null +++ b/wordy/README.md @@ -0,0 +1,93 @@ +# Wordy + +Parse and evaluate simple math word problems returning the answer as an integer. + +## Iteration 1 — Addition + +Add two numbers together. + +> What is 5 plus 13? + +Evaluates to 18. + +Handle large numbers and negative numbers. + + +## Iteration 2 — Subtraction, Multiplication and Division + +Now, perform the other three operations. + +> What is 7 minus 5? + +2 + +> What is 6 multiplied by 4? + +24 + +> What is 25 divided by 5? + +5 + + +## Iteration 3 — Multiple Operations + +Handle a set of operations, in sequence. + +Since these are verbal word problems, evaluate the expression from +left-to-right, _ignoring the typical order of operations._ + +> What is 5 plus 13 plus 6? + +24 + +> What is 3 plus 2 times 3? + +15 (i.e. not 9) + + +## Bonus — Exponentials + +If you'd like, handle exponentials. + +> What is 2 raised to the 5th power? + +32 + + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Inspired by one of the generated questions in the Extreme Startup game. [https://github.com/rchatley/extreme_startup](https://github.com/rchatley/extreme_startup) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/wordy/wordy.rb b/wordy/wordy.rb new file mode 100644 index 0000000..293a50b --- /dev/null +++ b/wordy/wordy.rb @@ -0,0 +1,46 @@ +class WordProblem + def initialize(question) + @question=question + @question.gsub! 'plus','+' + @question.gsub! 'multiplied','*' + @question.gsub! 'minus','-' + @question.gsub! 'divided','/' + if @question =~ /[\*\-\+\/]/ + @question.gsub!(/[^\/\*\-\+0-9 ]/,'') + else + @question=' ' + end + + end + + def answer + + if @question.length <= 2 + raise ArgumentError + else + @equa_array=@question.split(' ') + @temp_eq=0 + (0...@equa_array.length).each do |i| + if @equa_array[i] =~ /^[\/\*\-\+]?$/ + @temp_eq=eval(@equa_array[i-1].to_s+@equa_array[i].to_s+@equa_array[i+1].to_s) + @equa_array[i+1] = @temp_eq.to_s + end + end + return @temp_eq unless @temp_eq == 0 + end + end +end +module BookKeeping + VERSION=1 +end +# question = 'What is 1 plus 1?' +# WordProblem.new(question).answer + +# question = 'What is 20 minus 4 minus 13?' +# WordProblem.new(question).answer + +# question = 'What is 2 multiplied by -2 multiplied by 3?' +# WordProblem.new(question).answer + +# question = 'Who is the President of the United States?' +# WordProblem.new(question).answer \ No newline at end of file diff --git a/wordy/wordy_test.rb b/wordy/wordy_test.rb new file mode 100644 index 0000000..84b00c4 --- /dev/null +++ b/wordy/wordy_test.rb @@ -0,0 +1,128 @@ +require 'minitest/autorun' +require_relative 'wordy' + +# Test data version: aa12f2e +class WordyTest < Minitest::Test + def test_addition + # skip + question = 'What is 1 plus 1?' + assert_equal(2, WordProblem.new(question).answer) + end + + def test_more_addition + #skip + question = 'What is 53 plus 2?' + assert_equal(55, WordProblem.new(question).answer) + end + + def test_addition_with_negative_numbers + #skip + question = 'What is -1 plus -10?' + assert_equal(-11, WordProblem.new(question).answer) + end + + def test_large_addition + #skip + question = 'What is 123 plus 45678?' + assert_equal(45801, WordProblem.new(question).answer) + end + + def test_subtraction + #skip + question = 'What is 4 minus -12?' + assert_equal(16, WordProblem.new(question).answer) + end + + def test_multiplication + #skip + question = 'What is -3 multiplied by 25?' + assert_equal(-75, WordProblem.new(question).answer) + end + + def test_division + #skip + question = 'What is 33 divided by -3?' + assert_equal(-11, WordProblem.new(question).answer) + end + + def test_multiple_additions + #skip + question = 'What is 1 plus 1 plus 1?' + assert_equal(3, WordProblem.new(question).answer) + end + + def test_addition_and_subtraction + #skip + question = 'What is 1 plus 5 minus -2?' + assert_equal(8, WordProblem.new(question).answer) + end + + def test_multiple_subtraction + #skip + question = 'What is 20 minus 4 minus 13?' + assert_equal(3, WordProblem.new(question).answer) + end + + def test_subtraction_then_addition + #skip + question = 'What is 17 minus 6 plus 3?' + assert_equal(14, WordProblem.new(question).answer) + end + + def test_multiple_multiplication + #skip + question = 'What is 2 multiplied by -2 multiplied by 3?' + assert_equal(-12, WordProblem.new(question).answer) + end + + def test_addition_and_multiplication + #skip + question = 'What is -3 plus 7 multiplied by -2?' + answer = WordProblem.new(question).answer + message = "You should ignore order of precedence. -3 + 7 * -2 = -8, not #{answer}" + assert_equal(-8, answer, message) + end + + def test_multiple_division + #skip + question = 'What is -12 divided by 2 divided by -3?' + assert_equal(2, WordProblem.new(question).answer) + end + + def test_unknown_operation + #skip + question = 'What is 52 cubed?' + assert_raises ArgumentError do + WordProblem.new(question).answer + end + end + + def test_non_math_question + #skip + question = 'Who is the President of the United States?' + assert_raises ArgumentError do + WordProblem.new(question).answer + end + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + def test_bookkeeping + # skip + assert_equal 1, BookKeeping::VERSION + end +end From fb7389d8a58fa0a2fbd6ada3f3e998ba49aab30b Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Wed, 18 Jan 2017 16:59:59 +0530 Subject: [PATCH 20/23] largest series product find inside given string --- largest-series-product/README.md | 50 ++++++++ .../largest_series_product.rb | 21 ++++ .../largest_series_product_test.rb | 113 ++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 largest-series-product/README.md create mode 100644 largest-series-product/largest_series_product.rb create mode 100644 largest-series-product/largest_series_product_test.rb diff --git a/largest-series-product/README.md b/largest-series-product/README.md new file mode 100644 index 0000000..b3a2319 --- /dev/null +++ b/largest-series-product/README.md @@ -0,0 +1,50 @@ +# Largest Series Product + +Write a program that, when given a string of digits, can calculate the largest product for a contiguous substring of digits of length n. + +For example, for the input `'1027839564'`, the largest product for a +series of 3 digits is 270 (9 * 5 * 6), and the largest product for a +series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5). + +Note that these series are only required to occupy *adjacent positions* +in the input; the digits need not be *numerically consecutive*. + +For the input `'73167176531330624919225119674426574742355349194934'`, +the largest product for a series of 6 digits is 23520. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +A variation on Problem 8 at Project Euler [http://projecteuler.net/problem=8](http://projecteuler.net/problem=8) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/largest-series-product/largest_series_product.rb b/largest-series-product/largest_series_product.rb new file mode 100644 index 0000000..e3fe42f --- /dev/null +++ b/largest-series-product/largest_series_product.rb @@ -0,0 +1,21 @@ +class Series + VERSION = 1 + attr_reader :series + + def initialize(series) + raise ArgumentError unless series =~ /\A\d*\z/ + # this one is convert String into Integer Array + @series = series.chars.map(&:to_i) + end + + def largest_product(length) + return 1 if length.zero? + raise ArgumentError if length > series.length || length < 0 + + series.each_cons(length). + map { |*digits| digits.flatten.inject(:*) }.max + end +end +module BookKeeping + VERSION=2 +end \ No newline at end of file diff --git a/largest-series-product/largest_series_product_test.rb b/largest-series-product/largest_series_product_test.rb new file mode 100644 index 0000000..8810200 --- /dev/null +++ b/largest-series-product/largest_series_product_test.rb @@ -0,0 +1,113 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'largest_series_product' + +# Test data version: +# deb225e Implement canonical dataset for scrabble-score problem (#255) + +class Seriestest < Minitest::Test + def test_can_find_the_largest_product_of_2_with_numbers_in_order + assert_equal 72, Series.new('0123456789').largest_product(2) + end + + def test_can_find_the_largest_product_of_2 + #skip + assert_equal 48, Series.new('576802143').largest_product(2) + end + + def test_finds_the_largest_product_if_span_equals_length + #skip + assert_equal 18, Series.new('29').largest_product(2) + end + + def test_can_find_the_largest_product_of_3_with_numbers_in_order + #skip + assert_equal 504, Series.new('0123456789').largest_product(3) + end + + def test_can_find_the_largest_product_of_3 + #skip + assert_equal 270, Series.new('1027839564').largest_product(3) + end + + def test_can_find_the_largest_product_of_5_with_numbers_in_order + #skip + assert_equal 15120, Series.new('0123456789').largest_product(5) + end + + def test_can_get_the_largest_product_of_a_big_number + #skip + assert_equal 23520, Series.new('73167176531330624919225119674426574742355349194934').largest_product(6) + end + + def test_can_get_the_largest_product_of_a_big_number_ii + #skip + assert_equal 28350, Series.new('52677741234314237566414902593461595376319419139427').largest_product(6) + end + + def test_can_get_the_largest_product_of_a_big_number_project_euler + #skip + assert_equal 23514624000, Series.new('7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450').largest_product(13) + end + + def test_reports_zero_if_the_only_digits_are_zero + #skip + assert_equal 0, Series.new('0000').largest_product(2) + end + + def test_reports_zero_if_all_spans_include_zero + #skip + assert_equal 0, Series.new('99099').largest_product(3) + end + + def test_rejects_span_longer_than_string_length + #skip + assert_raises(ArgumentError) { Series.new('123').largest_product(4) } + end + + def test_reports_1_for_empty_string_and_empty_product_0_span + #skip + assert_equal 1, Series.new('').largest_product(0) + end + + def test_reports_1_for_nonempty_string_and_empty_product_0_span + #skip + assert_equal 1, Series.new('123').largest_product(0) + end + + def test_rejects_empty_string_and_nonzero_span + #skip + assert_raises(ArgumentError) { Series.new('').largest_product(1) } + end + + def test_rejects_invalid_character_in_digits + #skip + assert_raises(ArgumentError) { Series.new('1234a5').largest_product(2) } + end + + def test_rejects_negative_span + #skip + assert_raises(ArgumentError) { Series.new('12345').largest_product(-1) } + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module. + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + + def test_bookkeeping + assert_equal 2, BookKeeping::VERSION + end +end From b0637af8d5c209d97cfecf87f8841bbc6957ddc2 Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Wed, 18 Jan 2017 19:00:47 +0530 Subject: [PATCH 21/23] pascals triangle uploaded --- pascals-triangle/README.md | 52 +++++++++++++++++++++++ pascals-triangle/pascals_triangle.rb | 21 +++++++++ pascals-triangle/pascals_triangle_test.rb | 45 ++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 pascals-triangle/README.md create mode 100644 pascals-triangle/pascals_triangle.rb create mode 100644 pascals-triangle/pascals_triangle_test.rb diff --git a/pascals-triangle/README.md b/pascals-triangle/README.md new file mode 100644 index 0000000..9d08a99 --- /dev/null +++ b/pascals-triangle/README.md @@ -0,0 +1,52 @@ +# Pascals Triangle + +Write a program that computes Pascal's triangle up to a given number of rows. + +In Pascal's Triangle each number is computed by adding the numbers to +the right and left of the current position in the previous row. + +```plain + 1 + 1 1 + 1 2 1 + 1 3 3 1 +1 4 6 4 1 +# ... etc +``` + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Pascal's Triangle at Wolfram Math World [http://mathworld.wolfram.com/PascalsTriangle.html](http://mathworld.wolfram.com/PascalsTriangle.html) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/pascals-triangle/pascals_triangle.rb b/pascals-triangle/pascals_triangle.rb new file mode 100644 index 0000000..3e184ff --- /dev/null +++ b/pascals-triangle/pascals_triangle.rb @@ -0,0 +1,21 @@ +class Triangle + + def initialize(number) + @pascal_array=[] + (0...number).each do |i| + x=1 + pascal_temp_array=[] + (0..i).each do |j| + pascal_temp_array << x + x = x * (i - j) / (j + 1) + end + @pascal_array << pascal_temp_array + end + end + def rows + @pascal_array + end + def last + @pascal_array[@pascal_array.length-1] + end +end \ No newline at end of file diff --git a/pascals-triangle/pascals_triangle_test.rb b/pascals-triangle/pascals_triangle_test.rb new file mode 100644 index 0000000..bfb2aeb --- /dev/null +++ b/pascals-triangle/pascals_triangle_test.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'pascals_triangle' + +class TriangleTest < Minitest::Test + def test_one_row + triangle = Triangle.new(1) + assert_equal [[1]], triangle.rows + end + + def test_two_rows + # skip + triangle = Triangle.new(2) + assert_equal [[1], [1, 1]], triangle.rows + end + + def test_three_rows + # skip + triangle = Triangle.new(3) + assert_equal [[1], [1, 1], [1, 2, 1]], triangle.rows + end + + def test_fourth_row + # skip + triangle = Triangle.new(4) + assert_equal [1, 3, 3, 1], triangle.rows.last + end + + def test_fifth_row + # skip + triangle = Triangle.new(5) + assert_equal [1, 4, 6, 4, 1], triangle.rows.last + end + + def test_twentieth_row + # skip + triangle = Triangle.new(20) + expected = [ + 1, 19, 171, 969, 3876, 11_628, 27_132, 50_388, 75_582, 92_378, 92_378, + 75_582, 50_388, 27_132, 11_628, 3876, 969, 171, 19, 1 + ] + assert_equal expected, triangle.rows.last + end +end From 86016538816ca682b1f7dbba5cf47bc7d6454e4d Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Thu, 19 Jan 2017 15:09:21 +0530 Subject: [PATCH 22/23] isogram return letter is or not --- isogram/README.md | 50 +++++++++++++++++++++++++++ isogram/isogram.rb | 16 +++++++++ isogram/isogram_test.rb | 76 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 isogram/README.md create mode 100644 isogram/isogram.rb create mode 100644 isogram/isogram_test.rb diff --git a/isogram/README.md b/isogram/README.md new file mode 100644 index 0000000..d301079 --- /dev/null +++ b/isogram/README.md @@ -0,0 +1,50 @@ +# Isogram + +Determine if a word or phrase is an isogram. + +An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter. + +Examples of isograms: + +- lumberjacks +- background +- downstream + +The word *isograms*, however, is not an isogram, because the s repeats. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +Wikipedia [https://en.wikipedia.org/wiki/Isogram](https://en.wikipedia.org/wiki/Isogram) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/isogram/isogram.rb b/isogram/isogram.rb new file mode 100644 index 0000000..6ac743f --- /dev/null +++ b/isogram/isogram.rb @@ -0,0 +1,16 @@ +class Isogram + def self.is_isogram?(string) + string.downcase! + string.gsub!(/[ \-]/,'') + string_arr=string.chars.to_a + string_arr2=string_arr.uniq + if string_arr.length != string_arr2.length + return false + else + return true + end + end +end +module BookKeeping + VERSION=2 +end diff --git a/isogram/isogram_test.rb b/isogram/isogram_test.rb new file mode 100644 index 0000000..230a9e7 --- /dev/null +++ b/isogram/isogram_test.rb @@ -0,0 +1,76 @@ +#!/usr/bin/env ruby +# encoding: utf-8 +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'isogram' + +# Common test data version: c59c2c4 +class IsogramTest < Minitest::Test + def test_empty_string + #skip + string = '' + assert Isogram.is_isogram?(string) + end + + def test_isogram_with_only_lower_case_characters + #skip + string = 'isogram' + assert Isogram.is_isogram?(string) + end + + def test_word_with_one_duplicated_character + # skip + string = 'eleven' + refute Isogram.is_isogram?(string) + end + + def test_longest_reported_english_isogram + # skip + string = 'subdermatoglyphic' + assert Isogram.is_isogram?(string) + end + + def test_word_with_duplicated_character_in_mixed_case + # skip + string = 'Alphabet' + refute Isogram.is_isogram?(string) + end + + def test_hypothetical_isogrammic_word_with_hyphen + # skip + string = 'thumbscrew-japingly' + assert Isogram.is_isogram?(string) + end + + def test_isogram_with_duplicated_non_letter_character + # skip + string = 'Hjelmqvist-Gryb-Zock-Pfund-Wax' + assert Isogram.is_isogram?(string) + end + + def test_made_up_name_that_is_an_isogram + # skip + string = 'Emily Jung Schwartzkopf' + assert Isogram.is_isogram?(string) + end + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + def test_bookkeeping + # skip + assert_equal 2, BookKeeping::VERSION + end +end From 412471b3a0024abb781f01444aca7d0a81a7f4ff Mon Sep 17 00:00:00 2001 From: mayur-kambariya Date: Fri, 20 Jan 2017 11:10:42 +0530 Subject: [PATCH 23/23] ETL Commit Successfully 1 --- etl/README.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ etl/etl.rb | 11 +++++++ etl/etl_test.rb | 58 ++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 etl/README.md create mode 100644 etl/etl.rb create mode 100644 etl/etl_test.rb diff --git a/etl/README.md b/etl/README.md new file mode 100644 index 0000000..9ee0723 --- /dev/null +++ b/etl/README.md @@ -0,0 +1,82 @@ +# Etl + +We are going to do the `Transform` step of an Extract-Transform-Load. + +### ETL +Extract-Transform-Load (ETL) is a fancy way of saying, "We have some crufty, legacy data over in this system, and now we need it in this shiny new system over here, so +we're going to migrate this." + +(Typically, this is followed by, "We're only going to need to run this +once." That's then typically followed by much forehead slapping and +moaning about how stupid we could possibly be.) + +### The goal +We're going to extract some scrabble scores from a legacy system. + +The old system stored a list of letters per score: + +- 1 point: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T", +- 2 points: "D", "G", +- 3 points: "B", "C", "M", "P", +- 4 points: "F", "H", "V", "W", "Y", +- 5 points: "K", +- 8 points: "J", "X", +- 10 points: "Q", "Z", + +The shiny new scrabble system instead stores the score per letter, which +makes it much faster and easier to calculate the score for a word. It +also stores the letters in lower-case regardless of the case of the +input letters: + +- "a" is worth 1 point. +- "b" is worth 3 points. +- "c" is worth 3 points. +- "d" is worth 2 points. +- Etc. + +Your mission, should you choose to accept it, is to write a program that +transforms the legacy data format to the shiny new format. + +### Notes + +A final note about scoring, Scrabble is played around the world in a +variety of languages, each with its own unique scoring table. For +example, an "E" is scored at 2 in the Māori-language version of the +game while being scored at 4 in the Hawaiian-language version. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + +The test files may have the execution bit set so you may also be able to +run it like this: + + ./hello_world_test.rb + +## Source + +The Jumpstart Lab team [http://jumpstartlab.com](http://jumpstartlab.com) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/etl/etl.rb b/etl/etl.rb new file mode 100644 index 0000000..50b9aa0 --- /dev/null +++ b/etl/etl.rb @@ -0,0 +1,11 @@ +class ETL + def self.transform(hash_pass) + temp_hash = Hash.new{} + hash_pass.each do |key,value| + value.each do |array_value| + temp_hash[array_value.downcase] = key + end + end + temp_hash + end +end \ No newline at end of file diff --git a/etl/etl_test.rb b/etl/etl_test.rb new file mode 100644 index 0000000..e580c9b --- /dev/null +++ b/etl/etl_test.rb @@ -0,0 +1,58 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'etl' + +class TransformTest < Minitest::Test + def test_transform_one_value + old = { 1 => ['A'] } + expected = { 'a' => 1 } + + assert_equal expected, ETL.transform(old) + end + + def test_transform_more_values + # skip + old = { 1 => %w(A E I O U) } + expected = { 'a' => 1, 'e' => 1, 'i' => 1, 'o' => 1, 'u' => 1 } + + assert_equal expected, ETL.transform(old) + end + + def test_more_keys + # skip + old = { 1 => %w(A E), 2 => %w(D G) } + expected = { + 'a' => 1, + 'e' => 1, + 'd' => 2, + 'g' => 2 + } + + assert_equal expected, ETL.transform(old) + end + + def test_full_dataset +# skip + old = { + 1 => %w(A E I O U L N R S T), + 2 => %w(D G), + 3 => %w(B C M P), + 4 => %w(F H V W Y), + 5 => %w(K), + 8 => %w(J X), + 10 => %w(Q Z) + } + + expected = { + 'a' => 1, 'b' => 3, 'c' => 3, 'd' => 2, 'e' => 1, + 'f' => 4, 'g' => 2, 'h' => 4, 'i' => 1, 'j' => 8, + 'k' => 5, 'l' => 1, 'm' => 3, 'n' => 1, 'o' => 1, + 'p' => 3, 'q' => 10, 'r' => 1, 's' => 1, 't' => 1, + 'u' => 1, 'v' => 4, 'w' => 4, 'x' => 8, 'y' => 4, + 'z' => 10 + } + + assert_equal expected, ETL.transform(old) + end +end