Skip to content

Commit ebf87f6

Browse files
committed
isPowerOfTen now takes decimals.
1 parent 732f617 commit ebf87f6

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
2020
# SOFTWARE. #
2121
#####################################################################################################
22+
23+
pass

lib/config.py

Whitespace-only changes.

lib/printing.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#####################################################################################################
2222

2323
import sys
24+
import os
2425

2526
from lib.constants import WINDOWS
2627

@@ -79,3 +80,34 @@ def bold() -> str:
7980
if sys.stdout.isatty():
8081
return "\033[1m"
8182
return ""
83+
84+
85+
def print_progressbar(
86+
iteration: int,
87+
total: int,
88+
prefix: str = "",
89+
suffix: str = "",
90+
decimals: int = 1,
91+
length: int = 100,
92+
fill: str = "\u2588",
93+
) -> None:
94+
"""
95+
Call in a loop to create terminal progress bar.
96+
:param iteration: Current iteration.
97+
:param total: Total iterations.
98+
:param prefix: Prefix string.
99+
:param suffix: Suffix string.
100+
:param decimals: Positive number of decimals in percent complete.
101+
:param length: Character length of bar.
102+
:param fill: Bar fill character.
103+
"""
104+
if not os.isatty(sys.stdout.fileno()):
105+
print(f"{prefix} {iteration}/{total} {suffix}")
106+
return
107+
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
108+
filled_length = int(length * iteration // total)
109+
bar = fill * filled_length + " " * (length - filled_length)
110+
print("\r%s |%s| %s%% %s" % (prefix, bar, percent, suffix), end="")
111+
# Print New Line on Complete
112+
if iteration == total:
113+
print()

src/multiply/multiply.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ namespace steppable::__internals::arithmetic
8282
{
8383
if (steps == 2)
8484
out << "Since " << a << " is a power of 10, we can move the decimal places to obtain the result.\n";
85-
out << moveDecimalPlaces(b, static_cast<long long>(aInteger.length() - 1));
85+
out << moveDecimalPlaces(b, determineScale(a));
8686
return out.str();
8787
}
8888
if (isPowerOfTen(b))
8989
{
9090
if (steps == 2)
9191
out << "Since " << b << " is a power of 10, we can move the decimal places to obtain the result.\n";
92-
out << moveDecimalPlaces(a, static_cast<long long>(bInteger.length() - 1));
92+
out << moveDecimalPlaces(a, determineScale(b));
9393
return out.str();
9494
}
9595

src/util.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,21 @@ namespace steppable::__internals::numUtils
257257

258258
bool isDecimal(const std::string& number) { return not isInteger(number); }
259259

260-
bool isPowerOfTen(const std::string& number)
260+
bool isPowerOfTen(const std::string& _number)
261261
{
262+
auto number = _number;
263+
if (isDecimal(number))
264+
{
265+
number = number.substr(0, number.length() - 1);
266+
return not std::ranges::any_of(number, [](const auto& c) { return c != '0' and c != '.'; });
267+
}
262268
if (number == "1")
263269
return true; // 1 is a power of 10.
264270
if (number.front() != '1')
265271
return false; // The number must start with 1.
266272

267-
// NOLINTNEXTLINE(readability-use-anyofallof)
268-
for (const char c : number.substr(1, number.length()))
269-
if (c != '0' and c != '.')
270-
return false; // The rest of the number must be zeros or decimal points.
271-
return true;
273+
number = number.substr(1, number.length() - 1);
274+
return not std::ranges::any_of(number, [](const auto& c) { return c != '0' and c != '.'; });
272275
}
273276
} // namespace steppable::__internals::numUtils
274277

tests/testUtil.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,11 @@ _.assertIsEqual(roundDown(number2), "1");
183183
_.assertIsEqual(roundUp(number2), "2");
184184
SECTION_END()
185185

186+
SECTION(Test isPowerOfTen)
187+
_.assertTrue(isPowerOfTen("10"));
188+
_.assertTrue(isPowerOfTen("0.1"));
189+
_.assertTrue(isPowerOfTen("0.01"));
190+
_.assertFalse(isPowerOfTen("20"));
191+
SECTION_END()
192+
186193
TEST_END()

0 commit comments

Comments
 (0)