Skip to content

Commit

Permalink
Merge pull request #181 from Thunno/add-all-permutations
Browse files Browse the repository at this point in the history
Add "all permutations"
  • Loading branch information
nayakrujul authored Aug 15, 2023
2 parents c3f88eb + 7603074 commit a039350
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 12 deletions.
14 changes: 11 additions & 3 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,11 @@ Tokens: `product`,`prod`

***

# `q` (Quit)
# `q` (All permutations)

Tokens: `quit`
Tokens: `all_permutations`

Terminate the program.
* `any a`: all permutations of `a`

***

Expand Down Expand Up @@ -2630,6 +2630,14 @@ Tokens: `connected_uniquify`

***

# `µq` (Quit)

Tokens: `quit`

Terminate the program.

***

# `µr` (Random shuffle)

Tokens: `random_shuffle`
Expand Down
Binary file modified src/thunno2/__pycache__/commands.cpython-39.pyc
Binary file not shown.
Binary file modified src/thunno2/__pycache__/helpers.cpython-39.pyc
Binary file not shown.
Binary file modified src/thunno2/__pycache__/lexer.cpython-39.pyc
Binary file not shown.
Binary file modified src/thunno2/__pycache__/tests.cpython-39.pyc
Binary file not shown.
Binary file modified src/thunno2/__pycache__/version.cpython-311.pyc
Binary file not shown.
Binary file modified src/thunno2/__pycache__/version.cpython-39.pyc
Binary file not shown.
7 changes: 6 additions & 1 deletion src/thunno2/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,12 @@ def __call__(self, pop=True):
0,
("product", "prod"),
),
# q is defined in the run function
"q": Overload(
1,
{Number: range_permutations, Iterable: all_permutations},
0,
("all_permutations",),
),
"r": Overload(1, {Number: digit_reverse, Iterable: reverse}, 0, ("reverse",)),
"s": Overload(2, {(Any[0], Any[0]): swap}, 0, ("swap",)),
"t": Overload(1, {Number: num_tail, Iterable: tail}, 0, ("tail", "last")),
Expand Down
10 changes: 10 additions & 0 deletions src/thunno2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,8 @@ def permutations1(n, x):
n = len(x)
if n < 1:
n = 1
if isinstance(x, str):
return list(map("".join, itertools.permutations(x, n)))
return list(map(list, itertools.permutations(x, n)))


Expand All @@ -1634,6 +1636,14 @@ def permutations3(x, y):
return permutations1(x, one_range(y))


def all_permutations(l):
return permutations1(len(l), l)


def range_permutations(n):
return permutations3(abs(n), n)


def set_difference(x, y):
r = []
for i in y:
Expand Down
10 changes: 5 additions & 5 deletions src/thunno2/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,17 @@ def tokenise(code, expected_end=""):
elif y == "µı":
i, r = tokenise(code[index + 1 :], expected_end=";")
index += i + 1
ret.append((char, "nmap", r))
ret.append((y, "nmap", r))
elif y == "µ²":
i, r = tokenise(code[index + 1 :], expected_end=";")
index += i + 1
ret.append((char, "2map", r))
ret.append((y, "2map", r))
elif y == "µ³":
i, r = tokenise(code[index + 1 :], expected_end=";")
index += i + 1
ret.append((char, "3map", r))
ret.append((y, "3map", r))
elif y == "µq":
ret.append((y, "quit", 0))
else:
ret.append((y, "digraph", get_a_function(y)))
except:
Expand Down Expand Up @@ -557,8 +559,6 @@ def tokenise(code, expected_end=""):
)
except:
pass
elif char == "q":
ret.append((char, "quit", 0))
elif char == "$":
ret.append((char, "next input", 0))
elif char == "¤":
Expand Down
53 changes: 52 additions & 1 deletion src/thunno2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,57 @@ def assert_eq(a, *b):

assert_eq(call("p", [1, 2, "abc", 3]), 6)

# q

assert_eq(
call("q", 3), [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
)
assert_eq(call("q", -2.1), [[-1, -2], [-2, -1]])

assert_eq(call("q", ""), [])
assert_eq(
call("q", "abcd"),
[
"abcd",
"abdc",
"acbd",
"acdb",
"adbc",
"adcb",
"bacd",
"badc",
"bcad",
"bcda",
"bdac",
"bdca",
"cabd",
"cadb",
"cbad",
"cbda",
"cdab",
"cdba",
"dabc",
"dacb",
"dbac",
"dbca",
"dcab",
"dcba",
],
)

assert_eq(call("q", 0), [])
assert_eq(
call("q", [123, 456, 789]),
[
[123, 456, 789],
[123, 789, 456],
[456, 123, 789],
[456, 789, 123],
[789, 123, 456],
[789, 456, 123],
],
)

# r

assert_eq(call("r", 1.23), 32.1)
Expand Down Expand Up @@ -1399,7 +1450,7 @@ def assert_eq(a, *b):
)
assert_eq(
call("ṗ", "xyz", 2),
[["x", "y"], ["x", "z"], ["y", "x"], ["y", "z"], ["z", "x"], ["z", "y"]],
["xy", "xz", "yx", "yz", "zx", "zy"],
)
assert_eq(
call("ṗ", 10, [1, 2, 3]),
Expand Down
2 changes: 1 addition & 1 deletion src/thunno2/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def test():
"ȥ": ("add_to_global_array",),
"K": ("stack",),
"ṇ": ("codepage_compression",),
"q": ("quit",),
"$": ("next_input",),
"¤": ("input_list",),
"°": ("first_input",),
Expand Down Expand Up @@ -130,6 +129,7 @@ def test():
"µı": ("nmap",),
"µ²": ("2map",),
"µ³": ("3map",),
"µq": ("quit",),
}


Expand Down
2 changes: 1 addition & 1 deletion src/thunno2/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
That's it!
"""

THUNNO_VERSION = "2.2.23"
THUNNO_VERSION = "2.2.24"

0 comments on commit a039350

Please sign in to comment.