Python polynomial division - wrong remainder output - python-3.x

I made this polynomial division program, and it works just fine for float() values.
The script is supposed to be called using cmd like this: python main.py input.txt
The file input.txt contains two lines of numbers which the script converts to two lists of polynomial coeficients. So when the first line of input.txt is , say, 1 5 3 7, the script reads the line as 1x^0 + 5x^1 + 3x^2 + 7x^3.
When the program reads the text.txt it does the division and prints out two lines, the first being the result and the second being the remainder.
So basically if input.txt contains these lines:
0 12 -2 -4
3 -2
The program prints:
0 4 2
0
Where the first line is the result and the second one the remainder.
Now here's the code:
import sys
text = open(sys.argv[1])
dividend = list(text.readline().split())
divisor = list(text.readline().split())
for i in range(0, len(divisor)):
divisor[i] = int(divisor[i])
for i in range(0, len(dividend)):
dividend[i] = int(dividend[i])
def polynomial_division(dividend, divisor):
divid = dividend
divis = divisor[0]
for v in range(len(dividend)-(len(divisor)-1)):
divid[v] /= divis
coef = divid[v]
if coef != 0:
for i in range(1, len(divisor)):
divid[v + i] += -divisor[i] * coef
separator = -(len(divisor)-1)
output = (divid[:separator])
remainder = (divid[separator:])
print(*output, sep=' ')
print(*remainder, sep=' ')
polynomial_division(dividend, divisor)
However my teacher said the output should be int() values, so I modified it with the // operator and now the output contains int() values.
import sys
text = open(sys.argv[1])
dividend = list(text.readline().split())
divisor = list(text.readline().split())
for i in range(0, len(divisor)):
divisor[i] = int(divisor[i])
for i in range(0, len(dividend)):
dividend[i] = int(dividend[i])
def polynomial_division(dividend, divisor):
divid = dividend
divis = divisor[0]
for v in range(len(dividend)-(len(divisor)-1)):
divid[v] //= divis
coef = divid[v]
if coef != 0:
for i in range(1, len(divisor)):
divid[v + i] += -divisor[i] * coef
separator = -(len(divisor)-1)
output = (divid[:separator])
remainder = (divid[separator:])
print(*output, sep=' ')
print(*remainder, sep=' ')
polynomial_division(dividend, divisor)
But when the input.txt contains:
5 -3 6 2 -8
2 0 3 4
The output is:
2 -2
0 0 0
Which means the output is wrong as it should be:
2 -2
1 1
So my question is, what did I do wrong?

Related

python get majority element using divide and conquer

your textHello can someone explain the code with sample 1 input(specially recursive call) . i am confused with these 2 recursive call lines,
left_maj = get_majority_element(a, left, mid)
right_maj = get_majority_element(a, mid, right)
Task. The goal in this code problem is to check whether an input sequence contains a majority element.
Input Format. The first line contains an integer 𝑛, the next one contains a sequence of 𝑛 non-negative
integers 𝑎0, 𝑎1, . . . , 𝑎𝑛−1.
Constraints. 1 ≤ 𝑛 ≤ 105
; 0 ≤ 𝑎𝑖 ≤ 109
for all 0 ≤ 𝑖 < 𝑛.
Output Format. Output 1 if the sequence contains an element that appears strictly more than 𝑛/2 times,
and 0 otherwise.
Sample 1.
Input:
5
2 3 9 2 2
Output:
1
2 is the majority element.
code :
import sys, math
def get_majority_element(a, left, right):
if left == right:
return -1
if left + 1 == right:
return a[left]
mid = (left + right) // 2
half_element_count = (right - left) // 2
left_maj = get_majority_element(a, left, mid)
right_maj = get_majority_element(a, mid, right)
left_maj_count = get_element_count(a, left, right, left_maj)
if left_maj_count > half_element_count:
return left_maj
right_maj_count = get_element_count(a, left, right, right_maj)
if right_maj_count > half_element_count:
return right_maj
return -1
def get_element_count(a, left, right, x):
maj_count = 0
for i in range(left, right):
if a[i] == x:
maj_count += 1
return maj_count
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
if get_majority_element(a, 0, n) != -1:
print(1)
else:
print(0)
i tried to debug using pdb but got confused during recursive call

How to create a loop that iterates adding logical test to an if

