Marcelo hace 3 años
commit
2594b5ea95
Se han modificado 4 ficheros con 498 adiciones y 0 borrados
  1. 25 0
      lesson-01/main.cpp
  2. 436 0
      lesson-01/main.py
  3. 18 0
      lesson-01/task1.py
  4. 19 0
      lesson-01/task2.py

+ 25 - 0
lesson-01/main.cpp

@@ -0,0 +1,25 @@
+#include <iostream>
+#include <bitset>
+
+using namespace std;
+
+int main()
+{
+    float a = -0.33333333333; // 1 / 4
+
+    int b = 1092830;
+
+    cout << a << endl;
+
+    float *addr = &a;
+    int *int_addr = (int *)addr;
+
+    int a_as_int = *int_addr;
+
+    cout << bitset<32>(a_as_int) << endl;
+
+    cout << 0b01111101 - 127 << endl;
+
+    // cout << bitset<32>(a) << endl;
+    // cout << bitset<32>(b) << endl;
+}

+ 436 - 0
lesson-01/main.py

@@ -0,0 +1,436 @@
+from auditorium import Show
+import math
+from auditorium.show import Context
+from markdown.core import markdown
+import numpy as np
+import matplotlib.pyplot as plt
+
+show = Show('My Show')
+
+
+@show.slide
+def presentation(ctx):
+    """
+    Welcome to Mathematical Foundation of Algorithms
+
+    Marcelo Fornet
+
+    * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
+    * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
+    """
+
+
+@show.slide
+def score_system(ctx):
+    """
+    # Score
+    - Maximum score: 180 points
+    - Maximum points to accumulate: 200
+    - Final Grade: $\\frac{min(P, 180)}{180} \cdot 100$
+    """
+
+
+@show.slide
+def number_representation(ctx: Context):
+    """
+    # Decimal system
+    """
+
+    with ctx.fragment(ctx):
+        ctx.markdown("How do we represent number in base 10?")
+
+    with ctx.fragment(ctx):
+        ctx.markdown(
+            "$$2021 = 2 \cdot 10^3 + 0 \cdot 10^2 + 2 \cdot 10^1 + 1 \cdot 10^0$$")
+
+
+def int_to_base(number, base):
+    assert base <= 36
+    digits = []
+    while number > 0:
+        dig = number % base
+
+        if base > 10:
+            dig = str(dig) if dig < 10 else chr(97 + dig - 10)
+
+        digits.append(dig)
+        number //= base
+
+    if len(digits) == 0:
+        digits.append(0)
+    return list(reversed(digits))
+
+
+@number_representation.slide
+def dynamic_base_10(ctx: Context):
+    """
+    # Decimal system
+    """
+
+    number = ctx.text_input("2021")
+    try:
+        number = int(number)
+        assert number >= 0
+
+        digits = int_to_base(number, 10)
+
+        n = len(digits)
+
+        # Uncomment if fix latex issue
+        # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
+        #                     in enumerate(reversed(digits)) if d > 0)
+        # output = f'$${output}$$'
+
+        output = ' + '.join(f"{d} * {10**(n-p-1)}" for (p, d)
+                            in enumerate(digits) if d > 0)
+
+        if output == '':
+            output = '0'
+    except Exception as e:
+        output = f"{number} is not a valid number"
+    ctx.markdown(output)
+
+
+@number_representation.slide
+def number_representation_poll_1(ctx: Context):
+    """
+    # Decimal system
+    """
+
+    ctx.markdown("Can we represent all integer numbers using decimal system?")
+
+    with ctx.fragment(ctx):
+        with ctx.columns(2) as cl:
+            with ctx.fragment('highlight-green'):
+                ctx.markdown('Yes')
+            cl.tab()
+            ctx.markdown('No')
+
+
+@number_representation.slide
+def number_representation_poll_2(ctx: Context):
+    """
+    # Decimal system
+    """
+    ctx.markdown("Can we represent every number in a unique way?")
+    with ctx.fragment(ctx):
+        with ctx.columns(2) as cl:
+            with ctx.fragment('highlight-green'):
+                ctx.markdown('Yes')
+            cl.tab()
+            ctx.markdown('No')
+
+
+@ show.slide
+def number_to_list_of_digits(ctx):
+    """
+    # Practice exercise
+
+    1. Convert a python integer to its decimal representation.
+
+    https://ideone.com/Ekmuzk
+    """
+
+
+@ show.slide
+def list_of_digits_to_number(ctx):
+    """
+    # Practice exercise
+
+    2. Convert a number in decimal representation to python integer.
+
+    https://ideone.com/CkX1rd
+    """
+
+
+@ show.slide
+def what_is_base2(ctx):
+    """
+    # Base 2
+    """
+
+    number = ctx.text_input("5")
+    try:
+        number = int(number)
+        assert number >= 0
+
+        digits = int_to_base(number, 2)
+
+        n = len(digits)
+
+        # Uncomment if fix latex issue
+        # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
+        #                     in enumerate(reversed(digits)) if d > 0)
+        # output = f'$${output}$$'
+
+        output_2 = ' + '.join(f"{2**(n-p-1)}" for (p, d)
+                              in enumerate(digits) if d > 0)
+
+        if output_2 == '':
+            output_2 = '0'
+
+        output_1 = bin(number)
+    except Exception as e:
+        output_1 = f"{number} is not a valid number"
+        output_2 = ""
+
+    ctx.markdown(output_1)
+    ctx.markdown(output_2)
+
+
+@show.slide
+def what_is_base_b(ctx):
+    """
+    # Base b
+
+    $$v = \sum_i d_i \cdot b^i$$
+    """
+
+
+@what_is_base_b.slide
+def what_is_base_b_2(ctx):
+    """
+    # Base b
+    """
+    number = 3**10
+    base = ctx.text_input("10")
+    try:
+        base = int(base)
+        assert 2 <= base <= 36
+        digits = int_to_base(number, base)
+        output = ''.join(str(x) for x in digits)
+    except Exception as e:
+        print(e)
+        output = str(e)
+    ctx.markdown(str(number))
+    ctx.markdown(output)
+
+
+@ show.slide
+def fibonacci_base(ctx: Context):
+    """
+    # Fibonacci base
+    """
+
+
+@fibonacci_base.slide
+def fibonacci_sequence(ctx: Context):
+    """
+    # Fibonacci sequence
+    """
+    with ctx.fragment(ctx):
+        ctx.markdown("$$F_1 = 1$$")
+        ctx.markdown("$$F_2 = 2$$")
+        ctx.markdown("$$F_n = F_{n-1} + F_{n-2}$$")
+
+    L = [1, 2]
+
+    while len(L) < 10:
+        L.append(L[-1] + L[-2])
+
+    with ctx.fragment(ctx):
+        ctx.markdown(', '.join(map(str, L)))
+
+
+@ fibonacci_base.slide
+def fibonacci_base_description(ctx: Context):
+    """
+    # Fibonacci base
+
+    $$v = \sum_{1 <= i} d_i \cdot F_i $$
+
+    $$d_i \in (0, 1)$$
+    """
+
+
+@ fibonacci_base.slide
+def fibonacci_base_example(ctx: Context):
+    """
+    # Fibonacci base
+    """
+    value = str(ctx.text_input("1100101"))
+    n = len(value)
+    L = [1, 2]
+    while len(L) < n:
+        L.append(L[-1] + L[-2])
+
+    output_number = 0
+    output_sum = ""
+
+    R = []
+
+    for i, d in enumerate(value):
+        if d == '1':
+            output_number += L[n - i - 1]
+            R.append(L[n - i - 1])
+
+    output_sum = ' + '.join(map(str, R))
+
+    ctx.markdown(str(output_number))
+    ctx.markdown(output_sum)
+
+
+@fibonacci_base.slide
+def fibonacci_base_poll_1(ctx: Context):
+    """
+    # Fibonacci base
+    """
+
+    ctx.markdown(
+        "Can we represent all integer numbers using fibonacci base?")
+
+    with ctx.fragment(ctx):
+        with ctx.columns(2) as cl:
+            with ctx.fragment('highlight-green'):
+                ctx.markdown('Yes')
+            cl.tab()
+            ctx.markdown('No')
+
+
+@fibonacci_base.slide
+def fibonacci_base_poll_2(ctx: Context):
+    """
+    # Fibonacci base
+    """
+    ctx.markdown("Can we represent every number in a unique way?")
+    with ctx.fragment(ctx):
+        with ctx.columns(2) as cl:
+            ctx.markdown('Yes')
+            cl.tab()
+            with ctx.fragment('highlight-red'):
+                ctx.markdown('No')
+
+# Make a break
+
+
+@ show.slide
+def computer_representation(ctx: Context):
+    """
+    ### Representation in the computer
+    """
+
+    with ctx.fragment(ctx):
+        ctx.markdown("Binary")
+
+    with ctx.fragment(ctx):
+        ctx.markdown("Bytes")
+
+    with ctx.fragment(ctx):
+        ctx.markdown("C++ Demo")
+
+
+@ show.slide
+def base_16_64_application(ctx):
+    """
+    ### Application of higher base
+
+    * Hexadecimal
+    * Base64
+    """
+
+    with ctx.fragment(ctx):
+        ctx.markdown("Demo")
+
+
+@ show.slide
+def decimal_number_in_base_10(ctx):
+    """
+    Decimal number
+
+    $$\sum_{-\infty \lt i \lt \infty} d_i \cdot 10^{i}$$
+    """
+
+
+@ show.slide
+def decimal_number_in_computer(ctx):
+    """
+    Single precision floating point format
+    """
+
+    with ctx.fragment(ctx):
+        ctx.markdown("Wikipedia")
+
+    with ctx.fragment(ctx):
+        ctx.markdown("C++ Demo")
+
+    # Some precision issues with catastrophic cancelation (substraction)
+    # Compare numbers directly with each other
+    # a / b == c (not always the same)
+
+    # Case where the two numbers have same representation
+    # but a == b (is false) This is with nan
+    # and different representation but a == b (is true) this is 0 and -0
+
+
+@ show.slide
+def homework(ctx):
+    """
+    Homework
+    """
+
+
+@homework.slide
+def task_1(ctx):
+    """
+    `*`
+
+    Code that convert from any base to any other base
+
+    Hint: Try converting to python number first
+    """
+
+
+@homework.slide
+def task_2(ctx):
+    """
+    `*`
+
+    Code that convert from base 2 to base 16 without converting to python number
+    """
+
+
+@homework.slide
+def task_3(ctx):
+    """
+    `***`
+
+    On fibonacci base, if we forbid consecutive 1, prove that every number is represented uniquely
+
+    [Share LaTeX example]
+    """
+    # > Proof that fibonacci base without non-consecutive 1 is unique
+    #   > Hint 1: Try using induction
+    #   > Hint 2: Assume all numbers less than F_n
+
+
+@homework.slide
+def task_4(ctx):
+    """
+    `**`
+
+    Make a program that converts from a number to fibonacci base
+    """
+
+
+@homework.slide
+def task_5(ctx):
+    """
+    `***`
+
+    There are $n$ books. Each of them can be painted in one of $k$ different colors.
+
+    1. How many different ways can the books be painted.
+    2. Make a program that prints all different possible ways.
+    """
+
+
+@ show.slide
+def bye(ctx):
+    """
+    Mathematical Foundation of Algorithms
+
+    Marcelo Fornet
+
+    * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
+    * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
+    """

