istr-python 1.1.39__tar.gz → 1.1.40__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.39
3
+ Version: 1.1.40
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
@@ -880,6 +880,53 @@ E.g. `print(istr.long_sqrt(123 ** 2, as_str=True))` will print
880
880
  0
881
881
  ```
882
882
 
883
+ #### long multiplication
884
+
885
+ The method `long_multiplication` gives a list of all lines to do a long multiplition.
886
+
887
+ E.g. `print(istr.long_multiplication(1234,567)` will print
888
+
889
+ `[istr('1234'), istr('567'), istr('8638'), istr('7404'), istr('6170'), istr('699678')]`
890
+
891
+ The method has an optional parameter `as_str`, that can be used t0 get a nice representation of the long multiplication lines.
892
+
893
+ E.g. `print(istr.long_multiplication(1234,567, as_str=True)` will print
894
+
895
+ ```
896
+ 1234
897
+ 567
898
+ ------ x
899
+ 8638
900
+ 7404
901
+ 6170
902
+ ------
903
+ 699678
904
+ ```
905
+
906
+ #### long division
907
+
908
+ The method `long_division` gives a list of all lines to do a long division (note that the divisor and dividend count as separate lines).
909
+
910
+ E.g. `print(istr.long_division(1395, 45)` will print
911
+
912
+ [istr('31'), istr('45'), istr('1395'), istr('135'), istr('45'), istr('45'), istr('0')]
913
+
914
+ The method has an optional parameter `as_str`, that can be used to get a nice representation of the long multiplication lines.
915
+
916
+ E.g. `print(istr.long_division(1395, 45, as_str=True)` will print
917
+
918
+ ```
919
+ 31
920
+ ----
921
+ 45 ) 1395
922
+ 135
923
+ ---
924
+ 45
925
+ 45
926
+ --
927
+ 0
928
+ ```
929
+
883
930
  #### generate istr with digits
884
931
 
885
932
  The class method `digits` can be used to return an istr of digits according to a given specification.
@@ -867,6 +867,53 @@ E.g. `print(istr.long_sqrt(123 ** 2, as_str=True))` will print
867
867
  0
868
868
  ```
869
869
 
870
+ #### long multiplication
871
+
872
+ The method `long_multiplication` gives a list of all lines to do a long multiplition.
873
+
874
+ E.g. `print(istr.long_multiplication(1234,567)` will print
875
+
876
+ `[istr('1234'), istr('567'), istr('8638'), istr('7404'), istr('6170'), istr('699678')]`
877
+
878
+ The method has an optional parameter `as_str`, that can be used t0 get a nice representation of the long multiplication lines.
879
+
880
+ E.g. `print(istr.long_multiplication(1234,567, as_str=True)` will print
881
+
882
+ ```
883
+ 1234
884
+ 567
885
+ ------ x
886
+ 8638
887
+ 7404
888
+ 6170
889
+ ------
890
+ 699678
891
+ ```
892
+
893
+ #### long division
894
+
895
+ The method `long_division` gives a list of all lines to do a long division (note that the divisor and dividend count as separate lines).
896
+
897
+ E.g. `print(istr.long_division(1395, 45)` will print
898
+
899
+ [istr('31'), istr('45'), istr('1395'), istr('135'), istr('45'), istr('45'), istr('0')]
900
+
901
+ The method has an optional parameter `as_str`, that can be used to get a nice representation of the long multiplication lines.
902
+
903
+ E.g. `print(istr.long_division(1395, 45, as_str=True)` will print
904
+
905
+ ```
906
+ 31
907
+ ----
908
+ 45 ) 1395
909
+ 135
910
+ ---
911
+ 45
912
+ 45
913
+ --
914
+ 0
915
+ ```
916
+
870
917
  #### generate istr with digits
871
918
 
872
919
  The class method `digits` can be used to return an istr of digits according to a given specification.
@@ -5,7 +5,7 @@
5
5
  # |_||___/ \__||_|
6
6
  # strings you can count on
7
7
 
8
- __version__ = "1.1.39"
8
+ __version__ = "1.1.40"
9
9
  import functools
10
10
  import itertools
11
11
  import types
@@ -868,10 +868,9 @@ class istr(str):
868
868
 
869
869
  lines = []
870
870
  if len(number)%2==1:
871
- pairs=list(istr.batched('x'|number,2,join=True))
872
- pairs[0]=pairs[0][1]
871
+ pairs=istr.chain(number[0],istr.batched(number[1:],2,join=True))
873
872
  else:
874
- pairs=list(istr.batched(number,2,join=True))
873
+ pairs=istr.batched(number,2,join=True)
875
874
 
876
875
  root = istr(0)
877
876
  remainder = 0
@@ -897,7 +896,7 @@ class istr(str):
897
896
  lines = [root] + [number] + lines[1:]
898
897
  if as_str:
899
898
  result = []
900
- extra = len(pairs[0])==2
899
+ extra = len(number)%2==0
901
900
  result.append(f" {extra*' '}{' '.join(lines[0])}")
902
901
  result.append(f" {len(lines[1])*'-'}")
903
902
  result.append(f"\/{lines[1]}")
@@ -920,7 +919,93 @@ class istr(str):
920
919
  return "\n".join(result)
921
920
  else:
922
921
  return lines
923
-
922
+
923
+ def long_multiplication(self, number,as_str=False):
924
+ number0 = istr(istr.interpret_as_str(self))
925
+ number1=istr(number)
926
+ lines=[number0,number1]
927
+ for i in number1.reversed():
928
+ lines.append(number0*i)
929
+ lines.append(number0*number1)
930
+ if as_str:
931
+ result=[]
932
+ n=len(lines[-1])
933
+ result.append(f'{lines[0]:>{n}}')
934
+ result.append(f'{lines[1]:>{n}}')
935
+ result.append(f"{n*'-'} x")
936
+ for i,line in enumerate(lines[2:-1]):
937
+ result.append(f"{line:>{n-i}}")
938
+ result.append(f"{n*'-'}")
939
+ result.append(lines[-1])
940
+ return '\n'.join(result)
941
+ else:
942
+ return lines
943
+
944
+ def long_division(self, divisor, as_str=False):
945
+ dividend = istr(istr.interpret_as_str(self))
946
+ divisor = istr(divisor)
947
+
948
+ if dividend < 0:
949
+ raise ValueError("dividend must be non-negative.")
950
+
951
+ if divisor <= 0:
952
+ raise ValueError("divisor must be positive.")
953
+
954
+ dividend_start = len(divisor) + 3
955
+ total_width = dividend_start + len(dividend)
956
+
957
+ quotient_characters = [" "] * len(dividend)
958
+
959
+ remainder = 0
960
+ division_started = False
961
+ lines=[]
962
+
963
+ for digit_index, digit_character in enumerate(dividend):
964
+ current = remainder * 10 + digit_character
965
+ quotient_digit = current // divisor
966
+
967
+ if quotient_digit != 0 or division_started:
968
+ division_started = True
969
+
970
+ if digit_index == len(dividend) - 1:
971
+ division_started = True
972
+
973
+ if not division_started:
974
+ remainder = current
975
+ continue
976
+
977
+ quotient_characters[digit_index] = quotient_digit
978
+
979
+ product = quotient_digit * divisor
980
+ remainder = current - product
981
+
982
+ if as_str:
983
+ end_column = dividend_start + digit_index
984
+ current_start = end_column - len(current) + 1
985
+ product_start = end_column - len(product) + 1
986
+ bar_start = min(current_start, product_start)
987
+ bar_length = end_column - bar_start + 1
988
+ lines.append(" " * current_start | current)
989
+ lines.append(" " * product_start | product)
990
+ lines.append(" " * bar_start + "-" * bar_length)
991
+
992
+ else:
993
+ lines.append(current)
994
+ lines.append(product)
995
+
996
+ quotient = istr.join(quotient_characters).rstrip()
997
+ if as_str:
998
+ remainder_start = total_width - len(remainder)
999
+ lines.append(" " * remainder_start | remainder)
1000
+ lines=[" " * dividend_start | quotient," " * dividend_start + "-" * len(dividend),f"{divisor} ) {dividend}",*lines[1:]]
1001
+ return "\n".join(lines)
1002
+
1003
+ else:
1004
+ lines.append(remainder)
1005
+ lines=[istr(quotient),divisor,dividend,*lines[1:]]
1006
+ return lines
1007
+
1008
+
924
1009
  def this_base(self):
925
1010
  return self._this_base
926
1011
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: istr-python
3
- Version: 1.1.39
3
+ Version: 1.1.40
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
@@ -880,6 +880,53 @@ E.g. `print(istr.long_sqrt(123 ** 2, as_str=True))` will print
880
880
  0
881
881
  ```
882
882
 
883
+ #### long multiplication
884
+
885
+ The method `long_multiplication` gives a list of all lines to do a long multiplition.
886
+
887
+ E.g. `print(istr.long_multiplication(1234,567)` will print
888
+
889
+ `[istr('1234'), istr('567'), istr('8638'), istr('7404'), istr('6170'), istr('699678')]`
890
+
891
+ The method has an optional parameter `as_str`, that can be used t0 get a nice representation of the long multiplication lines.
892
+
893
+ E.g. `print(istr.long_multiplication(1234,567, as_str=True)` will print
894
+
895
+ ```
896
+ 1234
897
+ 567
898
+ ------ x
899
+ 8638
900
+ 7404
901
+ 6170
902
+ ------
903
+ 699678
904
+ ```
905
+
906
+ #### long division
907
+
908
+ The method `long_division` gives a list of all lines to do a long division (note that the divisor and dividend count as separate lines).
909
+
910
+ E.g. `print(istr.long_division(1395, 45)` will print
911
+
912
+ [istr('31'), istr('45'), istr('1395'), istr('135'), istr('45'), istr('45'), istr('0')]
913
+
914
+ The method has an optional parameter `as_str`, that can be used to get a nice representation of the long multiplication lines.
915
+
916
+ E.g. `print(istr.long_division(1395, 45, as_str=True)` will print
917
+
918
+ ```
919
+ 31
920
+ ----
921
+ 45 ) 1395
922
+ 135
923
+ ---
924
+ 45
925
+ 45
926
+ --
927
+ 0
928
+ ```
929
+
883
930
  #### generate istr with digits
884
931
 
885
932
  The class method `digits` can be used to return an istr of digits according to a given specification.
@@ -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.39"
13
+ version = "1.1.40"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.10"
16
16
  dependencies = []
@@ -218,6 +218,7 @@ def test_divmod():
218
218
  def test_iter():
219
219
  assert [x for x in istr.range(3)] == [istr(0), istr(1), istr(2)]
220
220
 
221
+
221
222
  def test_none():
222
223
  assert istr(None) is None
223
224
 
@@ -1066,12 +1067,13 @@ def test_is_triangular():
1066
1067
  assert istr(424581).is_triangular()
1067
1068
  assert not istr(424582).is_triangular()
1068
1069
  assert istr.is_triangular(424581)
1069
-
1070
+
1071
+
1070
1072
  def test_sum():
1071
- assert istr(1234).sum() == istr('10')
1072
- assert istr.sum('1234') == istr('10')
1073
- assert istr.sum(1234) == istr('10')
1074
- assert istr.sum(1234, 5) == istr('15')
1073
+ assert istr(1234).sum() == istr("10")
1074
+ assert istr.sum("1234") == istr("10")
1075
+ assert istr.sum(1234) == istr("10")
1076
+ assert istr.sum(1234, 5) == istr("15")
1075
1077
 
1076
1078
 
1077
1079
  def test_prod():
@@ -1217,18 +1219,49 @@ def test_compose():
1217
1219
  assert istr(":=") == ":="
1218
1220
  assert istr("=") == "="
1219
1221
 
1222
+
1220
1223
  def test_long_sqrt():
1221
- lines=istr.long_sqrt(1234**2)
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')]
1225
- lines=istr.long_sqrt(1234**2+1)
1226
- lines=istr(1234**2).long_sqrt()
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=='''\
1224
+ assert istr.long_sqrt(1234**2) == [
1225
+ istr("1234"),
1226
+ istr("1522756"),
1227
+ istr("1"),
1228
+ istr("52"),
1229
+ istr("44"),
1230
+ istr("827"),
1231
+ istr("729"),
1232
+ istr("9856"),
1233
+ istr("9856"),
1234
+ istr("0"),
1235
+ ]
1236
+ assert istr.long_sqrt(1234**2 + 1) == [
1237
+ istr("1234"),
1238
+ istr("1522757"),
1239
+ istr("1"),
1240
+ istr("52"),
1241
+ istr("44"),
1242
+ istr("827"),
1243
+ istr("729"),
1244
+ istr("9857"),
1245
+ istr("9856"),
1246
+ istr("1"),
1247
+ ]
1248
+ lines = istr.long_sqrt(1234**2 + 1)
1249
+ assert istr(1234**2).long_sqrt() == [
1250
+ istr("1234"),
1251
+ istr("1522756"),
1252
+ istr("1"),
1253
+ istr("52"),
1254
+ istr("44"),
1255
+ istr("827"),
1256
+ istr("729"),
1257
+ istr("9856"),
1258
+ istr("9856"),
1259
+ istr("0"),
1260
+ ]
1261
+ assert istr.long_sqrt(34**2) == [istr("34"), istr("1156"), istr("9"), istr("256"), istr("256"), istr("0")]
1262
+ assert (
1263
+ istr.long_sqrt(34**2, as_str=True)
1264
+ == """\
1232
1265
  3 4
1233
1266
  ----
1234
1267
  \\/1156
@@ -1237,8 +1270,45 @@ def test_long_sqrt():
1237
1270
  256
1238
1271
  256
1239
1272
  ---
1240
- 0'''
1241
-
1273
+ 0"""
1274
+ )
1275
+
1276
+
1277
+ def test_long_multiplication():
1278
+ assert istr.long_multiplication(1234, 567) == [istr("1234"), istr("567"), istr("8638"), istr("7404"), istr("6170"), istr("699678")]
1279
+ assert istr(1234).long_multiplication(567) == [istr("1234"), istr("567"), istr("8638"), istr("7404"), istr("6170"), istr("699678")]
1280
+ assert (
1281
+ istr.long_multiplication(1234, 567, as_str=True)
1282
+ == """\
1283
+ 1234
1284
+ 567
1285
+ ------ x
1286
+ 8638
1287
+ 7404
1288
+ 6170
1289
+ ------
1290
+ 699678"""
1291
+ )
1292
+
1293
+
1294
+ def test_long_division():
1295
+ assert istr.long_division(1395, 45) == [istr("31"), istr("45"), istr("1395"), istr("135"), istr("45"), istr("45"), istr("0")]
1296
+ assert istr(1395).long_division(45) == [istr("31"), istr("45"), istr("1395"), istr("135"), istr("45"), istr("45"), istr("0")]
1297
+ assert (
1298
+ istr.long_division(1395, 45, as_str=True)
1299
+ == """\
1300
+ 31
1301
+ ----
1302
+ 45 ) 1395
1303
+ 135
1304
+ ---
1305
+ 45
1306
+ 45
1307
+ --
1308
+ 0"""
1309
+ )
1310
+
1311
+
1242
1312
  if __name__ == "__main__":
1243
1313
  pytest.main(["-vv", "-s", "-x", __file__])
1244
1314
 
File without changes