diff --git a/For CS - Module 2 and 3 - Contest - Python /Goat Latin/README.md b/For CS - Module 2 and 3 - Contest - Python /Goat Latin/README.md new file mode 100644 index 0000000..6fbdafc --- /dev/null +++ b/For CS - Module 2 and 3 - Contest - Python /Goat Latin/README.md @@ -0,0 +1,24 @@ +## Problem Statement +# Goat Latin Conversion + +Given a sentence, S, consisting of words separated by spaces, we want to convert it into "Goat Latin," which is a fictional language similar to Pig Latin. + +The rules for Goat Latin conversion are as follows: + +If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word. For example, the word 'apple' becomes 'applema'. + +If a word begins with a consonant (a letter that is not a vowel), remove the first letter, append it to the end of the word, and then add "ma". For example, the word "goat" becomes "oatgma". + +Add one letter 'a' to the end of each word based on its word index in the sentence, starting with 1. For example, the first word gets "a" added to the end, the second word gets "aa" added to the end, and so on. + +Write a program that takes the sentence S as input and returns the final sentence representing its conversion into Goat Latin. + +Important Note: Ensure that you save your solution before progressing to the next question and before submitting your answer. + +Exercise 1: + +Input: +aasssdd + +Output: +aasssddmaa diff --git a/For CS - Module 2 and 3 - Contest - Python /Goat Latin/main.py b/For CS - Module 2 and 3 - Contest - Python /Goat Latin/main.py new file mode 100644 index 0000000..e99f12c --- /dev/null +++ b/For CS - Module 2 and 3 - Contest - Python /Goat Latin/main.py @@ -0,0 +1,18 @@ +def toGoatLatin(S): + vowels = set('aeiouAEIOU') + words = S.split() + result = [] + + for i, word in enumerate(words, 1): + if word[0] in vowels: + word += 'ma' + 'a' * i + else: + word = word[1:] + word[0] + 'ma' + 'a' * i + + result.append(word) + + return ' '.join(result) + +S = input() + +print(toGoatLatin(S)) diff --git a/For CS - Module 2 and 3 - Contest - Python /Network connection/README.md b/For CS - Module 2 and 3 - Contest - Python /Network connection/README.md new file mode 100644 index 0000000..4e8fb9b --- /dev/null +++ b/For CS - Module 2 and 3 - Contest - Python /Network connection/README.md @@ -0,0 +1,73 @@ +## Number of Operations to Make Network Connected +## Problem Statement +# Connecting a network + +In a geographical region, there are N cities labeled from 1 to N. These cities are interconnected using various transportation routes forming a network. The city connectivity is represented by a list of connections between cities in the form of a 2D array route[][]. Each row (city1, city2) denotes a direct transportation route between city1 and city2. + +Your task is to ensure that all cities are connected either directly or indirectly. To achieve this, you can perform the following operations: + + Remove any existing transportation route between two cities. + Add a new transportation route between two disconnected cities. + +Your goal is to find the minimum number of operations required to achieve full connectivity of all cities in the region. + +Return the minimum number of times you need to do this in order to make all the cities connected. If it is not possible, return -1. + +Example +Input: N = 6, + +routes[][] = {{1, 2}, {2, 3}, {3, 5}, {4, 5}, {5, 6}} + +Output: +1 + +Exercise-1 + +Input : + +6 + +5 + +0 1 + +0 2 + +0 3 + +1 2 + +1 3 + +Note: +Line 1: Available cities +Line 2 : No of routes +From line 3 : connected cities pair + +Output : + +2 + +Exercise-2 + +Input: + +5 + +5 + +0 1 + +0 2 + +0 3 + +1 2 + +2 3 + + +Output: + +1 + diff --git a/For CS - Module 2 and 3 - Contest - Python /Network connection/main.py b/For CS - Module 2 and 3 - Contest - Python /Network connection/main.py new file mode 100644 index 0000000..1e3dcf1 --- /dev/null +++ b/For CS - Module 2 and 3 - Contest - Python /Network connection/main.py @@ -0,0 +1,48 @@ +class UnionFind: + def __init__(self, n): + self.parent = list(range(n)) + self.rank = [0] * n + + def find(self, x): + if self.parent[x] != x: + self.parent[x] = self.find(self.parent[x]) + return self.parent[x] + + def union(self, x, y): + root_x = self.find(x) + root_y = self.find(y) + + if root_x != root_y: + if self.rank[root_x] < self.rank[root_y]: + root_x, root_y = root_y, root_x + self.parent[root_y] = root_x + if self.rank[root_x] == self.rank[root_y]: + self.rank[root_x] += 1 + +def makeConnected(n, connections): + uf = UnionFind(n) + extra_connections = 0 + + for u, v in connections: + if uf.find(u) != uf.find(v): + uf.union(u, v) + else: + extra_connections += 1 + + components = len(set(uf.find(i) for i in range(n))) + + if extra_connections >= components - 1: + return components - 1 + else: + return -1 + +# Input +n = int(input()) # Number of available cities +m = int(input()) # Number of routes +connections = [] +for _ in range(m): + u, v = map(int, input().split()) + connections.append([u, v]) + +result = makeConnected(n, connections) +print(result)