#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    How to input testcases


    Hi everybody!

    I just started programming in Python, so sorry if this question is a very basic one.

    My problem is the following: I would like to start using files including the input for programs to run them. Especially, since I want to possibly attend some contests in some time, I think this would be useful.

    Now, I'd like to know how I can do that. I searched all over the internet, but haven't really found any answer that satisfies me.

    What I would like to do would be something like the following:


    Let's say we have t testcases. For each of those there are n inputs. How would I tell my program that it should the first time take t and the first n values to run, the second time the second line of numbers and so on?


    To illustrate I made a little example (not sure if I expressed myself well enough..):

    We want to test a program. We get a file with the content

    Code:
    2
    1 2 3 
    4 5 6
    Here t = 2, n = 3.

    I am unsure about how to put this into a program. The way I know it from C++, I can just assign variables to each value of the input. However, the only way I have seen this done in Python was by including it into arrays, which confused me a lot since everybody seemed to do it a different way.


    I'd greatly appreciate your help! Please tell me if you have any questions or if I did not express myself clearly. I think I did not really describe my problem too well, but this is the best way I can think of right now.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Code:
    def test_function(c0,c1,c2,*args):
        print('polynomial(1): {}'.format(int(c0)+int(c1)+int(c2)+sum(map(int,args))))
    
    def test_driver(test_function, INPUT = 'testcases.dat', n = 3):
    
        if True:
            data = '2 1 2 3 4 5 6'  # fake data
        else:
            with open(INPUT,'r') as inf:  # real data
                data = inf.read()
    
        tests = data.split()
        number_of_tests = int(tests[0])
        for i in range(1,len(tests),n):
            test_function(*tests[i:i+n])
    something like that.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo