Skip to content

Commit 3afd69e

Browse files
Aadit KamatMadhavBahl
Aadit Kamat
authored andcommitted
Add Ruby implementation for Day 6 problem; Add Ruby, C++ & Java implementations for Day 7 problem (#72)
* 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 * Add solution for Day 3: Hamming Problem * Add condition for strings of unequal lengths * Update README * Change project name and owner in.all-contributorsrc * Remove merge conflict lines * Add @shivank86 as a contributor * Add C++ files for different patterns * Add author and date comments at the top of C++ files * Update README.md * Add solution for Day 6 Problem in Python * Update README * Refactor code files * Modify string representation of output in python files * Add Ruby solutions for Day 6 problem * Update README for Ruby code * Add first version of solutions for Day 7 problem in C++, Java & Ruby * Modify solutions * Update Day 7 README * Remove merge conflicts from CONTRIBUTORS.md * Add back removed lines in CONTRIBUTORS.md * Add code sections contributed by @imkaka to day 6 README * Update README.md
1 parent dc7860b commit 3afd69e

12 files changed

+645
-24
lines changed

.all-contributorsrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,4 @@
164164
]
165165
}
166166
]
167-
}
167+
}

CONTRIBUTORS.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
21
[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors)
32

4-
53
## Contributors
64

75
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
@@ -14,4 +12,4 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
1412
| [<img src="https://avatars2.githubusercontent.com/u/25405726?v=4" width="100px;"/><br /><sub><b>Vishal Shirke</b></sub>](https://github.com/vishalshirke7)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=vishalshirke7 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=vishalshirke7 "Code") |
1513
<!-- ALL-CONTRIBUTORS-LIST:END -->
1614

17-
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
15+
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!

day6/Python/reverse_words.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def reverse_words(string):
1111

1212
print("Enter a string: ", end= '')
1313
string = input()
14-
print("Reverse of string: ", reverse_words(string))
14+
print("Reverse of string \'", string, "\': ", reverse_words(string), sep='')

day6/Python/sentence_capitalization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ def capitalize_sentence_long(string):
1717

1818
print("Enter a string: ", end= ' ')
1919
string = input()
20-
print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string))
21-
print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string))
20+
print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string), sep='')
21+
print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string), sep='')

day6/README.md

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ string = input()
226226
print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string))
227227
print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string))
228228
```
229+
229230
## C++ Implementation
230231

231232
### [Solution](./cpp/Capialise.cpp)
@@ -255,19 +256,15 @@ int main() {
255256
}
256257
```
257258

258-
## C++ Implementation
259-
260259
### [Solution](./C++/day6_1.cpp)
261260

262261
```cpp
263262
/*
264263
* @author : dhruv-gupta14
265264
* @date : 27/12/2018
266265
*/
267-
268266
#include<bits/stdc++.h>
269267
using namespace std;
270-
271268
int main()
272269
{
273270
string s;
@@ -318,7 +315,30 @@ int main(){
318315
cout << "Result: " << str;
319316
return 0;
320317
}
318+
```
319+
320+
## Ruby Implementation
321+
322+
### [sentence_capitalization.rb](./Ruby/sentence_capitalization.rb)
323+
324+
```ruby
325+
326+
=begin
327+
@author: aaditkamat
328+
@date: 27/12/2018
329+
=end
321330

331+
def capitalize_sentence(string)
332+
new_string = ''
333+
string.split(' ').each do |word|
334+
new_string += word.capitalize + ' '
335+
end
336+
new_string
337+
end
338+
339+
print"Enter a string: "
340+
string = gets().chomp
341+
puts "String \" #{string} \" with first letter of each word capitalized: #{capitalize_sentence(string)}"
322342
```
323343

324344
## Part B -- Word Reversal
@@ -515,8 +535,9 @@ def reverse_words(string):
515535

516536
print("Enter a string: ", end= '')
517537
string = input()
518-
print("Reverse of string: ", reverse_words(string)})
538+
print("Reverse of string \'", string, "\': ", reverse_words(string), sep='')
519539
```
540+
520541
## C++ Implementation
521542

522543
### [Solution](./cpp/wrdReversal.cpp)
@@ -621,6 +642,30 @@ int main(){
621642
}
622643
```
623644

