Skip to content

Conversation

@yashuatla
Copy link
Owner

@yashuatla yashuatla commented Jun 22, 2025

PR Summary

Add Competitive Programming Solutions and Enhance Python Examples

Overview

This PR adds multiple competitive programming solutions from CodeChef and HackerEarth platforms, primarily focusing on algorithmic problems. It also enhances existing Python examples and adds new utility scripts for network scanning and NumPy operations.

Change Types

Type Description
Feature Added new competitive programming solutions for CodeChef and HackerEarth
Enhancement Improved implementation of existing Python examples
Feature Added utility scripts for network scanning and NumPy operations

Affected Modules

Module / File Change Description
CodeChef/P37_NDIFFPAL.py New solution for constructing strings with N different palindrome pairs
CodeChef/P38_PRINCESS.py Implementation of Manacher's algorithm for palindromic substrings
CodeChef/P39_ALATE.py Solution for processing array queries and calculating squared value sums
HackerEarth/Algorithms/Graphs/Monk-At-The-Graph-Factory.py New solution to determine if a graph is a tree based on vertex degrees
HackerEarth/Algorithms/Graphs/Monk-In-The-Real-Estate.py Solution for counting unique cities from a list of roads using sets
P08_NumpyArithmeticOperations.py New file demonstrating various NumPy arithmetic operations
P07_PrimeNumber.py Improved prime number checking logic using a flag approach
P08_Fibonacci.py Replaced function to return nth Fibonacci number instead of printing sequence
P09_Factorial.py Added non-recursive factorial implementation
P79_SimplePythonKeylogger.py Added documentation for required dependencies
P12_ScriptToFindDevicesConnectedInNetwork.py New script to scan and display devices on local network

Notes for Reviewers

  • The network scanning script (P12) requires nmap to be installed on the system
  • The keylogger script (P79) has external dependencies that are now documented

Comment on lines +50 to +57
if (n <= 26):
print(s[:n])
else:
a = n // 26
b = n % 26
c = a * s
c = c + s[:b]
print (c)
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)


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):

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)

Comment on lines +61 to +65
def func(x):
sum = 0
for i in range(x, int(n) + 1, x):
sum = sum + array[i] * array[i]
return sum
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

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)))


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

def checkPrime(number):
'''This function checks for prime number'''
isPrime = 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

isPrime = False
if number == 2:
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):

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

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: ')

fact = fact * number
number = number - 1
print('Factorial of', number,'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

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))

# function to scan network and display IPs of conected devices
def scan_network():
scanner = nmap.PortScanner()
myIP = subprocess.check_output(['hostname -I'], shell=True)
Copy link

Choose a reason for hiding this comment

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

🔒 Security Issue

Security: Command Injection Vulnerability.

Using shell=True with subprocess.check_output creates a command injection vulnerability, even with hardcoded commands.

Current Code (Diff):

-     myIP = subprocess.check_output(['hostname -I'], shell=True)
+     myIP = subprocess.check_output(['hostname', '-I'], shell=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
myIP = subprocess.check_output(['hostname -I'], shell=True)
myIP = subprocess.check_output(['hostname', '-I'], shell=False)

scanner = nmap.PortScanner()
myIP = subprocess.check_output(['hostname -I'], shell=True)
myIP = str(myIP, 'utf-8').split('.')
print(myIP[:3])
Copy link

Choose a reason for hiding this comment

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

🐛 Correctness Issue

Correctness: Debug Print in Production.

Debug print statement left in production code will cause unexpected output during normal operation.

Current Code (Diff):

-     print(myIP[:3])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants