Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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]
share|improve this answer
    
I actually figured out part a. For part b, I have placeList[list_] := Table[{i, list[[i]]}, {i, 1, Length[list]}] –  user720329 Apr 18 at 7:02
    
@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. –  user720329 Apr 18 at 21:46

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!

share|improve this answer
    
I actually have trouble with the "Flatten" command. I tried quite a few times, but it doesn't work. –  user720329 Apr 18 at 22:11
    
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. –  Victor K. Apr 18 at 22:14
    
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. –  user720329 Apr 18 at 22:17
    
Let's read Flatten documentation together. What does it do? –  Victor K. Apr 18 at 22:18
    
Well, it flattens list to a certain level –  user720329 Apr 18 at 22:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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