Hackerrank - why is my output being written one character at a time? - python-3.x

I am solving the following "Vertical Sticks "hackerrank challenge: https://www.hackerrank.com/challenges/vertical-sticks/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen
Here is my solution:
def solve(y):
out = []
x = list(itertools.permutations(y))
for yp in x:
arr = []
arr.append(1)
for i in range(int(1),int(len(yp))):
#flag = 0
for j in range(int(i-1),int(-1),int(-1)):
if yp[j] >= yp[i]:
arr.append(i-j)
#flag+=1
break
if j==0:
arr.append(i+1)
out.append(sum(arr))
p = round((sum(out)/len(out)),2)
pp = "%0.2f" % (p)
print(pp)
return pp
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input().strip())
for t_itr in range(t):
y_count = int(input().strip())
y = list(map(int, input().rstrip().split()))
result = solve(y)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
My print(pp) output comes out correctly for the test case as:
4.33
3.00
4.00
6.00
5.80
11.15
But my return pp stdout comes out as:
4
.
3
3
3
.
0
0
4
.
0
0
6
.
0
0
5
.
8
0
1
1
.
1
5
i.e. one character per line, and is classified incorrect. Could somebody point me into the direction of why this is?

The return from solve is already a string. When you call join on it, you are splitting it into its individual characters, separated by newlines.

Related

Python polynomial division - wrong remainder output

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?

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

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.

Path finding: A star not same length from A to B than from B to A

I am implementing the A star algorithm with the Manhattan distance for the 8 puzzle. [ The solution is in spiral form]
1 2 3
8 0 4
7 6 5
In some case, going from A to B will not take the same number of steps as going from B to A.
I think this is because it does not pick the same state on the open list, when they have the same cost, thus, not expanding the same nodes.
From
7 6 4
1 0 8
2 3 5
(A -> B)
7 6 4
1 8 0
2 3 5
(B -> A)
7 6 4
1 3 8
2 0 5
Which both have the same value using Manhattan distance.
Should I explore all path with the same value?
Or should I change the heuristic to have some kind of tie-breaker?
Here is the relevant part of the code
def solve(self):
cost = 0
priority = 0
self.parents[str(self.start)] = (None, 0, 0)
open = p.pr() #priority queue
open.add(0, self.start, cost)
while open:
current = open.get()
if current == self.goal:
return self.print_solution(current)
parent = self.parents[str(current)]
cost = self.parents[str(current)][2] + 1
for new_state in self.get_next_states(current):
if str(new_state[0]) not in self.parents or cost < self.parents[str(new_state[0])][2]:
priority = self.f(new_state) + cost
open.add(priority, new_state[0], cost)
self.parents[str(new_state[0])] = (current, priority, cost)
After wasting so much time re-writing my "solve" function many different ways, for nothing,
I finally found the problem.
def get_next_states(self, mtx, direction):
n = self.n
pos = mtx.index(0)
if direction != 1 and pos < self.length and (pos + 1) % n:
yield (self.swap(pos, pos + 1, mtx),pos, 3)
if direction != 2 and pos < self.length - self.n:
yield (self.swap(pos, pos + n, mtx),pos, 4)
if direction != 3 and pos > 0 and pos % n:
yield (self.swap(pos, pos - 1, mtx),pos, 1)
if direction != 4 and pos > n - 1:
yield (self.swap(pos, pos - n, mtx),pos, 2)
It was in this function. The last if used to be "if 4 and pos > n:"
So there were unexplored states..
2 days for a "-1"
It will teach me to do more unit testing

Resources