Message Boards Message Boards

[WSG20] Programming Fundamentals Week 2

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.

79 Replies

Daily challenge May 11: Write the following in a simpler form:

f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}}

EIWL: Expressions and Their Structure

Posted 5 years ago

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!

POSTED BY: Dave McCollum
Posted 5 years ago
ClearAll[f];

x1 = f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}};
x2 = f @@@ {{1, 2}, {7, 2}, {5, 4}};

x1 === x2
( True *)
POSTED BY: Updating Name

@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 <a href="mailto:f@@@{{1,2},{7,2},{5,4}">f@@@{{1,2},{7,2},{5,4}}

POSTED BY: Michael Kelly
Posted 5 years ago
ClearAll[f];

x1 = f @@ # & /@ {{1, 2}, {7, 2}, {5, 4}};
x2 = f @@@ {{1, 2}, {7, 2}, {5, 4}};

x1 === x2
( True *)
POSTED BY: Rohit Namjoshi
Apply[f, {{1, 2}, {7, 2}, {5, 4}}, 1]
POSTED BY: Yuliia Maidannyk

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)?

Posted 5 years ago
EntityClass["Country", "Asia"]["Flag"] // Map[DominantColors] // Flatten // Counts //
  TakeLargest[3]
POSTED BY: Rohit Namjoshi

enter image description here

Attachments:
POSTED BY: Yuliia Maidannyk
Posted 5 years ago

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 *)
POSTED BY: Lee Godfrey

Hi Abrita!

If by "simple" you mean "easier to read"...here's my entry :-)

Apply[f, {{1, 2}, {7, 2}, {5, 4}}, {1}]

POSTED BY: Lori Johnson

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]

enter image description here

POSTED BY: Lori Johnson

"NearestHTMLColor:...NICE!!!

POSTED BY: Lori Johnson

Only top 3 colors, without frequencies:

Keys@ReverseSort[
   Counts@Flatten[
     DominantColors /@ 
      EntityClass["Country", "Asia"][
       EntityProperty["Country", "Flag"]]]][[1 ;; 3]]

enter image description here

Top 3 colors with their frequencies:

ReverseSort[
  Counts@Flatten[
    DominantColors /@ 
     EntityClass["Country", "Asia"][
      EntityProperty["Country", "Flag"]]]][[1 ;; 3]]

enter image description here

The shortest code:

TakeLargest[3]@Counts@Flatten@DominantColors@EntityClass["Country", "Asia"]["Flag"]

enter image description here

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 &]

enter image description here

Posted 5 years ago

DominantColors[ ImageCollage[ EntityValue[EntityClass["Country", "Asia"], "FlagImage"]], 3, {"Color", "NearestHTMLColor", "Count"}, "ColorPropertyAssociation"]

The 3 most common "nearest HTML colours" are FireBrick, White and SeaGreen.

POSTED BY: rjehanathan
Posted 5 years ago

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]

enter image description here

POSTED BY: Marko Rossi

Hi Marco,

I have tested your code:

enter image description here

I can suggest you to Clear[] the variable flags, but I am not sure that this is the solution!

Another suggestion - to restart Mathematica!

Posted 5 years ago

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]

enter image description here

POSTED BY: Rohit Namjoshi

It's simply to observe the correctness of the result in the following picture:

enter image description here

Posted 5 years ago

Hi Valeriu,

You should use Partition[..., UpTo[6]]. Your grid is missing Yemen.

Looks like an OS dependent difference / bug. When I run your code on Mac OS, I get the same result as my code and Marko's, which is different from yours.

Marko, What version are you running?

POSTED BY: Rohit Namjoshi

Hi Rohit,

I has highlighted only the count of the White color! The number is larger than 9!

I have tested the same code on the Wolfram Cloud and obtained another result!

Is this a bug?

Posted 5 years ago

A late entry to Tuesday's challenge, not very elegant compared to some of the other postings, but it seems to work.

