10000 anagram check, k-frequent-words, pattern-match, substring-check, vowe… · AllAlgorithms/python@7afdd98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7afdd98

Browse files
tusharkanakagiriabranhe
authored andcommitted
anagram check, k-frequent-words, pattern-match, substring-check, vowel-count
1 parent f7f877b commit 7afdd98

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

strings/anagram-check.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# function to check if two strings are
2+
# anagram or not
3+
def check(s1, s2):
4+
5+
# the sorted strings are checked
6+
if(sorted(s1)== sorted(s2)):
7+
print("The strings are anagrams.")
8+
else:
9+
print("The strings aren't anagrams.")
10+
11+
# driver code
12+
s1 ="listen" #String 1
13+
s2 ="silent" #String 2
14+
check(s1, s2)

strings/k-frequent-words.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python program to find the k most frequent words
2+
# from data set
3+
from collections import Counter
4+
5+
data_set = "" #Enter long string or paragraph here
6+
7+
# split() returns list of all the words in the string
8+
split_it = data_set.split()
9+
10+
# Pass the split_it list to instance of Counter class.
11+
Counter = Counter(split_it)
12+
13+
# most_common() produces k frequently encountered
14+
# input values and their respective counts.
15+
most_occur = Counter.most_common(4)
16+
17+
print(most_occur)

strings/pattern-match.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# A Python program to demonstrate working
2+
# of re.match().
3+
import re
4+
5+
# a sample function that uses regular expressions
6+
# to find month and day of a date.
7+
def findMonthAndDate(string):
8+
9+
regex = r"([a-zA-Z]+) (\d+)"
10+
match = re.match(regex, string)
11+
12+
if match == None:
13+
print "Not a valid date"
14+
return
15+
16+
print "Given Data: %s" % (match.group())
17+
print "Month: %s" % (match.group(1))
18+
print "Day: %s" % (match.group(2))
19+
20+
21+
# Driver Code
22+
findMonthAndDate("" #Enter pattern to match)
23+
print("")
24+
findMonthAndDate("") #Enter string

strings/substring-check.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# function to check if small string is
2+
# there in big string
3+
def check(string, sub_str):
4+
if (string.find(sub_str) == -1):
5+
print("NO")
6+
else:
7+
print("YES")
8+
9+
# driver code
10+
string = "" #Enter string
11+
sub_str ="" #Enter sub string
12+
check(string, sub_str)

strings/vowel-count.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Python3 code to count vowel in
2+
# a string using set
3+
4+
# Function to count vowel
5+
def vowel_count(str):
6+
7+
# Intializing count variable to 0
8+
count = 0
9+
10+
# Creating a set of vowels
11+
vowel = set("aeiouAEIOU")
12+
13+
# Loop to traverse the alphabet
14+
# in the given string
15+
for alphabet in str:
16+
17+
# If alphabet is present
18+
# in set vowel
19+
if alphabet in vowel:
20+
count = count + 1
21+
22+
print("No. of vowels :", count)
23+
24+
# Driver code
25+
str = "" #Enter string
26+
27+
# Function Call
28+
vowel_count(str)

0 commit comments

Comments
 (0)
0