Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 4th, 2013, 12:06 PM
PinkPanther1 PinkPanther1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 1 PinkPanther1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 24 sec
Reputation 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.

Reply With Quote
  #2  
Old March 4th, 2013, 02:12 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to input testcases

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap