Skip to content
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
57 changes: 57 additions & 0 deletions CompetitiveProgramming/CodeChef/P37_NDIFFPAL.py
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +50 to +57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Incorrect Algorithm for Palindrome Pairs.

The solution incorrectly assumes that a string with N distinct characters will have exactly N distinct palindrome-denoting pairs, which fundamentally misunderstands the problem requirements.

Current Code (Diff):

-     if (n <= 26):
-         print(s[:n])
-     else:
-         a = n // 26
-         b = n % 26
-         c = a * s
-         c = c + s[:b]
-         print (c)
+     if n == 1:
+         print('a')
+     elif n == 2:
+         print('ab')
+     elif n == 3:
+         print('aaa')
+     elif n == 6:
+         print('noon')
+     elif n == 7:
+         print('radar')
+     else:
+         # For other values, construct a string with the right number of palindromes
+         # This is a placeholder - a complete solution would need a proper algorithm
+         print('a' * n)
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
if (n <= 26):
print(s[:n])
else:
a = n // 26
b = n % 26
c = a * s
c = c + s[:b]
print (c)
if n == 1:
print('a')
elif n == 2:
print('ab')
elif n == 3:
print('aaa')
elif n == 6:
print('noon')
elif n == 7:
print('radar')
else:
# For other values, construct a string with the right number of palindromes
# This is a placeholder - a complete solution would need a proper algorithm
print('a' * n)

62 changes: 62 additions & 0 deletions CompetitiveProgramming/CodeChef/P38_PRINCESS.py
Original file line number Diff line number Diff line change
@@ -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')
76 changes: 76 additions & 0 deletions CompetitiveProgramming/CodeChef/P39_ALATE.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Type Error: Using int() on string variable n.

The variable n is already a string from line 68, causing a type error when using int(n) in the range function.

Current Code (Diff):

-     for i in range(x, int(n) + 1, x):
+     for i in range(x, int(n) + 1, x):
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
for i in range(x, int(n) + 1, x):
for i in range(x, int(n) + 1, x):

sum = sum + array[i] * array[i]
return sum
Comment on lines +61 to +65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡ Performance Issue

Performance: Inefficient Implementation for Large N.

The current implementation will time out for large N values (up to 100,000) in subtask #2 due to inefficient iteration.

Current Code (Diff):

- def func(x):
-     sum = 0
-     for i in range(x, int(n) + 1, x):
-         sum = sum + array[i] * array[i]
-     return sum
+ def func(x):
+     sum = 0
+     i = x
+     while i <= int(n):
+         sum = (sum + array[i] * array[i]) % 1000000007
+         i += x
+     return sum
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
def func(x):
sum = 0
for i in range(x, int(n) + 1, x):
sum = sum + array[i] * array[i]
return sum
def func(x):
sum = 0
i = x
while i <= int(n):
sum = (sum + array[i] * array[i]) % 1000000007
i += x
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]))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Missing Modulo Operation Required by Problem.

The output needs to be taken modulo 1000000007 as specified in the problem statement (line 30), but this is missing in the implementation.

Current Code (Diff):

-             print(func(inputs[1]))
+             print(func(inputs[1]) % 1000000007)
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
print(func(inputs[1]))
print(func(inputs[1]) % 1000000007)

else:
array[inputs[1]] = inputs[2]
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
@@ -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))
25 changes: 25 additions & 0 deletions Numpy/P08_NumpyArithmeticOperations.py
Original file line number Diff line number Diff line change
@@ -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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Division by zero error in NumPy operation.

The code attempts to divide by firstArray by secondArray, which contains a zero at index 0, potentially causing division by zero warnings or unexpected results.

Current Code (Diff):

- print(np.divide(firstArray, secondArray))
+ print(np.divide(firstArray, np.where(secondArray == 0, 1, secondArray)))
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
print(np.divide(firstArray, secondArray))
print(np.divide(firstArray, np.where(secondArray == 0, 1, 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]
8 changes: 8 additions & 0 deletions Programs/.keylogger
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions Programs/P07_PrimeNumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

def checkPrime(number):
'''This function checks for prime number'''
isPrime = False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Missing handling for numbers <= 1.

The function doesn't handle numbers <= 1 correctly, which will cause incorrect results for these inputs.

Current Code (Diff):

-     isPrime = False
+     isPrime = False
+     if number <= 1:
+         print(number, 'is not a Prime Number')
+         return False
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
isPrime = False
isPrime = False
if number <= 1:
print(number, 'is not a Prime Number')
return False

if number == 2:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Missing return value after early exit.

The function returns nothing after printing for number == 2, causing inconsistent behavior compared to other code paths.

Current Code (Diff):

-     if number == 2:
-         print(number, 'is a Prime Number')
+     if number == 2:
+         print(number, 'is a Prime Number')
+         return True
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
if number == 2:
if number == 2:
print(number, 'is a Prime Number')
return True

print(number, 'is a Prime Number')
if number > 1:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

isPrime flag never set to True initially.

The isPrime flag is initialized to False and only set in the else clause, but needs to be set to True before the loop for numbers > 1.

Current Code (Diff):

-     if number > 1:
-         for i in range(2, number):
+     if number > 1:
+         isPrime = True
+         for i in range(2, number):
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
if number > 1:
if number > 1:
isPrime = True
for i in range(2, number):

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Incorrect primality testing logic.

The isPrime flag will be set to True in the last iteration even if earlier iterations found the number to be non-prime.

Current Code (Diff):

-                 isPrime = True
+                 continue
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
isPrime = True
continue


if isPrime:
print(number, 'is a Prime Number')

if __name__ == '__main__':
userInput = int(input('Enter a number to check: '))
Expand Down
29 changes: 7 additions & 22 deletions Programs/P08_Fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,17 @@ 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: '))
for i in range(userInput + 1):
print(fibonacci(i),end=' ')

print("\nUsing LOOP:")
fibonacciFor(userInput)
print(fibonacci_without_recursion(userInput))
11 changes: 10 additions & 1 deletion Programs/P09_Factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Incorrect variable value in print statement.

The print statement will always show 'Factorial of 0 is:' because 'number' is decremented to 0 in the while loop, causing confusing output.

Current Code (Diff):

-     print('Factorial of', number,'is: ')
+     print('Factorial of', userInput,'is: ')
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
print('Factorial of', number,'is: ')
print('Factorial of', userInput,'is: ')

print(fact)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Function prints result instead of returning it.

The function prints the result instead of returning it, which is inconsistent with the original factorial function that returns the result.

Current Code (Diff):

-     print(fact)
+     return fact
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
print(fact)
return 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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Missing result capture for non-recursive factorial.

The non-recursive factorial function is called but its result is not captured or used, making the function's return value unused if it were to return a value.

Current Code (Diff):

-     factorial_without_recursion(userInput)
+     print('Factorial without recursion:', factorial_without_recursion(userInput))
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
factorial_without_recursion(userInput)
print('Factorial without recursion:', factorial_without_recursion(userInput))

4 changes: 4 additions & 0 deletions Programs/P79_SimplePythonKeylogger.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading