Skip to main content
corrected typo for function name “r” > “f"
Source Link
TransferOrbit
  • 3.6k
  • 14
  • 26

Here's a short alternative (44 characters):

f = Union@{#[[p = Ceiling[Length@#/2]]], #[[-p]]} & ;

Now apply your function

f@yourinputlist

So for these sample input lists:

evenlist = CharacterRange["a", "z"]
oddlist = CharacterRange["a", "y"]
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} *)
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"} *)

you get the following results:

r@evenlistf@evenlist
r@oddlistf@oddlist
(* {m, n} *)
(* {m} *)

Here's a short alternative (44 characters):

f = Union@{#[[p = Ceiling[Length@#/2]]], #[[-p]]} & ;

Now apply your function

f@yourinputlist

So for these sample input lists:

evenlist = CharacterRange["a", "z"]
oddlist = CharacterRange["a", "y"]
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} *)
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"} *)

you get the following results:

r@evenlist
r@oddlist
(* {m, n} *)
(* {m} *)

Here's a short alternative (44 characters):

f = Union@{#[[p = Ceiling[Length@#/2]]], #[[-p]]} & ;

Now apply your function

f@yourinputlist

So for these sample input lists:

evenlist = CharacterRange["a", "z"]
oddlist = CharacterRange["a", "y"]
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} *)
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"} *)

you get the following results:

f@evenlist
f@oddlist
(* {m, n} *)
(* {m} *)
Set the function in a variable to demonstrate repeat applicability; added example application.
Source Link
TransferOrbit
  • 3.6k
  • 14
  • 26

Here's a short alternative (44 characters):

f = Union@{#[[p = Ceiling[Length@#/2]]], #[[-p]]} &@yourinputlist& ;

Now apply your function

f@yourinputlist

So for these sample input lists:

evenlist = CharacterRange["a", "z"]
oddlist = CharacterRange["a", "y"]
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} *)
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"} *)

you get the following results:

r@evenlist
r@oddlist
(* {m, n} *)
(* {m} *)

Here's a short alternative (44 characters):

Union@{#[[p = Ceiling[Length@#/2]]], #[[-p]]} &@yourinputlist

Here's a short alternative (44 characters):

f = Union@{#[[p = Ceiling[Length@#/2]]], #[[-p]]} & ;

Now apply your function

f@yourinputlist

So for these sample input lists:

evenlist = CharacterRange["a", "z"]
oddlist = CharacterRange["a", "y"]
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} *)
(* {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"} *)

you get the following results:

r@evenlist
r@oddlist
(* {m, n} *)
(* {m} *)
deleted earlier clunky, brute force approach
Source Link
TransferOrbit
  • 3.6k
  • 14
  • 26

PerhapsHere's a useful, if brute force,short alternative (44 characters):

If[EvenQ[Length@#],
    Union@{#[[#[[p = Ceiling[Length@#/2] ]]2]]], #[[ -Ceiling[Length@#/2] ]]p]]},
    #[[ Ceiling[Length@#/2] ]]
] & [yourinputlist]&@yourinputlist

Perhaps a useful, if brute force, alternative:

If[EvenQ[Length@#],
    {#[[ Ceiling[Length@#/2] ]], #[[ -Ceiling[Length@#/2] ]]},
    #[[ Ceiling[Length@#/2] ]]
] & [yourinputlist]

Here's a short alternative (44 characters):

Union@{#[[p = Ceiling[Length@#/2]]], #[[-p]]} &@yourinputlist
Source Link
TransferOrbit
  • 3.6k
  • 14
  • 26
Loading