It is bad practice to have different return types. Maybe instead you could return 0
if the input is invalid.
You may also want to specify whether floats should be converted too.
def convert(number): return number*-1 if type(number) == int else 0
- def convert(number):
if isinstance(number, int):# Check if number is negitiveif number < 0:# negitive -> positivereturn (abs(number))else:# positive -> negitivereturn (number * -1)else:return False- return number*-1 if type(number) == int else 0
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("These are your Test results") def test_group(): @test.it("test case") def test_case(): test.assert_equals(convert(-1), 1) test.assert_equals(convert(234), -234) test.assert_equals(convert(-3472398574349325748132), 3472398574349325748132) test.assert_equals(convert([2, 42, "3"]), 0) def converter(number): return number*-1 if type(number) == int else 0 @test.describe('Random Tests') def random(): import random for i in range(100): input = random.randint(-10000,10000) result = converter(input) test.assert_equals(convert(input),result)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("These are your Test results")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(convert(-1), 1)
- test.assert_equals(convert(234), -234)
- test.assert_equals(convert(-3472398574349325748132), 3472398574349325748132)
test.assert_equals(convert([2, 42, "3"]), False)- test.assert_equals(convert([2, 42, "3"]), 0)
- def converter(number):
- return number*-1 if type(number) == int else 0
- @test.describe('Random Tests')
- def random():
- import random
- for i in range(100):
- input = random.randint(-10000,10000)
- result = converter(input)
- test.assert_equals(convert(input),result)
sort_names = lambda s: ' '.join(sorted(s.title().split()))
def sort_names(s):return ' '.join(sorted(s.title().split()))- sort_names = lambda s: ' '.join(sorted(s.title().split()))
@test.describe('Fixed Tests') def fixed(): @test.it('Basic Tests') def basic(): test.assert_equals(sort_names('Kate Amara Bob Justin'),'Amara Bob Justin Kate') test.assert_equals(sort_names('kate amara justin bob'),'Amara Bob Justin Kate') @test.describe('Random Tests') def random_tests(): @test.it("Random Tests") def test_cases(): for i in range(100): input = get_input(random.randint(0,15)) result = sort_names_correct(input[:]) test.it(f'{input}') test.assert_equals(sort_names(input),result)
- @test.describe('Fixed Tests')
- def fixed():
- @test.it('Basic Tests')
- def basic():
- test.assert_equals(sort_names('Kate Amara Bob Justin'),'Amara Bob Justin Kate')
- test.assert_equals(sort_names('kate amara justin bob'),'Amara Bob Justin Kate')
- @test.describe('Random Tests')
- def random_tests():
- @test.it("Random Tests")
- def test_cases():
- for i in range(100):
- input = get_input(random.randint(0,15))
- result = sort_names_correct(input[:])
test.it(f'Testing {input}: should equal {result}')- test.it(f'{input}')
- test.assert_equals(sort_names(input),result)
Create a function called sort_names()
which takes a string of names each separated by a space and returns them as a string with the names sorted and in title case. See below.
'Kate Amara Bob Justin' --> 'Amara Bob Justin Kate'
'kate amara bob justin' --> 'Amara Bob Justin Kate'
def sort_names(s):
return ' '.join(sorted(s.title().split()))
@test.describe('Fixed Tests')
def fixed():
@test.it('Basic Tests')
def basic():
test.assert_equals(sort_names('Kate Amara Bob Justin'),'Amara Bob Justin Kate')
test.assert_equals(sort_names('kate amara justin bob'),'Amara Bob Justin Kate')
@test.describe('Random Tests')
def random_tests():
@test.it("Random Tests")
def test_cases():
for i in range(100):
input = get_input(random.randint(0,15))
result = sort_names_correct(input[:])
test.it(f'Testing {input}: should equal {result}')
test.assert_equals(sort_names(input),result)
Anagrams are words that consist of the same letters. One example is 'felt' and 'left'.
Super anagrams also consist of the same words, but their first and last letter are the same. For example, 'Bart' and 'Brat'
Your task is to create a function called 'anagram_check' that determines whether a pair of words are anagrams, super anagrams or not anagrams at all.
If it is a super anagram, it should return 'Super Anagram'.
If it is an anagram, it should return 'Anagram'.
If it is not an anagram, it should return False.
The input will be stored in one variable. The program should not be case-sensitive. The test will not contain any other characters apart from letters of the Latin/English alphabet.
Notes:
Make sure to return the result exactly as it is shown (i.e. in titlecase)
The program should not be case sensitive.
All input will be valid.
If any improvements can be made, please let me know, otherwise have fun and enjoy!
def anagram_check(words):
listed = words.split()
first = listed[0]
second = listed[1]
first_split = (list(first))
second_split = (list(second))
if first_split[0]==second_split[0] and first_split[-1]==second_split[-1]:
first_split_alpha = sorted(first_split)
second_split_alpha = sorted(second_split)
if first_split_alpha == second_split_alpha:
return'Super Anagram'
else:
return False
else:
return 'Anagram'
test.describe('Fixed Tests')
test.assert_equals(anagram_check('felt left'),'Anagram')
test.assert_equals(anagram_check('Bart Brat'),'Super Anagram')
test.assert_equals(anagram_check('feet foot'),False)
test.assert_equals(anagram_check('ten tin'),False)
test.assert_equals(anagram_check('grid gird'),'Super Anagram')