From ca321c1c808f17c7373832aac476d6a5cb111bb6 Mon Sep 17 00:00:00 2001 From: Vishal Shirke Date: Wed, 26 Dec 2018 14:20:56 +0530 Subject: [PATCH] Added python solutions for Day 4 (#51) * Add @vishalshirke7 as a contributor * Update @vishalshirke7 as a contributor * Added python solutions for Day 4 * modified readme * Add @vishal * reverted adding as a contributor * Update README.md * Update CONTRIBUTORS.md * Update CONTRIBUTORS.md --- day4/Python/MaxChars.py | 19 ++++++ day4/Python/NumOfVowels.py | 13 ++++ day4/README.md | 127 ++++++++++++++++++++++++++++++++++++- 3 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 day4/Python/MaxChars.py create mode 100644 day4/Python/NumOfVowels.py diff --git a/day4/Python/MaxChars.py b/day4/Python/MaxChars.py new file mode 100644 index 00000000..82b3122f --- /dev/null +++ b/day4/Python/MaxChars.py @@ -0,0 +1,19 @@ +""" + @author : vishalshirke7 + @date : 25/12/2018 +""" + +input_str = input() +dictnry = {} +maxCount, maxChar = 0, '' +for char in input_str: + if char in dictnry.keys(): + dictnry[char] += 1 + else: + dictnry[char] = 1 + if dictnry[char] > maxCount: + maxCount = dictnry[char] + maxChar = char + +print(maxChar) + diff --git a/day4/Python/NumOfVowels.py b/day4/Python/NumOfVowels.py new file mode 100644 index 00000000..b51efb40 --- /dev/null +++ b/day4/Python/NumOfVowels.py @@ -0,0 +1,13 @@ +import re + +""" + @author : vishalshirke7 + @date : 25/12/2018 +""" + +""" +This solution is based on regular expression module in python. +""" + +print(len(re.findall(r'[a,e,i,o,u,A,E,I,O,U]', input()))) + diff --git a/day4/README.md b/day4/README.md index a980ebf1..04cb71c9 100644 --- a/day4/README.md +++ b/day4/README.md @@ -216,6 +216,61 @@ public class NumVowels2 { } ``` +## Python Implementation + +### [Solution 1](./Python/NumOfVowels.py) + +```python +import re + +""" + This solution makes use of python's re module i.e regular expressions + Here findall method takes two parameters 1st - regualr expression, 2nd - input string + The regular expression simply checks for all vowels present in the input string + and returns them as a list. +""" +print(len(re.findall(r'[a,e,i,o,u,A,E,I,O,U]', input()))) + +``` + +### [Solution 2](./Python/partA_sol.py) + +```python +# Input the String. +string=input("Enter the String : ") + +# Create a list of vowels in lowercase. +vowels=['a','e','i','o','u'] + +# Initialize the count variable to zero. +count=0 + +# Now iterate every character in the string. +for char in string: + # And if the current character we are iterating is + # present in the vowels list then increment count. + if char.lower() in vowels: + count+=1 + +# Print the result +print("Number of vowels in the string are : ",count) + +``` + +##[Solution 3](./Python/Shashankvowels.py) + +```Python +a=input("Enter the string to count no. of vowels?") +b=list(a.replace(" ","").lower()) +c=['a','e','i','o','u'] +count=0 +for i in b: + for j in c: + if (j==i): + count=count+1 +print(count) +``` + ## C++ Implementation ### [NumVowelsPartA.cpp](./C++/NumVowelsPartA.cpp) @@ -487,6 +542,75 @@ function maxChars (sentence) { maxChars('helllllo worlld'); ``` +## Python Implementation + +### [Solution 1](./Python/MaxChars.py) + +```python +input_str = input() +dictnry = {} +maxCount, maxChar = 0, '' +for char in input_str: + """ + In this solution we are simply maintaining a dictionary of characters + in the input string as key and their count as value + """ + if char in dictnry.keys(): + dictnry[char] += 1 + else: + dictnry[char] = 1 + """ + We check for maxCount of each character in a single loop + by comparing it with present maxCount, hence not iterating over the dictionary again + to find character with maximum count. + """ + if dictnry[char] > maxCount: + maxCount = dictnry[char] + maxChar = char + +print(maxChar) + +``` +### [Solution 2](./Python/partB_sol.py) + +```python +# Input the string +string=input("Enter the string : ") + +# Create an empty dictionary to store the frequency of characters +characters={} + +# Iterate every character in the string +for char in string: + # And if the character already exists in the dictionary then + # increment its frequency by 1. + if char.lower() in characters: + characters[char.lower()]+=1 + # Else initialize its frequency by 1 + else: + characters[char.lower()]=1 + +# Print the character which has the maximum frequency +print("The most occouring character in the string is : ", max(characters,key=characters.get)) + +``` + +###[Solution 3] (./Python/Shashankchar.py) + +``` Python +a=input("Enter the string to count frequent occuring characters?") +b=list(a.replace(" ","").lower()) +c=[] +for i in b: + d=(i,b.count(i)) + c.append(d) + +e=dict(list(set(c))) +f=max(e) +g=max(e.values()) +print("maximum occurence is of {0}:{1}".format(f,g)) +``` + ## C++ Implementation ### [NumVowelsPartB.cpp](./C++/NumVowelsPartB.cpp) @@ -617,7 +741,6 @@ c=[] for i in b: d=(i,b.count(i)) c.append(d) - e=dict(list(set(c))) f=max(e) g=max(e.values()) @@ -684,4 +807,4 @@ print "Enter a string: " str = gets str.chomp! puts "The most frequent character in #{str} is : #{most_frequent_character(str)}" -``` \ No newline at end of file +```