I find my self trying to analyse a data set and find how some variables correlate.
I need to add a loop that adds a logical test to the if statement:
Edited:
Example:
Take this data frame as example
In [11]: df
Out[11]:
INPUT1 INPUT2 INPUT3 ... OUTPUT
0 8 5 6 ... 1
1 3 2 5 ... 0
2 3 1 5 ... 1
3 1 2 5 ... 0
4 4 3 5 ... 0
I'm testing the combinations of inputs to check how they match the output
def greater_than(a,b):
return a > b
def greater_equal_than(a,b):
return a >= b
def lower_equal_than(a,b):
return a <= b
def lower_than(a,b):
return a < b
def equal(a,b):
return a == b
operation = { '>': greater_than, '>=': greater_equal_than, '<=': lower_equal_than, '<': lower_than }
escenario = pd.DataFrame(columns=['esc','pf'])
for i in range(len(names)):
for j in names[i+1:]:
for op in operation:
escenario['esc'] = df.apply(lambda x : 1 if operation[op]( names[i], j ) else 0, axis=1)
escenario['pf'] = df['OUTPUT']
match = escenario.apply(lambda x : 1 if x['pf'] == 1 and x['pf'] == x['esc'] else 0, axis=1 )
percent_match = (100 * match.sum())/escenario['pf'].sum()
percent_no_match = (100 *(escenario['esc'].sum() - match.sum())) / escenario['esc'].sum()
print( f"{names[i]} {op} {j} -> { percent_match } / {percent_no_match} " )
I need to check all the combinations of input combinations that keeps percent_match closer to a 100% and percent_no_match closer to 0%
for example:
first iteration:
INPUT2 < INPUT3
SECOND INTERATION
INPUT2 < INPUT3 and INPUT1 > INPUT2
Right now I'm running the code, sorting the print and getting the couple where the match is closer to 100 and the modifying the code to add the match, Example:
First run better output is INPUT2 < INPUT3
Then I modify this line:
escenario['esc'] = df.apply(lambda x : 1 if operation[op]( names[i], j ) else 0, axis=1)
to add the first output, like:
escenario['esc'] = df.apply(lambda x : 1 if df['INPUT2'] < DF['INPUT3'] and operation[op]( names[i], j ) else 0, axis=1)
and check again...
This last part is the one I want to automate through a loop.
Thanks
I found self modifying python script that fits perfectly into my need.
It allows to recreate a function about of a text string and that's exactly what I need.
Thanks!

Python(AI Constraint satisfaction problem) Fitting square and/or rectangular (2d) tiles onto a rectangular ground

