istr-python 1.1.37.post0__tar.gz → 1.1.38.post0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: istr-python
3
- Version: 1.1.37.post0
3
+ Version: 1.1.38.post0
4
4
  Summary: istr - strings you can count on
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/istr
@@ -858,7 +858,27 @@ Note that this method has the advantage that it can accept an istr as index, in
858
858
  The method `long_sqrt` gives a list of the lines of a long square root. Both the result (root) and the original number, possibly with a trailing 0 are returned, along with the calculated values.
859
859
  E.g. `istr.long_sqrt(123 ** 2)` results in
860
860
 
861
- `[istr('123'), istr('015129'), istr('1'), istr('51'), istr('44'), istr('729'), istr('729'), istr('0')]`
861
+ `[istr('123'), istr('15129'), istr('1'), istr('51'), istr('44'), istr('729'), istr('729'), istr('0')]`
862
+
863
+ Note the final `istr('0')` is included to support also long square roots that are not perfect.
864
+
865
+ The method `long_sqrt`has an optional parameter `as_str`, that can be used to get a nice string representation of the long square root lines.
866
+
867
+ E.g. `print(istr.long_sqrt(123 ** 2, as_str=True))` will print
868
+ ```
869
+ 1 2 3
870
+ -----
871
+ \/15129
872
+ 1
873
+ -
874
+ 51
875
+ 44
876
+ --
877
+ 729
878
+ 729
879
+ ---
880
+ 0
881
+ ```
862
882
 
863
883
  #### generate istr with digits
864
884
 
@@ -845,7 +845,27 @@ Note that this method has the advantage that it can accept an istr as index, in
845
845
  The method `long_sqrt` gives a list of the lines of a long square root. Both the result (root) and the original number, possibly with a trailing 0 are returned, along with the calculated values.
846
846
  E.g. `istr.long_sqrt(123 ** 2)` results in
847
847
 
848
- `[istr('123'), istr('015129'), istr('1'), istr('51'), istr('44'), istr('729'), istr('729'), istr('0')]`
848
+ `[istr('123'), istr('15129'), istr('1'), istr('51'), istr('44'), istr('729'), istr('729'), istr('0')]`
849
+
850
+ Note the final `istr('0')` is included to support also long square roots that are not perfect.
851
+
852
+ The method `long_sqrt`has an optional parameter `as_str`, that can be used to get a nice string representation of the long square root lines.
853
+
854
+ E.g. `print(istr.long_sqrt(123 ** 2, as_str=True))` will print
855
+ ```
856
+ 1 2 3
857
+ -----
858
+ \/15129
859
+ 1
860
+ -
861
+ 51
862
+ 44
863
+ --
864
+ 729
865
+ 729
866
+ ---
867
+ 0
868
+ ```
849
869
 
850
870
  #### generate istr with digits
851
871
 
@@ -5,7 +5,7 @@
5
5
  # |_||___/ \__||_|
6
6
  # strings you can count on
7
7
 
8
- __version__ = "1.1.37"
8
+ __version__ = "1.1.38"
9
9
  import functools
10
10
  import itertools
11
11
  import types
@@ -841,8 +841,8 @@ class istr(str):
841
841
 
842
842
  def prod(self, *, start=1):
843
843
  return math.prod(self, start=istr(start))
844
-
845
- def sum(self, / , start=0):
844
+
845
+ def sum(self, /, start=0):
846
846
  return sum(istr(self), start=int(start))
847
847
 
848
848
  @classmethod
@@ -855,42 +855,71 @@ class istr(str):
855
855
  def enumerate(cls, iterable, start=0):
856
856
  for i, value in enumerate(iterable, int(start)):
857
857
  yield cls(i), value
858
-
859
- def long_sqrt(self):
858
+
859
+ def long_sqrt(self, as_str=False):
860
860
  """
861
861
  this function does long squre root and returns all the lines to be written.
862
862
  (modified from a ChatGPT code suggestion)
863
- returns root, number (possibly with an extra 0) and the lines below the number, all istr-ed
863
+ returns root, number and the lines below the number, all istr-ed
864
+ if as_str is True, a formatted output will be returned
864
865
  """
865
- number=istr(istr.interpret_as_str(self))
866
- lines = []
867
866
 
868
- if len(number)%2==1:
869
- number = "0" | number
870
-
871
- pairs=list(istr.batched(number,2,join=True))
867
+ number = istr(istr.interpret_as_str(self))
868
+
869
+ lines = []
870
+ if len(number)%2==1:
871
+ pairs=list(istr.batched('x'|number,2,join=True))
872
+ pairs[0]=pairs[0][1]
873
+ else:
874
+ pairs=list(istr.batched(number,2,join=True))
875
+
872
876
  root = istr(0)
873
877
  remainder = 0
874
-
878
+
875
879
  for pair in pairs:
876
880
  dividend = remainder * 100 + pair
877
-
881
+
878
882
  x = 0
879
883
  for d in range(10):
880
884
  if (20 * root + d) * d <= dividend:
881
885
  x = d
882
886
  else:
883
887
  break
884
-
888
+
885
889
  trial = (20 * root + x) * x
886
890
  remainder = dividend - trial
887
-
891
+
888
892
  root = root * 10 + x
889
893
  lines.append(dividend)
890
894
  lines.append(trial)
895
+
891
896
  lines.append(remainder)
892
- return [root]+[number]+ lines[1:] # ignore the first dividend
893
-
897
+ lines = [root] + [number] + lines[1:]
898
+ if as_str:
899
+ result = []
900
+ extra = len(pairs[0])==2
901
+ result.append(f" {extra*' '}{' '.join(lines[0])}")
902
+ result.append(f" {len(lines[1])*'-'}")
903
+ result.append(f"\/{lines[1]}")
904
+
905
+ last_len=len(lines[2])
906
+ for i, line in enumerate(lines):
907
+
908
+ if i >= 2:
909
+ if i == len(lines) - 1:
910
+ n = i - 2 + extra
911
+ else:
912
+ n = i + extra
913
+
914
+ if i % 2 == 0:
915
+ result.append(f" {line:>{n}}")
916
+ result.append(f" {last_len*'-':>{n}}")
917
+ else:
918
+ result.append(f" {line:>{n+1}}")
919
+ last_len=len(line)
920
+ return "\n".join(result)
921
+ else:
922
+ return lines
894
923
 
895
924
  def this_base(self):
896
925
  return self._this_base
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: istr-python
3
- Version: 1.1.37.post0
3
+ Version: 1.1.38.post0
4
4
  Summary: istr - strings you can count on
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/istr
@@ -858,7 +858,27 @@ Note that this method has the advantage that it can accept an istr as index, in
858
858
  The method `long_sqrt` gives a list of the lines of a long square root. Both the result (root) and the original number, possibly with a trailing 0 are returned, along with the calculated values.
859
859
  E.g. `istr.long_sqrt(123 ** 2)` results in
860
860
 
861
- `[istr('123'), istr('015129'), istr('1'), istr('51'), istr('44'), istr('729'), istr('729'), istr('0')]`
861
+ `[istr('123'), istr('15129'), istr('1'), istr('51'), istr('44'), istr('729'), istr('729'), istr('0')]`
862
+
863
+ Note the final `istr('0')` is included to support also long square roots that are not perfect.
864
+
865
+ The method `long_sqrt`has an optional parameter `as_str`, that can be used to get a nice string representation of the long square root lines.
866
+
867
+ E.g. `print(istr.long_sqrt(123 ** 2, as_str=True))` will print
868
+ ```
869
+ 1 2 3
870
+ -----
871
+ \/15129
872
+ 1
873
+ -
874
+ 51
875
+ 44
876
+ --
877
+ 729
878
+ 729
879
+ ---
880
+ 0
881
+ ```
862
882
 
863
883
  #### generate istr with digits
864
884
 
@@ -10,7 +10,7 @@ authors = [
10
10
  { name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com" },
11
11
  ]
12
12
  description = "istr - strings you can count on"
13
- version = "1.1.37.post0"
13
+ version = "1.1.38.post0"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.10"
16
16
  dependencies = []
@@ -1219,12 +1219,26 @@ def test_compose():
1219
1219
 
1220
1220
  def test_long_sqrt():
1221
1221
  lines=istr.long_sqrt(1234**2)
1222
- assert lines==[istr('1234'), istr('01522756'), istr('1'), istr('52'), istr('44'), istr('827'), istr('729'), istr('9856'), istr('9856'), istr('0')]
1222
+ assert lines==[istr('1234'), istr('1522756'), istr('1'), istr('52'), istr('44'), istr('827'), istr('729'), istr('9856'), istr('9856'), istr('0')]
1223
+ lines=istr.long_sqrt(1234**2+1)
1224
+ assert lines==[istr('1234'), istr('1522757'), istr('1'), istr('52'), istr('44'), istr('827'), istr('729'), istr('9857'), istr('9856'), istr('1')]
1223
1225
  lines=istr.long_sqrt(1234**2+1)
1224
- assert lines==[istr('1234'), istr('01522757'), istr('1'), istr('52'), istr('44'), istr('827'), istr('729'), istr('9857'), istr('9856'), istr('1')]
1225
1226
  lines=istr(1234**2).long_sqrt()
1226
- assert lines==[istr('1234'), istr('01522756'), istr('1'), istr('52'), istr('44'), istr('827'), istr('729'), istr('9856'), istr('9856'), istr('0')]
1227
-
1227
+ assert lines==[istr('1234'), istr('1522756'), istr('1'), istr('52'), istr('44'), istr('827'), istr('729'), istr('9856'), istr('9856'), istr('0')]
1228
+ lines=istr.long_sqrt(34**2)
1229
+ assert lines==[istr('34'), istr('1156'), istr('9'), istr('256'), istr('256'), istr('0')]
1230
+ lines=istr.long_sqrt(34**2,as_str=True)
1231
+ assert lines=='''\
1232
+ 3 4
1233
+ ----
1234
+ \\/1156
1235
+ 9
1236
+ -
1237
+ 256
1238
+ 256
1239
+ ---
1240
+ 0'''
1241
+
1228
1242
  if __name__ == "__main__":
1229
1243
  pytest.main(["-vv", "-s", "-x", __file__])
1230
1244