Group Abstract Group Abstract

Message Boards Message Boards

6
|
45.2K Views
|
58 Replies
|
51 Total Likes
View groups...
Share
Share this post:

[WSG20] Programming Fundamentals Week 1

During week 1 of Daily Wolfram Study Group, May 2020, we will be looking at the following topics:

  • Day 1: Working with Data
  • Day 2: Building Interfaces
  • Day 3: Functional Programming
  • Day 4: Tips for Writing Fast Code

We will look at lecture videos from the Wolfram U archives and also work on simple coding examples and mini projects.

Feel free to post questions on the material we cover in these sessions here.

58 Replies

Can we download the notebooks in this series? If so where can we find the links too the notebooks?

POSTED BY: Charles Glover

Can we download the notebooks in this series? If so where can we find the links too the notebooks?

POSTED BY: Charles Glover
Posted 6 years ago

My final attempt at the PigLatin challenge. The code is much improved since my last submission. It passes all of the tests on the specification, including preserving punctuation + Rohit NamJoshi's Test procedure. I haven't tried to submit to the challenge web page, but will do so later. Any comments welcome: Having never really had a need to process strings before, I found this to be a difficult task...

PigLatin[text_String] := Module[{s = text},

  vlclst = {"a", "e", "i", "o", "u"};
  vuclst = {"A", "E", "I", "O", "U"};
  punclist = {".", ",", ";", ":", "!"};
  lst = StringExtract[s, All];

  StrngReorder[strRO_] :=
   StringInsert[
    StringTake[strRO, 1 - StringLength[strRO]],
    StringPart[strRO, 1],
    If[StringEndsQ[strRO, punclist], -2, -1]
    ];

  StrngStartConsonant[strSC_] :=
   StringFreeQ[
    StringPart[strSC, 1],
    vlclst,
    IgnoreCase -> True
    ];

  StrngAddWay[strAW_] :=
   StringInsert[strAW,
    "way",
    If[StringEndsQ[strAW, punclist], -2, -1]
    ];

  StrngAddAy[strAY_] :=
   StringInsert[
    NestWhile[StrngReorder[#] &, strAY, StrngStartConsonant[#] &],
    "ay",
    If[StringEndsQ[strAY, punclist], -2, -1]
    ];

  StrngMoveFirstVowel[strFV_] :=
   StringInsert[
    StringTake[strFV, 1 - StringLength[strFV]],
    StringTake[strFV, 1],
    If[StringEndsQ[str, punclist], -2, -1]
    ];

  StrngAddAy2[strAY2_] := Module
    [{str = strAY2},
    str = StrngMoveFirstVowel[strAY2];
    StrngAddAy[str]
    ];

  StrngRule2[str2_] := If[
    StringStartsQ[str2, vlclst],
    StrngAddWay[str2],
    str2];

  StrngRule3[str3_] := If[
    StrngStartConsonant[str3],
    StrngAddAy[str3],
    str3];

  StrngRule4[str4_] := If[
    StringStartsQ[str4, vuclst] && 
     StringContainsQ[StringTake[str4, {2, StringLength[str4]}], 
      vlclst],
    StrngAddAy2[str4],
    str4
    ];

  lst = StrngRule4 /@ StrngRule3 /@ StrngRule2 /@ lst;

  StringRiffle[lst]
  ]
POSTED BY: Updating Name

A very unproductive way of doing this challenge but I am not familiar with If statements in Wolfram

Attachments:
POSTED BY: Yuliia Maidannyk
Posted 6 years ago

Hi Yuliia,

Use pigTests from my answer above to test your solution.

pigTests // 
 KeyValueMap[If[(r = textpiglatin[#1]) == #2, True, Print["Expected: ", #2, " Got: ", r]; False] &]

Expected: If ethay ordway ontainscay onay owercaselay owelsvay, othingnay isway angedchay.

Got: If ethay ordway ontainscay onay owercaselay owels,vay othingnay isway anged.chay

It fails on the sentence with punctuation test.

POSTED BY: Rohit Namjoshi

Thank you. I fixed the code to account for punctuation and cleaned everything up a bit.

Attachments:
POSTED BY: Yuliia Maidannyk

Thank you. I fixed the code to account for punctuation and cleaned everything up a bit.

Attachments:
POSTED BY: Yuliia Maidannyk

Thanks for the posts folks. We'll continue the discussion on the "Pig Latin" challenge here.

Follow the Wolfram Study Group discussions for "Programming Fundamentals Week 2" at https://community.wolfram.com/groups/-/m/t/1970834

Posted 6 years ago
POSTED BY: Stuart Reilly
Posted 6 years ago

Hi Stuart,

There are a few cases that your solution does not handle correctly. If you take pigTests from my solution above and run your code against those tests there are three failures.

pigTests // 
 KeyValueMap[
  If[(r = PigLatin[#1]) == #2, True, Print["Expected: ", #2, " Got:", r]; False] &]

Expected: eOchray Got:Ochre

Expected: eeAgray Got:Agree

Expected: If ethay ordway ontainscay onay owercaselay owelsvay, othingnay isway angedchay. Got:If ethay ordway ontainscay onay owercaselay owelsvay othingnay isway angedchay

POSTED BY: Rohit Namjoshi
PigLatin[x_] :=

 Block[{lowerVowels = {"a", "e", "i", "o", "u"}, consonants}, 
  consonants = {"B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N",
     "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z", "b", "c", "d", 
    "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", 
    "v", "w", "x", "y", "z", "A", "E", "I", "O", "U"};
  StringRiffle[Map[If[StringFreeQ[#, lowerVowels], #,
      If[StringContainsQ[StringTake[#, 1], lowerVowels],
       If[
        Total[Boole[
           StringContainsQ[Characters[#], PunctuationCharacter]]] == 0,
        # <> "way", 
        StringJoin[Drop[Characters[#], -1]] <> "way" <> 
         Last[Characters[x]]],
       If[
        Total[Boole[
           StringContainsQ[Characters[#], PunctuationCharacter]]] == 0,
        StringJoin[Reverse[TakeDrop[Characters[#],

        LengthWhile[
         Boole[StringContainsQ[Characters[#], consonants]], #1 == 
           1 &]
        ]]] <> "ay",
    StringJoin[Reverse[TakeDrop[Drop[Characters[#], -1],

        LengthWhile[
         Boole[StringContainsQ[Characters[#], consonants]], #1 == 
           1 &]
        ]]] <> "ay" <> Last[Characters[#]]]]] &, StringSplit[x]]]]

Cloud version of the code

POSTED BY: Arturo Silva
Posted 6 years ago

enter image description here

I feel like we are wrapping layer after layer, forming a snowball of code that I hope doesn't melt. This is "brakety" and "wrappery" hence I get lost in the brackets and can't visualize what is happening underneath very well. Having said that I find all this valuable and instructive!

POSTED BY: Aaron Moment
Posted 6 years ago

Hi Aaron,

Languages that use a Lisp-like syntax (Lisp, Scheme, Racket, Clojure, WL, ...) suffer from "bracket/parenthesis hell". I find it hard to understand deeply nested expressions. Good formatting and indenting helps, but you still have to comprehend it from "inside out".

In WL you can use Postfix (//), Composition (@*), RightComposition (/*) and operator forms of functions to write expressions in a left to right order that I find easier to understand..

Consider: "Which of the first 100 prime numbers have digits whose sum is also prime?" Here is one way to answer it using infix notation

Select[Table[Prime[n], {n, 100}], PrimeQ[Total[IntegerDigits[#]]] &]

I find this to be much easier to understand

Table[Prime[n], {n, 100}] // Select[IntegerDigits /* Total /* PrimeQ]

From left to right I read this as "From the first 100 primes select those whose digits when totaled is prime".

Experiment with both styles and see what works best for you.

POSTED BY: Rohit Namjoshi

We didn't get to patterns on Thrusday. We'll look at them tomorrow and then get back to the challenge.

Hi Abrita, Here's my code. It works!

PigLatin[x_] :=

 Block[{lowerVowels = {"a", "e", "i", "o", "u"}, consonants}, 
  consonants = {"B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N",
     "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z", "b", "c", "d", 
    "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", 
    "v", "w", "x", "y", "z", "A", "E", "I", "O", "U"};
  StringRiffle[Map[If[StringFreeQ[#, lowerVowels], #,
      If[StringContainsQ[StringTake[#, 1], lowerVowels],
       If[
        Total[Boole[
           StringContainsQ[Characters[#], PunctuationCharacter]]] == 0,
        # <> "way", 
        StringJoin[Drop[Characters[#], -1]] <> "way" <> 
         Last[Characters[x]]],
       If[
        Total[Boole[
           StringContainsQ[Characters[#], PunctuationCharacter]]] == 0,
        StringJoin[Reverse[TakeDrop[Characters[#],

            LengthWhile[
             Boole[StringContainsQ[Characters[#], consonants]], #1 == 
               1 &]
            ]]] <> "ay",
        StringJoin[Reverse[TakeDrop[Drop[Characters[#], -1],

            LengthWhile[
             Boole[StringContainsQ[Characters[#], consonants]], #1 == 
               1 &]
            ]]] <> "ay" <> Last[Characters[#]]]]] &, StringSplit[x]]]]

Cloud version of the code

POSTED BY: Arturo Silva

For some reason, my code didn't seem to load, here it goes again, just in case:

PigLatin[x_] :=

 Block[{lowerVowels = {"a", "e", "i", "o", "u"}, consonants}, 
  consonants = {"B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N",
     "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z", "b", "c", "d", 
    "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", 
    "v", "w", "x", "y", "z", "A", "E", "I", "O", "U"};
  StringRiffle[Map[If[StringFreeQ[#, lowerVowels], #,
      If[StringContainsQ[StringTake[#, 1], lowerVowels],
       If[
        Total[Boole[
           StringContainsQ[Characters[#], PunctuationCharacter]]] == 0,
        # <> "way", 
        StringJoin[Drop[Characters[#], -1]] <> "way" <> 
         Last[Characters[x]]],
       If[
        Total[Boole[
           StringContainsQ[Characters[#], PunctuationCharacter]]] == 0,
        StringJoin[Reverse[TakeDrop[Characters[#],

        LengthWhile[
         Boole[StringContainsQ[Characters[#], consonants]], #1 == 
           1 &]
        ]]] <> "ay",
    StringJoin[Reverse[TakeDrop[Drop[Characters[#], -1],

        LengthWhile[
         Boole[StringContainsQ[Characters[#], consonants]], #1 == 
           1 &]
        ]]] <> "ay" <> Last[Characters[#]]]]] &, StringSplit[x]]]]
POSTED BY: Arturo Silva

Hi Abrita,

To initiate the challenge, let's start with the following code:

PigLatin[text_String] := 
 Block[{vowels = {"a", "e", "i", "o", "u"}, consonants},
  consonants = Complement[Alphabet[], vowels];
  consonants = Join[consonants, ToUpperCase@consonants];
  StringReplace[text,
   {
    x__ /; 
      StringMatchQ[x, 
       Flatten[{consonants, ToUpperCase@vowels}] .. ~~ vowels ~~ 
        LetterCharacter ...] :> 
     StringTake[x, StringPosition[x, vowels][[1, 1]] ;; -1] <> 
      StringTake[x, StringPosition[x, vowels][[1, 1]] - 1] <> "ay",
    x__ /; 
      StringMatchQ[x, 
       Flatten@{consonants, ToUpperCase[Alphabet[]]} ~~ 
        LetterCharacter ...] :> x,
    x__ /; StringMatchQ[x, vowels ~~ LetterCharacter ...] :> x <> "way"
    }
   ]
  ]
Posted 6 years ago
POSTED BY: Rohit Namjoshi

Hello, For some reason, my previous posts weren't published.

Here's my proposed solution to the challenge:

PigLatin[x_] := 
 Block[{lowerVowels = {"a", "e", "i", "o", "u"}, consonants}, 
  consonants = {"B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N",
     "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z", "b", "c", "d", 
    "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", 
    "v", "w", "x", "y", "z", "A", "E", "I", "O", "U"};
  StringRiffle[
   Map[If[StringFreeQ[#, lowerVowels], #, 
      If[StringContainsQ[StringTake[#, 1], lowerVowels], 
       If[Total[
          Boole[StringContainsQ[Characters[#], 
            PunctuationCharacter]]] == 0, # <> "way", 
        StringJoin[Drop[Characters[#], -1]] <> "way" <> 
         Last[Characters[x]]], 
       If[Total[
          Boole[StringContainsQ[Characters[#], 
            PunctuationCharacter]]] == 0, 
        StringJoin[
          Reverse[TakeDrop[Characters[#], 
            LengthWhile[
             Boole[StringContainsQ[Characters[#], consonants]], #1 == 
               1 &]]]] <> "ay", 
        StringJoin[
          Reverse[TakeDrop[Drop[Characters[#], -1], 
            LengthWhile[
             Boole[StringContainsQ[Characters[#], consonants]], #1 == 
               1 &]]]] <> "ay" <> Last[Characters[#]]]]] &, 
    StringSplit[x]]]]
POSTED BY: Arturo Silva

Hi! Is it possible to watch the earlier lessons from this week? Thanks, Garfield

[WSG20] Programming Fundamentals Week 1

Attachments:
POSTED BY: Yuliia Maidannyk
Posted 6 years ago
POSTED BY: Arturo Silva
Posted 6 years ago

POSTED BY: Stuart Reilly
Posted 6 years ago
POSTED BY: Colin Scully

enter image description here

POSTED BY: Yuliia Maidannyk

A simple snippet to get you started:

Manipulate[
 ImageResize[movie["Image"], Medium], 
 {movie, EntityList[EntityClass["Movie", "StarWarsFranchise"]]}]
Posted 6 years ago
POSTED BY: Dave McCollum

Good point.

So the the entities are part of the continuously updated Wolfram Knowledgebase also used in Wolfram|Alpha. Here are some of the data sources used for the knowledgebase: https://reference.wolfram.com/language/ref/entity/Source.html

You'll find more information about each entity on its associated documentation page. For e.g. https://reference.wolfram.com/language/ref/entity/Movie.html. The curated data can be a result of pulling in information from multiple sources and the specifics of the curation process are not usually included.

Meanwhile for the datasets on the Wolfram Data Repository you will find the "Source Metadata" at the bottom of each data resource that gives you more detailed information.

Posted 6 years ago
POSTED BY: Muhammad Ali

Hi Abrita, where can I access to the weekly quizzes and videos of the session? Can you give me any page link?

POSTED BY: Regina Cervantes
Posted 6 years ago

POSTED BY: Stuart Reilly

enter image description here

POSTED BY: Arturo Silva

I wanted to upload my Notebook, but I can't because I have to log in to my Wolfram Cloud account. I am logged in another tab in my browser, but I don't know how to link that to this group, can you help me please?

POSTED BY: Arturo Silva
POSTED BY: Arturo Silva

Perfectly ok to copy paste code snippets. Notebook not needed.

Posted 6 years ago

POSTED BY: Tingting Zhao

Hi Valeriu, I had a DateListPlot in mind. But the fun of using the Wolfram Language is that there are so many ways of doing the same thing. And your chart highlights the information just as well. Thanks for sharing.

Sure, Abrita, with DateListPlot[] it's much more simpler:

DateListPlot[
 EntityClass["Movie", 
   "StarWarsFranchise"][{EntityProperty["Movie", "ReleaseDate"], 
   EntityProperty["Movie", "ProductionBudget"]}]]

enter image description here

@Sviatoslav Archava could you please email wolfram-u@wolfram.com and we will send the link to you.

You can register for study groups at https://www.wolfram.com/wolfram-u/special-event/study-groups/

For this week specifically: sign up here

Posted 6 years ago

Please send the link of the study group to regster

POSTED BY: M al

Daily Challenge (May 4): A challenge in the spirit of "May the fourth be with you".

Chart the production budget against the release date of the Star Wars movies (from the WL EntityClass "StarWarsFranchise"). Which movie had the lowest production budget?

Posted 6 years ago

"Star Wars: The Clone Wars" 8.5 million USD

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