I have to arrange and/or fit 2d tiles into a 2d square or rectangular plane with AI algorithm using python program. Each tile has a length and width. For example if a plane is 4x3 and set of tiles is
S={(2,3),(1,2),(2,2)}
these tiles can be rotated 90 degrees in order to fit the matrix.
input
first line contains length and width of the plane
second line number of tiles
and then the length,width of the subsequent tiles
but the inputs should be tab seperated
for eg
4 3
3
2 3
1 2
2 2
output
for eg
1 1 2 2
1 1 3 3
1 1 3 3
I have trouble solving this as i have to use only standard libraries in python no NumPy and no CSP library
~Edit 2`
my code so far I cant figure out how to add algorithm without csp library or to generate grid
from sys import stdin
a = stdin.readline()
x = a.split()
rectangular_plane = [[0] * int(x[0]) for i in range(int(x[1]))]
num_of_rectangles = stdin.readline()
r_widths = []
r_lengths= []
for l in range(int(num_of_rectangles)):
b = stdin.readline()
y = b.split()
r_lengths.insert(l,y[0])
r_widths.insert(l,y[1])
I've solved task with backtracking approach and without any non-standard modules.
Try it online!
import sys
nums = list(map(int, sys.stdin.read().split()))
pw, ph = nums[0:2]
ts = list(zip(nums[3::2], nums[4::2]))
assert len(ts) == nums[2]
if sum([e[0] * e[1] for e in ts]) != pw * ph:
print('Not possible!')
else:
def Solve(*, it = 0, p = None):
if p is None:
p = [[0] * pw for i in range(ph)]
if it >= len(ts):
for e0 in p:
for e1 in e0:
print(e1, end = ' ')
print()
return True
for tw, th in [(ts[it][0], ts[it][1]), (ts[it][1], ts[it][0])]:
zw = [0] * tw
ow = [it + 1] * tw
for i in range(ph - th + 1):
for j in range(pw - tw + 1):
if all(p[k][j : j + tw] == zw for k in range(i, i + th)):
for k in range(i, i + th):
p[k][j : j + tw] = ow
if Solve(it = it + 1, p = p):
return True
for k in range(i, i + th):
p[k][j : j + tw] = zw
return False
if not Solve():
print('Not possible!')
Example input:
4 3
3
2 3
1 2
2 2
Output:
1 1 2 2
1 1 3 3
1 1 3 3

Max Points on a Line with python 3

algorithm question:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Example 1:
Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:
^
|
| o
| o
| o
+------------->
0 1 2 3 4
Example 2:
Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
the working python 3 code is below:
wondering
snippet 1 d[slope] = d.get(slope, 1) + 1 is working
but why this snippet 2 is not working correctly for example 2 even though snippet 1 and 2 are the same
if slope in d:
d[slope] += 1
else:
d[slope] = 1
def gcd(self, a, b):
if b == 0:
return a
return self.gcd(b, a%b)
def get_slope(self, p1, p2):
dx = p1[0] - p2[0]
dy = p1[1] - p2[1]
c = self.gcd(dx, dy)
dx /= c
dy /= c
return str(dy) + "/" + str(dx)
def is_same_points(self, p1:List[int], p2:List[int]):
return p1[0] == p2[0] and p1[1] == p2[1]
def maxPoints(self, points: List[List[int]]) -> int:
if not points:
return 0
n = len(points)
count = 1
for i in range(0, n):
d = {}
duped = 0
localmax = 1
p1 = points[i]
for j in range(i+1, n):
p2 = points[j]
if self.is_same_points(p1, p2):
duped += 1
else:
slope = self.get_slope(p1, p2)
# 1) not work: output is 3 in example 2
# if slope in d:
# d[slope] += 1
# else:
# d[slope] = 1
# 2) works: correct output 4 for example 2
d[slope] = d.get(slope, 1) + 1
localmax = max(localmax, d[slope]);
count = max(count, localmax + duped)
return count
Interesting problem and nice solution.
The reason why the commented out code doesn't work is because of that:
else:
d[slope] = 1 ## correct would be d[slope] = 2
Every 2 points are on the same line, you are counting only one point for the first two p1 p2, thus you get one less in the final answer.

Code fails on Test case. (InterQuartile Range)

This is a challenge from 10 Day statistics on Hackerrank.(https://www.hackerrank.com/challenges/s10-interquartile-range/problem?h_r=next-challenge&h_v=zen)
Task :
Task
The interquartile range of an array is the difference between its first (Q1) and third (Q3) quartiles (i.e., Q3 - Q1).
Given an array,X, of n integers and an array, F, representing the respective frequencies of X's elements, construct a data set, S, where each xi occurs at frequency fi. Then calculate and print S's interquartile range, rounded to a scale of 1 decimal place (i.e., 12.3 format).
Following is my code.
n = int(input())
x = list(map(int, input().split()))
f = list(map(int, input().split()))
s = []
for i in range(len(x)):
j = f[i]
for k in range(j):
s.append(x[i])
n = len(s)
s.sort()
if n%2 == 0:
Q21 = s[n//2]
Q22 = s[n//2 - 1]
Q2 = (Q21 + Q22) / 2
else:
Q2 = s[n//2]
LH = s[:n//2]
if n%2==0:
UH = s[n//2:]
else:
UH = s[n//2+1:]
Q1_len = len(LH)
Q3_len = len(UH)
if Q1_len%2 == 0:
Q11 = LH[Q1_len//2]
Q12 = LH[Q1_len//2 - 1]
Q1 = (Q11 + Q12) / 2
else:
Q1 = LH[Q1_len//2]
if Q3_len%2 == 0:
Q31 = UH[Q3_len//2]
Q32 = UH[Q3_len//2 - 1]
Q3 = (Q31 + Q32) / 2
else:
Q3 = UH[Q3_len//2]
print(round(Q3 - Q1,1))
# print(int(Q2))
# print(int(Q3))
Here is the test case: with std input.
5
10 40 30 50 20
1 2 3 4 5
Expected output:
30.0
My code output:
30.0 # I get this output on my code editor but not on Hackerrank
Can someone help me on this where I am wrong ?
I get the output what is expected but it shows as failed.
print(float(Q3 - Q1))
Basically is the answer.

Resources