-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kristian Rother
committed
Jan 3, 2024
1 parent
7b8a63d
commit 04dd796
Showing
24 changed files
with
382 additions
and
390 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
Backpack Problem | ||
================ | ||
|
||
**🎯 Optimize the value of a heist.** | ||
|
||
.. figure:: burglar.png | ||
:alt: Burglar | ||
|
||
|
||
A burglar broke into a villa. There he finds so many valuables that he | ||
can’t put them all in his backpack. Write a program that makes an | ||
optimal selection. | ||
|
||
The burglar is an experienced professional who can estimate the market | ||
value and size of each item in no time: | ||
|
||
================ ==== ====== | ||
item size value | ||
================ ==== ====== | ||
laptop 2 600,- | ||
cutlery 2 400,- | ||
spotify speakers 3 300,- | ||
jewels 2 1100,- | ||
vase 5 700,- | ||
camera 2 500,- | ||
painting 4 900,- | ||
cash 1 800,- | ||
================ ==== ====== | ||
|
||
The backpack has a capacity of ``8``. | ||
|
||
When your program manages to pack items worth ``3000``, it can be used | ||
as an app for amateur burglars. | ||
|
||
Hints | ||
----- | ||
|
||
- the optimal solution uses **dynamic programming**. | ||
|
||
Use the following pseudocode: | ||
|
||
1. create an empty list that will include the best combination(s) of | ||
items for each backpack size | ||
2. insert an empty combination for a size 0 backpack | ||
3. start with a size 1 backpack | ||
4. copy the best combination for the current size from the previous | ||
size, store it as ``current best`` | ||
5. go through all objects | ||
6. create a new combination usign an item plus the best combination for | ||
the space remaining | ||
7. if the combination is more valuable than the ``current best``, | ||
replace ``current best`` by the new combination | ||
8. if the combination is worth the same amount, save both | ||
9. increase the size of the backpack by 1 | ||
10. repeat step 4 until you reach the desired size | ||
11. print the best combination for the desired size | ||
|
||
*Translated with* `www.DeepL.com <https://www.DeepL.com/Translator>`__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
Blockchain | ||
========== | ||
|
||
**🎯 Implement your own blockchain algorithm.** | ||
|
||
Step 1 | ||
------ | ||
|
||
Write a function that generates random transactions in the format | ||
``(name1, name2, amount)``. | ||
|
||
We want to save these transactions *forgery-proof*, so that they are as | ||
difficult to manipulate as possible afterwards. | ||
|
||
Step 2 | ||
------ | ||
|
||
Define a data type *"Block"* that contains the following: | ||
|
||
- The hash of a previous block | ||
- Some transactions | ||
- A checksum (any number or string) | ||
|
||
Step 3 | ||
------ | ||
|
||
Write a function that calculates a hash from all properties of a block. | ||
To do this, represent the entire block as a string. Use the hash | ||
function ``sha256``: | ||
|
||
:: | ||
|
||
import hashlib | ||
|
||
h = hashlib.sha256() | ||
h.update(text.encode()) | ||
print(h.hexdigest()) | ||
|
||
Step 4 | ||
------ | ||
|
||
Create the blockchain as an empty list. | ||
|
||
Create the first block, the "Genesis block". Use ‘genesis’ as previous | ||
hash. Place some random transactions in the block. | ||
|
||
Find a checksum so that the *sha256-hexdigest* ends with four zeros | ||
(``0000``). You may need to try many checksums. | ||
|
||
Add the finished block to the blockchain. | ||
|
||
Step 5 | ||
------ | ||
|
||
Create the second block: | ||
|
||
- The hash is the ``hexdigest`` of the previous block | ||
- Add more transactions. | ||
- Again find a checksum that generates a ``hexdigest`` with four zeros | ||
at the end. | ||
- Add the finished block to the blockchain. | ||
|
||
Step 6 | ||
------ | ||
|
||
Generate more blocks. | ||
|
||
Questions | ||
--------- | ||
|
||
- What happens if the number of necessary zeros in the hex digest is | ||
set to 2 or 6? | ||
- What happens if someone changes a transaction in the Genesis block? | ||
- What makes the blockchain forgery-proof? | ||
- How could a blockchain still be forged? | ||
- Why is finding the checksum also called *"proof of work"*? | ||
- Why are several computers involved in a blockchain? | ||
- What is a *"consensus algorithm"*? | ||
|
||
*Translated with* `www.DeepL.com <https://www.DeepL.com/Translator>`__ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Magic Square | ||
============ | ||
|
||
**🎯 Solve a magic square.** | ||
|
||
- create a magic square with 3 \* 3 fields. | ||
- fill the square with the numbers from 1-9 | ||
- the sum of the numbers in each row, column and diagonal shall be 15 | ||
- use each number only once | ||
- print the finished square | ||
|
||
Hints | ||
----- | ||
|
||
- Write a function that calculates all sums | ||
- A *brute-force* approach is to try out all permutations\* | ||
- See the ``itertools`` module | ||
|
||
Extra Challenge | ||
--------------- | ||
|
||
Fill a magic square with 4 \* 4 fields with the numbers 1-16 (sum 34). | ||
|
||
If you don’t want to try all the possibilities (9!), you can describe | ||
the magic square as a linear system of equations. The Python package | ||
**PuLP** allows you to express the necessary equations in a very compact | ||
way. | ||
|
||
*Translated with* `www.DeepL.com <https://www.DeepL.com/Translator>`__ |
Oops, something went wrong.