-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial.py
54 lines (44 loc) · 1.03 KB
/
factorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from programlib import Program
factorial_tests = [
(['6'], ['720']),
(['5'], ['120']),
(['4'], ['24']),
(['8'], ['40320'])
]
factorial_cpp = """
using namespace std;
#include <iostream>
int main() {
int n;
cin >> n;
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
cout << fact << endl;
return 0;
}
"""
print('C++ score')
program = Program(source=factorial_cpp, language='C++')
program.test(factorial_tests)
print(program.avg_score)
factorial_clj = """
(defn factorial [n] (if (< n 1) 1 (* n (factorial (- n 1)))))
(-> (read-line) (read-string) (factorial) (println))
"""
print('Clojure score')
program = Program(source=factorial_clj, language='Clojure')
print(program.test(factorial_tests))
print(program.avg_score)
factorial_py = """
def factorial(n):
if n < 1:
return 1
return n * factorial(n - 1)
print(factorial(int(input())))
"""
print('Python score')
program = Program(source=factorial_py, language='Python')
program.test(factorial_tests)
print(program.avg_score)