+ 18 - 0
lesson-01/task1.py

@@ -0,0 +1,18 @@
+def convert_base(number):
+    """
+    Given a number represented as a list of digits in a base,
+    convert it to the same number written as a list of digits in the other base.
+
+    >>> convert_base([5, 9, 5, 7], 10, 6)
+    [4, 3, 3, 2, 5]
+
+    >>> convert_base([0, 0, 3, 2, 3, 3], 5, 20)
+    [1, 2, 3]
+
+    >>> convert_base([1, 2, 1, 0], 3, 17)
+    [2, 14]
+
+    >>> convert_base([1, 2, 1, 6], 7, 5)
+    [3, 3, 0, 4]
+    """
+    # Your code here...

+ 19 - 0
lesson-01/task2.py

@@ -0,0 +1,19 @@
+def convert_base_2_to_16(number):
+    """
+    Given a number represented as base 2, convert it to base 16.
+    To solve this task properly, you should convert directly from
+    one base to the other without computing the whole number.
+
+    >>> convert_base_2_to_16([0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0])
+    [3, 0, 0, 14]
+
+    >>> convert_base_2_to_16([0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0])
+    [6, 0, 8, 6]
+
+    >>> convert_base_2_to_16([0, 0, 1])
+    [1]
+
+    >>> convert_base_2_to_16([1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1])
+    [1, 6, 10, 1]
+    """
+    # Your code here...