A palindrome is a word or phrase that is read the same forward as backwards
Some popular examples of palindrome words are "racecar" and "kayak", and phrases are "a man a plan a canal panama" and "madam im adam"
Some phrases are not as simple as reversing a word
Write a function that returns true if a phrase is a palindrome and false if it is not, case and spaces doesn't matter.
'racecar' == True
'Racecar' == True
'racecars' == False
'a man a plan panama' == True
'a Man a plan panama' == True
'a man 2 plan panama' == False
def ispalindrome(p):
if p.replace(' ', '').upper()==p.replace(' ', '').upper()[::-1]: return True
return False
import codewars_test as test
import random, string
# test.assert_equals(actual, expected, [optional] message)
def makepalindrome():
palindrome = ''
for x in range(10):
letter = random.choice(string.ascii_letters)
space = random.choice(['', ' ', ' '])
palindrome += letter + space
x = random.choice([False,True])
if x:
palindrome += palindrome[::-1]
return palindrome, x
@test.describe("Examples")
def test_group():
@test.it("True")
def true_tests():
test.assert_equals(ispalindrome('racecar'), True)
test.assert_equals(ispalindrome('kayak'), True)
test.assert_equals(ispalindrome('madam im adam'), True)
test.assert_equals(ispalindrome('a mAn a plan a caNal panama'), True)
test.assert_equals(ispalindrome('able was I ere I saw Elba'), True)
@test.it("False")
def false_tests():
test.assert_equals(ispalindrome('racecars'), False)
test.assert_equals(ispalindrome('kayaking'), False)
test.assert_equals(ispalindrome('madam, im adam'), False)
test.assert_equals(ispalindrome('a mAn, a plan, a caNal panama'), False)
test.assert_equals(ispalindrome('able was I when I saw Elba'), False)
@test.it("Random")
def random_tests():
for i in range(100):
x, y = makepalindrome()
test.assert_equals(ispalindrome(x), y)
import string def SameCase(a, b): if a not in string.ascii_letters or b not in string.ascii_letters: return -1 return int(a.isupper() == b.isupper())
public class Kata {public static int SameCase(char a, char b) {- import string
- def SameCase(a, b):
- if a not in string.ascii_letters or b not in string.ascii_letters:
- return -1
string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";char tempA = a;char tempB = b;if((!letters.Contains(tempA = char.ToUpper(tempA)) && letters.Contains(tempB = char.ToUpper(tempB))) ||(!letters.Contains(tempB = char.ToUpper(tempB)) && letters.Contains(tempA = char.ToUpper(tempA))) ||(!letters.Contains(tempA = char.ToUpper(tempA)) && !letters.Contains(tempB = char.ToUpper(tempB)))) return -1;if(char.IsUpper(a) && char.IsUpper(b)){return 1;} else if (char.IsLower(a) && char.IsLower(b)){return 1;} else {return 0;}}}- return int(a.isupper() == b.isupper())
import string, random @test.describe("True Tests") def TrueTests(): test.assert_equals(SameCase("a","u"), 1) test.assert_equals(SameCase("A","U"), 1) test.assert_equals(SameCase("Q","P"), 1) test.assert_equals(SameCase("w","y"), 1) test.assert_equals(SameCase("c","m"), 1) test.assert_equals(SameCase("N","W"), 1) @test.describe("False Tests") def FalseTests(): test.assert_equals(SameCase("a","U"), 0) test.assert_equals(SameCase("A","u"), 0) test.assert_equals(SameCase("Q","p"), 0) test.assert_equals(SameCase("w","U"), 0) test.assert_equals(SameCase("c","M"), 0) test.assert_equals(SameCase("N","w"), 0) @test.describe("Not letters") def NotLetters(): test.assert_equals(SameCase("a","*"), -1) test.assert_equals(SameCase("A","/"), -1) test.assert_equals(SameCase("Q","-"), -1) test.assert_equals(SameCase("w","="), -1) test.assert_equals(SameCase("c","."), -1) test.assert_equals(SameCase("N","1"), -1) def Solution(a, b): if a not in string.ascii_letters or b not in string.ascii_letters: return -1 return int(a.isupper() == b.isupper()) @test.describe("Random tests") def RandomTests(): for i in range(6): a = random.choice(string.ascii_letters) b = random.choice(string.ascii_letters) test.assert_equals(SameCase(a, b), Solution(a, b))
namespace Solution {using NUnit.Framework;using System;- import string, random
- @test.describe("True Tests")
- def TrueTests():
- test.assert_equals(SameCase("a","u"), 1)
- test.assert_equals(SameCase("A","U"), 1)
- test.assert_equals(SameCase("Q","P"), 1)
- test.assert_equals(SameCase("w","y"), 1)
- test.assert_equals(SameCase("c","m"), 1)
- test.assert_equals(SameCase("N","W"), 1)
- @test.describe("False Tests")
- def FalseTests():
- test.assert_equals(SameCase("a","U"), 0)
- test.assert_equals(SameCase("A","u"), 0)
- test.assert_equals(SameCase("Q","p"), 0)
- test.assert_equals(SameCase("w","U"), 0)
- test.assert_equals(SameCase("c","M"), 0)
- test.assert_equals(SameCase("N","w"), 0)
- @test.describe("Not letters")
- def NotLetters():
- test.assert_equals(SameCase("a","*"), -1)
- test.assert_equals(SameCase("A","/"), -1)
- test.assert_equals(SameCase("Q","-"), -1)
- test.assert_equals(SameCase("w","="), -1)
- test.assert_equals(SameCase("c","."), -1)
- test.assert_equals(SameCase("N","1"), -1)
[TestFixture]public class SolutionTest{[Test]public void TrueTests(){Assert.AreEqual(1, Kata.SameCase('a', 'u'));Assert.AreEqual(1, Kata.SameCase('A', 'U'));Assert.AreEqual(1, Kata.SameCase('Q', 'P'));Assert.AreEqual(1, Kata.SameCase('w', 'y'));Assert.AreEqual(1, Kata.SameCase('c', 'm'));Assert.AreEqual(1, Kata.SameCase('N', 'W'));}[Test]public void FalseTests(){Assert.AreEqual(0, Kata.SameCase('a', 'U'));Assert.AreEqual(0, Kata.SameCase('A', 'u'));Assert.AreEqual(0, Kata.SameCase('Q', 'p'));Assert.AreEqual(0, Kata.SameCase('w', 'Y'));Assert.AreEqual(0, Kata.SameCase('c', 'M'));Assert.AreEqual(0, Kata.SameCase('N', 'w'));}[Test]public void NotLetters(){Assert.AreEqual(-1, Kata.SameCase('a', '*'));Assert.AreEqual(-1, Kata.SameCase('A', '%'));Assert.AreEqual(-1, Kata.SameCase('Q', '1'));Assert.AreEqual(-1, Kata.SameCase('w', '-'));Assert.AreEqual(-1, Kata.SameCase('c', '8'));Assert.AreEqual(-1, Kata.SameCase('N', ':'));}[Test]public void RandomTest() {string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890~/*-+:.,><.#@!&";Random rand = new Random();char char1 = 'a';char char2 = 'a';for (int i = 0; i < 200; i++) {char1 = chars[rand.Next(0,76)];char2 = chars[rand.Next(0,76)];Assert.AreEqual(SameCaseSolution(char1,char2), Kata.SameCase(char1, char2));}}- def Solution(a, b):
- if a not in string.ascii_letters or b not in string.ascii_letters:
- return -1
private static int SameCaseSolution(char a, char b) {if (!char.IsLetter(a) || !char.IsLetter(b)) return -1;if ((a >= 97) == (b >= 97)) return 1;return 0;}}}- return int(a.isupper() == b.isupper())
- @test.describe("Random tests")
- def RandomTests():
- for i in range(6):
- a = random.choice(string.ascii_letters)
- b = random.choice(string.ascii_letters)
- test.assert_equals(SameCase(a, b), Solution(a, b))