From a5f73d423a358c35ec557587fc0f61098d325347 Mon Sep 17 00:00:00 2001 From: Kuba Date: Mon, 14 May 2018 22:30:09 +0200 Subject: [PATCH] labs02 first part of tasks --- labs02/task01.py | 7 +++++-- labs02/task02.py | 18 +++++++++++++++++- labs02/task03.py | 5 ++--- labs02/task04.py | 5 ++++- labs02/task05.py | 7 +++++-- labs02/task06.py | 5 ++++- labs02/task07.py | 2 +- labs02/task08.py | 2 +- labs02/task11.py | 2 +- 9 files changed, 40 insertions(+), 13 deletions(-) diff --git a/labs02/task01.py b/labs02/task01.py index 7c08c56..74a8048 100644 --- a/labs02/task01.py +++ b/labs02/task01.py @@ -7,8 +7,11 @@ która zawiera tylko elementy z list o parzystych indeksach. """ def even_elements(lista): - pass - + result = [] + for i in range(len(lista)): + if i % 2 == 0: + result.append(lista[i]) + return result def tests(f): inputs = [[[1, 2, 3, 4, 5, 6]], [[]], [[41]]] diff --git a/labs02/task02.py b/labs02/task02.py index a6d6321..69d4d65 100644 --- a/labs02/task02.py +++ b/labs02/task02.py @@ -6,7 +6,23 @@ """ def days_in_year(days): - pass + if days % 4 == 0: + if days % 100 == 0: + if days % 400 == 0: + return 366 + else: + return 365 + else: + return 366 + else: + return 365 + + # To determine whether a year is a leap year, follow these steps: + # If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5. + # If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4. + # If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5. + # The year is a leap year (it has 366 days). + # The year is not a leap year (it has 365 def tests(f): inputs = [[2015], [2012], [1900], [2400], [1977]] diff --git a/labs02/task03.py b/labs02/task03.py index a1c3a85..a1ee98d 100644 --- a/labs02/task03.py +++ b/labs02/task03.py @@ -13,9 +13,8 @@ jak 'set', która przechowuje elementy bez powtórzeń.) def oov(text, vocab): - pass - - + words = text.lower().split() + return (set(words) - set(vocab)) def tests(f): inputs = [("this is a string , which i will use for string testing", diff --git a/labs02/task04.py b/labs02/task04.py index 37413f1..134f9cc 100644 --- a/labs02/task04.py +++ b/labs02/task04.py @@ -7,7 +7,10 @@ Jeśli podany argument jest mniejszy od 1 powinna być zwracana wartość 0. """ def sum_from_one_to_n(n): - pass + if n < 1: + return 0 + else: + return sum(i for i in range(1, n+1)) def tests(f): diff --git a/labs02/task05.py b/labs02/task05.py index f59268a..77faf29 100644 --- a/labs02/task05.py +++ b/labs02/task05.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +import math as m """ Napisz funkcję euclidean_distance obliczającą odległość między @@ -10,7 +10,10 @@ np. odległość pomiędzy punktami (0, 0, 0) i (3, 4, 0) jest równa 5. """ def euclidean_distance(x, y): - pass + dist = 0 + for i, j in zip(x, y): + dist += (i-j) ** 2 + return m.sqrt(dist) def tests(f): inputs = [[(2.3, 4.3, -7.5), (2.3, 8.5, -7.5)]] diff --git a/labs02/task06.py b/labs02/task06.py index ff4a9d3..09c0861 100644 --- a/labs02/task06.py +++ b/labs02/task06.py @@ -10,7 +10,10 @@ ma być zwracany napis "It's not a Big 'No!'". """ def big_no(n): - pass + if n < 5: + return "It's not a Big \'No!\'" + else: + return "N" + n * "O" + "!" def tests(f): inputs = [[5], [6], [2]] diff --git a/labs02/task07.py b/labs02/task07.py index 80cbd37..42f719f 100644 --- a/labs02/task07.py +++ b/labs02/task07.py @@ -6,7 +6,7 @@ Napisz funkcję char_sum, która dla zadanego łańcucha zwraca sumę kodów ASCII znaków. """ def char_sum(text): - pass + return sum(ord(x) for x in text) def tests(f): inputs = [["this is a string"], ["this is another string"]] diff --git a/labs02/task08.py b/labs02/task08.py index 252b10d..5061dce 100644 --- a/labs02/task08.py +++ b/labs02/task08.py @@ -7,7 +7,7 @@ przez 3 lub 5 mniejszych niż n. """ def sum_div35(n): - pass + return sum(x for x in range(n) if (x % 3 == 0 or x % 5 == 0)) def tests(f): inputs = [[10], [100], [3845]] diff --git a/labs02/task11.py b/labs02/task11.py index 7d36767..d8a8488 100644 --- a/labs02/task11.py +++ b/labs02/task11.py @@ -9,7 +9,7 @@ Oba napisy będą składać się wyłacznie z małych liter. """ def common_chars(string1, string2): - pass + return sorted(set(string1.replace(' ', '')) & set(string2.replace(' ', ''))) def tests(f):