From ed251db11c3bcc8c6dc799af751990c82a6c32f2 Mon Sep 17 00:00:00 2001 From: Martin Mares <mj@ucw.cz> Date: Wed, 19 Feb 2025 12:13:33 +0100 Subject: [PATCH] =?UTF-8?q?Aritmetika:=20vylep=C5=A1en=C3=A1=20verze=20202?= =?UTF-8?q?5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01-aritmetika/aritmetika.py | 54 ++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/01-aritmetika/aritmetika.py b/01-aritmetika/aritmetika.py index f0fe53d..5a7755b 100755 --- a/01-aritmetika/aritmetika.py +++ b/01-aritmetika/aritmetika.py @@ -2,11 +2,14 @@ # Aritmetické algoritmy pracující po číslicích # Číslo je reprezentované seznamem číslic počínaje _nejnižším_ řádem -def nacti(): - return [ int(x) for x in reversed(input()) ] + +def z_retezce(s): + return [int(x) for x in reversed(s)] + def na_retezec(a): - return "".join([ str(x) for x in reversed(a) ]) + return "".join([str(x) for x in reversed(a)]) + def secti(a, b): if len(a) < len(b): @@ -29,6 +32,42 @@ def secti(a, b): return c + +def secti2(a, b): + n = max(len(a), len(b)) + a += [0] * (n - len(a)) + b += [0] * (n - len(b)) + + c = [] + prenos = 0 + + for i in range(n): + s = a[i] + b[i] + prenos + c.append(s % 10) + prenos = s // 10 + + if prenos > 0: + c.append(prenos) + + return c + + +def odecti(a, b): + n = max(len(a), len(b)) + a += [0] * (n - len(a)) + b += [0] * (n - len(b)) + + c = [] + prenos = 0 + + for i in range(n): + s = a[i] - b[i] + prenos + c.append(s % 10) + prenos = s // 10 + + return c + + def vynasob_cislici(a, cislice): c = [] prenos = 0 @@ -43,6 +82,7 @@ def vynasob_cislici(a, cislice): return c + def vynasob(a, b): c = [] @@ -52,8 +92,10 @@ def vynasob(a, b): return c + # Příklad použití: -a = nacti() -b = nacti() -print("Součet:", na_retezec(secti(a, b))) +a = z_retezce(input("a=")) +b = z_retezce(input("b=")) +print("Součet:", na_retezec(secti2(a, b))) +print("Rozdíl:", na_retezec(odecti(a, b))) print("Součin:", na_retezec(vynasob(a, b))) -- GitLab