7
$\begingroup$

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?

$\endgroup$
8
  • 2
    $\begingroup$ As one begins learning Mathematica executing smaller parts of the overall function will give strong hints as to what might not be working as expected/desired. For example, Flatten[Position[Alphabet[], "a"]] gets you {1} and not 1. $\endgroup$
    – JimB
    Commented Feb 15, 2022 at 3:10
  • $\begingroup$ @JimB But EvenQ[{1}] will show [False]. So, it shouldn't influence the answer. $\endgroup$
    – Y. zeng
    Commented Feb 15, 2022 at 3:12
  • $\begingroup$ With that you get {False} and what you need is just plain False. $\endgroup$
    – JimB
    Commented Feb 15, 2022 at 3:13
  • $\begingroup$ How about Select[Alphabet[], EvenQ[Flatten[Position[Alphabet[], #]]][[1]] &]. $\endgroup$
    – JimB
    Commented Feb 15, 2022 at 3:14
  • $\begingroup$ @JimB Thank you. You are so clever. Should I close the question. I fear somebody will vote this question to close. $\endgroup$
    – Y. zeng
    Commented Feb 15, 2022 at 3:17

8 Answers 8

17
$\begingroup$

Much simpler solution:

Alphabet[][[1 ;; All ;; 2]]
Alphabet[][[2 ;; All ;; 2]]
$\endgroup$
1
  • 1
    $\begingroup$ Alphabet[][[1 ;; ;; 2]] is equivalent and slightly shorter. $\endgroup$
    – bbgodfrey
    Commented Feb 16, 2022 at 19:20
3
$\begingroup$

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"}

$\endgroup$
3
$\begingroup$

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"}
 
$\endgroup$
2
  • 1
    $\begingroup$ (+1) - You don't need the braces around EvenQ: Pick[Alphabet[], Range[26], _?EvenQ] $\endgroup$
    – eldo
    Commented Jun 15 at 14:43
  • $\begingroup$ @eldo. Thanks. Edited. $\endgroup$
    – RGL
    Commented Jun 17 at 12:27
3
$\begingroup$

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"}} *)
$\endgroup$
2
$\begingroup$

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"}}*)
$\endgroup$
2
$\begingroup$

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"}}

$\endgroup$
1
$\begingroup$

I've used Range to determine the sequence.

Alphabet[][[Range[1, 26, 2]]] for odd positions, for even positions Alphabet[][[Range[2, 26, 2]]]

$\endgroup$
1
  • $\begingroup$ This appraoch is very similar to the one already given. $\endgroup$
    – bbgodfrey
    Commented Feb 16, 2022 at 19:21
1
$\begingroup$

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"}

*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.