main.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. from auditorium import Show
  2. import math
  3. from auditorium.show import Context
  4. from markdown.core import markdown
  5. show = Show('My Show')
  6. @show.slide
  7. def presentation(ctx):
  8. """
  9. Welcome to Mathematical Foundation of Algorithms
  10. Marcelo Fornet
  11. * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
  12. * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
  13. """
  14. @show.slide
  15. def score_system(ctx):
  16. """
  17. # Score
  18. - Maximum score: 180 points
  19. - Maximum points to accumulate: 200
  20. - Final Grade: $\\frac{min(P, 180)}{180} \cdot 100$
  21. """
  22. @show.slide
  23. def number_representation(ctx: Context):
  24. """
  25. # Decimal system
  26. """
  27. with ctx.fragment(ctx):
  28. ctx.markdown("How do we represent number in base 10?")
  29. with ctx.fragment(ctx):
  30. ctx.markdown(
  31. "$$2021 = 2 \cdot 10^3 + 0 \cdot 10^2 + 2 \cdot 10^1 + 1 \cdot 10^0$$")
  32. def int_to_base(number, base):
  33. assert base <= 36
  34. digits = []
  35. while number > 0:
  36. dig = number % base
  37. if base > 10:
  38. dig = str(dig) if dig < 10 else chr(97 + dig - 10)
  39. digits.append(dig)
  40. number //= base
  41. if len(digits) == 0:
  42. digits.append(0)
  43. return list(reversed(digits))
  44. @number_representation.slide
  45. def dynamic_base_10(ctx: Context):
  46. """
  47. # Decimal system
  48. """
  49. number = ctx.text_input("2021")
  50. try:
  51. number = int(number)
  52. assert number >= 0
  53. digits = int_to_base(number, 10)
  54. n = len(digits)
  55. # Uncomment if fix latex issue
  56. # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
  57. # in enumerate(reversed(digits)) if d > 0)
  58. # output = f'$${output}$$'
  59. output = ' + '.join(f"{d} * {10**(n-p-1)}" for (p, d)
  60. in enumerate(digits) if d > 0)
  61. if output == '':
  62. output = '0'
  63. except Exception as e:
  64. output = f"{number} is not a valid number"
  65. ctx.markdown(output)
  66. @number_representation.slide
  67. def number_representation_poll_1(ctx: Context):
  68. """
  69. # Decimal system
  70. """
  71. ctx.markdown("Can we represent all integer numbers using decimal system?")
  72. with ctx.fragment(ctx):
  73. with ctx.columns(2) as cl:
  74. with ctx.fragment('highlight-green'):
  75. ctx.markdown('Yes')
  76. cl.tab()
  77. ctx.markdown('No')
  78. @number_representation.slide
  79. def number_representation_poll_2(ctx: Context):
  80. """
  81. # Decimal system
  82. """
  83. ctx.markdown("Can we represent every number in a unique way?")
  84. with ctx.fragment(ctx):
  85. with ctx.columns(2) as cl:
  86. with ctx.fragment('highlight-green'):
  87. ctx.markdown('Yes')
  88. cl.tab()
  89. ctx.markdown('No')
  90. @ show.slide
  91. def number_to_list_of_digits(ctx):
  92. """
  93. # Practice exercise
  94. 1. Convert a python integer to its decimal representation.
  95. https://ideone.com/Ekmuzk
  96. """
  97. @ show.slide
  98. def list_of_digits_to_number(ctx):
  99. """
  100. # Practice exercise
  101. 2. Convert a number in decimal representation to python integer.
  102. https://ideone.com/CkX1rd
  103. """
  104. @ show.slide
  105. def what_is_base2(ctx):
  106. """
  107. # Base 2
  108. """
  109. number = ctx.text_input("5")
  110. try:
  111. number = int(number)
  112. assert number >= 0
  113. digits = int_to_base(number, 2)
  114. n = len(digits)
  115. # Uncomment if fix latex issue
  116. # output = ' + '.join(f"{d} \cdot 10^{n-p-1}" for (p, d)
  117. # in enumerate(reversed(digits)) if d > 0)
  118. # output = f'$${output}$$'
  119. output_2 = ' + '.join(f"{2**(n-p-1)}" for (p, d)
  120. in enumerate(digits) if d > 0)
  121. if output_2 == '':
  122. output_2 = '0'
  123. output_1 = bin(number)
  124. except Exception as e:
  125. output_1 = f"{number} is not a valid number"
  126. output_2 = ""
  127. ctx.markdown(output_1)
  128. ctx.markdown(output_2)
  129. @show.slide
  130. def what_is_base_b(ctx):
  131. """
  132. # Base b
  133. $$v = \sum_i d_i \cdot b^i$$
  134. """
  135. @what_is_base_b.slide
  136. def what_is_base_b_2(ctx):
  137. """
  138. # Base b
  139. """
  140. number = 3**10
  141. base = ctx.text_input("10")
  142. try:
  143. base = int(base)
  144. assert 2 <= base <= 36
  145. digits = int_to_base(number, base)
  146. output = ''.join(str(x) for x in digits)
  147. except Exception as e:
  148. print(e)
  149. output = str(e)
  150. ctx.markdown(str(number))
  151. ctx.markdown(output)
  152. @ show.slide
  153. def fibonacci_base(ctx: Context):
  154. """
  155. # Fibonacci base
  156. """
  157. @fibonacci_base.slide
  158. def fibonacci_sequence(ctx: Context):
  159. """
  160. # Fibonacci sequence
  161. """
  162. with ctx.fragment(ctx):
  163. ctx.markdown("$$F_1 = 1$$")
  164. ctx.markdown("$$F_2 = 2$$")
  165. ctx.markdown("$$F_n = F_{n-1} + F_{n-2}$$")
  166. L = [1, 2]
  167. while len(L) < 10:
  168. L.append(L[-1] + L[-2])
  169. with ctx.fragment(ctx):
  170. ctx.markdown(', '.join(map(str, L)))
  171. @ fibonacci_base.slide
  172. def fibonacci_base_description(ctx: Context):
  173. """
  174. # Fibonacci base
  175. $$v = \sum_{1 <= i} d_i \cdot F_i $$
  176. $$d_i \in (0, 1)$$
  177. """
  178. @ fibonacci_base.slide
  179. def fibonacci_base_example(ctx: Context):
  180. """
  181. # Fibonacci base
  182. """
  183. value = str(ctx.text_input("1100101"))
  184. n = len(value)
  185. L = [1, 2]
  186. while len(L) < n:
  187. L.append(L[-1] + L[-2])
  188. output_number = 0
  189. output_sum = ""
  190. R = []
  191. for i, d in enumerate(value):
  192. if d == '1':
  193. output_number += L[n - i - 1]
  194. R.append(L[n - i - 1])
  195. output_sum = ' + '.join(map(str, R))
  196. ctx.markdown(str(output_number))
  197. ctx.markdown(output_sum)
  198. @fibonacci_base.slide
  199. def fibonacci_base_poll_1(ctx: Context):
  200. """
  201. # Fibonacci base
  202. """
  203. ctx.markdown(
  204. "Can we represent all integer numbers using fibonacci base?")
  205. with ctx.fragment(ctx):
  206. with ctx.columns(2) as cl:
  207. with ctx.fragment('highlight-green'):
  208. ctx.markdown('Yes')
  209. cl.tab()
  210. ctx.markdown('No')
  211. @fibonacci_base.slide
  212. def fibonacci_base_poll_2(ctx: Context):
  213. """
  214. # Fibonacci base
  215. """
  216. ctx.markdown("Can we represent every number in a unique way?")
  217. with ctx.fragment(ctx):
  218. with ctx.columns(2) as cl:
  219. ctx.markdown('Yes')
  220. cl.tab()
  221. with ctx.fragment('highlight-red'):
  222. ctx.markdown('No')
  223. # Make a break
  224. @ show.slide
  225. def computer_representation(ctx: Context):
  226. """
  227. ### Representation in the computer
  228. """
  229. with ctx.fragment(ctx):
  230. ctx.markdown("Binary")
  231. with ctx.fragment(ctx):
  232. ctx.markdown("Bytes")
  233. with ctx.fragment(ctx):
  234. ctx.markdown("C++ Demo")
  235. @ show.slide
  236. def base_16_64_application(ctx):
  237. """
  238. ### Application of higher base
  239. * Hexadecimal
  240. * Base64
  241. """
  242. with ctx.fragment(ctx):
  243. ctx.markdown("Demo")
  244. @ show.slide
  245. def decimal_number_in_base_10(ctx):
  246. """
  247. Decimal number
  248. $$\sum_{-\infty \lt i \lt \infty} d_i \cdot 10^{i}$$
  249. """
  250. @ show.slide
  251. def decimal_number_in_computer(ctx):
  252. """
  253. Single precision floating point format
  254. """
  255. with ctx.fragment(ctx):
  256. ctx.markdown("Wikipedia")
  257. with ctx.fragment(ctx):
  258. ctx.markdown("C++ Demo")
  259. # Some precision issues with catastrophic cancelation (substraction)
  260. # Compare numbers directly with each other
  261. # a / b == c (not always the same)
  262. # Case where the two numbers have same representation
  263. # but a == b (is false) This is with nan
  264. # and different representation but a == b (is true) this is 0 and -0
  265. @ show.slide
  266. def homework(ctx):
  267. """
  268. Homework
  269. """
  270. @homework.slide
  271. def task_1(ctx):
  272. """
  273. `*`
  274. Code that convert from any base to any other base
  275. Hint: Try converting to python number first
  276. """
  277. @homework.slide
  278. def task_2(ctx):
  279. """
  280. `*`
  281. Code that convert from base 2 to base 16 without converting to python number
  282. """
  283. @homework.slide
  284. def task_3(ctx):
  285. """
  286. `***`
  287. On fibonacci base, if we forbid consecutive 1, prove that every number is represented uniquely
  288. [Share LaTeX example]
  289. """
  290. # > Proof that fibonacci base without non-consecutive 1 is unique
  291. # > Hint 1: Try using induction
  292. # > Hint 2: Assume all numbers less than F_n
  293. @homework.slide
  294. def task_4(ctx):
  295. """
  296. `**`
  297. Make a program that converts from a number to fibonacci base
  298. """
  299. @homework.slide
  300. def task_5(ctx):
  301. """
  302. `***`
  303. There are $n$ books. Each of them can be painted in one of $k$ different colors.
  304. 1. How many different ways can the books be painted.
  305. 2. Make a program that prints all different possible ways.
  306. """
  307. @ show.slide
  308. def bye(ctx):
  309. """
  310. Mathematical Foundation of Algorithms
  311. Marcelo Fornet
  312. * Email: [mfornet94@gmail.com](mailto:mfornet94@gmail.com)
  313. * Telegram: [@mnaeraxr](https://t.me/mnaeraxr)
  314. """