8000 init: some katas + tests · benmtz/codewars-python@81a0faa · GitHub
[go: up one dir, main page]

Skip to content

Commit 81a0faa

Browse files
committed
init: some katas + tests
0 parents  commit 81a0faa

10 files changed

+215
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
env

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"python": "${workspaceFolder}/bin/python3.9",
9+
"name": "Python: Current File",
10+
"type": "python",
11+
"request": "launch",
12+
"program": "${file}",
13+
"console": "integratedTerminal"
14+
}
15+
]
16+
}

.vscode/tasks.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "run python file",
6+
"type": "shell",
7+
"command": "while inotifywait -e close_write ${file}; do ./bin/python3.9 ${file}; done"
8+
}
9+
]
10+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Codewars python
2+
3+
This repository is where I test my codewars kata solutions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 520446778469526ec0000001
2+
3+
# Complete the function/method (depending on the language) to return true/True when its argument is an array that has the same nesting structures
4+
# and same corresponding length of nested arrays as the first array.
5+
# For example:
6+
7+
# # should return True
8+
# same_structure_as([ 1, 1, 1 ], [ 2, 2, 2 ] )
9+
# same_structure_as([ 1, [ 1, 1 ] ], [ 2, [ 2, 2 ] ] )
10+
11+
# # should return False
12+
# same_structure_as([ 1, [ 1, 1 ] ], [ [ 2, 2 ], 2 ] )
13+
# same_structure_as([ 1, [ 1, 1 ] ], [ [ 2 ], 2 ] )
14+
15+
# # should return True
16+
# same_structure_as([ [ [ ], [ ] ] ], [ [ [ ], [ ] ] ] )
17+
18+
# # should return False
19+
# same_structure_as([ [ [ ], [ ] ] ], [ [ 1, 1 ] ] )
20+
21+
import collections
22+
23+
same_type = lambda x, y : type(x) == type(y)
24+
is_sequence = lambda s: isinstance(s, collections.Sequence)
25+
26+
def same_structure_as(original,other):
27+
if not same_type(original, other):
28+
return False
29+
for idx, val in enumerate(original):
30+
if other[idx] is None: # Out of bound protection
31+
return False
32+
other_val = other[idx]
33+
if not same_type(val, other_val):
34+
return False
35+
if is_sequence(val) and (len(other_val) != len(val) or not same_structure_as(val, other_val)):
36+
return False
37+
return True
38+
39+
import unittest
40+
41+
class TestAnagrams(unittest.TestCase):
42+
43+
def test_1(self):
44+
self.assertEqual(same_structure_as([ 1, 1, 1 ], [ 2, 2, 2 ]), True)
45+
self.assertEqual(same_structure_as([ 1, [ 1, 1 ] ], [ 2, [ 2, 2 ] ] ), True)
46+
47+
self.assertEqual(same_structure_as([ 1, [ 1, 1 ] ], [ [ 2, 2 ], 2 ] ), False)
48+
self.assertEqual(same_structure_as([ 1, [ 1, 1 ] ], [ [ 2 ], 2 ] ), False)
49+
50+
self.assertEqual(same_structure_as([ [ [ ], [ ] ] ], [ [ [ ], [ ] ] ] ), True)
51+
52+
self.assertEqual(same_structure_as([ [ [ ], [ ] ] ], [ [ 1, 1 ] ] ), False)
53+
54+
self.assertEqual(same_structure_as([], 1), False)
55+
self.assertEqual(same_structure_as([], {}), False)
56+
57+
self.assertEqual(same_structure_as([1,'[',']'], ['[',']',1]), True)
58+
59+
if __name__ == '__main__':
60+
unittest.main()

