diff --git a/Spr24/HW/Homework5/github/gr.py b/Spr24/HW/Homework5/github/gr.py new file mode 100644 index 0000000..5ee76b7 --- /dev/null +++ b/Spr24/HW/Homework5/github/gr.py @@ -0,0 +1,71 @@ +class Student: + def __init__(self, name, age, grades): + self.name = name + self.age = age + self.grades = grades + + def average_grade(self): + return sum(self.grades) / len(self.grades) + +def find_top_student(students): + top_student = students[0] + for student in students: + if student.average_grade() > top_student.average_grade(): + top_student = student + return top_student + +def factorial(n): + if n == 0: + return 1 + else: + return n * factorial(n-1) + +def fibonacci(n): + if n <= 0: + return [] + elif n == 1: + return [0] + elif n == 2: + return [0, 1] + else: + fibs = fibonacci(n-1) + fibs.append(fibs[-1] + fibs[-2]) + return fibs + +def add_grades_to_set(s, grades): + for grade in grades: + s.add(grade) + return s + +def calculate_total_length(strings): + total = 0 + for s in strings: + total += len(s) + return total + +students = [ + Student('Alice', 20, [90, 92, 85]), + Student('Bob', 22, [85, 87, 90]), + Student('Charlie', 23, [100, 95, 90]) +] + +top_student = find_top_student(students) +print(f'Top student: {top_student.name} with average grade {top_student.average_grade()}') +factorial(5) +fibonacci(10) +find_top_student(students) +add_grades_to_set({1, 2, 3}, [4, 5, 6]) +add_grades_to_set({1, 2, 3}, [4, 5, 'six']) +students[0].average_grade() +students[1].average_grade() +students[2].average_grade() +fibonacci(15) +factorial(0) +factorial(10) +add_grades_to_set(set(), [7, 8, 9]) +calculate_total_length(['hello', 'world']) +calculate_total_length(['hello', 'world', 123]) +calculate_total_length(['one', 'two', 'three']) +students[0].name +students[1].age +students[2].grades diff --git a/Spr24/HW/Homework5/github/q1.expected b/Spr24/HW/Homework5/github/q1.expected new file mode 100644 index 0000000..d25034d --- /dev/null +++ b/Spr24/HW/Homework5/github/q1.expected @@ -0,0 +1,35 @@ +val it = () : unit +val it = [[(0,0),(0,1)],[(1,0),(1,1)],[(2,0),(2,1)]] : (int * int) list list +val it = [] : (int * int) list list +val it = [[(0,0)],[(1,0)],[(2,0)]] : (int * int) list list +val it = [[(0,0),(0,1),(0,2)],[(1,0),(1,1),(1,2)],[(2,0),(2,1),(2,2)]] + : (int * int) list list +val it = + [[(1,1),(2,1),(3,1),(4,1)],[(1,2),(2,2),(3,2),(4,2)], + [(1,3),(2,3),(3,3),(4,3)]] : (int * int) list list +val it = [[(1,1),(2,1),(3,1)],[(1,2),(2,2),(3,2)],[(1,3),(2,3),(3,3)]] + : (int * int) list list +val it = + [[(1,1),(2,1),(3,1)],[(1,2),(2,2),(3,2)],[(1,3),(2,3),(3,3)], + [(1,4),(2,4),(3,4)]] : (int * int) list list +val it = [(1,1),(2,1),(1,2),(3,1),(2,2),(1,3),(4,1),(3,2),(2,3),(1,4)] + : (int * int) list +val it = Cons ((0,0),fn) : (int * int) Seq +exec: (1, 0) +val it = Cons ((1,0),fn) : (int * int) Seq +exec: (0, 1) +val it = Cons ((0,1),fn) : (int * int) Seq +exec: (2, 0) +val it = Cons ((2,0),fn) : (int * int) Seq +exec: (1, 1) +val it = Cons ((1,1),fn) : (int * int) Seq +exec: (0, 2) +val it = Cons ((0,2),fn) : (int * int) Seq +exec: (2, 1) +val it = Cons ((2,1),fn) : (int * int) Seq +exec: (1, 2) +val it = Cons ((1,2),fn) : (int * int) Seq +exec: (2, 2) +val it = Cons ((2,2),fn) : (int * int) Seq +val it = Nil : (int * int) Seq + diff --git a/Spr24/HW/Homework5/github/q1.in b/Spr24/HW/Homework5/github/q1.in new file mode 100644 index 0000000..a31d71a --- /dev/null +++ b/Spr24/HW/Homework5/github/q1.in @@ -0,0 +1,47 @@ +(* redefine in case the code wasn't copied *) + +fun coords (3, _) = DNil + | coords (_, 3) = DNil + | coords (x, y) = DCons((x, y), fn () => coords (x + 1, y), fn () => coords (x, y + 1)); +fun pcoords (3, _) = DNil + | pcoords (_, 3) = DNil + | pcoords (x, y) = ( + print ("exec: (" ^ Int.toString x ^ ", " ^ Int.toString y ^ ")\n"); + DCons((x, y), fn () => pcoords (x, y + 1), fn () => pcoords (x + 1,y)) + ); + +fun next Nil = Nil + | next (Cons(x, xf)) = xf (); + +fun take s 0 = [] + | take Nil _ = [] + | take (Cons (x, xf)) n = x :: take (xf ()) (n - 1); + +val p = pcoords (0, 0); +val s = coords (0, 0); + +print "===TEST START===\n"; + +(* Testing toMatr function *) +toMatrix s (3, 2); +toMatrix s (0, 3); +toMatrix s (3, 1); +toMatrix s (3, 3); + +(* Testing Q *) +toMatrix (Q ()) (3, 4); +toMatrix (Q ()) (3, 3); +toMatrix (Q ()) (4, 3); + +(* Testing diags *) +take (diags (Q ())) 10; +diags p; +next it; +next it; +next it; +next it; +next it; +next it; +next it; +next it; +next it; \ No newline at end of file diff --git a/Spr24/HW/Homework5/github/q1_def.sml b/Spr24/HW/Homework5/github/q1_def.sml new file mode 100644 index 0000000..73434ae --- /dev/null +++ b/Spr24/HW/Homework5/github/q1_def.sml @@ -0,0 +1,29 @@ +(* datatype definitions *) + +datatype 'a DSeq = DNil | DCons of 'a * (unit -> 'a DSeq) * (unit -> 'a DSeq); +datatype 'a Seq = Nil | Cons of 'a * (unit -> 'a Seq); + +(* sequence auxilary functions *) + +fun take s 0 = [] +| take Nil _ = [] +| take (Cons (x, xf)) n = x :: take (xf ()) (n - 1); + +fun next Nil = Nil + | next (Cons(x, xf)) = xf (); + +(* provided examples *) + +fun coords (3, _) = DNil + | coords (_, 4) = DNil + | coords (x, y) = DCons((x, y), fn () => coords (x + 1, y), fn () => coords (x, y + 1)); + +fun pcoords (3, _) = DNil + | pcoords (_, 3) = DNil + | pcoords (x, y) = ( + print ("exec: (" ^ Int.toString x ^ ", " ^ Int.toString y ^ ")\n"); + DCons((x, y), fn () => pcoords (x, y + 1), fn () => pcoords (x + 1,y)) + ); + +val s = coords (0, 0); +val p = pcoords (0, 0); diff --git a/Spr24/HW/Homework5/github/q2.expected b/Spr24/HW/Homework5/github/q2.expected new file mode 100644 index 0000000..f3c6d98 --- /dev/null +++ b/Spr24/HW/Homework5/github/q2.expected @@ -0,0 +1,13 @@ +true. + +true. + +false. + +false. + +true. + +L = [[3, 17], [7, 13], [13, 7], [17, 3]]. + + diff --git a/Spr24/HW/Homework5/github/q2.in b/Spr24/HW/Homework5/github/q2.in new file mode 100644 index 0000000..0e0e595 --- /dev/null +++ b/Spr24/HW/Homework5/github/q2.in @@ -0,0 +1,17 @@ +use_module(library(yall)). +print("===TEST START==="). + +once(pythagorean(3, 4, 5)). +once(pythagorean(3, 3, 5)). + +once(prime(4)). +once(prime(17)). + +{L}/( + findall( + [X, Y], + goldbach(X, Y, 20), + L1 + ), + msort(L1, L) +). diff --git a/Spr24/HW/Homework5/solution/q1.sml b/Spr24/HW/Homework5/solution/q1.sml new file mode 100644 index 0000000..2a2a20e --- /dev/null +++ b/Spr24/HW/Homework5/solution/q1.sml @@ -0,0 +1,56 @@ +(* datatype definitions *) + +datatype 'a DSeq = DNil | DCons of 'a * (unit -> 'a DSeq) * (unit -> 'a DSeq); +datatype 'a Seq = Nil | Cons of 'a * (unit -> 'a Seq); + +(* sequence auxilary functions *) + +fun take s 0 = [] +| take Nil _ = [] +| take (Cons (x, xf)) n = x :: take (xf ()) (n - 1); + +fun next Nil = Nil + | next (Cons(x, xf)) = xf (); + +(* provided examples *) + +fun coords (3, _) = DNil + | coords (_, 4) = DNil + | coords (x, y) = DCons((x, y), fn () => coords (x + 1, y), fn () => coords (x, y + 1)); + +fun pcoords (3, _) = DNil + | pcoords (_, 3) = DNil + | pcoords (x, y) = ( + print ("exec: (" ^ Int.toString x ^ ", " ^ Int.toString y ^ ")\n"); + DCons((x, y), fn () => pcoords (x, y + 1), fn () => pcoords (x + 1,y)) + ); + +val s = coords (0, 0); +val p = pcoords (0, 0); + +(* solution *) + +local + fun fetchLine 0 _ = [] + | fetchLine _ DNil = [] + | fetchLine n (DCons(x, _, xf)) = x :: fetchLine (n - 1) (xf ()); +in + fun toMatrix _ (0, _) = [] + | toMatrix DNil _ = [] + | toMatrix (s as DCons(_, xf, _)) (x, y) = fetchLine y s :: toMatrix (xf ()) (x - 1, y) +end; + +local + fun t (x, y) = DCons((x, y), fn () => t (x, y + 1), fn () => t (x + 1,y)); +in + fun Q () = t (1, 1) +end; + +fun diag [] [] = Nil + | diag [] nd = diag nd [] + | diag (xf :: xs) nd = ( + case xf () of + DNil => diag xs nd + | DCons (x, yf, xf) => Cons (x, fn () => diag xs (nd @ [xf] @ (case xs of [] => [yf] | _ => []))) + ); +fun diags s = diag [fn () => s] []; \ No newline at end of file diff --git a/Spr24/HW/Homework5/solution/q2.pl b/Spr24/HW/Homework5/solution/q2.pl new file mode 100644 index 0000000..ac34ae9 --- /dev/null +++ b/Spr24/HW/Homework5/solution/q2.pl @@ -0,0 +1,10 @@ +:-use_module(library(clpfd)). + +pythagorean(A,B,C):-C*C =:= (A*A)+(B*B). + + +helper1(A,A). +helper1(A,X):-A mod X =\= 0 ,N is X+1,helper1(A,N). +prime(A):- A>1,helper1(A,2). + +goldbach(X, Y, Z):- Z mod 2 =:= 0, N is Z-2,X in 2..N,indomain(X),prime(X),Y is (Z-X),prime(Y). diff --git a/docker/prolog/Dockerfile b/docker/prolog/Dockerfile new file mode 100644 index 0000000..dde74f1 --- /dev/null +++ b/docker/prolog/Dockerfile @@ -0,0 +1,8 @@ +FROM swipl + +RUN apt update \ + && apt install -y git \ + && apt clean \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +CMD ["swipl", "/app/start.pl"] \ No newline at end of file