Skip to content

Commit

Permalink
Python implementation for Day 6 Problem (#71)
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

* 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
  • Loading branch information
Aadit Kamat authored and MadhavBahl committed Dec 27, 2018
1 parent d403256 commit 5462d07
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
27 changes: 27 additions & 0 deletions day6/Python/anagram_check.py
@@ -0,0 +1,27 @@
'''
@author: aaditkamat
@date: 27/12/2018
'''

def check_anagram(first_str, second_str):
first_word_dict = {}
second_word_dict = {}
first_str = first_str.replace(' ', '').lower()
second_str = first_str.replace(' ', '').lower()
for ch in first_str:
if ch not in first_word_dict:
first_word_dict[ch] = 1
else:
first_word_dict[ch] += 1

for ch in second_str:
if ch not in second_word_dict:
second_word_dict[ch] = 1
else:
second_word_dict[ch] += 1
return first_word_dict == second_word_dict

print("Enter two strings: ")
first_str = input()
second_str = input()
print("Are ", first_str, "and ", second_str, "anagrams? ", check_anagram(first_str, second_str))
14 changes: 14 additions & 0 deletions day6/Python/reverse_words.py
@@ -0,0 +1,14 @@
'''
@author: aaditkamat
@date: 27/12/2018
'''

def reverse_words(string):
new_string = ''
for word in string.split(' '):
new_string += word[::-1] + ' '
return new_string

print("Enter a string: ", end= '')
string = input()
print("Reverse of string: ", reverse_words(string))
21 changes: 21 additions & 0 deletions day6/Python/sentence_capitalization.py
@@ -0,0 +1,21 @@
'''
@author: aaditkamat
@date: 27/12/2018
'''

#short version
from string import capwords
def capitalize_sentence_short(string):
return capwords(string)

#slightly long version
def capitalize_sentence_long(string):
new_string = ''
for word in string.split(' '):
new_string += word.capitalize() + ' '
return new_string

print("Enter a string: ", end= ' ')
string = input()
print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string))
print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string))
78 changes: 76 additions & 2 deletions day6/README.md
Expand Up @@ -176,7 +176,6 @@ public class SentenceCap2 {
}
}
```

## Python Implementation

### [Solution](./Python/sentenceCap.py)
Expand All @@ -202,6 +201,32 @@ Str = input("Enter a string = ")
print("Capitalize words = ", Cap(Str))
```

### [sentence_capitalization.py](./Python/sentence_capitalization.py)

```python
'''
@author: aaditkamat
@date: 27/12/2018
'''

#short version
from string import capwords
def capitalize_sentence_short(string):
return capwords(string)

#slightly long version
def capitalize_sentence_long(string):
new_string = ''
for word in string.split(' '):
new_string += word.capitalize() + ' '
return new_string

print("Enter a string: ", end= ' ')
string = input()
print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string))
print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string))
```

## Part B -- Word Reversal

**Question** - Given a sentence, Write a program to reverse each word in it.
Expand Down Expand Up @@ -381,6 +406,24 @@ Str = input("Enter a string = ")
print("Words Reversed =", wordRev(Str))
```

### [reverse_words.py](./Python/reverse_words.py)
```python
'''
@author: aaditkamat
@date: 27/12/2018
'''

def reverse_words(string):
new_string = ''
for word in string.split(' '):
new_string += word[::-1] + ' '
return new_string

print("Enter a string: ", end= '')
string = input()
print("Reverse of string: ", reverse_words(string)})
```

## Part C -- Anagram Check

**Question** - Write a program to check whether the two provided strings are anagrams of each other.
Expand Down Expand Up @@ -540,4 +583,35 @@ Str2 = input("Enter string 2 = ")
print(Str1, "&", Str2, "are", end=" ")
if( sorted(Str1) != sorted(Str2) ): print("not", end=" ")
print("anagrams")
```
```

### [reverse_words.py](./Python/reverse_words.py)
```python
'''
@author: aaditkamat
@date: 27/12/2018
'''

def check_anagram(first_str, second_str):
first_word_dict = {}
second_word_dict = {}
first_str = first_str.replace(' ', '').lower()
second_str = first_str.replace(' ', '').lower()
for ch in first_str:
if ch not in first_word_dict:
first_word_dict[ch] = 1
else:
first_word_dict[ch] += 1

for ch in second_str:
if ch not in second_word_dict:
second_word_dict[ch] = 1
else:
second_word_dict[ch] += 1
return first_word_dict == second_word_dict

print("Enter two strings: ")
first_str = input()
second_str = input()
print("Are ", first_str, "and ", second_str, "anagrams? ", check_anagram(first_str, second_str))
```

0 comments on commit 5462d07

Please sign in to comment.