Skip to content

Instantly share code, notes, and snippets.

@yvbbrjdr
Created July 5, 2017 23:49
Show Gist options
  • Select an option

  • Save yvbbrjdr/a534dfdb563ecb27925687b9d6eed627 to your computer and use it in GitHub Desktop.

Select an option

Save yvbbrjdr/a534dfdb563ecb27925687b9d6eed627 to your computer and use it in GitHub Desktop.
A tool converting sznoi problems to FPS format.
#!/usr/bin/env python
import csv
from lxml import etree
def append(root, name, content = None):
element = etree.SubElement(root, name)
if content != None:
element.text = etree.CDATA(content.decode('utf8'))
return element
def main():
csvPath = raw_input('Enter csv path: ')
savePath = raw_input('Enter save path: ')
fps = etree.Element('fps')
fps.set('version', '1.2')
fps.set('url', 'https://github.com/zhblue/freeproblemset/')
generator = append(fps, 'generator')
generator.set('name', 'yvbbrjdr')
generator.set('url', 'https://github.com/yvbbrjdr')
with open(csvPath, 'rb') as csvFile:
csvReader = csv.DictReader(csvFile)
for problem in csvReader:
if problem['pid'] != '':
item = append(fps, 'item')
append(item, 'title', problem['title'])
append(item, 'time_limit', '1').set('unit', 's')
append(item, 'memory_limit', '64').set('unit', 'mb')
append(item, 'description', problem['content'])
if problem['theinput'] != '':
append(item, 'input', problem['theinput'])
if problem['theoutput'] != '':
append(item, 'output', problem['theoutput'])
append(item, 'sample_input', problem['sampleinput'])
append(item, 'sample_output', problem['sampleoutput'])
if problem['hint'] != '':
append(item, 'hint', problem['hint'])
if problem['code'] != '':
append(item, 'solution', problem['code']).set('language', problem['language'])
with open(savePath, 'w') as saveFile:
saveFile.write(etree.tostring(fps, encoding = 'UTF-8', pretty_print = True, xml_declaration = True))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment