# Stuck on problems in coding

i’ve been stuck on this for a long time and id appreciate any help on these, especially question three where i don’t know where to begin.QUESTIONS.mov - Google Drive

This video from the link is hard to deal with.
That may be one of the reasons why there are no replies yet.

You can:

• post the problem
• post your code(what have you tried)
• provide some of your thoughts about it - if it does not work - why do you think that is the case
• if you ‘don’t know where to begin’, and there is no code - there should be the way to try and ask specific question about the problem

Also would be a good idea to discuss one coding problem in one thread, not a bunch of them at once.

I have uplaoded two screenshots of the proble at hand. I do not know where to start for this and what kind of way to complete the calcMissing function so that it has 20 rows

``````
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'calcMissing' function below.
#
# The function accepts STRING_ARRAY readings as parameter.
#

if __name__ == '__main__':

``````

here is what i have tried so far for question 1, but it does not work and has a runtime error: Screen Recording 2022-09-12 at 5.16.02 PM.mov - Google Drive

``````#!/bin/python3

import math
import os
import random
import re
import sys

#!/bin/python3
import math
import os
import random
import re
import sys

import numpy as np

#
# Complete the 'valuation' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
#  1. LONG_INTEGER reqArea
#  2. LONG_INTEGER_ARRAY area
#  3. LONG_INTEGER_ARRAY price
#

def valuation(reqArea, area, price):
def removeOutliers(areaOut, priceOut):
newArea = []
newPrice = []
visited = []
duplicates = {}
for i in range(len(areaOut)):
compList = [priceOut[x] for x in range(len(areaOut)) if areaOut[i] == areaOut[x] and i != x]

if not compList:
if areaOut[i] in newArea:
if areaOut[i] in duplicates:
val = duplicates.get(areaOut[i])
val[0] += priceOut[i]
val[1] += 1
duplicates[areaOut[i]] = val
else:
duplicates[areaOut[i]] = [priceOut[i], 2]
else:
newArea.append(areaOut[i])
newPrice.append(priceOut[i])
else:
if (abs(priceOut[i] - (sum(compList)/(len(compList)))) > (3 * np.std(compList))) == False:
if areaOut[i] in newArea:
if areaOut[i] in duplicates:
val = duplicates.get(areaOut[i])
val[0] += priceOut[i]
val[1] += 1
duplicates[areaOut[i]] = val
else:
duplicates[areaOut[i]] = [priceOut[i], 2]
else:
newArea.append(areaOut[i])
newPrice.append(priceOut[i])

return newArea, newPrice, duplicates

if reqArea in area:
areasEqualed = [price[i] for i in range(len(area)) if area[i] == reqArea]
return int(sum(areasEqualed)/len(areasEqualed))

uniques_Area = []
uniques_Price = []
nonUniques_Area = []
nonUniques_Price = []
visited = []

for i in range(len(area)):
if area[i] not in uniques_Area and area[i] not in visited:
uniques_Area.append(area[i])
uniques_Price.append(price[i])
visited.append(area[i])
else:
if area[i] in uniques_Area:
index = uniques_Area.index(area[i])

nonUniques_Area.append(area[i])
nonUniques_Price.append(price[i])
nonUniques_Area.append(area[i])
nonUniques_Price.append(uniques_Price[index])

uniques_Area.remove(area[i])
uniques_Price.remove(uniques_Price[index])
else:
nonUniques_Area.append(area[i])
nonUniques_Price.append(price[i])

a, b, dp = removeOutliers(nonUniques_Area, nonUniques_Price)
a += uniques_Area
b += uniques_Price

if min(a) < reqArea < max(a):
y1_index = a.index(min([i for i in a if i > reqArea]))
y2_index = a.index(max([i for i in a if i < reqArea]))
interSquare = [a[y1_index], a[y2_index]]
interPrice = [b[y1_index], b[y2_index]]

for i in range(len(interSquare)):
if interSquare[i] in dp:
dpVal = dp.get(interSquare[i])
interPrice[i] = (interPrice[i] + dpVal[0])/dpVal[1]

# Uncomment prints for debugging
# print(reqArea)
# print(interSquare)
# print(interPrice)

ans = interPrice[0] + (((interPrice[1]-interPrice[0])* (reqArea - interSquare[0]))/(interSquare[1] - interSquare[0]))
# print("Internpolate : ", int(round(ans)))
return int(round(ans))

else:
# ExtraPolate
if max(a) < reqArea:
y1_index = a.index(max(a))
y2_index = a.index(max([i for i in a if i != a[y1_index]]))
interSquare = [a[y2_index], a[y1_index]]
interPrice = [b[y2_index], b[y1_index]]
else:
y1_index = a.index(min(a))
y2_index = a.index(min([i for i in a if i != a[y1_index]]))
interSquare = [a[y2_index], a[y1_index]]
interPrice = [b[y2_index], b[y1_index]]

for i in range(len(interSquare)):
if interSquare[i] in dp:
dpVal = dp.get(interSquare[i])
interPrice[i] = (interPrice[i] + dpVal[0])/dpVal[1]

# print(reqArea)
# print(interSquare, interPrice)
ans = interPrice[1] + (reqArea - interSquare[1]) * ((interPrice[1] - interPrice[0])/(interSquare[1]-interSquare[0]))
if ans > 1000000:
ans = 1000000
if ans < 1000:
ans = 1000
# print("Extrapolate : ", int(round(ans)))
return int(round(ans))

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
reqArea = int(input().strip())
area_count = int(input().strip())
area = []
for _ in range(area_count):
area_item = int(input().strip())
area.append(area_item)
price_count = int(input().strip())
price = []
for _ in range(price_count):
price_item = int(input().strip())
price.append(price_item)
result = valuation(reqArea, area, price)
print(result)
fptr.write(str(result) + '\n')
fptr.close()

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

reqArea = int(input().strip())

area_count = int(input().strip())

area = []

for _ in range(area_count):
area_item = int(input().strip())
area.append(area_item)

price_count = int(input().strip())

price = []

for _ in range(price_count):
price_item = int(input().strip())
price.append(price_item)

result = valuation(reqArea, area, price)

fptr.write(str(result) + '\n')

fptr.close()

``````
1 Like

Have you managed to get a solution for the calcmissing?

Solution:

``````#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'calcMissing' function below.
#
# The function accepts STRING_ARRAY readings as parameter.
#

import numpy as np
from scipy import interpolate

values = []
x=[]
values_float = []
x_unknown = []

for i in range(n):

for i in range(n):
if "Missing" in values[i]:
x_unknown.append(i)
else:
x.append(i)
values_float.append(float(values[i]))

y= np.array(values_float)
f = interpolate.UnivariateSpline(x, y, s=1)

for i in x_unknown:
print (f(i))

if __name__ == '__main__':