Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Solution Files #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.idea/*
48 changes: 48 additions & 0 deletions common_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Given a array of N strings, find the longest common prefix among all strings present in the array.

Example:
Input:
geeksforgeeks geeks geek geezer
apple ape april

Output:
gee
ap
"""


def common_prefix(usr_string):
""" This function finds the prefix from given words. """
prefix_list = [] # Empty list for prefix letters.
words_list = usr_string.split(',') # Splitting the input by ',' seperator.
words_list.sort() # Sorting the list.
first_word = words_list.pop(0) # Getting first word from the list.
len_first_word = len(first_word) # Finding length of first word of the list.

for i in range(len(words_list)):
for j in range(0, len_first_word): # The highest length of the prefix can be of length first word.
if words_list[i][j] == first_word[j]: # Comparing the first letter of each list item with dirst word letter.

if not words_list[i][j] in prefix_list:
prefix_list.append(words_list[i][j]) # Appending the non repeating letter to the list.

prefix = "".join(prefix_list) # Formatting the word.

return prefix


if __name__ == '__main__':
usr_input = input("Enter The List: ").lower()

if usr_input != '':
result = common_prefix(usr_input)

if result == '':
print("\n *** There Is NO Common Prefix In The Given Input. ***")
else:
print("The Longest Common Prefix IS: ", result)

else:
print("\n *** You Have Entered the Empty Values. ***")

37 changes: 37 additions & 0 deletions reverse_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Given a String of length S, reverse the whole string without reversing the individual words in it. Words are separated by dots.

Example:
Input:

i.like.this.program.very.much
pqr.mno

Output:
much.very.program.this.like.i
mno.pqr

** For More Input/Output Examples Use 'Expected Output' option **

"""


def string_reverse(sentence):

""" This function will reverse the string that user enters. """

sentence_list = sentence.split(sep='.') # string will be seperated by '.' and will be stored as list.
reversed_list = sentence_list[::-1] # reversed string will be stored
custom_sep = '.' # defining the custom_sep for new string.
result= custom_sep.join(reversed_list) # reversed string will be appended with custom_sep.
return result


if __name__ == '__main__':
usr_string = input("Enter The Sentence: ")

if (usr_string == ''):
print("\n *** Empty Sentence Can't be Reversed !!! *** ")
else:
result = string_reverse(usr_string)
print(f"Reversed String is : {result}")
42 changes: 42 additions & 0 deletions roman_number_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Given an string in roman no format (s) your task is to convert it to integer .

Example:
Input
2
V
III
Output
5
3
"""


def roman_conversion(number):
""" This function will convert the roman number to integer number. """

roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} # Roman values dictionary.
sum = 0 # It calculates the integer value

for item in number:
if (roman_dict.get(item)):
print('Integer Value Of {0} Is: {1}'.format(item, roman_dict[item])) # prints the roman value of each item.
sum += roman_dict[item]

return sum


if __name__ == '__main__':
user_num = input("Enter Your Roman Number: ").upper()
if user_num != '':
result = roman_conversion(user_num) # calling function.

if result == 0:
print(" The \'{}\' is not a Roman Number.".format(user_num)) # when user input is invalid.
else:
print("\n The Integer Value of '{0}' Roman Number Is: {1} ".format(user_num, result))

else:
print("\n *** Empty Values are not Accepted *** ")


39 changes: 39 additions & 0 deletions string_anagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “act” and “tac” are anagram of each other.

Example:
Input:
2
geeksforgeeks forgeeksgeeks
allergy allergic

Output:
YES
NO

Explanation:
Testcase 1: Both the string have same characters with same frequency. So, both are anagrams.
Testcase 2: Characters in both the strings are not same, so they are not anagrams.

"""


def anagram_check( word1, word2):
""" Checks whether the strings are anagram or not. """

if (sorted(word1) == sorted(word2)): # It sorts the strings and compares each element.
result = "The Given Words:\'{}\' and \'{}\' are Anagram.".format(word1, word2)
else:
result = "The Given Words:\'{}\' and \'{}\' are not Anagram.".format(word1, word2)
return result


