-
Notifications
You must be signed in to change notification settings - Fork 0
Feature implementation from commits 5c86720..71f7668 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature-base-5
Are you sure you want to change the base?
Changes from all commits
1a0a864
7634ad5
5e02d9c
66c1e60
1096c95
a9755bd
aa3bd48
1c02f15
8e18e28
6964227
643d005
363cfeb
4cc4cbd
bc7d334
71f7668
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| __pycache__ |
| 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) | ||
| 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') |
| 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): | ||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||
| sum = sum + array[i] * array[i] | ||||||||||||||||||||||||||
| return sum | ||||||||||||||||||||||||||
|
Comment on lines
+61
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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])) | ||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||
| 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)) |
| 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)) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||
|
|
||||||
| # 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] | ||||||
| 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 |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,18 +3,20 @@ | |||||||||||
|
|
||||||||||||
| def checkPrime(number): | ||||||||||||
| '''This function checks for prime number''' | ||||||||||||
| isPrime = False | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||
| if number == 2: | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||
| print(number, 'is a Prime Number') | ||||||||||||
| if number > 1: | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||
| 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 | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||
|
|
||||||||||||
| if isPrime: | ||||||||||||
| print(number, 'is a Prime Number') | ||||||||||||
|
|
||||||||||||
| if __name__ == '__main__': | ||||||||||||
| userInput = int(input('Enter a number to check: ')) | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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: ') | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||
| print(fact) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||
|
|
||||||
| 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) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||
There was a problem hiding this comment.
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):
📝 Committable suggestion