From b8fb26a94507018c5429afe3af3b04c1eea00490 Mon Sep 17 00:00:00 2001 From: Marcus Vinicius Date: Wed, 9 Jul 2025 14:10:57 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20Adiciona=20solu=C3=A7=C3=A3o=20TDD=20pa?= =?UTF-8?q?ra=20problema=208=20(atoi)=20na=20liguagem=20python?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__pycache__/solution.cpython-312.pyc | Bin 0 -> 1163 bytes ...test_solution.cpython-312-pytest-7.4.4.pyc | Bin 0 -> 4739 bytes Python/8_string_to_integer_atoi/solution.py | 28 ++++++++++++++++++ .../8_string_to_integer_atoi/test_solution.py | 19 ++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 Python/8_string_to_integer_atoi/__pycache__/solution.cpython-312.pyc create mode 100644 Python/8_string_to_integer_atoi/__pycache__/test_solution.cpython-312-pytest-7.4.4.pyc create mode 100644 Python/8_string_to_integer_atoi/solution.py create mode 100644 Python/8_string_to_integer_atoi/test_solution.py diff --git a/Python/8_string_to_integer_atoi/__pycache__/solution.cpython-312.pyc b/Python/8_string_to_integer_atoi/__pycache__/solution.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..39837a4ebcc55fcdbb1edc8e8e8c43e70d0d01c5 GIT binary patch literal 1163 zcmZuwO-vI}5T3WcEp36)B7X@11hg?&5fU|uhR}!vAregdxik%Lflb?8-Q617Y-3|Q z6y&hTf%Zyt>&6EA=s);$qD_0|MVz0|j*EtNP)-^{%EdEYlX{qFHJ0FmFD z((*ML;E(N8g{c_(eqw9_1_3Y<&?1AWXUu&nNe37UsTQXQT0)T)P_>R|yOgwp{kPq(xrorI!FV|L{Agnrz6a!JOF7_%W)pd zL2EwDrrBEjL@mxe0;Q?u@#T&bcG$Vqt{A1cTBU|-y%(s*RJ4;IU~xlPFG?H&ozvMA z3os9w&9W@j{#9N_5$vqqrO2?;JgKI!D@AxlLjbC$&bPTX>O6Ma5yh#qm_0l-`G{aJR;HM6_Q@1sHb7f6nZHGo(U*t&9u)F70i~TBMaPx6j1w{8s?tqddy7>2zCa^{3QRZZ P$f^Y(%1{q%Rj&LCUcfB% literal 0 HcmV?d00001 diff --git a/Python/8_string_to_integer_atoi/__pycache__/test_solution.cpython-312-pytest-7.4.4.pyc b/Python/8_string_to_integer_atoi/__pycache__/test_solution.cpython-312-pytest-7.4.4.pyc new file mode 100644 index 0000000000000000000000000000000000000000..640aceb0086218c2722f949340e99dda5905c1d3 GIT binary patch literal 4739 zcmeHLTW=dh6rNeH*Ow$Mg(4^|#WY;9w29-yO&o~;6+%!6qDt@p5z=Vu-Nak=V$H0Z z+SnpO{SENKAF%QV_zQ@a3W0)FNPXY|^$j%=4|yP*Gkfu-4Kzs211O{QnKQHJ%*@Wt zch0x|eqy3bpnUt4*ZPwZ@+&ryqQuJ1d!T$xOkz@ptWsGkZmy|N;;O58igy%8a|^2l z&~vxRYSC0dmdrfJvZ;X_GYcTcyHqdU!(mr+8W!9xGXmdk#`DOk05S)^ogZL1Fex37 z0JNy|$yTfnsHqH;KJ6=?O%XE4^>gC80R3dSp(fg>Ho<0%nkxFNDO<6HnGbMAS#A^) zZB%c?>P;ffWX6}9HnKu3NSnzTdDde*y`3r#dw3MqGkS^xd`3I)2s@gr;t_UAVrOdK zs67q#QZ)-^v9H=xw(`NrWWkw{o=Eyebw8hsl+<6sQMm>04}KJWxq;FpdTAw8>T~yA z1IHoVRhE~Bv^V?mRJ&W(MfvkssVrAeo7Ke>)^obh%Y({t)Vrt)j(apz!yMS4m_t!V zvABD}5W?bt9AHTo*ts+;NsqAnR=XQmB47p&PqC)qIE-6u-t^tJ!L4u{ZnNth+X-w> zgqq`ng$SQzqUCoSQ<}1M$G>hk%rjgoEHPoMSvW#?;)+^)+Y799i%Sqz<-Um6e5TzE&qh7A+jcD0 z_JtkTH!bFM-0K$S7hqli6iG?L!!L&k56T#Z2HMZbAffiv zLj(ym4idTJAdzf(|KT8!mq8*Q2MO)aK|)J{LsCBwBvc3#${;5LMNfmHJzbyUc+5Gz zd7buZhmIA~Xqf~HjuE?eG7}qW5M4sG-L1{?Q#f>ipT%j;p?Drd7K9CcKJAIXx3mw5 zHG!*(kXYd_qOC5`(=t5Mu>{*_+0bqqO^ZPf^Q?7<2!JMGuX|hvd-NBdflTbdiDx7Ejg{lj6N0_GY~ zM<9tbLmyL81k_RKlaxCCk)bv*F@*J}C8dtw^B>L%>CR5s$KVWcd`dn60=ekIK7!ha2x=orK^m{3=_n69m~|HUt6=aqplmvZ z3#bHM=Nh+=+c5yAf79Y?j=#Y#gCnj%v_Wi=pHDoq`R-ly;`Y&Bchrl=ScOd|*^~XK z&%9%-!hXfva35Bod$Tt-oq}~KN@X+9AARFR=)1bYXR&Jz#XJg0@-)7P`hJA*I`$oi ztVx;fGRU&M1X&%=jG@&4cN{&1)@g*+rR}4??x>d}wDgIvn0&YL)3B8A42!;WXClT6 zUw|{*+Tg%K!&2H67UDksB>EJSufK>B;3uBsrZS<+0K)N6heXaP`M_@Rb=NmLj`aqA t6K>(3GvXBxzvn2WzY%hq{6x;(S1EnrzDDUwC}!?!3ca*bROqxE=P# int: + if not s: + return 0 + + i, n = 0, len(s) + INT_MAX = 2**31 - 1 + INT_MIN = -2**31 + + while i < n and s[i] == ' ': + i += 1 + + sign = 1 + if i < n and s[i] in ['+', '-']: + if s[i] == '-': + sign = -1 + i += 1 + + result = 0 + while i < n and '0' <= s[i] <= '9': + digit = ord(s[i]) - ord('0') + + if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 7): + return INT_MAX if sign == 1 else INT_MIN + + result = result * 10 + digit + i += 1 + + return result * sign \ No newline at end of file diff --git a/Python/8_string_to_integer_atoi/test_solution.py b/Python/8_string_to_integer_atoi/test_solution.py new file mode 100644 index 00000000..bce2be43 --- /dev/null +++ b/Python/8_string_to_integer_atoi/test_solution.py @@ -0,0 +1,19 @@ +# test_solution.py +from solution import my_atoi + +def test_simple_positive_number(): + assert my_atoi("42") == 42 + +def test_handles_whitespace_and_negative_sign(): + assert my_atoi(" -42") == -42 + +def test_stops_at_non_digit_characters(): + assert my_atoi("4193 with words") == 4193 + +def test_clamps_to_max_int_on_overflow(): + INT_MAX = 2**31 - 1 + assert my_atoi("2147483648") == INT_MAX + +def test_clamps_to_min_int_on_underflow(): + INT_MIN = -2**31 + assert my_atoi("-2147483649") == INT_MIN \ No newline at end of file