Skip to content

Commit

Permalink
Day 2: String Reverse and Palindrome problem Ruby Implementation (#31)
Browse files Browse the repository at this point in the history
* Add @aaditkamat as a contributor

* Add Ruby code for Day 1: FizzBuzz problem

* Add Ruby code for Day 2: String reverse problem

* Update README.md for Day 2

* Modify Ruby code and README

* Add condition for nil and wrong type edge cases

* Add a seperate Ruby source code file for palindrome

* Modify code for reverse.rb

* Add seperate palindrome and reverse code sections in README

* Update gitignore

* Refactor palindrome.rb and rename heading in README
  • Loading branch information
Aadit Kamat authored and MadhavBahl committed Dec 23, 2018
1 parent 40c87f7 commit 82fbf88
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 145 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,4 +4,5 @@ youtube/
Instagram/
a.out
.vscode/
test/
*.o
2 changes: 1 addition & 1 deletion Day2/C++/reverse.cpp
@@ -1,5 +1,5 @@
/**
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
* @github: https://github.com/razdeep
* @date: 21/12/2018
*/
Expand Down
59 changes: 59 additions & 0 deletions Day2/README.md
Expand Up @@ -332,6 +332,50 @@ void main(){
}
```

## Ruby Implementation

### [reverse.rb](./Ruby/reverse.rb)

```ruby
=begin
@author: aaditkamat
@date: 22/12/2018
=end

def short_solution(str)
if str.class != String or str === nil
return false
end
str.reverse
end

def long_solution_iterative(str)
if str.class != String or str === nil
return false
end
reverse = ''
i = 0
until i >= str.length do
reverse.insert(0, str[i])
i += 1
end
reverse
end

def long_solution_recursive(str)
if str.class != String or str === nil
return false
end
if str === ''
return ''
end
temp = str[0]
str[0] = str[-1]
str[-1] = temp
return long_solution(str[1..-2])
end
```

<hr />

## Part B -- Palindrome Check
Expand Down Expand Up @@ -613,4 +657,19 @@ if d==e:
else:
print("Not a pallindrome")
```
## Ruby implementation

### [palindrome.rb](./Ruby/palindrome.rb)

```ruby
=begin
@author: aaditkamat
@date: 22/12/2018
=end
require './reverse'

def palindrome(str)
str != nil and str === short_solution(str)
end
```

9 changes: 9 additions & 0 deletions Day2/Ruby/palindrome.rb
@@ -0,0 +1,9 @@
=begin
@author: aaditkamat
@date: 22/12/2018
=end
require './reverse'

def solution(str)
str != nil and str === short_solution(str)
end
36 changes: 36 additions & 0 deletions Day2/Ruby/reverse.rb
@@ -0,0 +1,36 @@
=begin
@author: aaditkamat
@date: 22/12/2018
=end

def short_solution(str)
if str.class != String or str === nil
return nil
end
str.reverse
end

def long_solution_iterative(str)
if str.class != String or str === nil
return nil
end
reverse = ''
i = 0
until i >= str.length do
reverse.insert(0, str[i])
i += 1
end
reverse
end

def long_solution_recursive(str)
if str.class != String or str === nil
return nil
end
if str === ''
return ''
end
return long_solution_recursive(str[1..-1]) + str[0]
end


0 comments on commit 82fbf88

Please sign in to comment.