From a6c2e3af77eb14934491385d5b7f49ee97076368 Mon Sep 17 00:00:00 2001 From: s45157 Date: Tue, 2 Jan 2018 02:45:14 +0100 Subject: [PATCH] Merge branch 'master' of https://git.wmi.amu.edu.pl/tdwojak/Python2017 # Conflicts: # labs02/test_task.py --- labs05/task00.py | 5 +++-- labs05/task01.py | 7 +++++++ labs05/task02.py | 12 ++++++++++++ labs05/task03.py | 15 +++++++++++++++ labs05/task04.py | 21 ++++++++++++++++++--- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/labs05/task00.py b/labs05/task00.py index ea96b5e..dd146ce 100644 --- a/labs05/task00.py +++ b/labs05/task00.py @@ -2,10 +2,11 @@ # -*- coding: utf-8 -*- def suma(liczby): - pass + assert len(liczby) > 0 + return reduce(lambda a, b: a+b, liczby) def main(): - print(summa([1, 2, 3, 4])) + print(suma([1, 2, 3, 4])) if __name__ == "__main__": main() 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__":