Skip to content

Commit 82fbf88

Browse files
Aadit KamatMadhavBahl
Aadit Kamat
authored andcommitted
Day 2: String Reverse and Palindrome problem Ruby Implementation (#31)
* 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
1 parent 40c87f7 commit 82fbf88

File tree

6 files changed

+250
-145
lines changed

6 files changed

+250
-145
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ youtube/
44
Instagram/
55
a.out
66
.vscode/
7+
test/
78
*.o

Day2/C++/reverse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @author: Rajdeep Roy Chowdhury<[email protected]>
2+
* @author: Rajdeep Roy Chowdhury<[email protected]>
33
* @github: https://github.com/razdeep
44
* @date: 21/12/2018
55
*/

Day2/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,50 @@ void main(){
332332
}
333333
```
334334

335+
## Ruby Implementation
336+
337+
### [reverse.rb](./Ruby/reverse.rb)
338+
339+
```ruby
340+
=begin
341+
@author: aaditkamat
342+
@date: 22/12/2018
343+
=end
344+
345+
def short_solution(str)
346+
if str.class != String or str === nil
347+
return false
348+
end
349+
str.reverse
350+
end
351+
352+
def long_solution_iterative(str)
353+
if str.class != String or str === nil
354+
return false
355+
end
356+
reverse = ''
357+
i = 0
358+
until i >= str.length do
359+
reverse.insert(0, str[i])
360+
i += 1
361+
end
362+
reverse
363+
end
364+
365+
def long_solution_recursive(str)
366+
if str.class != String or str === nil
367+
return false
368+
end
369+
if str === ''
370+
return ''
371+
end
372+
temp = str[0]
373+
str[0] = str[-1]
374+
str[-1] = temp
375+
return long_solution(str[1..-2])
376+
end
377+
```
378+
335379
<hr />
336380

337381
## Part B -- Palindrome Check
@@ -613,4 +657,19 @@ if d==e:
613657
else:
614658
print("Not a pallindrome")
615659
```
660+
## Ruby implementation
661+
662+
### [palindrome.rb](./Ruby/palindrome.rb)
663+
664+
```ruby
665+
=begin
666+
@author: aaditkamat
667+
@date: 22/12/2018
668+
=end
669+
require './reverse'
670+
671+
def palindrome(str)
672+
str != nil and str === short_solution(str)
673+
end
674+
```
616675

Day2/Ruby/palindrome.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=begin
2+
@author: aaditkamat
3+
@date: 22/12/2018
4+
=end
5+
require './reverse'
6+
7+
def solution(str)
8+
str != nil and str === short_solution(str)
9+
end

Day2/Ruby/reverse.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=begin
2+
@author: aaditkamat
3+
@date: 22/12/2018
4+
=end
5+
6+
def short_solution(str)
7+
if str.class != String or str === nil
8+
return nil
9+
end
10+
str.reverse
11+
end
12+
13+
def long_solution_iterative(str)
14+
if str.class != String or str === nil
15+
return nil
16+
end
17+
reverse = ''
18+
i = 0
19+
until i >= str.length do
20+
reverse.insert(0, str[i])
21+
i += 1
22+
end
23+
reverse
24+
end
25+
26+
def long_solution_recursive(str)
27+
if str.class != String or str === nil
28+
return nil
29+
end
30+
if str === ''
31+
return ''
32+
end
33+
return long_solution_recursive(str[1..-1]) + str[0]
34+
end
35+
36+

0 commit comments

Comments
 (0)