From 1a0a8643e0781a0455704a6011240dfc6ed8a65c Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Tue, 25 Jul 2017 07:21:33 +0530 Subject: [PATCH 01/15] Updated README --- Programs/.keylogger | 8 ++++++++ README.md | 1 + 2 files changed, 9 insertions(+) create mode 100644 Programs/.keylogger diff --git a/Programs/.keylogger b/Programs/.keylogger new file mode 100644 index 0000000..64b0be4 --- /dev/null +++ b/Programs/.keylogger @@ -0,0 +1,8 @@ +Shift_LOkar +Shift_LPathak +Shift_L H e l l o +t h e r e +Shift_L I +a m +Shift_L O m k a r +Shift_L P a t h a k diff --git a/README.md b/README.md index 92c4713..ad22e99 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ Pune, Maharashtra, India.
## Mini Projects * [Address Book](https://github.com/OmkarPathak/Python-Programs/blob/master/Programs/P61_AddressBook.py) With Add, Modify, Search. +* [Simple Python Keylogger](https://github.com/OmkarPathak/Python-Programs/blob/master/Programs/P79_SimplePythonKeylogger.py) ## Random Python Programs From 7634ad5968fc86a28e0a69ee8742de534b78a328 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Wed, 26 Jul 2017 07:14:05 +0530 Subject: [PATCH 02/15] Simple Python Keylogger --- Programs/P79_SimplePythonKeylogger.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Programs/P79_SimplePythonKeylogger.py b/Programs/P79_SimplePythonKeylogger.py index 91b637d..ff08573 100644 --- a/Programs/P79_SimplePythonKeylogger.py +++ b/Programs/P79_SimplePythonKeylogger.py @@ -1,5 +1,9 @@ # Author: OMKAR PATHAK +# This file requires two modules to be installed: +# 1. pyxhook.py: file is provided in the folder itself +# 2. Xlib: sudo pip3 install python3-Xlib + import pyxhook import time From 5e02d9c1820b92597966997c940b2e29f90d1eec Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Wed, 26 Jul 2017 07:14:33 +0530 Subject: [PATCH 03/15] Numpy Arithmetic Operations.py --- Numpy/P08_NumpyArithmeticOperations.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Numpy/P08_NumpyArithmeticOperations.py diff --git a/Numpy/P08_NumpyArithmeticOperations.py b/Numpy/P08_NumpyArithmeticOperations.py new file mode 100644 index 0000000..4c74718 --- /dev/null +++ b/Numpy/P08_NumpyArithmeticOperations.py @@ -0,0 +1,25 @@ +# Author: OMKAR PATHAK + +import numpy as np + +firstArray = np.arange(12).reshape(3, 4) +print(firstArray) + +secondArray = np.arange(4) +print(secondArray) + +# adding above two arrays (NOTE: array shapes should be same) +print(np.add(firstArray, secondArray)) + +# subtracting above two arrays +print(np.subtract(firstArray, secondArray)) + +# multiplying above two arrays +print(np.multiply(firstArray, secondArray)) + +# dividing the above two arrays +print(np.divide(firstArray, secondArray)) + +# numpy.power(): returns array element raised to the specified value result +array = np.array([1, 2, 3]) +print(np.power(array, 2)) # [1 4 9] From 66c1e60e4ba95062089150f332cf2dbc71314ca6 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Wed, 26 Jul 2017 07:15:43 +0530 Subject: [PATCH 04/15] Updated README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ad22e99..79dba69 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ Pune, Maharashtra, India.
5. [Numpy Array Manipulation operations](https://github.com/OmkarPathak/Python-Programs/blob/master/Numpy/P05_NumpyArrayManipulation.py) 6. [Numpy String Functions](https://github.com/OmkarPathak/Python-Programs/blob/master/Numpy/P06_NumpyStringFunctions.py) 7. [Numpy Mathematical Functions](https://github.com/OmkarPathak/Python-Programs/blob/master/Numpy/P07_NumpyMathematicalFunctions.py) +8. [Numpy Arithmetical Operations](https://github.com/OmkarPathak/Python-Programs/blob/master/Numpy/P08_NumpyArithmeticOperations.py) ## Mini Projects * [Address Book](https://github.com/OmkarPathak/Python-Programs/blob/master/Programs/P61_AddressBook.py) From 1096c95dc2365dc03322afda9e0595e8ea9c9330 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Wed, 26 Jul 2017 07:15:59 +0530 Subject: [PATCH 05/15] Added gitignore for pycache --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ From a9755bd0b2bdb41573f509df1fa3f8db2cfd5e28 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Fri, 28 Jul 2017 07:12:59 +0530 Subject: [PATCH 06/15] Bug fixes --- Programs/P07_PrimeNumber.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Programs/P07_PrimeNumber.py b/Programs/P07_PrimeNumber.py index e4ba9bf..485de60 100644 --- a/Programs/P07_PrimeNumber.py +++ b/Programs/P07_PrimeNumber.py @@ -3,18 +3,20 @@ def checkPrime(number): '''This function checks for prime number''' + isPrime = False if number == 2: print(number, 'is a Prime Number') if number > 1: for i in range(2, number): if number % i == 0: print(number, 'is not a Prime Number') + isPrime = False break else: - print(number, 'is a Prime Number') - break - else: - print(number, 'is not a Prime Number') + isPrime = True + + if isPrime: + print(number, 'is a Prime Number') if __name__ == '__main__': userInput = int(input('Enter a number to check: ')) From aa3bd48c2853ffe5e828160514706a5f703f0cb6 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Sun, 30 Jul 2017 07:38:38 +0530 Subject: [PATCH 07/15] Added iterative approach --- Programs/P08_Fibonacci.py | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/Programs/P08_Fibonacci.py b/Programs/P08_Fibonacci.py index 64b0afe..67133a1 100644 --- a/Programs/P08_Fibonacci.py +++ b/Programs/P08_Fibonacci.py @@ -8,27 +8,12 @@ def fibonacci(number): else: return (fibonacci(number - 1) + fibonacci(number - 2)) -def fibonacciFor(number): - '''This function calculates the fibonacci series for n-th term using loop''' - # first two terms - n1 = 0 - n2 = 1 - count = 2 - if number <= 0: - print("Please enter a positive integer") - elif number == 1: - print("Fibonacci sequence upto ",number,":") - print(n1) - else: - print("Fibonacci sequence upto ",number,":") - print(n1,n2,end=' ') - while count <= number: - nth = n1 + n2 - print(nth,end=' ') - # update values - n1 = n2 - n2 = nth - count += 1 +def fibonacci_without_recursion(number): + if number == 0: return 0 + fibonacci0, fibonacci1 = 0, 1 + for i in range(2, number + 1): + fibonacci1, fibonacci0 = fibonacci0 + fibonacci1, fibonacci1 + return fibonacci1 if __name__ == '__main__': userInput = int(input('Enter the number upto which you wish to calculate fibonnaci series: ')) @@ -36,4 +21,4 @@ def fibonacciFor(number): print(fibonacci(i),end=' ') print("\nUsing LOOP:") - fibonacciFor(userInput) + print(fibonacci_without_recursion(userInput)) From 1c02f15a27e249efdea8bec46b1275f0e7e9dc33 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Sun, 30 Jul 2017 07:38:47 +0530 Subject: [PATCH 08/15] Added iterative approach --- Programs/P09_Factorial.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Programs/P09_Factorial.py b/Programs/P09_Factorial.py index e9698b1..37b8dbc 100644 --- a/Programs/P09_Factorial.py +++ b/Programs/P09_Factorial.py @@ -8,6 +8,15 @@ def factorial(number): else: return number * factorial(number - 1) +def factorial_without_recursion(number): + fact = 1 + while(number > 0): + fact = fact * number + number = number - 1 + print('Factorial of', number,'is: ') + print(fact) + if __name__ == '__main__': userInput = int(input('Enter the number to find its factorial: ')) - print('Factorial of',userInput,'is:',factorial(userInput)) + print('Factorial of', userInput, 'is:', factorial(userInput)) + factorial_without_recursion(userInput) From 8e18e28056881ce472906ed1f6de9b4eb43b1a49 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Sun, 30 Jul 2017 07:38:58 +0530 Subject: [PATCH 09/15] Updated README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 79dba69..e03e7d0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # Python-Programs +[![GitHub stars](https://img.shields.io/github/stars/OmkarPathak/Python-Programs.svg)](https://github.com/OmkarPathak/Python-Programs/stargazers) +[![Python](https://img.shields.io/badge/Python-3.6-brightgreen.svg)] + This is my collection of Python Programs.
For python tutorials, visit my website:
http://www.omkarpathak.in From 696422734e7a9c130097e8f2e93e9db38654a5e0 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Fri, 4 Aug 2017 06:59:37 +0530 Subject: [PATCH 10/15] Script to find all IPs of devices connected in Network --- ...2_ScriptToFindDevicesConnectedInNetwork.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Scripts/P12_ScriptToFindDevicesConnectedInNetwork.py diff --git a/Scripts/P12_ScriptToFindDevicesConnectedInNetwork.py b/Scripts/P12_ScriptToFindDevicesConnectedInNetwork.py new file mode 100644 index 0000000..a46e215 --- /dev/null +++ b/Scripts/P12_ScriptToFindDevicesConnectedInNetwork.py @@ -0,0 +1,22 @@ +# Author: OMKAR PATHAK + +# This script helps to find the devices (mobiles and computers) connected to my wifi + +# This script needs python-nmap as a pre-requisite. To install: sudo pip3 install python-nmap + +import nmap +import subprocess + +# function to scan network and display IPs of conected devices +def scan_network(): + scanner = nmap.PortScanner() + myIP = subprocess.check_output(['hostname -I'], shell=True) + myIP = str(myIP, 'utf-8').split('.') + print(myIP[:3]) + scannedData = scanner.scan(hosts = '.'.join(myIP[:3]) + '.1/24', arguments = '-sP') + + # printing all the IP addresses of connected devices + for hostnames in scannedData['scan']: + print(hostnames) + +scan_network() From 643d005bcc5e6ef107ec1a1337760ba5a1da1fd9 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Fri, 4 Aug 2017 06:59:47 +0530 Subject: [PATCH 11/15] Updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e03e7d0..9a98c2a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python-Programs [![GitHub stars](https://img.shields.io/github/stars/OmkarPathak/Python-Programs.svg)](https://github.com/OmkarPathak/Python-Programs/stargazers) -[![Python](https://img.shields.io/badge/Python-3.6-brightgreen.svg)] +![Python](https://img.shields.io/badge/Python-3.6-brightgreen.svg) This is my collection of Python Programs.
For python tutorials, visit my website:
From 363cfebc941d8c497e626cb4e1a592c1e5a039a1 Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Sat, 12 Aug 2017 07:00:51 +0530 Subject: [PATCH 12/15] Updated README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 9a98c2a..4f7e945 100644 --- a/README.md +++ b/README.md @@ -164,3 +164,10 @@ An example of Python Lambda function Encryption/ Decryption using RSA Algorithm * [Python ftplib](https://github.com/OmkarPathak/Python-Programs/blob/master/Programs/P76_PythonFTP.py) A simple Python FTP file transfer example + +# Donation + +If you have found my softwares to be of any use to you, do consider helping me pay my internet bills. This would encourage me to create many such softwares :) + +| PayPal | Donate via PayPal! | +| ₹ (INR) | Donate via instamojo | From 4cc4cbda27cc60cb5ff8182cd278eb3d41caec0e Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Sat, 12 Aug 2017 07:02:03 +0530 Subject: [PATCH 13/15] Updated README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f7e945..ac9a677 100644 --- a/README.md +++ b/README.md @@ -170,4 +170,5 @@ A simple Python FTP file transfer example If you have found my softwares to be of any use to you, do consider helping me pay my internet bills. This would encourage me to create many such softwares :) | PayPal | Donate via PayPal! | -| ₹ (INR) | Donate via instamojo | +|:-------------------------------------------:|:-------------------------------------------------------------:| +| ₹ (INR) | Donate via Instamojo | From bc7d3346c445344acca91eec425566d1cd49020d Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Mon, 4 Sep 2017 07:14:11 +0530 Subject: [PATCH 14/15] Added Codechef easy problems --- .../CodeChef/P37_NDIFFPAL.py | 57 ++++++++++++++ .../CodeChef/P38_PRINCESS.py | 62 +++++++++++++++ CompetitiveProgramming/CodeChef/P39_ALATE.py | 76 +++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 CompetitiveProgramming/CodeChef/P37_NDIFFPAL.py create mode 100644 CompetitiveProgramming/CodeChef/P38_PRINCESS.py create mode 100644 CompetitiveProgramming/CodeChef/P39_ALATE.py diff --git a/CompetitiveProgramming/CodeChef/P37_NDIFFPAL.py b/CompetitiveProgramming/CodeChef/P37_NDIFFPAL.py new file mode 100644 index 0000000..73510e1 --- /dev/null +++ b/CompetitiveProgramming/CodeChef/P37_NDIFFPAL.py @@ -0,0 +1,57 @@ +# A palindrome is a string that reads same in both directions: forwards and backwards. For example, +# the strings radar and noon are palindromes, whereas the string chef is not a palindrome as being read +# backwards is becomes equal to fehc, which is not equal to chef. +# +# Let's say that the pair of indices (i, j) denotes a palindrome in some string S iff i ≤ j and the +# substring starting at the i-th character and ending at the j-th character of S is a palindrome. +# +# Given an integer N. Your task is to construct a string S such that there are exactly N different +# pairs (i, j) that denotes a palindrome. +# +# Input +# The first line of the input contains an integer T denoting the number of test cases. The description +# of T test cases follows. +# +# The first line of each test case contains a single integer N denoting the sought number of pairs that +# denote palindrome. +# +# Output +# For each test case, output a single line containing a string S, consisting of lowecase Latin letters, +# and having exactly N distinct palindrome-denoting pairs. If there's a few such strings, output any one. +# +# If such string S doesn't exist, output -1 instead of it. +# +# Constraints +# 1 ≤ T ≤ 100 +# 1 ≤ N ≤ 104 +# +# Example +# Input: +# 3 +# 6 +# 7 +# 2 +# +# Output: +# noon +# radar +# ab +# +# Explanation: +# Example case 1. In the string "noon", the pairs that denote a palindrome are (1-indexed): (1, 1), (1, 4), (2, 2), (2, 3), (3, 3), (4, 4). +# +# Example case 2. In the string "radar", the pairs that denote a palindrome are (1-indexed): (1, 1), (1, 5), (2, 2), (2, 4), (3, 3), (4, 4), (5, 5). +# +# Example case 3. In the string "ab", the pairs denoting a palindrome are : (1, 1), (2, 2) + +for _ in range(int(input())): + n = int(input()) + s = 'abcdefghijklmnopqrstuvwxyz' + if (n <= 26): + print(s[:n]) + else: + a = n // 26 + b = n % 26 + c = a * s + c = c + s[:b] + print (c) diff --git a/CompetitiveProgramming/CodeChef/P38_PRINCESS.py b/CompetitiveProgramming/CodeChef/P38_PRINCESS.py new file mode 100644 index 0000000..313a6a5 --- /dev/null +++ b/CompetitiveProgramming/CodeChef/P38_PRINCESS.py @@ -0,0 +1,62 @@ +# We all know that the princess is very beautiful but one day jealous from her beauty, a person asked a +# question from princess in order to check her wisdom. Since princess is not good at programming you need +# to help her in solving the problem. +# You are given a string of length N. You have to check among all the the substrings that whether a substring +# exist or not which is palindrome and having length greater than 1. If such a substring exists then print +# YES else print NO. +# +# Input +# The first line contains a single integer T, the number of test cases. Each test case is described by a +# single line containing a string. +# +# Output +# For each test case, output a single line containing the YES or NO. +# +# Constraints +# 1 ≤ T ≤ 10 +# 1 ≤ N ≤ 100000 +# +# Example +# Input: +# 2 +# ab +# babba +# +# Output: +# NO +# YES +# Explanation +# Example case 1.The only substring whose length is greater than 1 is ab, and its not a palindrome. +# +# Example case 2.abba is a substring of the string and its a palindrome thus YES. + +def manacher(string): + + string_with_bounds = '#'.join('^{}$'.format(string)) + length = len(string_with_bounds) + P = [0] * length + center = right = 0 + + for i in range(1, length - 1): + P[i] = (right > i) and min(right - i, P[2 * center - i]) + + # Attempt to expand palindrome centered at i + while string_with_bounds[i + 1 + P[i]] == string_with_bounds[i - 1 - P[i]]: + P[i] += 1 + + # If palindrome centered at i expand past R, + # adjust center based on expanded palindrome. + if i + P[i] > right: + center, right = i, i + P[i] + + # Find the maximum element in P and return the string + maxLen, centerIndex = max((n, i) for i, n in enumerate(P)) + return string[(centerIndex - maxLen)//2: (centerIndex + maxLen)//2] + +for _ in range(int(input())): + string = input() + result = manacher(string) + if len(result) > 1: + print('YES') + else: + print('NO') diff --git a/CompetitiveProgramming/CodeChef/P39_ALATE.py b/CompetitiveProgramming/CodeChef/P39_ALATE.py new file mode 100644 index 0000000..6d74eb6 --- /dev/null +++ b/CompetitiveProgramming/CodeChef/P39_ALATE.py @@ -0,0 +1,76 @@ +# Anmol always comes to class when the class is about to end. Frustrated by this behaviour of Anmol, his +# teacher has given him a special question as his homework. We all know that Anmol is very weak at computer +# science thus he came to you for help. Help Anmol in order to solve the problem. +# You are given an array A of length N(1 indexed). You have to process Q queries of two different types: +# 1 x — print func(x) +# 2 x y— change the value of A[x] to y. +# func(x) is defined as :: +# +# func(x) +# { +# sum = 0 ; +# for(i=x;i<=N;i+=x) +# sum = (sum + A[i]*A[i]) ; +# return sum ; +# } +# +# For each query of type 1 print the value of func(x) in a new line. +# Input +# The first line contains a single integer T, the number of test cases. +# Each test case is described as follows : +# The first line contains two numbers N and Q. +# In the next line N space separated numbers denoting the values of the array A. +# Each of the following Q lines contains a query of one of the above mentioned two types. +# Note :: Since the test files are large use scanf/printf for I/O. +# +# Output +# For each test case, For each query of type 1 print the required answer. +# +# +# Since the answer can be very large, output it modulo 1000000007 +# Constraints +# 1 ≤ T ≤ 10 +# 1 ≤ N ≤ 100000 +# 1 ≤ Q ≤ 100000 +# 1 ≤ A[i] ≤ 1e9 +# 1 ≤ x ≤ N +# 1 ≤ y ≤ 1e9 +# +# Subtasks +# +# Subtask #1 (20 points), Time limit : 1 sec +# 1 ≤ T<=10, N<=100 +# +# +# Subtask #2 (80 points), Time limit : 1 sec +# 1 ≤ T<=10, N<=100000 +# +# +# Example +# Input: +# 1 +# 5 3 +# 1 2 3 4 5 +# 1 1 +# 2 2 1 +# 1 2 +# Output: +# 55 +# 17 + +def func(x): + sum = 0 + for i in range(x, int(n) + 1, x): + sum = sum + array[i] * array[i] + return sum + +for _ in range(int(input())): + n, q = input().split() + array = [int(i) for i in input().split()] + array.insert(0, 0) + for _ in range(int(q)): + inputs = [int(i) for i in input().split()] + if len(inputs) == 2: + print(func(inputs[1])) + else: + array[inputs[1]] = inputs[2] From 71f7668e342ab385b84fb44f13570c65e0d06bfb Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Mon, 4 Sep 2017 07:14:38 +0530 Subject: [PATCH 15/15] Added HackerEarth Graph problems --- .../Graphs/Monk-At-The-Graph-Factory.py | 38 +++++++++++++++++++ .../Graphs/Monk-In-The-Real-Estate.py | 31 +++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-At-The-Graph-Factory.py create mode 100644 CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-In-The-Real-Estate.py diff --git a/CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-At-The-Graph-Factory.py b/CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-At-The-Graph-Factory.py new file mode 100644 index 0000000..ebe2167 --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-At-The-Graph-Factory.py @@ -0,0 +1,38 @@ +# Our Code Monk recently learnt about Graphs and is very excited! +# +# He went over to the Graph-making factory to watch some freshly prepared graphs. Incidentally, +# one of the workers at the factory was ill today, so Monk decided to step in and do her job. +# +# The Monk's Job is to Identify whether the incoming graph is a tree or not. He is given N, the number +# of vertices in the graph and the degree of each vertex. +# +# Find if the graph is a tree or not. +# +# Input: +# First line contains an integer N, the number of vertices. +# Second line contains N space-separated integers, the degrees of the N vertices. +# +# Output: +# Print "Yes" (without the quotes) if the graph is a tree or "No" (without the quotes) otherwise. +# +# Constraints: +# 1 ≤ N ≤ 100 +# 1 ≤ Degreei ≤ 1000 +# +# SAMPLE INPUT +# 3 +# 1 2 1 +# +# SAMPLE OUTPUT +# Yes + +n = int(input()) +degrees = [int(i) for i in input().split()] + +# Number of nodes are given thus in a tree number of edges are (n-1) and each edge has two degree +# thus in tree data structure total degree should be 2*(n-1) and this should be equal to sum of given degree + +if(2 * (n - 1) == sum(degrees)): + print('Yes') +else: + print('No') diff --git a/CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-In-The-Real-Estate.py b/CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-In-The-Real-Estate.py new file mode 100644 index 0000000..93fb5c7 --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Algorithms/Graphs/Monk-In-The-Real-Estate.py @@ -0,0 +1,31 @@ +# The Monk wants to buy some cities. To buy two cities, he needs to buy the road connecting those two cities. +# Now, you are given a list of roads, bought by the Monk. You need to tell how many cities did the Monk buy. +# +# Input: +# First line contains an integer T, denoting the number of test cases. The first line of each test case +# contains an integer E, denoting the number of roads. The next E lines contain two space separated +# integers X and Y, denoting that there is an road between city X and city Y. +# +# Output: +# For each test case, you need to print the number of cities the Monk bought. +# +# Constraint: +# 1 <= T <= 100 +# 1 <= E <= 1000 +# 1 <= X, Y <= 10000 +# +# SAMPLE INPUT +# 1 +# 3 +# 1 2 +# 2 3 +# 1 3 + +for _ in range(int(input())): + roads = int(input()) + lst = set([]) + for i in range(roads): + node1,node2 = map(int,input().split()) + lst.add(node1) + lst.add(node2) + print(len(lst))