You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@testpg_eval("function countdown(n)\n while n > 0\n println(n)\n n = n - 1\n end\nend\ncountdown(3)") =="3\n2\n1"
3290
+
@testpg_eval("function sumto(n)\n s = 0\n i = 1\n while i <= n\n s = s + i\n i = i + 1\n end\n return s\nend\nprintln(sumto(100))") =="5050"
3291
+
@testpg_eval("function power(base, exp)\n result = 1\n i = 0\n while i < exp\n result = result * base\n i = i + 1\n end\n return result\nend\nprintln(power(2, 10))") =="1024"
3292
+
@testpg_eval("function digits(n)\n count = 0\n while n > 0\n n = n / 10 |> floor\n count = count + 1\n end\n return count\nend\nprintln(digits(12345))") =="5"
3293
+
@testpg_eval("function gcd(a, b)\n while b != 0\n t = b\n b = a - (a / b |> floor) * b\n a = t\n end\n return a\nend\nprintln(gcd(56, 98))") =="14"
3294
+
end
3295
+
3296
+
# ---- Category 9: For loops (5 tests) ----
3297
+
@testset"For loops"begin
3298
+
@testpg_eval("function sum_range(n)\n s = 0\n for i in 1:n\n s = s + i\n end\n return s\nend\nprintln(sum_range(10))") =="55"
3299
+
@testpg_eval("function sum_squares(n)\n s = 0\n for i in 1:n\n s = s + i * i\n end\n return s\nend\nprintln(sum_squares(5))") =="55"
3300
+
@testpg_eval("function count_down(n)\n for i in 1:n\n println(n - i + 1)\n end\nend\ncount_down(3)") =="3\n2\n1"
3301
+
@testpg_eval("function triangular(n)\n t = 0\n for i in 1:n\n t = t + i\n end\n return t\nend\nprintln(triangular(7))") =="28"
3302
+
@testpg_eval("function product(n)\n p = 1\n for i in 1:n\n p = p * i\n end\n return p\nend\nprintln(product(5))") =="120"
3303
+
end
3304
+
3305
+
# ---- Category 10: Nested control flow (4 tests) ----
3306
+
@testset"Nested control flow"begin
3307
+
@testpg_eval("function nested_sum(n)\n total = 0\n for i in 1:n\n for j in 1:i\n total = total + 1\n end\n end\n return total\nend\nprintln(nested_sum(4))") =="10"
3308
+
@testpg_eval("function fizzbuzz_count(n)\n c = 0\n for i in 1:n\n if i > 3 * (i / 3 |> floor)\n c = c + 1\n end\n end\n return c\nend\nprintln(fizzbuzz_count(10))") =="7"
3309
+
@testpg_eval("function prime_check(n)\n if n < 2\n return false\n end\n i = 2\n while i * i <= n\n if n - (n / i |> floor) * i == 0\n return false\n end\n i = i + 1\n end\n return true\nend\nprintln(prime_check(7))\nprintln(prime_check(4))\nprintln(prime_check(13))") =="true\nfalse\ntrue"
3310
+
@testpg_eval("function collatz_steps(n)\n steps = 0\n while n != 1\n if n > 2 * (n / 2 |> floor)\n n = 3 * n + 1\n else\n n = n / 2\n end\n steps = steps + 1\n end\n return steps\nend\nprintln(collatz_steps(27))") =="111"
@testpg_eval("a = [5, 10, 15]\nfunction sum_arr(arr)\n s = 0\n for i in 1:length(arr)\n s = s + arr[i]\n end\n return s\nend\nprintln(sum_arr(a))") =="30"
@testpg_eval("function helper(x)\n return x + 10\nend\nfunction main_fn(x)\n return helper(helper(x))\nend\nprintln(main_fn(5))") =="25"
3357
+
@testpg_eval("function is_even(n)\n return n - 2 * (n / 2 |> floor) == 0\nend\nfunction count_even(n)\n c = 0\n for i in 1:n\n if is_even(i)\n c = c + 1\n end\n end\n return c\nend\nprintln(count_even(10))") =="5"
3358
+
@testpg_eval("function square(x)\n return x * x\nend\nfunction sum_of_squares(n)\n s = 0\n for i in 1:n\n s = s + square(i)\n end\n return s\nend\nprintln(sum_of_squares(5))") =="55"
@testpg_eval("function fib_iter(n)\n a = 0\n b = 1\n i = 0\n while i < n\n c = a + b\n a = b\n b = c\n i = i + 1\n end\n return a\nend\nprintln(fib_iter(10))") =="55"
3365
+
# Power via repeated squaring (iterative)
3366
+
@testpg_eval("function fast_pow(base, exp)\n result = 1.0\n b = base\n e = exp\n while e > 0\n if e > 2 * (e / 2 |> floor)\n result = result * b\n end\n b = b * b\n e = e / 2 |> floor\n end\n return result\nend\nprintln(fast_pow(2.0, 10))") =="1024"
3367
+
# Integer square root (Newton's method)
3368
+
@testpg_eval("function isqrt(n)\n x = n\n while x * x > n\n x = (x + n / x |> floor) / 2 |> floor\n end\n return x\nend\nprintln(isqrt(100))\nprintln(isqrt(50))") =="10\n7"
3369
+
# Sum of digits
3370
+
@testpg_eval("function digit_sum(n)\n s = 0\n while n > 0\n s = s + n - (n / 10 |> floor) * 10\n n = n / 10 |> floor\n end\n return s\nend\nprintln(digit_sum(12345))") =="15"
@testpg_eval("function describe(n)\n if n > 0\n return string(n, \" is positive\")\n else\n return string(n, \" is non-positive\")\n end\nend\nprintln(describe(5))\nprintln(describe(-3))") =="5 is positive\n-3 is non-positive"
0 commit comments