diff --git a/exercises/002-sum_of_three_numbers/app.py b/exercises/002-sum_of_three_numbers/app.py index f6422558..ee0bb4b6 100644 --- a/exercises/002-sum_of_three_numbers/app.py +++ b/exercises/002-sum_of_three_numbers/app.py @@ -1,7 +1,7 @@ # Sum all three input numbers and print on the console the result -first_number = input("First input") -second_number = input("Second input") -third_number = input("Third input") +first_number = int(input("First input")) +second_number = int(input("Second input")) +third_number = int(input("Third input")) # print here the sum of three inputs print(first_number+second_number) \ No newline at end of file diff --git a/exercises/002-sum_of_three_numbers/test.py b/exercises/002-sum_of_three_numbers/test.py index 2ce3d4f9..9d7850e7 100644 --- a/exercises/002-sum_of_three_numbers/test.py +++ b/exercises/002-sum_of_three_numbers/test.py @@ -8,23 +8,32 @@ def test_use_print(): regex = re.compile(r"print(\s)*\(") assert bool(regex.search(content)) == True -@pytest.mark.it('Almost there! But you need to convert the incoming input from string to integer') -def test_convert_inputs(capsys, app): +@pytest.mark.it('The solution should return the expected output. Tested with 2, 3, 6') +def test_sum_expected_inputs(capsys, app): - fake_input = ["2","3","4"] #fake input + fake_input = ["2","3","6"] #fake input with mock.patch('builtins.input', lambda x: fake_input.pop()): app() captured = capsys.readouterr() - assert captured.out != "432\n" + assert captured.out == "11\n" -@pytest.mark.it('Sum all three input numbers and print on the console the result') -def test_add_variables(capsys, app): +@pytest.mark.it('The solution must sum all entries. Tested with 0, 3, 7') +def test_sum_another_inputs(capsys, app): - fake_input = ["2","3","4"] #fake input + fake_input = ["0","3","7"] #fake input with mock.patch('builtins.input', lambda x: fake_input.pop()): app() captured = capsys.readouterr() - assert captured.out == "9\n" + assert captured.out == "10\n" + +@pytest.mark.it('The solution must sum all entries. Tested with 0, 0, 0') +def test_sum_with_zero(capsys, app): + + fake_input = ["0","0","0"] #fake input + with mock.patch('builtins.input', lambda x: fake_input.pop()): + app() + captured = capsys.readouterr() + assert captured.out == "0\n" diff --git a/exercises/003-area_of_right_triangle/app.py b/exercises/003-area_of_right_triangle/app.py index 1888dc8b..db54ac26 100644 --- a/exercises/003-area_of_right_triangle/app.py +++ b/exercises/003-area_of_right_triangle/app.py @@ -1,10 +1,7 @@ #Complete the function to return the area of the triangle. -b = input("Size of B") -h = input("Size of H") - def area_of_triangle(arg1, arg2): #your code here, please remove the "None" return None # Testing your function -print(area_of_triangle(b, h)) \ No newline at end of file +print(area_of_triangle(3, 5)) diff --git a/exercises/003-area_of_right_triangle/test.py b/exercises/003-area_of_right_triangle/test.py index c8b04183..2bd1909d 100644 --- a/exercises/003-area_of_right_triangle/test.py +++ b/exercises/003-area_of_right_triangle/test.py @@ -1,17 +1,21 @@ import io, sys, os, pytest, json, mock path = os.path.dirname(os.path.abspath(__file__))+'/app.py' -@pytest.mark.it('Calculate the area of the triangle.') -def test_area_of_triangle(stdin): - _stdin = json.loads(stdin) - with mock.patch('builtins.input', lambda x: _stdin.pop(0)): - - sys.stdout = buffer = io.StringIO() +@pytest.mark.it('The function area_of_triangle should exist') +def test_function_exists(capsys): + try: import app - _stdin = json.loads(stdin) - result = int(_stdin[0]) * int(_stdin[1]) / 2 - assert buffer.getvalue() == str(result) + "\n" - + app.area_of_triangle + except AttributeError: + raise AttributeError("The function 'area_of_triangle' should exist on app.py") +@pytest.mark.it('The solution should return the expected output. Testing with 3 and 5') +def test_convert_inputs(capsys, app): + result = app.area_of_triangle(3, 5) + assert result == 7.5 +@pytest.mark.it('The solution should return the expected output. Testing with 4 and 6') +def test_convert_inputs(capsys, app): + result = app.area_of_triangle(4, 6) + assert result == 12 diff --git a/exercises/004-hello_harry/app.py b/exercises/004-hello_harry/app.py index bd5c2d36..e2d36d22 100644 --- a/exercises/004-hello_harry/app.py +++ b/exercises/004-hello_harry/app.py @@ -3,7 +3,5 @@ def hello_name(name): return None - - #Invoke the function with your name as the function's argument. print(hello_name("Bob")) diff --git a/exercises/22-Integral/solution.hide.py b/exercises/22-Integral/solution.hide.py index 0a8baa3c..262f2213 100644 --- a/exercises/22-Integral/solution.hide.py +++ b/exercises/22-Integral/solution.hide.py @@ -1,6 +1,7 @@ -n=int(raw_input()) -d=dict() -for i in range(1,n+1): - d[i]=i*i +def generate_dict(n): + d=dict() + for i in range(1,n+1): + d[i]=i*i + return d -print d \ No newline at end of file +print(generate_dict(n)) \ No newline at end of file diff --git a/exercises/22-Integral/test.py b/exercises/22-Integral/test.py new file mode 100644 index 00000000..ee977613 --- /dev/null +++ b/exercises/22-Integral/test.py @@ -0,0 +1,19 @@ +import pytest,os,re,io,sys, mock, json + +@pytest.mark.it('The function generate_dict should exist') +def test_function_existence(capsys, app): + assert app.generate_dict + +@pytest.mark.it('The function should return a dictionary') +def test_typeof_return(capsys, app): + assert type(app.generate_dict(7)) == type({}) + +@pytest.mark.it('The function should return the expected output') +def test_expected_output(capsys, app): + assert app.generate_dict(8) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} + +@pytest.mark.it('The function should work with other entries') +def test_another_entry_5(capsys, app): + assert app.generate_dict(5) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25} + + diff --git a/exercises/23-list-and-tuple/solution.hide.py b/exercises/23-list-and-tuple/solution.hide.py index d9c1d42b..8ca57f24 100644 --- a/exercises/23-list-and-tuple/solution.hide.py +++ b/exercises/23-list-and-tuple/solution.hide.py @@ -1,5 +1,10 @@ -values=raw_input() +values=input("") l=values.split(",") t=tuple(l) -print l -print t \ No newline at end of file +print (l) +print (t) + +# +values=input("") +t=tuple(values.split(',')) +print(t) \ No newline at end of file diff --git a/exercises/23-list-and-tuple/test.py b/exercises/23-list-and-tuple/test.py new file mode 100644 index 00000000..aea10c4b --- /dev/null +++ b/exercises/23-list-and-tuple/test.py @@ -0,0 +1,20 @@ +import io, sys, os, pytest, json, mock +path = os.path.dirname(os.path.abspath(__file__))+'/app.py' + + +@pytest.mark.it('The output should be the expected for this solution') +def test_output(capsys, app): + fake_input = ["34,67,55,33,12,98"] #fake input + with mock.patch('builtins.input', lambda x: fake_input.pop()): + app() + captured = capsys.readouterr() + assert captured.out == "['34', '67', '55', '33', '12', '98']\n" or "('34', '67', '55', '33', '12', '98')\n" + +@pytest.mark.it('The solution should work with other parameters') +def test_output_2(capsys, app): + fake_input = ["11,56,23,12,02"] #fake input + with mock.patch('builtins.input', lambda x: fake_input.pop()): + app() + captured = capsys.readouterr() + assert captured.out == "['11', '56', '23', '12', '02']\n" or "('11', '56', '23', '12', '02')\n" + diff --git a/exercises/25-print-formula/solution.hide.py b/exercises/25-print-formula/solution.hide.py index 3c6ec870..10c47e5b 100644 --- a/exercises/25-print-formula/solution.hide.py +++ b/exercises/25-print-formula/solution.hide.py @@ -1,10 +1,5 @@ -#!/usr/bin/env python import math -c=50 -h=30 -value = [] -items=[x for x in raw_input().split(',')] -for d in items: - value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) +def print_formula(num): + return round(math.sqrt(2*50*num/30)) -print ','.join(value) \ No newline at end of file +print(print_formula(5)) \ No newline at end of file diff --git a/exercises/25-print-formula/test.py b/exercises/25-print-formula/test.py new file mode 100644 index 00000000..27181781 --- /dev/null +++ b/exercises/25-print-formula/test.py @@ -0,0 +1,15 @@ +import pytest, io, sys, json, mock, re, os +path = os.path.dirname(os.path.abspath(__file__))+'/app.py' + +@pytest.mark.it('The function print_formula must exist') +def test_function_existence(capsys, app): + assert app.print_formula + +@pytest.mark.it('The solution should work as expected') +def test_expected_output(capsys, app): + assert app.print_formula(100,150,180) == "18,22,24" + +@pytest.mark.it('The solution should work as expected') +def test_another_output(capsys, app): + assert app.print_formula(200,90,300) == "26,17,32" + diff --git a/exercises/26-two-dimensional-array/solution.hide.py b/exercises/26-two-dimensional-array/solution.hide.py index cd67031f..c1279fd2 100644 --- a/exercises/26-two-dimensional-array/solution.hide.py +++ b/exercises/26-two-dimensional-array/solution.hide.py @@ -1,11 +1,11 @@ -input_str = raw_input() -dimensions=[int(x) for x in input_str.split(',')] -rowNum=dimensions[0] -colNum=dimensions[1] -multilist = [[0 for col in range(colNum)] for row in range(rowNum)] +def two_dimensional_array(nList, nElements): + dimensions=[int(x) for x in "{},{}".format(nList,nElements).split(',')] + rowNum=dimensions[0] + colNum=dimensions[1] + multilist = [[0 for col in range(colNum)] for row in range(rowNum)] -for row in range(rowNum): - for col in range(colNum): - multilist[row][col]= row*col + for row in range(rowNum): + for col in range(colNum): + multilist[row][col]= row*col -print multilist \ No newline at end of file + return (multilist) diff --git a/exercises/26-two-dimensional-array/test.py b/exercises/26-two-dimensional-array/test.py new file mode 100644 index 00000000..5cd0300c --- /dev/null +++ b/exercises/26-two-dimensional-array/test.py @@ -0,0 +1,18 @@ + +import pytest,os,re,io,sys, mock, json + +@pytest.mark.it('The function two_dimensional_array must exist') +def test_function_existence(capsys, app): + assert app.two_dimensional_array + +@pytest.mark.it('The function should return the expected output.') +def test_expected_output(capsys, app): + assert app.two_dimensional_array(3,5) == [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] + +@pytest.mark.it('The function should work with other entries. Testing with 2,7') +def test_expected_output(capsys, app): + assert app.two_dimensional_array(2,7) == [[0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6]] + +@pytest.mark.it('The function should work with other entries. Testing with 2,7') +def test_expected_output(capsys, app): + assert app.two_dimensional_array(1,10) == [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] diff --git a/exercises/27-sequence-of-words/solution.hide.py b/exercises/27-sequence-of-words/solution.hide.py index 032a109d..75bddc17 100644 --- a/exercises/27-sequence-of-words/solution.hide.py +++ b/exercises/27-sequence-of-words/solution.hide.py @@ -1,3 +1,4 @@ -items=[x for x in raw_input().split(',')] -items.sort() -print ','.join(items) \ No newline at end of file +def sequence_of_words(words): + items=[x for x in "{}".format(words).split(',')] + items.sort() + return (','.join(items)) diff --git a/exercises/27-sequence-of-words/test.py b/exercises/27-sequence-of-words/test.py new file mode 100644 index 00000000..b48ffb1d --- /dev/null +++ b/exercises/27-sequence-of-words/test.py @@ -0,0 +1,14 @@ +import pytest, io, sys, json, mock, re, os +path = os.path.dirname(os.path.abspath(__file__))+'/app.py' + +@pytest.mark.it('The function sequence_of_words must exist') +def test_function_existence(capsys, app): + assert app.sequence_of_words + +@pytest.mark.it('The function should return the expected output') +def test_function_existence(capsys, app): + assert app.sequence_of_words("bag,hello,without,world") == "bag,hello,without,world" + +@pytest.mark.it('The function should return the expected output') +def test_function_existence(capsys, app): + assert app.sequence_of_words("No,pain,gain") == "No,gain,pain" \ No newline at end of file diff --git a/exercises/28-sequence-of-lines/solution.hide.py b/exercises/28-sequence-of-lines/solution.hide.py index 36504a9d..839fa111 100644 --- a/exercises/28-sequence-of-lines/solution.hide.py +++ b/exercises/28-sequence-of-lines/solution.hide.py @@ -1,10 +1,4 @@ -lines = [] -while True: - s = raw_input() - if s: - lines.append(s.upper()) - else: - break; +def lines(text): + return text.upper() -for sentence in lines: - print sentence \ No newline at end of file +print(lines('Hello world')) \ No newline at end of file diff --git a/exercises/28-sequence-of-lines/test.py b/exercises/28-sequence-of-lines/test.py new file mode 100644 index 00000000..25a9c103 --- /dev/null +++ b/exercises/28-sequence-of-lines/test.py @@ -0,0 +1,14 @@ +import pytest, io, sys, json, mock, re, os +path = os.path.dirname(os.path.abspath(__file__))+'/app.py' + +@pytest.mark.it('The function lines must exist') +def test_function_existence(capsys, app): + assert app.lines + +@pytest.mark.it('The function should return the expected output') +def test_expected_output(capsys, app): + assert app.lines("hello world") == "HELLO WORLD" + +@pytest.mark.it('The function should return the expected output') +def test_another_output(capsys, app): + assert app.lines("LeT the WOrld know YoU") == "LET THE WORLD KNOW YOU" diff --git a/exercises/29-remove-duplicate-words/solution.hide.py b/exercises/29-remove-duplicate-words/solution.hide.py index 4398984a..5bf7df2d 100644 --- a/exercises/29-remove-duplicate-words/solution.hide.py +++ b/exercises/29-remove-duplicate-words/solution.hide.py @@ -1,3 +1,3 @@ -s = input() -words = [word for word in s.split(" ")] -print (" ".join(sorted(list(set(words))))) \ No newline at end of file +def remove_duplicate_words(text): + words = text.split() + return (" ".join(sorted(list(set(words))))) diff --git a/exercises/29-remove-duplicate-words/test.py b/exercises/29-remove-duplicate-words/test.py index feb58b37..ea930f4d 100644 --- a/exercises/29-remove-duplicate-words/test.py +++ b/exercises/29-remove-duplicate-words/test.py @@ -1,9 +1,18 @@ import pytest, io, sys, json, mock, re, os -@pytest.mark.it('Your solution should work as expected') +@pytest.mark.it('The function remove_duplicate_words must exist') +def test_function_existence(capsys, app): + app.remove_duplicate_words + +@pytest.mark.it('The function should return the expected output') def test_expected_output(capsys, app): - fake_input=['Hola como Hola'] - with mock.patch('builtins.input', lambda x: fake_input.pop()): - app() - captured = capsys.readouterr() - assert captured.out == " como \n" \ No newline at end of file + assert app.remove_duplicate_words("hello world and practice makes perfect and hello world again") == "again and hello makes perfect practice world" + +@pytest.mark.it('The function should work with other entries') +def test_expected_output_2(capsys, app): + assert app.remove_duplicate_words("lets try this again with another try") == "again another lets this try with" + +@pytest.mark.it('The function should work with other entries') +def test_expected_output_3(capsys, app): + assert app.remove_duplicate_words("Jacke was Named Jacke by his mother") == "Jacke Named by his mother was" + diff --git a/exercises/30-divisable-binary/solution.hide.py b/exercises/30-divisable-binary/solution.hide.py index febe2835..e0656be2 100644 --- a/exercises/30-divisable-binary/solution.hide.py +++ b/exercises/30-divisable-binary/solution.hide.py @@ -1,8 +1,9 @@ -value = [] -items=[x for x in raw_input().split(',')] -for p in items: - intp = int(p, 2) - if not intp%5: - value.append(p) +def divisable_binary(text): + value=[] + items=[x for x in text.split(',')] + for p in items: + intp = int(p, 2) + if not intp%5: + value.append(p) -print ','.join(value) \ No newline at end of file + return (','.join(value)) \ No newline at end of file diff --git a/exercises/30-divisable-binary/test.py b/exercises/30-divisable-binary/test.py new file mode 100644 index 00000000..7c9bdc88 --- /dev/null +++ b/exercises/30-divisable-binary/test.py @@ -0,0 +1,17 @@ +import pytest, io, sys, json, mock, re, os + +@pytest.mark.it('The function divisable_binary must exist') +def test_function_existence(capsys, app): + assert app.divisable_binary + +@pytest.mark.it('The function should return the expected output') +def test_expected_output(capsys, app): + assert app.divisable_binary("0100,0011,1010,1001") == "1010" + +@pytest.mark.it('The function should work with other parameters. testing with 1111,1000,0101,0000') +def test_expected_output_2(capsys, app): + assert app.divisable_binary("1111,1000,0101,0000") == "1111,0101,0000" + +@pytest.mark.it("The function should work with other parameters. Testing with 1000") +def test_expected_output_3(capsys, app): + assert app.divisable_binary("1000,1000,1000,1000") == "" \ No newline at end of file diff --git a/exercises/32-numbers-of-letters-and-digits/solution.hide.py b/exercises/32-numbers-of-letters-and-digits/solution.hide.py index 4322bb8a..0c3044a4 100644 --- a/exercises/32-numbers-of-letters-and-digits/solution.hide.py +++ b/exercises/32-numbers-of-letters-and-digits/solution.hide.py @@ -1,11 +1,11 @@ -s = raw_input() -d={"DIGITS":0, "LETTERS":0} -for c in s: - if c.isdigit(): - d["DIGITS"]+=1 - elif c.isalpha(): - d["LETTERS"]+=1 - else: - pass -print "LETTERS", d["LETTERS"] -print "DIGITS", d["DIGITS"] \ No newline at end of file +def letters_and_digits(text): + d={"DIGITS":0, "LETTERS":0} + for c in text: + if c.isdigit(): + d["DIGITS"]+=1 + elif c.isalpha(): + d["LETTERS"]+=1 + else: + pass + + return ("LETTERS {} DIGITS {}".format(d['LETTERS'], d['DIGITS'])) \ No newline at end of file diff --git a/exercises/32-numbers-of-letters-and-digits/test.py b/exercises/32-numbers-of-letters-and-digits/test.py new file mode 100644 index 00000000..f941727f --- /dev/null +++ b/exercises/32-numbers-of-letters-and-digits/test.py @@ -0,0 +1,15 @@ +import mock, pytest, io, sys + + +@pytest.mark.it('The function letters_and_digits must exist') +def test_function_existence(capsys, app): + assert app.letters_and_digits + + +@pytest.mark.it('The function should return the expected output') +def test_output(capsys, app): + app.letters_and_digits("hello world! 123") == "LETTERS 10 DIGITS 3" + +@pytest.mark.it('The solution should work with other parameters') +def test_another_entry(capsys, app): + assert app.letters_and_digits("My name is C200 and i live in apartment 3H") == "LETTERS 29 DIGITS 4" \ No newline at end of file diff --git a/exercises/34-a_aa_aaa_aaaa/solution.hide.py b/exercises/34-a_aa_aaa_aaaa/solution.hide.py index 76b64b97..9db3c7d2 100644 --- a/exercises/34-a_aa_aaa_aaaa/solution.hide.py +++ b/exercises/34-a_aa_aaa_aaaa/solution.hide.py @@ -1,6 +1,6 @@ -a = raw_input() -n1 = int( "%s" % a ) -n2 = int( "%s%s" % (a,a) ) -n3 = int( "%s%s%s" % (a,a,a) ) -n4 = int( "%s%s%s%s" % (a,a,a,a) ) -print n1+n2+n3+n4 \ No newline at end of file +def computed_value(param): + n1 = int( "%s" % param ) + n2 = int( "%s%s" % (param,param) ) + n3 = int( "%s%s%s" % (param,param,param) ) + n4 = int( "%s%s%s%s" % (param,param,param,param) ) + return (n1+n2+n3+n4) \ No newline at end of file diff --git a/exercises/34-a_aa_aaa_aaaa/test.py b/exercises/34-a_aa_aaa_aaaa/test.py new file mode 100644 index 00000000..0df320d5 --- /dev/null +++ b/exercises/34-a_aa_aaa_aaaa/test.py @@ -0,0 +1,17 @@ +import io, sys, pytest, os, re, mock + +@pytest.mark.it('The function computed_value must exist') +def test_function_existence(capsys, app): + assert app.computed_value + +@pytest.mark.it('The function should return the expected output') +def test_expected_output(capsys, app): + assert app.computed_value(9) == 11106 + +@pytest.mark.it('The function should work with other enties. Testing with 123') +def test_another_output(capsys, app): + assert app.computed_value(123) == 123246369492 + +@pytest.mark.it('The function should work with other enties. Testing with 0') +def test_with_zero(capsys, app): + assert app.computed_value(0) == 0 diff --git a/exercises/36-net-amount/solution.hide.py b/exercises/36-net-amount/solution.hide.py index e6be199d..a6a55ceb 100644 --- a/exercises/36-net-amount/solution.hide.py +++ b/exercises/36-net-amount/solution.hide.py @@ -1,15 +1,9 @@ -netAmount = 0 -while True: - s = raw_input() - if not s: - break - values = s.split(" ") - operation = values[0] - amount = int(values[1]) - if operation=="D": - netAmount+=amount - elif operation=="W": - netAmount-=amount - else: - pass -print netAmount \ No newline at end of file +def net_amount(param): + netAmount = 0 + values = param.split() + for x in range(len(values)): + if values[x] == 'D': + netAmount+=int(values[x+1]) + elif values[x] == 'W': + netAmount-=int(values[x+1]) + return netAmount diff --git a/exercises/36-net-amount/test.py b/exercises/36-net-amount/test.py new file mode 100644 index 00000000..ce3e5b3f --- /dev/null +++ b/exercises/36-net-amount/test.py @@ -0,0 +1,22 @@ +import pytest, io, sys, json, mock, re, os +path = os.path.dirname(os.path.abspath(__file__))+'/app.py' + +@pytest.mark.it('The function net_amount must exist') +def test_function_existence(capsys, app): + assert app.net_amount + +@pytest.mark.it('The function net_amount must exist') +def test_function_existence(capsys, app): + assert app.net_amount + +@pytest.mark.it('The solution should return the expected output') +def test_output(capsys, app): + assert app.net_amount("D 300 D 300 W 200 D 100") == 500 + +@pytest.mark.it('The solution should work with other parameters. Testing with "W 300 D 300 W 200 D 300"') +def test_output_2(capsys, app): + assert app.net_amount("W 300 D 300 W 200 D 300") == 100 + +@pytest.mark.it('The solution should work with other parameters. Testing with "W 300 D 300 W 200 D 300"') +def test_output_negative(capsys, app): + assert app.net_amount("W 300 D 300 W 200 W 300") == -500 \ No newline at end of file diff --git a/exercises/37-validity-of-password/solution.hide.py b/exercises/37-validity-of-password/solution.hide.py index 84f614c8..eef5c148 100644 --- a/exercises/37-validity-of-password/solution.hide.py +++ b/exercises/37-validity-of-password/solution.hide.py @@ -1,22 +1,23 @@ import re -value = [] -items=[x for x in raw_input().split(',')] -for p in items: - if len(p)<6 or len(p)>12: - continue - else: - pass - if not re.search("[a-z]",p): - continue - elif not re.search("[0-9]",p): - continue - elif not re.search("[A-Z]",p): - continue - elif not re.search("[$#@]",p): - continue - elif re.search("\s",p): - continue - else: - pass - value.append(p) -print ",".join(value) \ No newline at end of file +def valid_password(password): + value = [] + items=[x for x in password.split(',')] + for p in items: + if len(p)<6 or len(p)>12: + continue + else: + pass + if not re.search("[a-z]",p): + continue + elif not re.search("[0-9]",p): + continue + elif not re.search("[A-Z]",p): + continue + elif not re.search("[$#@]",p): + continue + elif re.search("\s",p): + continue + else: + pass + value.append(p) + return (",".join(value)) \ No newline at end of file diff --git a/exercises/37-validity-of-password/test.py b/exercises/37-validity-of-password/test.py new file mode 100644 index 00000000..741504a2 --- /dev/null +++ b/exercises/37-validity-of-password/test.py @@ -0,0 +1,19 @@ +import pytest, io, sys, json, mock, re, os +path = os.path.dirname(os.path.abspath(__file__))+'/app.py' + + +@pytest.mark.it('The function valid_password must exist') +def test_function_existence(capsys, app): + assert app.valid_password + +@pytest.mark.it('The function should return the expected output') +def test_expected_output(capsys, app): + assert app.valid_password("ABd1234@1,a F1#,2w3E*,2We3345") == "ABd1234@1" + +@pytest.mark.it('Your solution should work as expected for valid passwords') +def test_expected_another_output(capsys, app): + assert app.valid_password("Lmd4567@2,a F1#,2w3E*,2We3345") == "Lmd4567@2" + +@pytest.mark.it('Your solution should work as expected when there is no valid password input') +def test_expected_output_no_valid_entries(capsys, app): + assert app.valid_password("ABd12,a F1#,2w3E*,2We3345") == "" \ No newline at end of file diff --git a/exercises/38-sort-tuples-ascending/solution.hide.py b/exercises/38-sort-tuples-ascending/solution.hide.py index 38076154..e1833148 100644 --- a/exercises/38-sort-tuples-ascending/solution.hide.py +++ b/exercises/38-sort-tuples-ascending/solution.hide.py @@ -1,10 +1,5 @@ from operator import itemgetter, attrgetter - -l = [] -while True: - s = raw_input() - if not s: - break - l.append(tuple(s.split(","))) - -print (sorted(l, key=itemgetter(0,1,2))) \ No newline at end of file +def sort_tuples_ascending(tuples): + l = [] + l.append(tuple(tuples.split(","))) + return (sorted(l, key=itemgetter(0,1,2))) diff --git a/exercises/38-sort-tuples-ascending/test.py b/exercises/38-sort-tuples-ascending/test.py index 6316701e..c8a4b30a 100644 --- a/exercises/38-sort-tuples-ascending/test.py +++ b/exercises/38-sort-tuples-ascending/test.py @@ -1,11 +1,17 @@ import pytest, io, sys, json, mock, re, os + path = os.path.dirname(os.path.abspath(__file__))+'/app.py' -@pytest.mark.it('The solution should return the expected output') -def test_convert_inputs(capsys, app): - fake_input = ["Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85"] #fake input - with mock.patch('builtins.input', lambda x: fake_input.pop()): - app() - captured = capsys.readouterr() - assert captured.out == "[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]\n" \ No newline at end of file +@pytest.mark.it('The function sort_tuples_ascending must exist') +def test_function_existence(capsys, app): + assert app.sort_tuples_ascending + + +@pytest.mark.it('The function should return the expected output') +def etest_expected_output(capsys, app): + app.sort_tuples_ascending("Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85") == [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')] + +@pytest.mark.it('The solution should work with other entries') +def test_another_entry(capsys, app): + app.sort_tuples_ascending("Martin,23,30 Tomas,25,27") == [('Martin', '23', '30', 'Tomas', '25', '27')] diff --git a/exercises/40-compute-robot-distance/solution.hide.py b/exercises/40-compute-robot-distance/solution.hide.py index f7fc7c83..f32439ae 100644 --- a/exercises/40-compute-robot-distance/solution.hide.py +++ b/exercises/40-compute-robot-distance/solution.hide.py @@ -1,21 +1,17 @@ import math -pos = [0,0] -while True: - s = raw_input() - if not s: - break - movement = s.split(" ") - direction = movement[0] - steps = int(movement[1]) - if direction=="UP": - pos[0]+=steps - elif direction=="DOWN": - pos[0]-=steps - elif direction=="LEFT": - pos[1]-=steps - elif direction=="RIGHT": - pos[1]+=steps - else: - pass - -print int(round(math.sqrt(pos[1]**2+pos[0]**2))) \ No newline at end of file +def compute_robot_distance(props): + pos = [0,0] + new_prop = props.split(" ") + for x in range(len(new_prop)): + if new_prop[x].upper() == 'UP': + pos[0]+=int(new_prop[x+1]) + elif new_prop[x].upper() == 'DOWN': + pos[0]-=int(new_prop[x+1]) + elif new_prop[x].upper() == 'LEFT': + pos[1]-=int(new_prop[x+1]) + elif new_prop[x].upper() == 'RIGHT': + pos[1]+=int(new_prop[x+1]) + else: + None + return (int(round(math.sqrt(pos[1]**2+pos[0]**2)))) + \ No newline at end of file diff --git a/exercises/40-compute-robot-distance/test.py b/exercises/40-compute-robot-distance/test.py new file mode 100644 index 00000000..33cc55e2 --- /dev/null +++ b/exercises/40-compute-robot-distance/test.py @@ -0,0 +1,19 @@ +import pytest, io, sys, json, mock, re, os + + +@pytest.mark.it('The function compute_robot_distance must exist') +def test_function_existence(capsys, app): + assert app.compute_robot_distance + + +@pytest.mark.it('The function should return the expected output') +def test_expected_output(capsys, app): + assert app.compute_robot_distance("UP 5 DOWN 3 LEFT 3 RIGHT 2") == 2 + +@pytest.mark.it('The solution should work with other entries') +def test_another_output(capsys, app): + assert app.compute_robot_distance("DOWN 20 UP 5 LEFT 5 RIGHT 2") == 15 + +@pytest.mark.it('The solution should work with other entries') +def test_negative_inputs(capsys, app): + assert app.compute_robot_distance("DOWN -1 UP -5 LEFT 50 RIGHT 20") == 30 diff --git a/exercises/41-frequency-of-words/solution.hide.py b/exercises/41-frequency-of-words/solution.hide.py index cbe27b20..8c9f61fb 100644 --- a/exercises/41-frequency-of-words/solution.hide.py +++ b/exercises/41-frequency-of-words/solution.hide.py @@ -1,5 +1,5 @@ freq = {} # frequency of words in text -line = raw_input() +line = input() for word in line.split(): freq[word] = freq.get(word,0)+1 @@ -7,4 +7,4 @@ words.sort() for w in words: - print "%s:%d" % (w,freq[w]) \ No newline at end of file + print ("%s:%d" % (w,freq[w])) \ No newline at end of file diff --git a/exercises/41-frequency-of-words/test.py b/exercises/41-frequency-of-words/test.py new file mode 100644 index 00000000..1437ecb8 --- /dev/null +++ b/exercises/41-frequency-of-words/test.py @@ -0,0 +1,12 @@ +import pytest, io, sys, json, mock, re, os + +path = os.path.dirname(os.path.abspath(__file__))+'/app.py' + +@pytest.mark.it('The solution should return the expected output') +def test_convert_inputs(capsys, app): + + fake_input = ["New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3"] #fake input + with mock.patch('builtins.input', lambda x: fake_input.pop()): + app() + captured = capsys.readouterr() + assert captured.out == "2:2 3.:1 3?:1 New:1 Python:5 Read:1 and:1 between:1 choosing:1 or:2 to:1\n" \ No newline at end of file