Skip to content

Instantly share code, notes, and snippets.

@codeeval
Created October 3, 2011 02:13
Show Gist options
  • Save codeeval/1258294 to your computer and use it in GitHub Desktop.
Save codeeval/1258294 to your computer and use it in GitHub Desktop.
Sample code to read in test cases
# On CodeEval, test cases are read in from a file which is the first argument to your program
# Open the file and read in line by line. Each line represents a different test case
# (unless given different instructions in the challenge description)
import sys
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
# ignore test if it is an empty line
# 'test' represents the test case, do something with it
# ...
# ...
test_cases.close()
@iskenxan
Copy link

I keep getting an Error"Program binary.exe' does not contain a staticMain' method suitable for an entry point" although I haven't changed the given Main function in the online editor,I'm using C#, try for yourself, you will get the same error even if you submit an empty Main function

@dan-me
Copy link

dan-me commented Jun 22, 2015

@iskenxan Got the same problem, did you find a solution?

@Wizmann
Copy link

Wizmann commented Dec 4, 2015

i'm confused that why you don't use stdin as the input of the program. It's quite commonly used on most online judge platforms.

@teal18aa9f
Copy link

How to read inputs using javascript?

@HeidiHansen
Copy link

Bnickel's gist is no longer valid for Objective-C. Use this, and don't forget to declare your variables outside of for loops, otherwise it will throw you loads of gnuC99 errors.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // read filename from the first argument
    NSString* filename = [NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding];
    NSString* content = [NSString stringWithContentsOfFile:filename encoding:NSASCIIStringEncoding error:nil];
    NSScanner* scanner = [NSScanner scannerWithString:content];

    while (![scanner isAtEnd]) {
        NSString* line;
        [scanner scanUpToString:@"\n" intoString:&line];

        //put your code here 

        printf("%s\n", [line cStringUsingEncoding:NSASCIIStringEncoding]);
    }

    [pool release];
    return 0;
}

@amit000
Copy link

amit000 commented Jan 27, 2016

here is another template for Java; this works fine in my local environement, but does not work on codeeval, what is the nme of the file that is being passed as input in codeeval? couldn't find it in templates as well.

public void readByLine(){   
try{
    FileReader inputFile= new FileReader("input.txt");
    BufferedReader buffer = new BufferedReader(inputFile);

    String line;

    while((line = buffer.readLine())!= null){

        convert(line); //method to implement logic
        // System.out.println(line); //just in case you need console testing

        }

    }
    buffer.close();


}catch(FileNotFoundException ex){
    System.out.println("file not found");

}catch(IOException ex){
    System.out.println("there are some issues");

}


}

@AnEmortalKid
Copy link

@amit000 they pass that with args so you can't hardcode the name m8

@watzon
Copy link

watzon commented Feb 3, 2016

Here's my working rust version:

use std::env;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::Path;

fn solve(s: String) {
    println!("{}", s)
}

fn main() {
    let args: Vec<_> = env::args().collect();
    let path = Path::new(&args[1]);
    let display = path.display();
    let file = match File::open(&path) {
        // The `description` method of `io::Error` returns a string that describes the error
        Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    };
    let reader = BufReader::new(file);
    let lines: Vec<_> = reader.lines().collect();
    for l in lines {
        solve(l.unwrap());
    }
}

@ryanbgraham
Copy link

ryanbgraham commented Apr 25, 2016

I must be getting severely confused about how to run a program with input containing multiple lines. Can someone walk me through how to submit an answer for the challenge linked below? Basically sum of the lines. The actual function that would add up all of the numbers is easy enough, but I do not understand how to make my input be the different numbers from a file with multiple lines.

Working in Javascript.

https://www.codeeval.com/open_challenges/24/

@AmbitiousAnil
Copy link

What do i write as input and output filenames?

@redknight99
Copy link

Here's some starter code for Fortran. #MakeFortranGreatAgain
program main
implicit none
character(32) :: tests
character(10) :: test
integer :: stat
!Get file name
call get_command_argument(1, arg)
!Open file
open (11,file=trim(arg),action='read')
do
!read in test
read (11,'(a)',iostat=stat) test
if (stat /= 0) exit
if (str /= '') then
.........
! Code goes here
.........
end if
end do
close (11)
end program main

@savcha-v
Copy link

Please show me an example for python3, if you need to pass a parameter to the function, how do I issue the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment