1
$\begingroup$

a. Get the list whose elements are of the form {language,number of words in dictionary)

I tried this, and it gave me the number if words in dictionary, but I don't know how to get the language part:

lang = DictionaryLookup[All];

langlength =Table[{lang[[i]], Length[DictionaryLookup[{lang[[i]], All}]]}, {i, 1,
Length[lang]}]

b. Now sort this list in descending order by the number of words and get a list of the form {position in sorted list, language, number of words}.

For part b, I have

sorted = Sort[langlength, #1[[2]] >= #2[[2]] &]
placeList[list_] := Table[{i, list[[i]]}, {i, 1, Length[list]}] 
pl = placeList[sorted]

And I got

{1, {"Finnish", 728498}, 2, {"Catalan", 602208}, 3, {"Galician", 515385}, 4, {"Portuguese", 459996}, 5, {"Hebrew", 455264}, 6, {"Danish", 376469}, 7, {"BrazilianPortuguese", 264713}, 8, {"Croatian", 260752}, 9, {"Polish", 234907}, 10, {"Hungarian", 230206}, 11, {"Dutch", 229368}, 12, {"French", 139657}, 13, {"Swedish", 121430}, 14, {"Italian", 116854}, 15, {"Faroese", 108508}, 16, {"English", 92518}, 17, {"BritishEnglish", 86135}, 18, {"Spanish", 86016}, 19, {"German", 76155}, 20, {"IrishGaelic", 65203}, 21, {"Arabic", 43768}, 22, {"Breton", 32733}, 23, {"Russian", 31801}, 24, {"Hindi", 15983}, 25, {"ScottishGaelic", 15670}, 26, {"Esperanto", 14780}, 27, {"Latin", 8777}}`

From here I don't know how to get rid of the braces around Finnish, 728498 and others.

$\endgroup$

2 Answers 2

1
$\begingroup$

Try this

langlength = ({#, Length@DictionaryLookup[{#, All}]} & /@ DictionaryLookup[All]);
lst = Sort[langlength, #1[[2]] > #2[[2]] &];
TableForm[lst, TableHeadings -> {Range[Length[lst]], None}]

Edit

langlength = ({#, Length@DictionaryLookup[{#, All}]} & /@ DictionaryLookup[All]);
lst = Sort[langlength, #1[[2]] > #2[[2]] &];
placeList = Table[{i, lst[[i]]}, {i, 1, Length[lst]}]
TableForm[placeList, TableDepth -> 2]
$\endgroup$
2
  • $\begingroup$ I actually figured out part a. For part b, I have placeList[list_] := Table[{i, list[[i]]}, {i, 1, Length[list]}] $\endgroup$
    – user720329
    Apr 18, 2014 at 7:02
  • $\begingroup$ @molekyla777 Can you help me with part b. I already figured out most of the part. I just need to get rid of those braces. $\endgroup$
    – user720329
    Apr 18, 2014 at 21:46
1
$\begingroup$

Let me answer your question by first clarifying some of the fundamentals about Mathematica:

A. Mapping over list. If you have a function, you can apply it to a list of values by using Map:

Range[10]

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Map[Sqrt, Range[10]]

$\left\{1,\sqrt{2},\sqrt{3},2,\sqrt{5},\sqrt{6},\sqrt{7},2 \sqrt{2},3,\sqrt{10}\right\}$

You can do it with your own functions as well:

f[n_] := n^2 + 1 (* this function squares it's argument and adds 1 *);
Map[f, Range[10]]

{2, 5, 10, 17, 26, 37, 50, 65, 82, 101}

B. Return values. You own functions can return any kind of value, including a list:

f[n_] := {n, n^2, N[Sqrt[n]]} (* returns a list of three elements: a number, 
                                 it's square and it's square root *)
Map[f, Range[10]]

Here, you get a list of lists as a result:

{{1, 1, 1.}, {2, 4, 1.41421}, {3, 9, 1.73205}, {4, 16, 2.}, {5, 25, 2.23607}, {6, 36, 2.44949}, {7, 49, 2.64575}, {8, 64, 2.82843}, {9, 81, 3.}, {10, 100, 3.16228}}

C. Solving (a). For your first question, you seem to know already how to return a list of all languages known to Mathematica

DictionaryLookup[All]

{"Arabic", "BrazilianPortuguese", "Breton", "BritishEnglish", "Catalan", "Croatian", "Danish", "Dutch", "English", "Esperanto", "Faroese", "Finnish", "French", "Galician", "German", "Hebrew", "Hindi", "Hungarian", "IrishGaelic", "Italian", "Latin", "Polish", "Portuguese", "Russian", "ScottishGaelic", "Spanish", "Swedish"}

as well as how to get a list of the form {language,number of words in dictionary}

wordsInLanguage[lang_] := {lang, Length[DictionaryLookup[{lang, All}]]}
wordsInLanguage["Russian"]

{"Russian", 31801}

so you can just use Map to get what your need in (a):

Map[wordsInLanguage, DictionaryLookup[All]]

{{"Arabic", 43768}, {"BrazilianPortuguese", 264713}, {"Breton", 32733}, {"BritishEnglish", 86135}, {"Catalan", 602208}, {"Croatian", 260752}, {"Danish", 376469}, {"Dutch", 229368}, {"English", 92518}, {"Esperanto", 14780}, {"Faroese", 108508}, {"Finnish", 728498}, {"French", 139657}, {"Galician", 515385}, {"German", 76155}, {"Hebrew", 455264}, {"Hindi", 15983}, {"Hungarian", 230206}, {"IrishGaelic", 65203}, {"Italian", 116854}, {"Latin", 8777}, {"Polish", 234907}, {"Portuguese", 459996}, {"Russian", 31801}, {"ScottishGaelic", 15670}, {"Spanish", 86016}, {"Swedish", 121430}}

D. Flattening lists. Now, to answer your second question, you need to learn how to flatten lists. For example, given a list {1, 2}, how can you get a list {1, 2, 3}?

There are quite a few ways:

(* works if you know that the list only contains two elements *)
addThirdElement[list_, element_] := {list[[1]], list[[2]], element}
addThirdElement[{1, 2}, 3]

{1, 2, 3}

Another option is this - you can even reorder elements in the list:

addThirdElement1[{first_, second_}, third_] := {third, second, first}
addThirdElement1[{1, 2}, 3]

{3, 2, 1}

There are many more - e.g. look up Flatten in Mathematica documentation.

That should give you enough to solve the last part. Good luck!

$\endgroup$
13
  • $\begingroup$ I actually have trouble with the "Flatten" command. I tried quite a few times, but it doesn't work. $\endgroup$
    – user720329
    Apr 18, 2014 at 22:11
  • $\begingroup$ If you want to get help, you would first need to explain: 1) What have you tried (with code); 2) What do you expect; 3) What did you receive instead. You don't need to use Flatten to solve (b) - just try to read and understand the section D above. $\endgroup$
    – Victor K.
    Apr 18, 2014 at 22:14
  • $\begingroup$ I tried Flatten[pl,1] and Flatten[pl,2], but none of these worked. And if you look at my part b, you'd know what is pl, in my case. $\endgroup$
    – user720329
    Apr 18, 2014 at 22:17
  • $\begingroup$ Let's read Flatten documentation together. What does it do? $\endgroup$
    – Victor K.
    Apr 18, 2014 at 22:18
  • $\begingroup$ Well, it flattens list to a certain level $\endgroup$
    – user720329
    Apr 18, 2014 at 22:22

Your Answer

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

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