From 73c6f32135ac6432a299117fda1c13025ef74970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Fri, 21 Mar 2025 13:40:24 +0100 Subject: [PATCH] Big changes in Numbers and Combinatorics --- README.md | 57 +++++++++++++++++++++--------------------- index.html | 55 ++++++++++++++++++++-------------------- parse.js | 10 ++++---- pdf/index_for_pdf.html | 4 +-- 4 files changed, 62 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index eb47db9..f7f64f1 100644 --- a/README.md +++ b/README.md @@ -500,10 +500,10 @@ Numbers = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1) = decimal.Decimal() # Or: Decimal((sign, digits, exponent)) ``` -* **`'int()'` and `'float()'` raise ValueError on malformed strings.** -* **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.** +* **Decimal numbers are stored exactly, unlike most floats where: `'1.1 + 2.2 != 3.3'`.** * **Floats can be compared with: `'math.isclose(, )'`.** * **Precision of decimal operations is set with: `'decimal.getcontext().prec = '`.** +* **Bools can be used anywhere ints can, because bool is a subclass of int: `'True + 1 == 2'`.** ### Basic Functions ```python @@ -526,18 +526,23 @@ from statistics import mean, median, variance # Also: stdev, quantiles, grou ### Random ```python -from random import random, randint, choice # Also: shuffle, gauss, triangular, seed. - = random() # A float inside [0, 1). - = randint(from_inc, to_inc) # An int inside [from_inc, to_inc]. - = choice() # Keeps the sequence intact. +from random import random, randint, uniform # Also: gauss, choice, shuffle, seed. ``` -### Bin, Hex ```python - = ±0b # Or: ±0x - = int('±', 2) # Or: int('±', 16) - = int('±0b', 0) # Or: int('±0x', 0) - = bin() # Returns '[-]0b'. Also hex(). + = random() # Returns a float inside [0, 1). + = randint/uniform(a, b) # Returns an int/float inside [a, b]. + = gauss(mean, stdev) # Also triangular(low, high, mode). + = choice() # Keeps it intact. Also sample(pop, k). +shuffle() # Shuffles the list in place. +``` + +### Hexadecimal Numbers +```python + = ±0x # Or: ±0b + = int('±', 16) # Or: int('±', 2) + = int('±0x', 0) # Or: int('±0b', 0) + = hex() # Returns '[-]0x'. Also bin(). ``` ### Bitwise Operators @@ -563,30 +568,24 @@ import itertools as it ``` ```python ->>> list(it.product('abc', 'abc')) # a b c -[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x - ('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x - ('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x -``` - -```python ->>> list(it.combinations('abc', 2)) # a b c -[('a', 'b'), ('a', 'c'), # a . x x - ('b', 'c')] # b . . x +>>> list(it.product('abc', 'abc')) # a b c +[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x + ('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x + ('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x ``` ```python ->>> list(it.combinations_with_replacement('abc', 2)) # a b c -[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x - ('b', 'b'), ('b', 'c'), # b . x x - ('c', 'c')] # c . . x +>>> list(it.permutations('abc', 2)) # a b c +[('a', 'b'), ('a', 'c'), # a . x x + ('b', 'a'), ('b', 'c'), # b x . x + ('c', 'a'), ('c', 'b')] # c x x . ``` ```python ->>> list(it.permutations('abc', 2)) # a b c -[('a', 'b'), ('a', 'c'), # a . x x - ('b', 'a'), ('b', 'c'), # b x . x - ('c', 'a'), ('c', 'b')] # c x x . +>>> list(it.combinations('abc', 2)) # a b c +[('a', 'b'), ('a', 'c'), # a . x x + ('b', 'c'), # b . . x +] # c . . . ``` diff --git a/index.html b/index.html index f280ede..9d17abd 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@
- +
@@ -463,10 +463,10 @@ Point(x=1, y=2
    -
  • 'int(<str>)' and 'float(<str>)' raise ValueError on malformed strings.
  • -
  • Decimal numbers are stored exactly, unlike most floats where '1.1 + 2.2 != 3.3'.
  • +
  • Decimal numbers are stored exactly, unlike most floats where: '1.1 + 2.2 != 3.3'.
  • Floats can be compared with: 'math.isclose(<float>, <float>)'.
  • Precision of decimal operations is set with: 'decimal.getcontext().prec = <int>'.
  • +
  • Bools can be used anywhere ints can, because bool is a subclass of int: 'True + 1 == 2'.

Basic Functions

<num> = pow(<num>, <num>)                         # Or: <number> ** <number>
 <num> = abs(<num>)                                # <float> = abs(<complex>)
@@ -481,16 +481,19 @@ Point(x=1, y=2
 

Statistics

from statistics import mean, median, variance     # Also: stdev, quantiles, groupby.
 
-

Random

from random import random, randint, choice        # Also: shuffle, gauss, triangular, seed.
-<float> = random()                                # A float inside [0, 1).
-<int>   = randint(from_inc, to_inc)               # An int inside [from_inc, to_inc].
-<el>    = choice(<sequence>)                      # Keeps the sequence intact.
+

Random

from random import random, randint, uniform       # Also: gauss, choice, shuffle, seed.
 
-

Bin, Hex

<int> = ±0b<bin>                                  # Or: ±0x<hex>
-<int> = int('±<bin>', 2)                          # Or: int('±<hex>', 16)
-<int> = int('±0b<bin>', 0)                        # Or: int('±0x<hex>', 0)
-<str> = bin(<int>)                                # Returns '[-]0b<bin>'. Also hex().
+
<float> = random()                                # Returns a float inside [0, 1).
+<num>   = randint/uniform(a, b)                   # Returns an int/float inside [a, b].
+<float> = gauss(mean, stdev)                      # Also triangular(low, high, mode).
+<el>    = choice(<sequence>)                      # Keeps it intact. Also sample(pop, k).
+shuffle(<list>)                                   # Shuffles the list in place.
+
+

Hexadecimal Numbers

<int> = ±0x<hex>                                  # Or: ±0b<bin>
+<int> = int('±<hex>', 16)                         # Or: int('±<bin>', 2)
+<int> = int('±0x<hex>', 0)                        # Or: int('±0b<bin>', 0)
+<str> = hex(<int>)                                # Returns '[-]0x<hex>'. Also bin().
 

Bitwise Operators

<int> = <int> & <int>                             # And (0b1100 & 0b1010 == 0b1000).
@@ -507,24 +510,20 @@ Point(x=1, y=2
 [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
  (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
 
-
>>> list(it.product('abc', 'abc'))                    #   a  b  c
-[('a', 'a'), ('a', 'b'), ('a', 'c'),                  # a x  x  x
- ('b', 'a'), ('b', 'b'), ('b', 'c'),                  # b x  x  x
- ('c', 'a'), ('c', 'b'), ('c', 'c')]                  # c x  x  x
-
-
>>> list(it.combinations('abc', 2))                   #   a  b  c
-[('a', 'b'), ('a', 'c'),                              # a .  x  x
- ('b', 'c')]                                          # b .  .  x
+
>>> list(it.product('abc', 'abc'))                #   a  b  c
+[('a', 'a'), ('a', 'b'), ('a', 'c'),              # a x  x  x
+ ('b', 'a'), ('b', 'b'), ('b', 'c'),              # b x  x  x
+ ('c', 'a'), ('c', 'b'), ('c', 'c')]              # c x  x  x
 
-
>>> list(it.combinations_with_replacement('abc', 2))  #   a  b  c
-[('a', 'a'), ('a', 'b'), ('a', 'c'),                  # a x  x  x
- ('b', 'b'), ('b', 'c'),                              # b .  x  x
- ('c', 'c')]                                          # c .  .  x
+
>>> list(it.permutations('abc', 2))               #   a  b  c
+[('a', 'b'), ('a', 'c'),                          # a .  x  x
+ ('b', 'a'), ('b', 'c'),                          # b x  .  x
+ ('c', 'a'), ('c', 'b')]                          # c x  x  .
 
-
>>> list(it.permutations('abc', 2))                   #   a  b  c
-[('a', 'b'), ('a', 'c'),                              # a .  x  x
- ('b', 'a'), ('b', 'c'),                              # b x  .  x
- ('c', 'a'), ('c', 'b')]                              # c x  x  .
+
>>> list(it.combinations('abc', 2))               #   a  b  c
+[('a', 'b'), ('a', 'c'),                          # a .  x  x
+ ('b', 'c'),                                      # b .  .  x
+]                                                 # c .  .  .
 

#Datetime

Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.

# $ pip3 install python-dateutil
 from datetime import date, time, datetime, timedelta, timezone
@@ -2945,7 +2944,7 @@ $ deactivate                # Deactivates the active
  
 
   
 
diff --git a/parse.js b/parse.js
index c83b9d4..a2d74d5 100755
--- a/parse.js
+++ b/parse.js
@@ -43,10 +43,10 @@ const TOC =
   '
\n'; const BIN_HEX = - '<int> = ±0b<bin> # Or: ±0x<hex>\n' + - '<int> = int(\'±<bin>\', 2) # Or: int(\'±<hex>\', 16)\n' + - '<int> = int(\'±0b<bin>\', 0) # Or: int(\'±0x<hex>\', 0)\n' + - '<str> = bin(<int>) # Returns \'[-]0b<bin>\'. Also hex().\n'; + '<int> = ±0x<hex> # Or: ±0b<bin>\n' + + '<int> = int(\'±<hex>\', 16) # Or: int(\'±<bin>\', 2)\n' + + '<int> = int(\'±0x<hex>\', 0) # Or: int(\'±0b<bin>\', 0)\n' + + '<str> = hex(<int>) # Returns \'[-]0x<hex>\'. Also bin().\n'; const CACHE = 'from functools import cache\n' + @@ -922,7 +922,7 @@ function fixClasses() { } function fixHighlights() { - $(`code:contains( = ±0b)`).html(BIN_HEX); + $(`code:contains( = ±0x)`).html(BIN_HEX); $(`code:contains( + fib(n)`).html(CACHE); $(`code:contains(>>> def add)`).html(SPLAT); $(`code:contains(@debug(print_result=True))`).html(PARAMETRIZED_DECORATOR); diff --git a/pdf/index_for_pdf.html b/pdf/index_for_pdf.html index cb62ed1..a4a44cc 100644 --- a/pdf/index_for_pdf.html +++ b/pdf/index_for_pdf.html @@ -13,7 +13,7 @@ audio, 40-41, 42

B

beautifulsoup library, 35
-binary representation, 7, 8
+binary representation, 7, 8
bitwise operators, 8, 30
bytes, 22-23, 25, 28-29

C

@@ -60,7 +60,7 @@ gui app, 35

H

hashable, 15, 16
-hexadecimal representation, 7, 8, 28

+hexadecimal representation, 7, 8, 28

I

image, 35, 39-40, 42-43
imports, 12