Issue with Problem 170 (Binary Search in Array)
source link: https://www.codeabbey.com/index/forum_topic/dd47ea4ad5f126a25e55f3d42067e5c6
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Issue with Problem 170 (Binary Search in Array)
Back to General discussions forum
I am having a strange issue with Problem 170 (Binary Search in Array). Whenever I submit my answer, I am getting an error of the form "You are expected to return 5651 country codes!" (replacing 5651 with however many country codes there are supposed to be for that particular set of input data). However, I have checked and I am submitting that exact number of country codes each time. I also am pretty sure I am meeting the time limit (although I would think that would produce a different error message). I am able to find country codes for all of the IPs, and my answer seems to be formatted correctly.
Does anyone know what is going on here?
I will include my code (in Python) for reference. I apologize if there are formatting issues here
I will note that I copied the input data into Notepad, manually removed the first line (which has the number of IP addresses to find country codes for), and then saved it as a text file in the same place I saved my code. I would then run the code and copy the answer string that gets printed out into the answer box in the problem.
ipCodesFile = open("IP Country Codes.txt", 'r')
ipCodesUnsplit = ipCodesFile.read()
ipCodesFile.close()
ipCodes = ipCodesUnsplit.split()
inputDataFile = open("Code Abbey Binary Search in Array Input.txt", 'r')
inputData = inputDataFile.read()
inputDataFile.close()
i = 0
answerString = ""
while i < len(inputData):
ipAddress = ""
while i < len(inputData):
if inputData[i] == "\n":
i += 1
break
else:
ipAddress += inputData[i]
i += 1
ipInteger = int(ipAddress, 36)
leftBound = 0
rightBound = len(ipCodes) // 3
firstCountryStart = int(ipCodes[0], 36)
firstCountryOffset = int(ipCodes[1], 36)
lastCountryStart = int(ipCodes[-3], 36)
lastCountryOffset = int(ipCodes[-2], 36)
if firstCountryStart <= ipInteger <= firstCountryStart + firstCountryOffset:
answerString += ipCodes[2] + " "
elif lastCountryStart <= ipInteger <= lastCountryStart + lastCountryOffset:
answerString += ipCodes[-1] + " "
else:
segmentSize = rightBound - leftBound
#I am ending when segmentSize == 1 because when
# segmentSize == 1 and countryStart <= ipInteger, midpoint = leftBound
# and hence the solution will not iterate further, getting stuck in an
# infinite loop
while segmentSize > 1:
midpoint = (leftBound + rightBound) // 2
countryStart = int(ipCodes[3 * midpoint], 36)
if countryStart <= ipInteger:
nextCountryStart = int(ipCodes[3 * (midpoint + 1)], 36)
if nextCountryStart > ipInteger:
countryOffset = int(ipCodes[3 * midpoint + 1], 36)
if ipInteger <= countryStart + countryOffset:
answerString += ipCodes[3 * midpoint + 2] + " "
else:
answerString += "Unknown "
break
else:
leftBound = midpoint
segmentSize = rightBound - leftBound
else:
rightBound = midpoint
segmentSize = rightBound - leftBound
if segmentSize == 1:
leftCountryStart = int(ipCodes[3 * leftBound], 36)
rightCountryStart = int(ipCodes[3 * rightBound], 36)
if leftCountryStart <= ipInteger:
if rightCountryStart > ipInteger:
leftCountryOffset = int(ipCodes[3 * leftBound + 1], 36)
if ipInteger <= leftCountryStart + leftCountryOffset:
answerString += ipCodes[3 * leftBound + 2] + " "
else:
answerString += "Unknown "
else:
rightCountryOffset = int(ipCodes[3 * rightBound + 1], 36)
if ipInteger <= rightCountryStart + rightCountryOffset:
answerString += ipCodes[3 * rightBound + 2] + " "
else:
answerString += "Unknown "
#I don't think it is possible to end with segmentSize of 0, but just in case
# I will include code for this case
elif segmentSize == 0:
countryStart = int(ipCodes[3 * leftBound], 36)
countryOffset = int(ipCodes[3 * leftBound + 1], 36)
if countryStart <= ipInteger <= countryStart + countryOffset:
answerString += ipCodes[3 * leftBount + 2] + " "
else:
answerString += "Unknown "
print(answerString)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK