Difference: SolutionsToREPLAndStandardPythonProgramming (3 vs. 4)

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"
 
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