diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ 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] 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)) 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] 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/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: ')) 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)) 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) 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 diff --git a/README.md b/README.md index 92c4713..ac9a677 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 @@ -107,10 +110,12 @@ 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) With Add, Modify, Search. +* [Simple Python Keylogger](https://github.com/OmkarPathak/Python-Programs/blob/master/Programs/P79_SimplePythonKeylogger.py) ## Random Python Programs @@ -159,3 +164,11 @@ 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 | 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()