abritac
(Abrita Chakravarty)
May 11, 2020, 3:27pm
1
This week we will be looking at the following topics:
Monday May 11: Expressions and Assignments
Tuesday May 12: Iterations, Counting and Lists
Wednesday May 13: Patterns
Thursday May 14: Flow Control, Pure Functions and Scoping
Friday May 15: Review of topics from this week
Please post your questions on this thread and look out for daily challenges.
1 Like
abritac
(Abrita Chakravarty)
May 11, 2020, 9:28pm
2
Daily challenge May 11 : Write the following in a simpler form:
f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}}
EIWL: Expressions and Their Structure
1 Like
Map[f, {{1, 2}, {7, 2}, {5, 4}}]
Update:
Oops! I copied the wrong statement from my notebook (I was trying out different things)…
It should be
Apply[f, {{1, 2}, {7, 2}, {5, 4}},{1}]
And, I should have done a quick verification:
In[80]:= f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}} ==
Apply[f, {{1, 2}, {7, 2}, {5, 4}}, {1}]
Out[80]= True
Thanks, Michael for pointing this out!
2 Likes
ClearAll[f];
x1 = f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}};
x2 = f @@@ {{1, 2}, {7, 2}, {5, 4}};
x1 === x2
( True *)
mkelly
(Michael Kelly)
May 12, 2020, 12:14am
5
@Dave
Map[f, {{1, 2}, {7, 2}, {5, 4}},{1}] results in the expression {f[{1,2}],f[{7,2}],f[{5,4}]}
which is not the same as the result of evaluating f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}} which says to Map the function f onto the heads of each of the expressions at level {1} in the list, which is:
{f[1,2],f[7,2],f[5,4]}
This is applying the function f to the heads of each of the sublists and is described as:
Apply[f,{{1,2},{7,2},{5,4}},{1}]
There is also a shortcut form
f@@@{{1,2},{7,2},{5,4}}
1 Like
ClearAll[f];
x1 = f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}};
x2 = f @@@ {{1, 2}, {7, 2}, {5, 4}};
x1 === x2
( True *)
2 Likes
Apply[f, {{1, 2}, {7, 2}, {5, 4}}, 1]
2 Likes
abritac
(Abrita Chakravarty)
May 12, 2020, 9:22pm
8
Daily challenge May 12 : Try the following:
Type Ctrl = and provide the natural language query “flags of asian countries”. It should return the flags of the countries in Asia.
Use DominantColors on an image to see the dominant colors in that image.
Use the above to find the dominant colors in the flag images of the Asian countries and build an association to show the number of times each color appears in the list of flags. Which are the top 3 colors (occurring in the most number of flags in our list)?
2 Likes
EntityClass[“Country”, “Asia”][“Flag”] // Map[DominantColors] // Flatten // Counts //
TakeLargest[3]
3 Likes
lgodfrey
(Lee Godfrey)
May 12, 2020, 10:43pm
11
The Wolfram Language supports many approaches to problem solving:
v1 = f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}};
v2 = {{1, 2}, {7, 2}, {5, 4}} /. {x_, y_} -> f[x, y];
v1 === v2 (* True *)
2 Likes
Hi Abrita!
If by “simple” you mean “easier to read”…here’s my entry
Apply[f, {{1, 2}, {7, 2}, {5, 4}}, {1}]
2 Likes
Here is a progression of my thought process:
asianCountries = EntityList@EntityClass["Country", "Asia"]r code here
asianFlags =
EntityList@EntityClass["Country", "Asia"][EntityProperty["Country", "Flag"]]
domColors =
DominantColors[#] & /@
EntityList@EntityClass["Country", "Asia"][EntityProperty["Country", "Flag"]]
Flatten:
flatdomColors = Flatten@domColors
Group by color to build an Association:
GroupBy[flatdomColors, {"Color"}][[1 ;; 5]]
Count the colors:
Map[Length, GroupBy[flatdomColors, {"Color"}]][[1 ;; 5]]
Sort largest to smallest, and take the top 3:
Take[ReverseSort@Map[Length, GroupBy[flatdomColors, {"Color"}]], 3]
"NearestHTMLColor:…NICE!!!
vaungureanu
(Valeriu Ungureanu)
May 13, 2020, 4:40am
15
Only top 3 colors, without frequencies:
Keys@ReverseSort[
Counts@Flatten[
DominantColors /@
EntityClass["Country", "Asia"][
EntityProperty["Country", "Flag"]]]][[1 ;; 3]]
Top 3 colors with their frequencies:
ReverseSort[
Counts@Flatten[
DominantColors /@
EntityClass["Country", "Asia"][
EntityProperty["Country", "Flag"]]]][[1 ;; 3]]
The shortest code:
TakeLargest[3]@Counts@Flatten@DominantColors@EntityClass["Country", "Asia"]["Flag"]
1 Like
vaungureanu
(Valeriu Ungureanu)
May 13, 2020, 6:31am
16
Hi, Yuliia,
There are 5 countries that have more than 4 colors in their flags:
Grid@Select[
Partition[
Riffle[EntityClass["Country", "Asia"]["Name"],
DominantColors@EntityClass["Country", "Asia"]["Flag"]], 2],
Length@#[[2]] > 4 &]
DominantColors[
ImageCollage[
EntityValue[EntityClass[“Country”, “Asia”],
“FlagImage”]], 3, {“Color”, “NearestHTMLColor”,
“Count”}, “ColorPropertyAssociation”]
The 3 most common “nearest HTML colours” are FireBrick, White and SeaGreen.
1 Like
I wonder why I get different counts for the top 3 dominant colors… my code looks quite similar to yours?
flags = EntityClass["Country", "Asia"][
EntityProperty["Country", "Flag"]];
TakeLargest[Counts[Flatten[DominantColors[flags]]], 3]
vaungureanu
(Valeriu Ungureanu)
May 13, 2020, 2:13pm
19
Hi Marco,
I have tested your code:
I can suggest you to Clear[] the variable flags, but I am not sure that this is the solution!
Another suggestion - to restart Mathematica!
I get the same result as Marko.
$Version
(* 12.1.0 for Mac OS X x86 (64-bit) (March 14, 2020) *)
EntityClass["Country", "Asia"]["Flag"] // Map[DominantColors] // Flatten // Counts //
TakeLargest[3]