From def69182d9f7444f21ca9850c47bee9ee448c9d1 Mon Sep 17 00:00:00 2001 From: s45167 Date: Fri, 26 Jan 2018 14:43:35 +0100 Subject: [PATCH] done --- labs05/task00.py | 4 +++- labs05/task01.py | 7 +++++++ labs05/task02.py | 12 ++++++++++++ labs05/task03.py | 15 +++++++++++++++ labs05/task04.py | 21 ++++++++++++++++++--- 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/labs05/task00.py b/labs05/task00.py index 2703c2b..dac156e 100644 --- a/labs05/task00.py +++ b/labs05/task00.py @@ -2,7 +2,9 @@ # -*- coding: utf-8 -*- def suma(liczby): - pass + assert len(liczby) > 0 + a=reduce(lambda a, b: a+b, liczby) + return a def main(): print(suma([1, 2, 3, 4])) diff --git a/labs05/task01.py b/labs05/task01.py index e69de29..0a79a36 100644 --- a/labs05/task01.py +++ b/labs05/task01.py @@ -0,0 +1,7 @@ +import task00, sys + +def main(): + print('%f' % (task00.suma(map(lambda x: float(x), sys.argv[1:])))) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/labs05/task02.py b/labs05/task02.py index e69de29..c16e445 100644 --- a/labs05/task02.py +++ b/labs05/task02.py @@ -0,0 +1,12 @@ +import task00, sys + +def main(): + def a_to_f(a): + try: + return float(a) + except: + return 0 + print('%f' % (task00.suma(map(a_to_f, sys.argv[1:])))) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/labs05/task03.py b/labs05/task03.py index e69de29..9c9af4a 100644 --- a/labs05/task03.py +++ b/labs05/task03.py @@ -0,0 +1,15 @@ +import task00, argparse + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("numbers", nargs="+") + args = parser.parse_args() + def a_to_f(a): + try: + return float(a) + except: + return 0 + print('%f' % (task00.suma(map(a_to_f, args.numbers)))) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/labs05/task04.py b/labs05/task04.py index 4dfc78c..4dc1af5 100644 --- a/labs05/task04.py +++ b/labs05/task04.py @@ -6,7 +6,7 @@ Implementacja narzedzia ``wc`` z linuksa (word counter). Zwraca liczbę słów, znaków i linii. """ -import sys +import sys, argparse def count_lines(text): @@ -19,7 +19,7 @@ def count_words(text): for line in text.split('\n')]) def count_chars(text): - """ return number of words. """ + """ return number of characters. """ return len(text) def wc(text): @@ -32,7 +32,22 @@ def wc(text): def main(): """ main """ - print(wc(sys.stdin.read())) + parser = argparse.ArgumentParser() + parser.add_argument('-l', help="number of lines", action="store_true") + parser.add_argument('-w', help="number of words", action="store_true") + parser.add_argument('-c', help="number of characters", action="store_true") + parser.add_argument('fn', nargs='?', type=file, default=sys.stdin, help='optional filename to read from') + args = parser.parse_args() + lines, words, chars = wc(args.fn.read()) + if( args.l or args.w or args.c ): + if(args.l): + print(lines) + if(args.w): + print(words) + if(args.c): + print(chars) + else: + print(lines, words, chars) if __name__ == "__main__":