if __name__ == '__main__':
usr_input1 = input("Enter The 1st Word: ").lower()
usr_input2 = input("Enter The 2nd Word: ").lower()

if usr_input1 == '' or usr_input2 == '':
print("\n *** You Have Entered the Empty Value. ***")
else:
result = anagram_check(usr_input1, usr_input2)
print(result)
47 changes: 47 additions & 0 deletions string_anagram_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “act” and “tac” are anagram of each other.

Example:
Input:
2
geeksforgeeks forgeeksgeeks
allergy allergic

Output:
YES
NO

Explanation:
Testcase 1: Both the string have same characters with same frequency. So, both are anagrams.
Testcase 2: Characters in both the strings are not same, so they are not anagrams.

"""

from itertools import permutations


def anagram_check(word1, word2):
""" Checks whether the strings are anagram or not. """

if len(word1) == len(word2): # Comparing the len of each word.
w1_per = permutations(word1) # Finding permutation strings of user first word.
for item in w1_per: # iterating and comparing the each word for finding match.
format_word = ''.join(item) # Reformatting the each word from permutation list.
if format_word == word2:
answer = "The Given Words:\'{}\' and \'{}\' are Anagram.".format(word1, word2)
else:
answer = "The Given Words: \'{}\' and \'{}\' are NOT Anagram.".format(word1, word2)

return answer


if __name__ == '__main__':
usr_input1 = input("Enter The 1st Word: ").lower()
usr_input2 = input("Enter The 2nd Word: ").lower()

if usr_input1 == '' or usr_input2 == '':
print("\n *** You Have Entered the Empty Value. ***") # If both or either of the values are empty.
else:
result = anagram_check(usr_input1,usr_input2)
print(result)

37 changes: 37 additions & 0 deletions string_permutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Given a string S. The task is to print all permutations of a given string.

Example:
Input:
ABC

Output:
ABC ACB BAC BCA CAB CBA
"""

from itertools import permutations


def string_permutation(word):
""" This Function returns the permutated list of the user string """
words_list = []

permutations_list = permutations(word) # This function will generate all the strings and return a list.

for item in permutations_list:
format_word = ''.join(item)
words_list.append(format_word)
return words_list


if __name__ == '__main__':
usr_string = input("Enter The Word: ")

if (usr_string == ''):
print("\n *** Permutation of Empty Word cannot be found !!! *** ")
else:
result = string_permutation(usr_string)
print('The Permutation List of the given \'{}\' Word are as follows:'.format(usr_string))
for i in range(1, len(result)):
print(" Word {0}: {1}".format(i, result[i]))


26 changes: 26 additions & 0 deletions strstr_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Your task is to implement the function strstr. The function takes two strings as arguments (s,x) and locates the occurrence of the string x in the string s. The function returns and integer denoting the first occurrence of the string x in s.

Example:
Input
2
GeeksForGeeks Fr
GeeksForGeeks For
Output
-1
5
"""

if __name__ == '__main__':
usr_input1 = input("Enter The Sentence: ").lower()
usr_input2 = input("Enter The Word To Be Found: ").lower()

if usr_input1 == '' or usr_input2 == '': # If empty values are entered
print("\n *** You Have Entered the Empty Value. ***")
else:
find_operation = lambda s1, s2: s1.find(s2) # Lambda function for searh operation.
pos = find_operation(usr_input1, usr_input2)
if pos == -1:
print(f'Word \'{usr_input2}\' NOT Found.')
else:
print("Position Of \'{0}\' Word Is: {1} ".format(usr_input2, pos))
23 changes: 23 additions & 0 deletions word_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Example test statement:

foxy fox running into the jungle with the other fox
'''


def word_count(data):
data = data.split()
d_data = dict()
for d in data:
if d not in d_data:
d_data[d] = 1
else:
d_data[d] += 1
return d_data


if __name__ == '__main__':
data = input()
o_data = word_count(data)
print(f"Output: {o_data}")