katas/5-anagram.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections.abc import Sequence
2+
import unittest
3+
4+
def anagram_matcher_factory(w1: str):
5+
_w1 = list(w1)
6+
_w1.sort()
7+
def is_anagram(w2: str):
8+
_w2 = list(w2)
9+
_w2.sort()
10+
return _w1 == _w2
11+
return is_anagram
12+
13+
def anagrams(word: str, words: Sequence[str]):
14+
return list(filter(anagram_matcher_factory(word), words))
15+
16+
17+
class TestAnagrams(unittest.TestCase):
18+
19+
def test_1(self):
20+
self.assertEqual(anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']), ['aabb', 'bbaa'], "Array differ")
21+
22+
def test_2(self):
23+
self.assertEqual(anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']) , ['carer', 'racer'], "Array differ")
24+
25+
if __name__ == "__main__":
26+
unittest.main()
27+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
3+
# Write a function named first_non_repeating_letter that takes a string input, and returns the first character that is not repeated anywhere in the string.
4+
# For example, if given the input 'stress', the function should return 't', since the letter t only occurs once in the string, and occurs first in the string.
5+
# As an added challenge, upper- and lowercase letters are considered the same character, but the function should return the correct case for the initial letter. For example, the input 'sTreSS' should return 'T'.
6+
# If a string contains all repeating characters, it should return an empty string ("") or None -- see sample tests.
7+
8+
def first_non_repeating_letter(word):
9+
is_repeating_in_word = lambda letter: word.upper().count(letter.upper()) == 1
10+
return next(filter(is_repeating_in_word, word), '')
11+
12+
class TestFirstNonRepeatingLetter(unittest.TestCase):
13+
14+
def test_simple(self):
15+
self.assertEquals(first_non_repeating_letter('a'), 'a')
16+
self.assertEquals(first_non_repeating_letter('stress'), 't')
17+
self.assertEquals(first_non_repeating_letter('moonmen'), 'e')
18+
19+
def test_should_handle_empty_strings(self):
20+
self.assertEquals(first_non_repeating_letter(''), '')
21+
22+
def test_should_handle_all_repeating_strings(self):
23+
self.assertEquals(first_non_repeating_letter('abba'), '')
24+
self.assertEquals(first_non_repeating_letter('aa'), '')
25+
26+
def test_should_handle_odd_characters(self):
27+
self.assertEquals(first_non_repeating_letter('~><#~><'), '#')
28+
self.assertEquals(first_non_repeating_letter('hello world, eh?'), 'w')
29+
30+
def test_should_handle_letter_cases(self):
31+
self.assertEquals(first_non_repeating_letter('sTreSS'), 'T')
32+
self.assertEquals(first_non_repeating_letter('Go hang a salami, I\'m a lasagna hog!'), ',')
33+
34+
if __name__ == "__main__":
35+
unittest.main()

katas/5-increment-string.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 54a91a4883a7de5d7800009c
2+
# Your job is to write a function which increments a string, to create a new string.
3+
# If the string already ends with a number, the number should be incremented by 1.
4+
# If the string does not end with a number. the number 1 should be appended to the new string.
5+
# Examples:
6+
# foo -> foo1
7+
# foobar23 -> foobar24
8+
# foo0042 -> foo0043
9+
# foo9 -> foo10
10+
# foo099 -> foo100
11+
# Attention: If the number has leading zeros the amount of digits should be considered.
12+
13+
import unittest
14+
import re
15+
16+
def increment_string(strng):
17+
splited = re.split("(\d+)$", strng)
18+
ending_digits = splited[1] if len(splited) > 2 else "0"
19+
pad_digit_token = "0{PadTo}".format(PadTo = len(ending_digits))
20+
return "{0}{1}".format(splited[0], format(int(ending_digits)+1, pad_digit_token))
21+
22+
class TestAnagrams(unittest.TestCase):
23+
24+
def test_1(self):
25+
self.assertEqual(increment_string("foo"), "foo1")
26+
self.assertEqual(increment_string("foobar001"), "foobar002")
27+
self.assertEqual(increment_string("foobar1"), "foobar2")
28+
self.assertEqual(increment_string("foobar00"), "foobar01")
29+
self.assertEqual(increment_string("foobar99"), "foobar100")
30+
self.assertEqual(increment_string("foobar099"), "foobar100")
31+
self.assertEqual(increment_string(""), "1")
32+
33+
if __name__ == '__main__':
34+
unittest.main()

katas/5-makereadable.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
3+
def make_readable(seconds):
4+
return "{hours:02d}:{minutes:02d}:{seconds:02d}".format(
5+
hours = int(seconds / 3600),
6+
minutes = int(seconds / 60) % 60,
7+
seconds = seconds % 60
8+
)
9+
10+
class TestMakeReadable(unittest.TestCase):
11+
def test_1(self):
12+
self.assertEqual(make_readable(0), "00:00:00")
13+
self.assertEqual(make_readable(5), "00:00:05")
14+
self.assertEqual(make_readable(60), "00:01:00")
15+
self.assertEqual(make_readable(86399), "23:59:59")
16+
self.assertEqual(make_readable(359999), "99:59:59")
17+
18+
19+
if __name__ == "__main__":
20+
unittest.main()
21+
22+
23+

test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
for kata in katas/*; do
4+
echo "testing $kata"
5+
./env/bin/python3.9 $kata
6+
done

0 commit comments

Comments
 (0)
0