Ok, I am stuck.
My code for this puzzle, works in all the test cases. But when I submit it, it doesn’t work with the test case 3, which is the single number 5526. I copied the code to PyCharm and ran it, and it works perfectly in Pycharm. So I don’t know what to do other then post in this forum in hopes that someone can help me figure this out.
Here is what I have. I am a beginner programmer so my code probably has a lot of bad practices in it, so I am open to any suggestions on improvement as well as finding the problem in my code of why the SUBMIT thing doesn’t work.
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
n = int(input()) # the number of temperatures to analyse
# print(f"{i}, {t}, {n}", file=sys.stderr, flush=True)
def check_if_result_is_0_or_1(number):
result = 0
if 1 in number:
result = True
return result
elif 0 in number:
result = False
return result
else:
result = 4
return result
def check_if_there_are_numbers(numbers):
are_there_numbers = True
print(f"{numbers}", file=sys.stderr, flush=True)
try:
number = numbers[0]
except IndexError:
are_there_numbers = False
return are_there_numbers
def final_check(t, positive_numbers, negative_numbers):
no_pos_number = False
no_negative_number = False
pos_number = 0
negative_number = -1
result=0
final_check_executed = 9999999
print(f"{final_check_executed}", file=sys.stderr, flush=True)
print(f"{positive_numbers}", file=sys.stderr, flush=True)
# print(f"{positive_numbers[0]}", file=sys.stderr, flush=True)
# To avoid the index error do this:
try:
pos_number = positive_numbers[0]
except IndexError:
no_pos_number = True
try:
negative_number = abs(negative_numbers[0])
except IndexError:
no_negative_number = True
print(f"{pos_number}", file=sys.stderr, flush=True)
print(f"{negative_numbers[0]}", file=sys.stderr, flush=True)
print(f"{negative_number}", file=sys.stderr, flush=True)
if not no_pos_number and not no_negative_number:
if negative_number>pos_number:
result=pos_number
print(f"{pos_number}", file=sys.stderr, flush=True)
elif negative_number<pos_number:
result=negative_numbers[0]
print(f"{negative_numbers[0]}", file=sys.stderr, flush=True)
elif negative_number == pos_number:
result=pos_number
return result
elif no_pos_number and not no_negative_number:
return negative_numbers[0]
elif not no_pos_number and no_negative_number:
return positive_numbers[0]
negative_numbers=[]
positive_numbers=[]
all_numbers=[]
temperature=0
negative_number=0
result=0
for i in input().split():
# t: a temperature expressed as an integer ranging from -273 to 5526
t = int(i)
# print(f"{negative_numbers}", file=sys.stderr, flush=True)
# print(f"{positive_numbers}", file=sys.stderr, flush=True)
print(f"{t}, {n}", file=sys.stderr, flush=True)
# print (type(i), type(t), type(n), type(negative_numbers), type(positive_numbers))
all_numbers.append(t)
if t<0:
negative_numbers.append(t)
elif t>0:
positive_numbers.append(t)
negative_numbers.sort(reverse=True)
positive_numbers.sort()
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
print(f"{negative_numbers}", file=sys.stderr, flush=True)
print(f"{positive_numbers}", file=sys.stderr, flush=True)
print(f"{all_numbers}", file=sys.stderr, flush=True)
all_numbers.sort()
print(f"{all_numbers}", file=sys.stderr, flush=True)
# all_numbers_sorted=all_numbers.sort()
# print(f"{all_numbers_sorted}", file=sys.stderr, flush=True)
check = check_if_result_is_0_or_1(all_numbers)
print(f"{check}", file=sys.stderr, flush=True)
if check == True:
result = 1
print (result)
elif check == False:
result = 0
print(result)
else:
check = check_if_there_are_numbers(all_numbers)
if check == True:
result=final_check(temperature, positive_numbers, negative_numbers)
else:
result = 0
print (result)