EntityClass["Country", "Asia"]["Flag"];
colours = DominantColors[%];
Flatten[%];
dataset = Counts[%];
Normal[dataset];
SortBy[%, Values[#] &];
Take[%, -3]
POSTED BY: Updating Name
Posted 5 years ago

That is strange, when I run your code I get an identical result to the code I've posted, i.e.

41, 13,13

POSTED BY: Stuart Reilly
Posted 5 years ago

I'm running Mathematica 12.1. on Mac. I tried now the same code with Mathematica Online, and I got different results again!

Mathematica Online: enter image description here

and results with Mathematica 12.1 running on Mac: enter image description here

Strange that the results depend on where I run the code.

POSTED BY: Marko Rossi
Posted 5 years ago

Still having trouble with Replace. I want to change a particular number in a list. Here’s the catch… I had matemática make the list for me from a calculation, now the numbers are in machine form which I don’t want. How do I tell Mathematica that not every number that has a decimal is in machine form? Let’s get specific:

a={80.5,84.9,81.5};
b={82.2,85.6,81.4};
c=b-a

Now I want to replace the first value in the list c with 5.

c/. 1.7->5

Replace won’t do it! Anyone got a work around?

POSTED BY: R. Casals
Posted 5 years ago

Hi Abrita,

As you can see from the answers below, Mac OS, Windows and Wolfram Cloud give different results for DominantColors. This appears to be a bug. Please report to the development team.

Thanks, Rohit

POSTED BY: Rohit Namjoshi
Posted 5 years ago

Matching on machine precision real numbers is brittle. Some options

Round[c, .1] /. 1.7 -> 5
(* {5, 0.7, -0.1} *)

Rationalize@c /. 17/10 -> 5
(* {5, 7/10, -(1/10)} *)
POSTED BY: Rohit Namjoshi

The list of colors returned by DominantColors represent clusters of colors that appear in the image. Internally the clustering algorithm may behave differently depending on initialization values. For e.g. on my system I get different results on restricting the clustering method and distance function.

$Version
(* "12.1.0 for Mac OS X x86 (64-bit) (March 18, 2020)" *)

TakeLargest[
     Counts@Flatten@
       DominantColors[
        EntityClass["Country", "Asia"][
         EntityProperty["Country", "Flag"]]], 3]

enter image description here

TakeLargest[
 Counts@Flatten@
   DominantColors[
    EntityClass["Country", "Asia"][EntityProperty["Country", "Flag"]],
     Method -> "KMeans", DistanceFunction -> "CIE76"], 3]

enter image description here

Do you want to pick the item to replace by it's index (like you say the "first" value in the list) or by its value (1.7)?

If its the former, then ReplacePart is an option:

a={80.5,84.9,81.5};
b={82.2,85.6,81.4};
c=b-a

ReplacePart[c, 1 -> 5]

Otherwise the following may work:

Replace[c, x_ /; (N[x, 2] == 1.7) -> 5, {1}]
ReplaceAll[c, x_ /; N[x, 2] == 1.7 -> 5]

Daily Challenge May 13: From EIWL chapter on Patterns

In IntegerDigits[Range[100]] replace all 0’s by Gray and all 9’s by Orange

Bonus challenge: Style all even digits in Green and odd digits in Red

Posted 5 years ago

I am starting to see what you mean by brittle. I tried to use Nearest function to no avail:

c/.Nearest[c,1.7]->5

Also wanted to mention that your proposed solution worked great with values for a and b, but failed with the following new lists:

a={80.5,84.9,81.5,70};
b={82.2,85.6,81.4,90.9};
c=b-a

When I try to round in the above example it fails to do what I had hoped for:

Round[c,.1]/. 20.9->2.9

These are the actual numbers I am working with in the dataset. I am not just being pedantic. Good News: the “Rationalize solution” works great. I just wish I had a more robust solution. Thanks again.

POSTED BY: R. Casals
IntegerDigits[Range@100] /. {0 -> Gray, 9 -> Orange}

enter image description here

IntegerDigits[Range@100] /. {x_?EvenQ -> Style[x, Green], 
  x_?OddQ -> Style[x, Red]}

enter image description here

IntegerDigits[Range@100] /. {x_?EvenQ -> Green, x_?OddQ -> Red}

enter image description here

And an extra bonus for fun :)

f[x_] := Row[
  Column /@ 
   Transpose@{IntegerDigits[x] /. {y_?EvenQ :> Style[y, Gray], 
       y_?OddQ :> Style[y, Orange]}, 
     IntegerDigits@x /. {y_?EvenQ -> Green, y_?OddQ -> Red}}]

f[2^220]

enter image description here

IntegerDigits[Range[100]] /. {0 -> Gray, 9 -> Orange}

Bonus challenge: Style all even digits in Green and odd digits in Red

IntegerDigits[Range[100]] /. {x_?EvenQ :> Style[x, Green], 
  x_?OddQ :> Style[x, Red]}

Bonus

POSTED BY: Mickaël Bouvier
Posted 5 years ago

My notebook with both versions of Wednesday's challenge

POSTED BY: Stuart Reilly

In IntegerDigits[Range[100]] replace all 0's by Gray and all 9's by Orange

ReplaceAll[IntegerDigits[Range[100]], {0 -> Gray, 9 -> Orange}]

Style all even digits in Green and odd digits in Red

ReplaceAll[
 IntegerDigits[Range[100]], {0 -> Gray, 9 -> Orange, 
  n_?EvenQ -> Style[n, Green], n_?OddQ -> Style[n, Red]}]

Result

POSTED BY: Yuliia Maidannyk
Posted 5 years ago

IntegerDigits[Range[100]] /. {0 -> Gray, 9 -> Orange}

IntegerDigits[Range[100]] /. {Integer?OddQ -> Red, Integer -> Green}

POSTED BY: rjehanathan

(* Hi Abrita!

I really want to think you are asking for Gray and Orange digits, so I did this...*)

integerStrings = IntegerString[Range[100]];
StringReplace[integerStrings, {"0" -> 
   ToString[Style["0", Gray, Bold], TraditionalForm], 
  "9" -> ToString[Style["9", Orange, Bold], TraditionalForm]}]

enter image description here

POSTED BY: Lori Johnson

"Bonus challenge: Style all even digits in Green and odd digits in Red"

Tried using Rules and it was a TOTAL DISASTER, so went old school:

evenOddTest[n_] := 
 If[EvenQ[n], ToString[Style[n, Darker@Green], StandardForm], 
  ToString[Style[n, Darker@Red], StandardForm]]
Table[If[Length[n] == 1, evenOddTest[n], 
  StringJoin[evenOddTest /@ IntegerDigits[n]]], {n, 1, 100}

enter image description here

POSTED BY: Lori Johnson
Posted 5 years ago

Most concise version I came up with:

IntegerDigits[Range[100]] /. {0 -> Gray, 9 -> Orange, 
  x_?OddQ -> Style[x, Red], x_?EvenQ -> Style[x, Green]}
POSTED BY: Lee Godfrey
Posted 5 years ago

Using Map rather than ReplaceAll.

IntegerDigits[Range[100]] //
   Map[
     Which[
       # == 0, Gray,
       # == 9, Orange,
       EvenQ@#, Style[#, Green],
       OddQ@#, Style[#, Red]
       ] &, #, {-1}] & //
  Partition[#, UpTo[10]] & // Grid[#, Alignment -> Right] &

enter image description here

POSTED BY: Rohit Namjoshi
Posted 5 years ago

Using Nearest

c /. x_ /; x == First@Nearest[c, 20.9] -> 2.9
POSTED BY: Rohit Namjoshi

Thought this would be something useful to share on "Pure Functions" on seeing some of the questions at the study group session today (from the chapter Pure Anonymous Functions in EIWL):

Why are they called “pure functions”?

Because all they do is serve as functions that can be applied to arguments. They’re also sometimes called anonymous functions, because, unlike say Blur, they’re not referred to by a name. Here I’m calling them “pure anonymous functions” to communicate both meanings.

===

Why does one need the &?

The & (ampersand) indicates that what comes before it is the “body” of a pure function, not the name of a function.

f/@{1, 2, 3} gives {f[1], f[2], f[3]}, but f&/@{1, 2, 3} gives {f, f, f}.

Daily Challenge May 13: From the chapter Pure Anonymous Functions in EIWL

Make a list of letters of the alphabet, in random colors, with frames having random background colors. enter image description here

And as I was late in posting the challenge today, have fun working on it tomorrow as well--only go ahead and add to it. Do something more interesting to showcase the power of a pure function.

Surprise us :)

a = {80.5, 84.9, 81.5};
b = {82.2, 85.6, 81.4};
c = b - a
c[[1]] = 5

After the calculations, I set the first value in c to 5.

POSTED BY: Jay Morreale

Let us start with the simplest code:

Framed[Style[#, RandomColor[]], Background -> RandomColor[]] & /@ 
 Alphabet[]

And something more interesting, with application of pure functions, MapIndexed[] and Hue[]:

Column /@ 
  Transpose@{MapIndexed[
     Framed[Style[#1, RandomColor[]], Background -> Hue[#2/28]] &, 
     Alphabet[]], 
    MapIndexed[
     Framed[Style[#1, RandomColor[]], Background -> Hue[1 - #2/27]] &,
      ToUpperCase@Alphabet[]]} // Row

enter image description here

Posted 5 years ago

Style[Framed[#, FrameStyle -> Black], RandomColor[], Background -> RandomColor[], Large] & /@ Alphabet[]

Attachments:
POSTED BY: rjehanathan
Posted 5 years ago

What's the procedure for adding a notebook? A list of steps will be appreciated. Thanks, Rajeev

POSTED BY: rjehanathan
Posted 5 years ago

One limitation is this doesn't prevent the same color being used for letter and background:

Framed[Style[#, RandomColor[]], Background -> RandomColor[]] & /@ 
 Alphabet[]

enter image description here

POSTED BY: Lee Godfrey

HI ABRITA!

Just saw this. Here's the alphabet, OUT OF ORDER!

c := RandomReal[]
Framed[Style[ToString[#], FontColor -> RGBColor[c, c, c]], 
   Background -> RGBColor[c, c, c]] & /@ RandomSample[Alphabet[]]

enter image description here Here it is with different sizes:

Framed[Style[ToString[#], FontSize -> RandomInteger[{5, 20}], 
    FontColor -> RGBColor[c, c, c]], 
   Background -> RGBColor[c, c, c]] & /@ RandomSample[Alphabet[]]

enter image description here

POSTED BY: Lori Johnson
Posted 5 years ago

This appears to work: Style[Framed[#, FrameStyle -> Black], Large, With[{x = RandomColor[]}, {x, Background -> ColorNegate[x]}]] & /@ Alphabet[]

POSTED BY: rjehanathan
Posted 5 years ago

Here is a straight forward solution

Framed[#, Background -> Hue[Random[], 1, 1, 0.5], 
   RoundingRadius -> 5] & /@ Alphabet[]

Applying the function twice

Framed[#, Background -> Hue[Random[], 1, 1, 0.5], 
    RoundingRadius -> 5] & /@ Alphabet[];
Framed[#, Background -> Hue[Random[], 1, 1, 0.5], 
   RoundingRadius -> 5] & /@ %
POSTED BY: Stuart Reilly
Posted 5 years ago
IntegerDigits[Range[100]] /.
      {x_?EvenQ :> Style[x, Green], 
       x_?OddQ :> Style[x, Red]} /.
     {Style[0, _] -> Gray, 
      Style[9, _] -> Orange}

enter image description here

POSTED BY: Fabio Alcaro
Posted 5 years ago
Framed[#, 
   Background -> 
    RandomColor[]] & /@ (Style[#, FontColor -> RandomColor[]] & /@ 
   Alphabet[])
POSTED BY: Updating Name
Framed[#, 
   Background -> 
    RandomColor[]] & /@ (Style[#, FontColor -> RandomColor[]] & /@ 
   Alphabet[])
POSTED BY: Yuliia Maidannyk

Just for fun:

Map[Graphics[{RandomColor[], EdgeForm[{Thick, RandomColor[]}], 
    RegularPolygon[2, RandomInteger[{3, 10}]], 
    Text[Style[#, 20, RandomColor[]]]}, ImageSize -> 25] &, 
 Alphabet[]]

enter image description here

Posted 5 years ago
Graphics[MapThread[
  Rotate[Text[Framed[Style[#, 20, RandomColor[]], Background -> RandomColor[]], #2], 
    Pi/2 - #3] &, 
  {RandomSample@Alphabet[], CirclePoints@26, Range[2 Pi/26, 2 Pi, 2 Pi/26]}]]

enter image description here

POSTED BY: Rohit Namjoshi

Hi Abrita,

The second problem of Quiz 2 contains an error in the first item!

The fifth problem has an inconsistency, too!

Noted. We're working to fix it. Thanks for the heads up

Posted 5 years ago

With unicode emoji and color schemes :

colors = ColorData[60, "ColorList"];
Row[Framed[Style[#, Magnification -> 1.3] , 
    Background -> Lighter[RandomChoice[colors]], 
    FrameStyle -> None] & /@ CharacterRange["\|01f601", "\|01f637"]]

enter image description here

POSTED BY: Marko Rossi

Applying it twice is very nice! I may borrow this :-)

POSTED BY: Lori Johnson

How fun! I LOVE IT!!!

POSTED BY: Lori Johnson
list = {};
For[i = 1, i <= Length[Alphabet[]], i++, 
 AppendTo[list, Framed[alphabet[[i]], Background -> Hue[i*1/27]]]]
Row[list]

enter image description here

POSTED BY: Yuliia Maidannyk

Rohit's code plotting the alphabet on the circle (learned a new command!). So, I was inspired to plot them onto a sine curve:

Graphics[MapThread[
  Text[
    Framed[
     Style[#, 20, RandomColor[]], 
     Background -> RandomColor[]], #2] &, {RandomSample@
    Alphabet[] (*values populate #*),
   Table[{x, Sin[2 x]}, {x, 0, Pi, Pi/25}] (*values populate #2*)}]]

enter image description here

Then, I decided that the letters should be TALLER at the top and bottom of the curve:

Graphics[MapThread[
  Text[
    Framed[
     Style[#, 20, RandomColor[], FontSize -> 20*Abs[#2[[2]]]], 
     Background -> RandomColor[]], #2] &, {RandomSample@
    Alphabet[] (*values populate #*),
   Table[{x, Sin[2 x]}, {x, 0, Pi, 
     Pi/25}] (*ordered pair values populate #2*)}]]

enter image description here

I can NOT believe it worked! Thank you, Rohit, for the inspiration!!!

NEXT: alphabet on a spiral!

POSTED BY: Lori Johnson

You inspired me! See my post below :-D

POSTED BY: Lori Johnson
Posted 5 years ago

Hi Lori,

That is cool!

POSTED BY: Rohit Namjoshi

Rohit and Lori inspired me to apply Graphics3D

Graphics3D[
 MapThread[
  Text[Framed[Style[#1, 20, RandomColor[]], 
     Background -> RandomColor[], 
     RoundingRadius -> 7], #2] &, {Riffle[ToUpperCase@Alphabet[], 
    Alphabet[]], SpherePoints@52}]]

enter image description here

Posted 5 years ago

I spent some time exploring color distances to make the randomly colored letters easier to read on randomly colored backgrounds. It was a great learning experience but I did not find a way to consistently enhance readability. If anyone with experience in color processing can take a look at the attached notebook and suggest a way forward, I would appreciate it.

Attachments:
POSTED BY: Lee Godfrey
Posted 5 years ago

Valeriu's alphabet disco ball

frames = Table[
   Graphics3D[
    MapThread[
     Text[Framed[Style[#1, 20, RandomColor[]], Background -> RandomColor[], 
        RoundingRadius -> 7], #2] &, 
     {Riffle[ToUpperCase@Alphabet[], Alphabet[]], SpherePoints@52}],
    Boxed -> False,
    ImageSize -> 500,
    Axes -> False,
    ViewVector -> {10 Cos[t], 5, 20 Sin[t]}], {t, -Pi, Pi, Pi/20}];

Export["alphabet_disco.gif", frames, "DisplayDurations" -> .2]

enter image description here

POSTED BY: Rohit Namjoshi
Posted 4 years ago

Use NestList to call it many times

NestList[Map[Framed[#, Background -> Hue[Random[], 1, 1, 0.5], RoundingRadius -> 5] &],
  Alphabet[], 3] // 
 Grid[#, Frame -> All] &

enter image description here

POSTED BY: Rohit Namjoshi

Very interesting, Rohit! We can apply MapIndexed[], too!

NestList[MapIndexed[
   Framed[#1, Background -> Hue[Random[], 1, 1, 0.5], 
     RoundingRadius -> 4 + #2] &], Alphabet[], 5] // Grid[#] &

enter image description here

Posted 4 years ago
lst = Framed[#, Background -> Hue[Random[], 0.2, 0.9, 1], 
         RoundingRadius -> 5] & /@ Alphabet[];
    Table[Text[
      Style[Nest[
        Framed[#, Background -> RandomColor[], RoundingRadius -> 5] &, 
        lst[[i]], RandomInteger[{1, 10}]], 
       FontSize -> RandomInteger[{10, 40}]]], {i, 1, 26}]
POSTED BY: Stuart Reilly
Posted 4 years ago

One more experiment with alphabet disco balls :)

First a function to create framed alphabets:

letter[char_String] :=  Framed[Style[Capitalize[char], FontSize -> 40, RandomColor[]], 
      Background -> Lighter[RandomColor[]], Alignment -> Center, 
      ImageSize -> {100, 100}, FrameStyle -> None]

Works like this:

faces = letter[#] & /@ RandomChoice[Alphabet[], 30];
faces[[1 ;; 6]] // Row

enter image description here

Mathematica has a built-in collection PolyhedronData[] (for creating alphabet disco balls of course), and Texture[] function to add some glitter, so I wanted to give them a try:

Texturing seems to work by using Texture[] together with Polygon's VertexTextureCoordinates property, which indicates how texture tile is mapped to the polygon. There might be a more elegant way to do this, but I ended up using ReplaceAll:

texturecoords = {{0, 0}, {1, 0}, {1, 1}, {0, 
    1}}; (*texture tile corners*)
polygons = 
 PolyhedronData["RhombicTriacontahedron", "Polygons"] /. 
  Polygon_[args_] :> 
   Polygon[args, 
    VertexTextureCoordinates -> 
     texturecoords]; (*add texturecoords to each polygon in the \
polyhedron*)

With some effort and by borrowing code and ideas from Valeriu's and Rohit's posts above, I managed to create an alphabet disco ball:

frames = Table[
   Graphics3D[{Specularity[White, 20], 
     Table[{Texture[faces[[k]]], polygons[[k]]}, {k, 1, 
       Length[polygons]}]}, Boxed -> False, RotationAction -> "Clip", 
    ViewVector -> {10 Cos[t], 5, 20 Sin[t]}], {t, -Pi, Pi, Pi/20}]; 

Export["alphabet_discoball.gif", frames, "DisplayDurations" -> .2, 
  ImageSize -> Medium];

enter image description here

POSTED BY: Marko Rossi

I am giggling with delight!

POSTED BY: Lori Johnson

This one reminds me of JEWELS! So good!

POSTED BY: Lori Johnson

My first 3D, 'boo hiss' no animation so you'll have to drag it around once its evaluated but it IS a SPIRAL!

Graphics3D[
 MapThread[
  Text[Framed[Style[#1, 5, Hue[1, .5, .5, #2[[1]]]], 
     Background -> RandomColor[], 
     RoundingRadius -> 10], #2 ] &, {Riffle[ToUpperCase@Alphabet[], 
    Alphabet[]], 
   Table[{u/2, Sin[u], Cos[u]}, {u, 0, 
     51}]}]] (*SPIRALING in 3D: modifying Valerieu's code*)

enter image description here

POSTED BY: Lori Johnson

I joined this study group in week 3, how to I find & complete the older quizzes?

POSTED BY: Paul Erickson

Links to all the quizzes should be listed in the reminder emails. A final collection of links will also be sent out on Tuesday June 2nd.

That's the point. I have not been getting any of the reminder emails - none to date. I received only the signup confirmation which allowed me to attend - and the 1st 3 quizzes where announced in the chat. The last was not. I'll wait 'til Wednesday, but don't expect to receive the email... ;-(

POSTED BY: Paul Erickson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract