Skip to content

Instantly share code, notes, and snippets.

@slowli slowli/template.py
Created Jun 21, 2015

Embed
What would you like to do?
Template for Rosalind.info solutions
#!/usr/bin/python
'''
Template for Rosalind.info problems
AFRQ problem (http://rosalind.info/problems/afrq/) is used
to illustrate tests and processing input / output.
'''
import unittest, sys
def get_input():
''' Scans and returns input until EOF symbol. '''
input_text = ''
try:
while True: input_text += raw_input() + '\n'
except EOFError:
pass
return input_text[:-1]
class Solver(object):
def solve(self, ps):
''' Code of the solution (this one is obviously incorrect). '''
return [ 0.0 for p in ps ]
def text_solve(self, text):
''' I/O-related code. '''
ps = map(float, text.split())
ps = self.solve(ps)
return ' '.join([ '{:.5f}'.format(p) for p in ps ])
class Tester(unittest.TestCase):
def setUp(self):
''' Called before each test. '''
self.solver = Solver()
def test_margins(self):
''' Testing marginal values is always a good idea. '''
ps = self.solver.solve([0.0, 1.0])
self.assertAlmostEqual(ps[0], 0.0)
self.assertAlmostEqual(ps[1], 1.0)
def test_example_text(self):
''' Tests I/O and running the solution on the example data. '''
out = self.solver.text_solve('0.1 0.25 0.5')
ps = map(float, out.split())
corr_ps = map(float, '0.532 0.75 0.914'.split())
dist = max([ abs(p - corr_p) for p, corr_p in zip(ps, corr_ps) ])
self.assertTrue(dist < 1e-3)
def test_example(self):
''' Tests the solution on the example dataset. '''
ps = self.solver.solve([0.1, 0.25, 0.5])
corr_ps = [0.532, 0.750, 0.914]
for p, cp in zip(ps, corr_ps):
self.assertAlmostEqual(p, cp, places=3)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'test':
del sys.argv[1]; unittest.main(); sys.exit(0)
print Solver().text_solve(get_input())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.