Python Code Questions

Asked 2 years ago, Updated 2 years ago, 22 views

num_of_char, time_of_del = map(int, input().split())
time = list(map(int, input().split()))
inteval = []
def intevalCal(time):
    n = -1
    result = []
    try:
        for num in time:
            n= n + 1
            result.append(time[n+1]-time[n])

    except IndexError:
        result.append(time[n]-time[n-1])
    return result
inteval = intevalCal(time)
def count(inteval, time_of_del):
    try:
        C = [x for x in inteval if x > time_of_del]
        F = inteval.index(C[-1])
        del inteval[:F+1]
        return len([x for x in inteval if x <= time_of_del])
    except IndexError:
        return len([x for x in inteval if x <= time_of_del])
final = count(inteval, time_of_del)
print(final)

I ran an example while solving the question, and it says 3 out of 10 are wrong answers. I can't find the problem. If there's anyone who knows, please leave a commentㅠ<

python

2022-09-22 16:58

2 Answers

I don't know what the problem requires, so I don't know the answer I want. Is it a question of removing the last number +1 of the C list that only filters out the value greater than time_of_del from the evaluation list that receives time data by num_of_char and stores the difference between each time data?

try:
        for num in time:
            n= n + 1
            result.append(time[n+1]-time[n])
    except IndexError:
        result.append(time[n]-time[n-1])

Here, as n grows from 1 to the last value, the difference from the previous value is stored Once the n gets bigger, it gets caught in the exception IndedxError Save the difference between the last value and the previous value once more.

So if you type 35 68, it's saved as [2 1 2 2 ]. If this isn't meant to be,

def intevalCal(time):
    n = -1
    result = []
    for i in range(1,len(time)):
        result.append(time[i]-time[i-1])
    return result

Please edit it and check it out

num_of_char, time_of_del = map(int, input().split())
time = list(map(int, input().split()))
inteval = []
def intevalCal(time):
    n = -1
    result = []
    for i in range(1,len(time)):
        result.append(time[i]-time[i-1])
        print(time[i],'-',time[i-1],end=' / ')
    print()
    return result
inteval = intevalCal(time)
print(inteval)
def count(inteval, time_of_del):
    try:
        C = [x for x in inteval if x > time_of_del]

        print('C=',C)
        print(inteval,end=' ')
        F = inteval.index(C[-1])
        print('-',inteval[:F+1])
        del inteval[:F+1]
        print('=',inteval)
        print('[x for x in inteval if x <= time_of_del]=',[x for x in inteval if x <= time_of_del])
        return len([x for x in inteval if x <= time_of_del])
    except IndexError:
        return len([x for x in inteval if x <= time_of_del])

final = count(inteval, time_of_del)
print(final)

Above is the code for course verification. If you let me know the problem, I will let you know in more detail.


2022-09-22 16:58

You said the first issue was intended, so I added the value at the end once more

As far as I understood, I thought it would be to find something less than time_of_del, erase all the values before the last value, and then find a number less than or equal to time_of_del.

If you remove the previous value based on the value at the end of the value less than time_of_del, you will not be able to find a value less than or equal to time_of_del, so I deleted the previous value as soon as I first found it. If you want to find the last value, use the for statement that you search for after it is surrounded by annotations.

Next, I didn't understand whether to count values less than or equal to time_of_del continuously or not, so I counted them all.

num_of_char, time_of_del = map(int, input().split())
time = list(map(int, input().split()))
inteval = []
def intevalCal(time):
    n = -1
    result = []
    for i in range(1,len(time)):
        result.append(time[i]-time[i-1])
        print(time[i],'-',time[i-1],end=' / ')
    result.append(time[-1]-time[-2])
    print()
    return result
inteval = intevalCal(time)
print(inteval)
def count(inteval, time_of_del):
    #Checking from the back
    #for i in range(len(inteval)-1,-1,-1):
    #    #    if inteval[i] < time_of_del:
    #        #        inteval=inteval[i+1:]
    #        #        break

    #Checking from the front
    for i in inteval:
        if i < time_of_del:
            print(i,'<',time_of_del)
            inteval=inteval[i+1:]
            break
    Exit if #inval has no value    
    if len(inteval)==0:
        return 0
    print('inteval=>',inteval,' / del=>',time_of_del)
    return len([x for x in inteval if x <= time_of_del])

final = count(inteval, time_of_del)
print(final)

Please check the code and if the answer is the same as the test case, please erase the middle comment and print statement. Thank you.


2022-09-22 16:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.