convert_to_c_simple.py 836 B

123456789101112131415161718192021222324252627
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can rewrite a very simple Python program into an equivalent C program."
  3. TAGS = ['code', 'c']
  4. question = """
  5. Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float).
  6. def my_sqrt(number, precision=0.0001):
  7. guess = number / 2 # Start with an initial guess
  8. while abs(guess * guess - number) >= precision:
  9. guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method
  10. return guess
  11. """
  12. test_case, answer = make_c_test([("(int)(my_sqrt(12345, .001)*10)", "1111")])
  13. TestProgramRewriteCSimple = question >> LLMRun() >> ExtractCode() >> CRun(test_case) >> SubstringEvaluator(answer)
  14. if __name__ == "__main__":
  15. from llm import llm
  16. print(run_test(TestProgramRewriteCSimple))