Skip to content

Commit

Permalink
Added K #4
Browse files Browse the repository at this point in the history
  • Loading branch information
seaneshbaugh committed Feb 24, 2019
1 parent d926c74 commit 48690bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions k/004/004.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/ 100+x Dyadic plus. Add 100 to each element in x.

/ !900 Monadic enumerate. Returns a list of integers from 0 to 899.

/ {100+x}'!900 Apply the above plus function to the above list to get a list of integers from 100 to 999.

/ g:{100+x}'!900 Assign the above list of integers to variable g.

/ {x*g}'g:{100+x}'!900 Dyadic multiply. Muliply each element in g by each element in g returning a list of lists of multiples for each element in g.

/ ,/{x*g}'g:{100+x}'!900 Apply dyadic join to the above list of lists.

/ f:,/{x*g}'g:{100+x}'!900 Assign the above list of multiples to variable f.

/ $f:,/{x*g}'g:{100+x}'!900 Monadic format. Convert each element in the above list to strings.

/ |x Monadic reverse. Reverses x, in this case as string.

/ x~|x Dyadic match. In this case matches x with its reverse returning 1 if they're the same and 0 if not.

/ {x~|x}'$f:,/{x*g}'g:{100+x}'!900 Apply the above to the previous list of strings.

/ &{x~|x}'$f:,/{x*g}'g:{100+x}'!900 Monadic where. Gets the indices of the previous that are 1.

/ f@&{x~|x}'$f:,/{x*g}'g:{100+x}'!900 Dyadic at. Gets elements at the previous indices from variable f.

/ |/f@&{x~|x}'$f:,/{x*g}'g:{100+x}'!900 Dyadic max/or over the previous.

`0:5:|/f@&{x~|x}'$f:,/{x*g}'g:{100+x}'!900

`0:"\n"

_exit(0)
2 changes: 1 addition & 1 deletion test/rosetta_euler/k_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule RosettaEuler.KTest do
System.cmd("k", [name <> ".k"], cd: Path.join("k", name))
end

rosetta_euler_tests 1..3 do
rosetta_euler_tests 1..4 do
@tag test_number: test_number
test "K #" <> to_string(test_number), %{test_number: test_number} do
answer = answer_for(test_number) <> "\n"
Expand Down

0 comments on commit 48690bf

Please sign in to comment.