Back to Freecodecamp

Challenge 240: Palindrome Characters

curriculum/challenges/english/blocks/daily-coding-challenges-python/69b58ce40693f140c84c8559.md

latest1.5 KB
Original Source

--description--

Given a string, determine if it's a palindrome and return the middle character (if it's odd length) or middle two characters (if it's even).

  • A palindrome is a string that is the same forward and backward.
  • If it's not a palindrome, return "none".

--hints--

palindrome_locator("racecar") should return "e".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("racecar"), "e")`)
}})

palindrome_locator("level") should return "v".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("level"), "v")`)
}})

palindrome_locator("freecodecamp") should return "none".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("freecodecamp"), "none")`)
}})

palindrome_locator("noon") should return "oo".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("noon"), "oo")`)
}})

palindrome_locator("11100111") should return "00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("11100111"), "00")`)
}})

--seed--

--seed-contents--

py
def palindrome_locator(s):

    return s

--solutions--

py
def palindrome_locator(s):
    if s != s[::-1]:
        return "none"
    mid = len(s) // 2
    return s[mid] if len(s) % 2 == 1 else s[mid - 1] + s[mid]