Difference: SolutionsToREPLAndStandardPythonProgramming (1 vs. 7)

Revision 72020-05-29 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Solutions"

Solutions to Exercise 1: REPL and standard Python programming

Exercise 1: Use of REPL

Line: 10 to 10
  In the first example we use fixed values for the calculations. To print out the results I first use integer (%d) format and then floating point format (%f) to get the division right.
Changed:
<
<
https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/assignments.py.txt
>
>
https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV1.py.txt
  In the second example I define 4 functions for the 4 basic arithmetic operations. The dictionary with the operator as key and the function as value allows me to select the function to be called depending on the operator given. I write a simple parser to check that user input is correct. Since I force a space between operands and operator it becomes easy to split the calculation into 3 tokens: 2 operands and the operator. Now all I need to do is converting the operator strings to floating values and call the corresponding function.
Changed:
<
<
https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculator.py.txt
>
>
https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV2.py.txt
  The last example is quite a bit more tricky as I cannot use the string method split to separate operands and operator any more. The parser allows the characters 0..9,"." and "+","-","*","/".

The operator may only consist of 1 or more digits and/or the decimal point. It may not contain more than 1 decimal point and it must contain at least 1 digit. The operator can be only one of the above basic arithmetic operators. Here is my solution. Test it yourself and try to find an illegal character combination that is not treated. Error handling is often the most difficult part of a program!

Changed:
<
<
https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV1.py.txt
>
>
https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV3.py.txt
 

Exercise 3: Conditions

There is nothing much new in this exercise except that it shows how to use conditional statements.

Line: 118 to 118
 
META FILEATTACHMENT attachment="mathSeries.py.txt" attr="" comment="" date="1590670253" name="mathSeries.py.txt" path="mathSeries.py.txt" size="3484" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesPlot.py.txt" attr="" comment="" date="1590670253" name="mathSeriesPlot.py.txt" path="mathSeriesPlot.py.txt" size="3233" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesTest.py.txt" attr="" comment="" date="1590670253" name="mathSeriesTest.py.txt" path="mathSeriesTest.py.txt" size="2920" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="calculatorV3.py.txt" attr="" comment="" date="1590768475" name="calculatorV3.py.txt" path="calculatorV3.py.txt" size="1206" user="UliRaich" version="1"

Revision 62020-05-28 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Solutions"

Solutions to Exercise 1: REPL and standard Python programming

Exercise 1: Use of REPL

Line: 108 to 108
 
META FILEATTACHMENT attachment="calculator.py.txt" attr="" comment="" date="1588683778" name="calculator.py.txt" path="calculator.py.txt" size="1650" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1588684838" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="3255" user="UliRaich" version="2"
META FILEATTACHMENT attachment="math.py.txt" attr="" comment="" date="1588701596" name="math.py.txt" path="math.py.txt" size="939" user="UliRaich" version="1"
Deleted:
<
<
META FILEATTACHMENT attachment="mathSeries.py.txt" attr="" comment="" date="1588691307" name="mathSeries.py.txt" path="mathSeries.py.txt" size="2784" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesPlot.py.txt" attr="" comment="" date="1588691307" name="mathSeriesPlot.py.txt" path="mathSeriesPlot.py.txt" size="3209" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesTest.py.txt" attr="" comment="" date="1588691307" name="mathSeriesTest.py.txt" path="mathSeriesTest.py.txt" size="2114" user="UliRaich" version="1"
 
META FILEATTACHMENT attachment="memoryAllocError.png" attr="" comment="" date="1588691385" name="memoryAllocError.png" path="memoryAllocError.png" size="13651" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditionsV1.py.txt" attr="" comment="" date="1588701596" name="conditionsV1.py.txt" path="conditionsV1.py.txt" size="417" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditionsV2.py.txt" attr="" comment="" date="1588701596" name="conditionsV2.py.txt" path="conditionsV2.py.txt" size="1218" user="UliRaich" version="1"
Line: 118 to 115
 
META FILEATTACHMENT attachment="fibonacciV2.py.txt" attr="" comment="" date="1588702187" name="fibonacciV2.py.txt" path="fibonacciV2.py.txt" size="799" user="UliRaich" version="1"
META FILEATTACHMENT attachment="fibonacciV1.py.txt" attr="" comment="" date="1588702187" name="fibonacciV1.py.txt" path="fibonacciV1.py.txt" size="1079" user="UliRaich" version="1"
META FILEATTACHMENT attachment="repl-1.png" attr="" comment="" date="1590660690" name="repl-1.png" path="repl-1.png" size="47351" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="mathSeries.py.txt" attr="" comment="" date="1590670253" name="mathSeries.py.txt" path="mathSeries.py.txt" size="3484" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesPlot.py.txt" attr="" comment="" date="1590670253" name="mathSeriesPlot.py.txt" path="mathSeriesPlot.py.txt" size="3233" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesTest.py.txt" attr="" comment="" date="1590670253" name="mathSeriesTest.py.txt" path="mathSeriesTest.py.txt" size="2920" user="UliRaich" version="1"

Revision 52020-05-28 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Solutions"

Solutions to Exercise 1: REPL and standard Python programming

Exercise 1: Use of REPL

The screen shot shows the REPL session

Changed:
<
<
repl.png
>
>
repl-1.png
 

Exercise 2: A simple calculator

In the first example we use fixed values for the calculations. To print out the results I first use integer (%d) format and then floating point format (%f) to get the division right.

Line: 104 to 104
 
META FILEATTACHMENT attachment="geoHarm.png" attr="" comment="" date="1588683360" name="geoHarm.png" path="geoHarm.png" size="29460" user="UliRaich" version="1"
META FILEATTACHMENT attachment="factFibonacci.png" attr="" comment="" date="1588683360" name="factFibonacci.png" path="factFibonacci.png" size="28907" user="UliRaich" version="1"
META FILEATTACHMENT attachment="approx.png" attr="" comment="" date="1588683360" name="approx.png" path="approx.png" size="48799" user="UliRaich" version="1"
Deleted:
<
<
META FILEATTACHMENT attachment="repl.png" attr="" comment="" date="1588683360" name="repl.png" path="repl.png" size="41102" user="UliRaich" version="1"
 
META FILEATTACHMENT attachment="assigments.py.txt" attr="" comment="" date="1588683778" name="assigments.py.txt" path="assigments.py.txt" size="438" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculator.py.txt" attr="" comment="" date="1588683778" name="calculator.py.txt" path="calculator.py.txt" size="1650" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1588684838" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="3255" user="UliRaich" version="2"
Line: 118 to 117
 
META FILEATTACHMENT attachment="conditionsV3.py.txt" attr="" comment="" date="1588701596" name="conditionsV3.py.txt" path="conditionsV3.py.txt" size="1507" user="UliRaich" version="1"
META FILEATTACHMENT attachment="fibonacciV2.py.txt" attr="" comment="" date="1588702187" name="fibonacciV2.py.txt" path="fibonacciV2.py.txt" size="799" user="UliRaich" version="1"
META FILEATTACHMENT attachment="fibonacciV1.py.txt" attr="" comment="" date="1588702187" name="fibonacciV1.py.txt" path="fibonacciV1.py.txt" size="1079" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="repl-1.png" attr="" comment="" date="1590660690" name="repl-1.png" path="repl-1.png" size="47351" user="UliRaich" version="1"

Revision 42020-05-05 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Solutions"

Solutions to Exercise 1: REPL and standard Python programming

Exercise 1: Use of REPL

Line: 24 to 24
 

Exercise 3: Conditions

There is nothing much new in this exercise except that it shows how to use conditional statements.

Added:
>
>
The first version uses the fixed values 5 and 7:

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/conditionsV1.py.txt

The second version takes user input, splits it along spaces and makes sure that exactly 2 tokens have been given

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/conditionsV2.py.txt

The third version also checks that the 2 tokens are valid integer numbers

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/conditionsV3.py.txt

 

Exercise 4: The Fibonacci Series

Changed:
<
<
In this exercise we try to implement a mathematical function. This should allow you to write your own small algorithm and test it. The function you develop here will be used in exercise 6.

Exercise 5: Some maths: Calculate the sine function

>
>
In this exercise we try to implement a mathematical function. This should allow you to write your own small algorithm and test it. The function you develop here will be used in exercise 6. Again there are 2 versions: The first one calculates the Fibonacci series F(n) where n is given by the user:

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/fibonacciV1.py.txt

while in the second version the user gives a maximum number not to be exceeded.

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/fibonaciiV2.py.txt

Exercise 5: A bit of Mathematics: Calculate the sine function

 
Changed:
<
<
Here we implement the calculation of the sine function we used with REPL in the first exercise. It shows how use external modules/libraries.
>
>
Here we implement the calculation of the sine function we used with REPL in the first exercise. It shows how use external modules/libraries. This exercise is pretty simple.

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/math.py.txt

 

Exercise 6: Classes

This exercise is a bit bigger! It shows you how to develop your own modules and your own classes. In addition you may study some interesting mathematical number series. The last 4 series are actually approximations to well known mathematical constants:

Line: 37 to 57
 
  • the Gregory-Leibniz algorithm, an approximation of pi
  • The Wallis algorithm, another approximation of pi
  • approximation of ln(2)
Changed:
<
<
When trying to get a good approximation for these numbers you will have to calculate a big number of iterations and you may see a difference between running the program on the PC and running it on the ESP32 where you may see this:
>
>
When trying to get a good approximation for these numbers you will have to calculate a big number of iterations and you may see a difference between running the program on the PC and running it on the ESP32 where you may see this:3.14159 26535 89793 23846
  memoryAllocError.png

The reason is simple: My PC has 16 GBytes of RAM memory while the ESP32 has 520 kBytes of RAM. When calculating a big number of iterations you will not be able to keep the intermediate results in a list.

Added:
>
>
For comparison, here are the correct numbers for pi, e and ln(2)
  • pi: 3.14159 26535 89793 23846
  • e: 2.71828 18284 59045 23536
  • ln(2): 0.69314 71805 59945 30941
 The other difference is that the ESP32 has no high resolution screen attached to it such that plotting does not make much sense. (TFT screens do exist and can be attached to the ESP32 such that this statement must be taken with a bit of caution)

Here is a plot of the factorial series and the Fibonacci numbers. Since factorial uses multiplication while Fibonacci uses additions, factorial increases much faster.

Line: 57 to 81
  approx.png
Added:
>
>
... and here is the code:

First we have the class implementing the calculation of the number series:

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/mathSeries.py.txt

Then the test program, calling each if the methods in the class:

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/mathSeriesTest.py.txt

and finally the program which generates the plots:

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/mathSeriesPlot.py.txt

 -- Uli Raich - 2020-05-03

Comments

Line: 70 to 108
 
META FILEATTACHMENT attachment="assigments.py.txt" attr="" comment="" date="1588683778" name="assigments.py.txt" path="assigments.py.txt" size="438" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculator.py.txt" attr="" comment="" date="1588683778" name="calculator.py.txt" path="calculator.py.txt" size="1650" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1588684838" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="3255" user="UliRaich" version="2"
Changed:
<
<
META FILEATTACHMENT attachment="math.py.txt" attr="" comment="" date="1588691307" name="math.py.txt" path="math.py.txt" size="939" user="UliRaich" version="1"
>
>
META FILEATTACHMENT attachment="math.py.txt" attr="" comment="" date="1588701596" name="math.py.txt" path="math.py.txt" size="939" user="UliRaich" version="1"
 
META FILEATTACHMENT attachment="mathSeries.py.txt" attr="" comment="" date="1588691307" name="mathSeries.py.txt" path="mathSeries.py.txt" size="2784" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesPlot.py.txt" attr="" comment="" date="1588691307" name="mathSeriesPlot.py.txt" path="mathSeriesPlot.py.txt" size="3209" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesTest.py.txt" attr="" comment="" date="1588691307" name="mathSeriesTest.py.txt" path="mathSeriesTest.py.txt" size="2114" user="UliRaich" version="1"
META FILEATTACHMENT attachment="memoryAllocError.png" attr="" comment="" date="1588691385" name="memoryAllocError.png" path="memoryAllocError.png" size="13651" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="conditionsV1.py.txt" attr="" comment="" date="1588701596" name="conditionsV1.py.txt" path="conditionsV1.py.txt" size="417" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditionsV2.py.txt" attr="" comment="" date="1588701596" name="conditionsV2.py.txt" path="conditionsV2.py.txt" size="1218" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditionsV3.py.txt" attr="" comment="" date="1588701596" name="conditionsV3.py.txt" path="conditionsV3.py.txt" size="1507" user="UliRaich" version="1"
META FILEATTACHMENT attachment="fibonacciV2.py.txt" attr="" comment="" date="1588702187" name="fibonacciV2.py.txt" path="fibonacciV2.py.txt" size="799" user="UliRaich" version="1"
META FILEATTACHMENT attachment="fibonacciV1.py.txt" attr="" comment="" date="1588702187" name="fibonacciV1.py.txt" path="fibonacciV1.py.txt" size="1079" user="UliRaich" version="1"

Revision 32020-05-05 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Solutions"

Solutions to Exercise 1: REPL and standard Python programming

Exercise 1: Use of REPL

Line: 21 to 21
 The operator may only consist of 1 or more digits and/or the decimal point. It may not contain more than 1 decimal point and it must contain at least 1 digit. The operator can be only one of the above basic arithmetic operators. Here is my solution. Test it yourself and try to find an illegal character combination that is not treated. Error handling is often the most difficult part of a program!

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV1.py.txt

Added:
>
>

Exercise 3: Conditions

There is nothing much new in this exercise except that it shows how to use conditional statements.

Exercise 4: The Fibonacci Series

In this exercise we try to implement a mathematical function. This should allow you to write your own small algorithm and test it. The function you develop here will be used in exercise 6.

Exercise 5: Some maths: Calculate the sine function

Here we implement the calculation of the sine function we used with REPL in the first exercise. It shows how use external modules/libraries.

Exercise 6: Classes

This exercise is a bit bigger! It shows you how to develop your own modules and your own classes. In addition you may study some interesting mathematical number series. The last 4 series are actually approximations to well known mathematical constants:

  • approximation of Euler's number e:
  • the Gregory-Leibniz algorithm, an approximation of pi
  • The Wallis algorithm, another approximation of pi
  • approximation of ln(2)
When trying to get a good approximation for these numbers you will have to calculate a big number of iterations and you may see a difference between running the program on the PC and running it on the ESP32 where you may see this:

memoryAllocError.png

The reason is simple: My PC has 16 GBytes of RAM memory while the ESP32 has 520 kBytes of RAM. When calculating a big number of iterations you will not be able to keep the intermediate results in a list.

The other difference is that the ESP32 has no high resolution screen attached to it such that plotting does not make much sense. (TFT screens do exist and can be attached to the ESP32 such that this statement must be taken with a bit of caution)

Here is a plot of the factorial series and the Fibonacci numbers. Since factorial uses multiplication while Fibonacci uses additions, factorial increases much faster.

factFibonacci.png

Here a comparison of the geometric and the harmonic number series. As you can easily see geometric series (here with base 2) converges while the harmonic series diverges (albeit slowly).

geoHarm.png

Finally the plot for the approximation of the mathematical constants. You can easily see that the Wallis algorithm converges faster that the Gregory-Leibniz one.

approx.png

  -- Uli Raich - 2020-05-03
Line: 35 to 70
 
META FILEATTACHMENT attachment="assigments.py.txt" attr="" comment="" date="1588683778" name="assigments.py.txt" path="assigments.py.txt" size="438" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculator.py.txt" attr="" comment="" date="1588683778" name="calculator.py.txt" path="calculator.py.txt" size="1650" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1588684838" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="3255" user="UliRaich" version="2"
Added:
>
>
META FILEATTACHMENT attachment="math.py.txt" attr="" comment="" date="1588691307" name="math.py.txt" path="math.py.txt" size="939" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeries.py.txt" attr="" comment="" date="1588691307" name="mathSeries.py.txt" path="mathSeries.py.txt" size="2784" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesPlot.py.txt" attr="" comment="" date="1588691307" name="mathSeriesPlot.py.txt" path="mathSeriesPlot.py.txt" size="3209" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesTest.py.txt" attr="" comment="" date="1588691307" name="mathSeriesTest.py.txt" path="mathSeriesTest.py.txt" size="2114" user="UliRaich" version="1"
META FILEATTACHMENT attachment="memoryAllocError.png" attr="" comment="" date="1588691385" name="memoryAllocError.png" path="memoryAllocError.png" size="13651" user="UliRaich" version="1"

Revision 22020-05-05 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Solutions"

Solutions to Exercise 1: REPL and standard Python programming

Added:
>
>

Exercise 1: Use of REPL

 
Changed:
<
<
Article text.
>
>
The screen shot shows the REPL session

repl.png

Exercise 2: A simple calculator

In the first example we use fixed values for the calculations. To print out the results I first use integer (%d) format and then floating point format (%f) to get the division right.

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/assignments.py.txt

In the second example I define 4 functions for the 4 basic arithmetic operations. The dictionary with the operator as key and the function as value allows me to select the function to be called depending on the operator given. I write a simple parser to check that user input is correct. Since I force a space between operands and operator it becomes easy to split the calculation into 3 tokens: 2 operands and the operator. Now all I need to do is converting the operator strings to floating values and call the corresponding function.

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculator.py.txt

The last example is quite a bit more tricky as I cannot use the string method split to separate operands and operator any more. The parser allows the characters 0..9,"." and "+","-","*","/".

The operator may only consist of 1 or more digits and/or the decimal point. It may not contain more than 1 decimal point and it must contain at least 1 digit. The operator can be only one of the above basic arithmetic operators. Here is my solution. Test it yourself and try to find an illegal character combination that is not treated. Error handling is often the most difficult part of a program!

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV1.py.txt

  -- Uli Raich - 2020-05-03

Comments

<--/commentPlugin-->
\ No newline at end of file
Added:
>
>
META FILEATTACHMENT attachment="geoHarm.png" attr="" comment="" date="1588683360" name="geoHarm.png" path="geoHarm.png" size="29460" user="UliRaich" version="1"
META FILEATTACHMENT attachment="factFibonacci.png" attr="" comment="" date="1588683360" name="factFibonacci.png" path="factFibonacci.png" size="28907" user="UliRaich" version="1"
META FILEATTACHMENT attachment="approx.png" attr="" comment="" date="1588683360" name="approx.png" path="approx.png" size="48799" user="UliRaich" version="1"
META FILEATTACHMENT attachment="repl.png" attr="" comment="" date="1588683360" name="repl.png" path="repl.png" size="41102" user="UliRaich" version="1"
META FILEATTACHMENT attachment="assigments.py.txt" attr="" comment="" date="1588683778" name="assigments.py.txt" path="assigments.py.txt" size="438" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculator.py.txt" attr="" comment="" date="1588683778" name="calculator.py.txt" path="calculator.py.txt" size="1650" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1588684838" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="3255" user="UliRaich" version="2"

Revision 12020-05-03 - UliRaich

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="Solutions"

Solutions to Exercise 1: REPL and standard Python programming

Article text.

-- Uli Raich - 2020-05-03

Comments

<--/commentPlugin-->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback