To make a list of the letters at even-numbered position in Alphabet, I wrote this code:
Select[Alphabet[], EvenQ[Flatten[Position[Alphabet[], #]]] &]
But it does not work. Where did I go wrong?
Much simpler solution:
Alphabet[][[1 ;; All ;; 2]]
Alphabet[][[2 ;; All ;; 2]]
Alphabet[][[1 ;; ;; 2]]
is equivalent and slightly shorter.
$\endgroup$
Commented
Feb 16, 2022 at 19:20
Using LetterNumber
and FromLetterNumber
1.
If all letters are different we can create an Association
asc = AssociationThread[#, LetterNumber @ #] & @ Alphabet[];
Keys @ Select[OddQ] @ asc
{"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}
Keys @ Select[EvenQ] @ asc
{"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}
Keys @ Select[Mod[#, 4] == 0 &] @ asc
{"d", "h", "l", "p", "t", "x"}
2.
If there are duplicated letters we can use Cases
list = {"c", "a", "b", "b", "c"}
Cases[LetterNumber @ list, x_?OddQ :> FromLetterNumber[x]]
{"c", "a", "c"}
Cases[LetterNumber @ list, x_?EvenQ :> FromLetterNumber[x]]
{"b", "b"}
Can also use Pick
Pick[Alphabet[], Range[26], _?EvenQ]
Or for the more general case with spaces removed:
lst = "element of sel matches patt";
clst=DeleteCases[Characters[lst]," "];
Pick[clst,Range[Length[clst]],_?EvenQ]
{"l", "m", "n", "o", "s", "l", "a", "c", "e", "p", "t"}
EvenQ
: Pick[Alphabet[], Range[26], _?EvenQ]
$\endgroup$
Partition
+ Transpose
can be used to separate even and odd letters:
Transpose[Partition[Alphabet[] , 2]]
(* {{"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}, {"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}} *)
Using the third argument of GroupBy
:
Values@GroupBy[Thread@{Range@Length@#, #} &@Alphabet[], OddQ, Last /@ # &]
(*{{"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"},
{"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}}*)
Using MapIndexed
with Sow
/Reap
:
MapIndexed[If[OddQ@*First@#2, Sow[#, "odd"], Sow[#, "even"]] &,
Alphabet[]] // Reap // Last
Using PositionIndex
:
PositionIndex[Alphabet[]] // GroupBy[OddQ@First@#1 &] //
Map[Keys] // Values
or
PositionIndex[Alphabet[]] //
GroupBy[#, OddQ@First@#1 &, Keys] & // Values
Result:
{{"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}, {"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}}
I've used Range to determine the sequence.
Alphabet[][[Range[1, 26, 2]]]
for odd positions,
for even positions Alphabet[][[Range[2, 26, 2]]]
Extending (slightly) the neat answer by @Sjoerd Smit:
Alphabet[odd] ^:= Alphabet[][[1 ;; All ;; 2]]
Alphabet[even] ^:= Alphabet[][[2 ;; All ;; 2]]
Alphabet[odd]
Alphabet[even]
(*
{"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}
{"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}
*)
Similarly:
Alphabet[vowels] ^:= Alphabet[][[{1, 5, 9, 15, 21}]]
Alphabet[noVowels] ^:= Alphabet[][[{2, 3, 4, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17,
18, 19, 20, 22, 23, 24, 25, 26}]]
Alphabet[vowels]
Alphabet[noVowels]
(* {"a", "e", "i", "o", "u"}
{"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q",
"r", "s", "t", "v", "w", "x", "y", "z"}
*)
Flatten[Position[Alphabet[], "a"]]
gets you{1}
and not1
. $\endgroup${False}
and what you need is just plainFalse
. $\endgroup$Select[Alphabet[], EvenQ[Flatten[Position[Alphabet[], #]]][[1]] &]
. $\endgroup$