From 417754c219aee03261ba7009b64c6fa574be2096 Mon Sep 17 00:00:00 2001 From: Driti Tannk Date: Tue, 5 May 2020 13:10:12 +0530 Subject: [PATCH 1/3] Update Changes and Add Files Remove Unwanted Files. Add string_permutation and reverse_string Files. --- .gitignore.txt | 2 ++ reverse_string.py | 37 +++++++++++++++++++++++++++++++++++++ string_permutation.py | 37 +++++++++++++++++++++++++++++++++++++ word_count.py | 23 +++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 .gitignore.txt create mode 100644 reverse_string.py create mode 100644 string_permutation.py create mode 100644 word_count.py diff --git a/.gitignore.txt b/.gitignore.txt new file mode 100644 index 0000000..4a3d485 --- /dev/null +++ b/.gitignore.txt @@ -0,0 +1,2 @@ + +.idea/* \ No newline at end of file diff --git a/reverse_string.py b/reverse_string.py new file mode 100644 index 0000000..3716719 --- /dev/null +++ b/reverse_string.py @@ -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}") diff --git a/string_permutation.py b/string_permutation.py new file mode 100644 index 0000000..3628fcf --- /dev/null +++ b/string_permutation.py @@ -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])) + + diff --git a/word_count.py b/word_count.py new file mode 100644 index 0000000..e5bed24 --- /dev/null +++ b/word_count.py @@ -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}") + From 347930ff6528c8c7132004024142f3b822e68e84 Mon Sep 17 00:00:00 2001 From: Driti Tannk Date: Tue, 5 May 2020 19:16:04 +0530 Subject: [PATCH 2/3] Add Anagram And Roman Number Conversion Solution Solve The String Anagram And Roman Number Conversion Problems. Add Two Different Ways Of Solving String Anagram Problem. --- roman_number_conversion.py | 42 ++++++++++++++++++++++++++++++++++ string_anagram.py | 39 +++++++++++++++++++++++++++++++ string_anagram_v1.py | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 roman_number_conversion.py create mode 100644 string_anagram.py create mode 100644 string_anagram_v1.py diff --git a/roman_number_conversion.py b/roman_number_conversion.py new file mode 100644 index 0000000..0a05aad --- /dev/null +++ b/roman_number_conversion.py @@ -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 *** ") + + diff --git a/string_anagram.py b/string_anagram.py new file mode 100644 index 0000000..4986685 --- /dev/null +++ b/string_anagram.py @@ -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) diff --git a/string_anagram_v1.py b/string_anagram_v1.py new file mode 100644 index 0000000..9f8d83b --- /dev/null +++ b/string_anagram_v1.py @@ -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) + From b349076487fea558a1d7abc0b2e3bf4f3a03bcdc Mon Sep 17 00:00:00 2001 From: Driti Tannk Date: Thu, 7 May 2020 10:34:19 +0530 Subject: [PATCH 3/3] Add common_prefix.py And strstr_solution.py File Add Solution For LongestCommonPrefix Problem And ImplementStrstr Problem. --- common_prefix.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ strstr_solution.py | 26 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 common_prefix.py create mode 100644 strstr_solution.py diff --git a/common_prefix.py b/common_prefix.py new file mode 100644 index 0000000..b98f183 --- /dev/null +++ b/common_prefix.py @@ -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. ***") + diff --git a/strstr_solution.py b/strstr_solution.py new file mode 100644 index 0000000..c248492 --- /dev/null +++ b/strstr_solution.py @@ -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))