645+
## Ruby Implementation
646+
647+
### [reverse_words.rb](./Ruby/reverse_words.rb)
648+
649+
```ruby
650+
651+
=begin
652+
@author: aaditkamat
653+
@date: 27/12/2018
654+
=end
655+
656+
def reverse_words(string)
657+
new_string = ''
658+
string.split(' ').each do |word|
659+
new_string += word.reverse + ' '
660+
end
661+
new_string
662+
end
663+
664+
print"Enter a string: "
665+
string = gets().chomp
666+
print"Reverse of string #{string}: #{reverse_words(string)}"
667+
```
668+
624669
## Part C -- Anagram Check
625670

626671
**Question** - Write a program to check whether the two provided strings are anagrams of each other.
@@ -639,7 +684,7 @@ int main(){
639684
function anagram (str1, str2) {
640685
let len1 = str1.length,
641686
len2 = str2.length;
642-
687+
643688
// Compare lengths
644689
if (len1 !== len2) {
645690
console.log ('Invalid Input');
@@ -675,7 +720,7 @@ anagram ('LISTEN', 'SILENT');
675720
function anagram (str1, str2) {
676721
let len1 = str1.length,
677722
len2 = str2.length;
678-
723+
679724
// Compare lengths
680725
if (len1 !== len2) {
681726
console.log ('Invalid Input');
@@ -705,7 +750,7 @@ function anagram (str1, str2) {
705750
}
706751

707752
console.log(`"${str1}" and "${str2}" are Anagrams`);
708-
}
753+
}
709754

710755
anagram ('LISTEN', 'MILENT');
711756
```
@@ -754,7 +799,7 @@ function anagram (str1, str2) {
754799
if (flag !== 1) {
755800
console.log (`${str1} and ${str2} are not Anagrams`);
756801
return 0;
757-
}
802+
}
758803
else {
759804
console.log (`${str1} and ${str2} are Anagrams`);
760805
return 1;
@@ -782,7 +827,7 @@ if( sorted(Str1) != sorted(Str2) ): print("not", end=" ")
782827
print("anagrams")
783828
```
784829

785-
### [reverse_words.py](./Python/reverse_words.py)
830+
### [anagram_check.py](./Python/anagram_check.py)
786831
```python
787832
'''
788833
@author: aaditkamat
@@ -794,12 +839,6 @@ def check_anagram(first_str, second_str):
794839
second_word_dict = {}
795840
first_str = first_str.replace(' ', '').lower()
796841
second_str = first_str.replace(' ', '').lower()
797-
for ch in first_str:
798-
if ch not in first_word_dict:
799-
first_word_dict[ch] = 1
800-
else:
801-
first_word_dict[ch] += 1
802-
803842
for ch in second_str:
804843
if ch not in second_word_dict:
805844
second_word_dict[ch] = 1
@@ -935,4 +974,42 @@ int main(){
935974

936975
return 0;
937976
}
938-
```
977+
```
978+
979+
## Ruby Implementation
980+
981+
### [anagram_check.rb](./Ruby/anagram_check.rb)
982+
```ruby
983+
984+
=begin
985+
@author: aaditkamat
986+
@date: 27/12/2018
987+
=end
988+
989+
def check_anagram(first_str, second_str)
990+
first_word_dict = {}
991+
second_word_dict = {}
992+
first_str.gsub!(" ", "").downcase!
993+
second_str.gsub!(" ", "").downcase!
994+
first_str.each_char do |ch|
995+
if first_word_dict.has_key?(ch)
996+
first_word_dict[ch] += 1
997+
else
998+
first_word_dict[ch] = 1
999+
end
1000+
end
1001+
second_str.each_char do |ch|
1002+
if second_word_dict.has_key?(ch)
1003+
second_word_dict[ch] += 1
1004+
else
1005+
second_word_dict[ch] = 1
1006+
end
1007+
end
1008+
first_word_dict == second_word_dict
1009+
end
1010+
1011+
puts "Enter two strings: "
1012+
first_str = gets().chomp
1013+
second_str = gets().chomp
1014+
puts "\nAre #{first_str} and #{second_str} anagrams? #{check_anagram(String.new(first_str), String.new(second_str))}"
1015+
```

day6/Ruby/anagram_check.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=begin
2+
@author: aaditkamat
3+
@date: 27/12/2018
4+
=end
5+
6+
def check_anagram(first_str, second_str)
7+
first_word_dict = {}
8+
second_word_dict = {}
9+
first_str.gsub!(" ", "").downcase!
10+
second_str.gsub!(" ", "").downcase!
11+
first_str.each_char do |ch|
12+
if first_word_dict.has_key?(ch)
13+
first_word_dict[ch] += 1
14+
else
15+
first_word_dict[ch] = 1
16+
end
17+
end
18+
second_str.each_char do |ch|
19+
if second_word_dict.has_key?(ch)
20+
second_word_dict[ch] += 1
21+
else
22+
second_word_dict[ch] = 1
23+
end
24+
end
25+
first_word_dict == second_word_dict
26+
end
27+
28+
puts "Enter two strings: "
29+
first_str = gets().chomp
30+
second_str = gets().chomp
31+
puts "\nAre #{first_str} and #{second_str} anagrams? #{check_anagram(String.new(first_str), String.new(second_str))}"

day6/Ruby/reverse_words.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=begin
2+
@author: aaditkamat
3+
@date: 27/12/2018
4+
=end
5+
6+
def reverse_words(string)
7+
new_string = ''
8+
string.split(' ').each do |word|
9+
new_string += word.reverse + ' '
10+
end
11+
new_string
12+
end
13+
14+
print"Enter a string: "
15+
string = gets().chomp
16+
print"Reverse of string #{string}: #{reverse_words(string)}"

day6/Ruby/sentence_capitalization.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=begin
2+
@author: aaditkamat
3+
@date: 27/12/2018
4+
=end
5+
6+
def capitalize_sentence(string)
7+
new_string = ''
8+
string.split(' ').each do |word|
9+
new_string += word.capitalize + ' '
10+
end
11+
new_string
12+
end
13+
14+
print"Enter a string: "
15+
string = gets().chomp
16+
puts "String \" #{string} \" with first letter of each word capitalized: #{capitalize_sentence(string)}"

day7/C++/one_edit_away.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @author: aaditkamat
3+
* @date: 28/12/2018
4+
*/
5+
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
bool can_replace_a_character(string str1, string str2) {
11+
if (str1.size() != str2.size()) {
12+
return false;
13+
}
14+
15+
int change = 0;
16+
17+
for (int i = 0; i < str1.size(); i++) {
18+
if (str1[i] != str2[i]) {
19+
change++;
20+
}
21+
}
22+
23+
return change == 1;
24+
}
25+
26+
bool is_a_modified_substring(string str1, string str2) {
27+
if (str1.empty()) {
28+
return true;
29+
}
30+
31+
if (str1[0] != str2[1] && str1[0] != str2[0]) {
32+
return false;
33+
}
34+
35+
int found = str2.find(str1[0]);
36+
37+
if (found == 1) {
38+
return str2.find(str1) != string::npos;
39+
}
40+
41+
int j = 0, ctr = 0;
42+
for (int i = 0; i < str1.size(); j++) {
43+
if (j >= str2.size()) {
44+
return false;
45+
}
46+
if (str1[i] != str2[j] && ctr == 0) {
47+
ctr++;
48+
continue;
49+
}
50+
if (str1[i] != str2[j]) {
51+
return false;
52+
}
53+
i++;
54+
}
55+
return true;
56+
}
57+
58+
bool can_add_a_character(string str1, string str2) {
59+
return str2.size() - str1.size() == 1 && is_a_modified_substring(str1, str2);
60+
}
61+
62+
bool can_delete_a_character(string str1, string str2) {
63+
return str1.size() - str2.size() == 1 && is_a_modified_substring(str2, str1);
64+
}
65+
66+
bool are_one_edit_away(string str1, string str2) {
67+
if (str1 == str2) {
68+
return true;
69+
}
70+
return can_replace_a_character(str1, str2) || can_add_a_character(str1, str2) || can_delete_a_character(str1, str2);
71+
}
72+
73+
int main() {
74+
string str1, str2;
75+
cout << "Enter two strings: " << endl;
76+
getline(cin, str1);
77+
getline(cin, str2);
78+
cout << "Are \"" << str1 << "\" and \"" << str2 << "\" one edit away? " << are_one_edit_away(str1, str2) << endl;
79+
}

0 commit comments

Comments
 (0)