Group Abstract Group Abstract

Message Boards Message Boards

[WSG20] Programming Fundamentals Week 3

This week we will be looking at the following topics:

  • Monday May 18: Defining Functions - Part I
  • Tuesday May 19: Defining Functions - Part II
  • Wednesday May 20: Evaluation Control
  • Thursday May 21: Entities and Weekly Review Session
  • Friday May 22: No study group (Day off at Wolfram)

Please post your questions on this thread and look out for daily challenges.

39 Replies
Posted 6 years ago

POSTED BY: Bernd Barsuhn

The extra credit is somewhat unclear, so let us begin with this code:

poly[n_Integer /; n >= 3, c_RGBColor : RGBColor[1, 0.5, 0]] := 
 Graphics[Style[RegularPolygon[n], c]]
poly[___] := " Enter an integer \[GreaterEqual] 3 "

Function applications:

{poly[3, Red], poly[7]}

enter image description here

Using Defer to leave expressions unevaluated for a quick quiz. Code is spelled out so the process is obvious:

someFractions := Table[r, {r, 1/10, 9/10, 1/20}]
Print@Column@
  MapThread[
   Row[{Defer[#1 + #2], " = "}] &, {RandomSample[someFractions], 
    RandomSample[someFractions]}]

enter image description here

BUT regular Thread with Grid is aligned more neatly:

Grid@Thread[{RandomSample[someFractions], " + ", 
   RandomSample[someFractions], " = "}]

enter image description here

NUMBERED:

Grid@Thread[{ToString[#] ~~ "." & /@ Range[Length[someFractions]], 
   RandomSample[someFractions], " + ", RandomSample[someFractions], 
   " = "}]

enter image description here

POSTED BY: Lori Johnson
POSTED BY: Yuliia Maidannyk
Posted 6 years ago
POSTED BY: math guy
POSTED BY: Ian Williams

For May 18 & 19 Challenge:

A slightly different approach. You can select the number of sides n via a slider. The default color is Orange, RGBColor[1,0.65,0]. A different color can be chosen via the color slider.

c = RGBColor[1, 0.65, 0];
c -> Dynamic[c];
Manipulate[Graphics[Style[RegularPolygon[n], c]], {n, 3, 20, 1}, 
 Column[{ColorSlider[Dynamic[c]]}], LocalizeVariables -> False]

After playing with numbers and colors, the number of sides and the RGBColor can be extracted.

n // InputForm
ColorConvert[{c}, "RGB"] // InputForm
POSTED BY: Jürgen Kanz
Posted 6 years ago

Hi Lori,

I certainly did not intend to offend you in any way. If I did, I sincerely apologize.

However, I think you misunderstood my response, which has nothing to do with fractions. I was just showing an alternative way of generating the row numbers (1., 2., ...) for the numbered version of the Grid.

There are usually many ways to solve a problem in WL. If I know of a different way, I usually share that. If you look the history of my responses on this site you will see many examples of this. I also really appreciate it when someone responds to one of my posts with an alternative solution. It is a great learning experience for me.

Regards, Rohit

POSTED BY: Rohit Namjoshi
Posted 6 years ago
Amino = <|"D" -> RGBColor["#E60A0A"], "E" -> RGBColor["#E60A0A"], 
   "C" -> RGBColor["#E6E600"], "M" -> RGBColor["#E6E600"], 
   "K" -> RGBColor["#145AFF"], "R" -> RGBColor["#145AFF"], 
   "S" -> RGBColor["#FA9600"], "T" -> RGBColor["#FA9600"], 
   "F" -> RGBColor["#3232AA"], "Y" -> RGBColor["#3232AA"], 
   "N" -> RGBColor["#00DCDC"], "Q" -> RGBColor["#00DCDC"], 
   "G" -> RGBColor["#EBEBEB"], "I" -> RGBColor["#0F820F"], 
   "L" -> RGBColor["#0F820F"], "V" -> RGBColor["#0F820F"], 
   "A" -> RGBColor["#C8C8C8"], "W" -> RGBColor["#B45AB4"], 
   "H" -> RGBColor["#8282D2"], "P" -> RGBColor["#DC9682"]|>;

ClearAll[colorResidues];
Options[colorResidues] = {ColorScheme -> Amino};

colorResidues[sequence_, opts : OptionsPattern[{colorResidues, Framed}]] :=
  sequence // Characters //
    Map[Framed[#,
       Background -> OptionValue[ColorScheme][#],
       FilterRules[{opts}, Options[Framed]]] &] //
    Row

muc6 = StringTake[ProteinData["MUC6", "Sequence"], 75];

colorResidues[muc6]

enter image description here

Options for Framed can be provided

colorResidues[muc6, FrameStyle -> None]

enter image description here

colorResidues[muc6, FrameStyle -> None, RoundingRadius -> 4]

enter image description here

colorResidues[muc6, FrameStyle -> {Thin, Blue, Dotted},
  RoundingRadius -> 4, ImageMargins -> 2]

enter image description here

Use a random ColorScheme

colorResidues[muc6, FrameStyle -> None, 
 ColorScheme -> (Amino /. _RGBColor :> RandomColor[] // Map[Evaluate])]

enter image description here

POSTED BY: Rohit Namjoshi
Posted 6 years ago

Had some difficulties using FilterOptions directly with Polygon, since an empty option list will be interpreted as the indexing form of polygon definition. So I had to use an index list as second argument and FilterRules as third argument.

POSTED BY: Bernd Barsuhn
Posted 6 years ago

That's clear and concise code :)

I didn't realise there is RegularPolygon function in Wolfram language, so here's a bit longer way to do the same with trigonometric approach:

poly[n_Integer/;n>2]:=Graphics[{Orange,Polygon[Table[{Cos[2 Pi k/n],Sin[2Pi k/n]},{k,1,n}]]}]

and with the extra color argument:

poly[n_Integer/;n>2, c_RGBColor:RGBColor[1, 0.5, 0] ]:=Graphics[{c,Polygon[Table[{Cos[2 Pi k/n],Sin[2Pi k/n]},{k,1,n}]]}]
POSTED BY: Marko Rossi
Posted 6 years ago
POSTED BY: Tingting Zhao
Posted 6 years ago
POSTED BY: Colin Scully
Posted 6 years ago
ColorResidues[seq_String, OptionsPattern[]] := 
Row@Map[Framed[#, 
Background -> 
If[OptionValue[ColorScheme] === None, None, 
OptionValue[ColorScheme][#]], FrameStyle -> None, 
FrameMargins -> OptionValue[FrameMargins]] &, Characters[seq]]

Options[ColorResidues] = {ColorScheme -> None, FrameMargins -> 1}

enter image description here

ColorResidues[StringTake[protein, 50], ColorScheme -> Amino, 
FrameMargins -> 5]

enter image description here

Color scheme in this format works also with bar chart:

    aminoCounts = protein // Characters // Counts // KeySort;
BarChart[KeyValueMap[Style[#2, Amino[#1]] &,  aminoCounts], 
 ChartLabels -> Keys[aminoCounts]]

enter image description here

POSTED BY: Marko Rossi

Hello, all! Nothing fancy, just the straight dope with an error message if necessary.

poly[n_] /; If[TrueQ[n >= 3], True, Message[poly::badint, n]; False] := Graphics[{Darker@Orange, RegularPolygon[n]}]

poly::badint = 
  "OOPS! The integer `1` should be greater than 2. TRY AGAIN!";

enter image description here

POSTED BY: Lori Johnson

Sorry for asking again, I found the discussion in the other thread.

POSTED BY: Yorick Zeschke

Dear Wolfram Study Group,

I'm trying to solve the auto-graded tasks for the Wolfram Technology Certified Level 1 Certificate and I guess there are technical issues with some of them. Task 1.4 asks for random strings so my solution (being random) does not match the expected output. I am quite sure that my code is correct. Can I maybe send it to you somehow (in order to not reveal solutions I did not post it here in the forum)? The same problem occures with task 1.8 (the spheres are randomly positioned). Suprisingly, my solution, despite not matching the expected output, is marked as correct for task 1.7 which also involves randomness. For task 1.11 I get the expected output on my local machine using Mathematica 11 but the exact same code throws many errors like "GenomeData::notent : 381 is not a known entity, class, or tag for GenomeData. Use GenomeData[] for a list of entities." when executed in the online notebook. For task 1.13, my code executes in the cloud notebook, yielding the expected output (or at least output which is visually not distinguishable from it), but is still marked as incorrect. The same seems to happen for task 1.14 (altough I can not compare the images by hand), 1.15., 1.18 and 1.20. What should I do? Can I maybe send you my solutions in my own notebook?

Best regards

Yorick Zeschke

POSTED BY: Yorick Zeschke
Posted 6 years ago

Okay. Thanks for the clarification. I thought the 5th was for both. Again, great webinar. I learned a lot.

POSTED BY: Horst Siffrin

The June 5th deadline is for the four quizzes that you must complete for the Certificate of Program Completion.

We have not yet sent out the link to the exercises that need to be completed for the Level I certification. You will have at least two weeks to complete the exercises.

Posted 6 years ago

Hi, I was wondering when and where we can find the exercises that we should complete to get the Level I certification. I thought we would receive an email, but so far I haven't gotten anything. Deadline of June 5th is coming up fast.

POSTED BY: Horst Siffrin

We've tried to fix this on our end. Let us know if you continue to see issues.

Posted 6 years ago
POSTED BY: math guy
Posted 6 years ago
POSTED BY: rjehanathan
POSTED BY: Lori Johnson

Right _ I just caught that too. Will add it.

Posted 6 years ago
POSTED BY: Rohit Namjoshi
Posted 6 years ago
POSTED BY: Rohit Namjoshi

Hi, Does anyone know how mathematica find their "example data?" For example: Import["ExampleData/elements.xls"] This gives me an element table but suppose I want an earlier version of the element table this is not what I want.. Also is there any database for those examples?

POSTED BY: Ji Zheng
Posted 6 years ago

Quiz1

PROBLEM 6 Which of the following will create a popup menu to allow the user to choose from among theoptions {“Sine”,“Cosine”,“Tangent”} to select the function for the plot?

Manipulate[Plot[f[x],{x,0,2Pi}],{f,{Sine,Cosine,Tangent},ControlTypePopupMenu}]

Manipulate[Plot[f[x],{x,0,2Pi}],{f,{Sin“Sine”,Cos“Cosine”,Tan“Tangent”},ControlTypePopupMenu}]

Manipulate[Plot[f[x],{x,0,2Pi}],{f,{Sin“Sine”,Cos“Cosine”,Tan“Tangent”}}]

Manipulate[Plot[f[x],{x,0,2Pi}],{f,{Sine,Cosine,Tangent}}]

None of them work correctly! Your example uses [OpenCurlyDoubleQuote] vs "!

This causes the error: Manipulate[Plot[f[x], {x, 0, 2*Pi}], {{f, Sin}, {Sin -> CurlyDoubleQuote[Sine], Cos -> \ CurlyDoubleQuote[Cosine], Tan -> CurlyDoubleQuote[Tangent]}, ControlType -> PopupMenu}]

in the PopupMenu in the Manipulate panel..

This works:

Manipulate[Plot[f[x], {x, 0, 2 Pi}], {f, {Sin -> Sine, Cos -> Cosine, Tan -> Tangent}, ControlType -> PopupMenu}]

POSTED BY: Syd Geraghty

Will be handed out tomorrow during the study group session and will also be included in the end-of-the-week email with links to all the notebooks, recordings and Q&A transcripts.

Posted 6 years ago
POSTED BY: Syd Geraghty

Is Wolfram Alpha Notebook Edition supposed to be adequate for the programming you're discussing?

I get "No Wolfram Language translation found" when I try to define a function with a name longer than one character.

I need to know if this is how Wolfram Alpha Notebook Edition is supposed to be, or if there's something wrong with my copy of it.

Posted 6 years ago
POSTED BY: rjehanathan

This should be helpful:

  • Making definitions for functions
  • The ordering of definitions

at the tech note on "Transformation Rules and Definitions"

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