Skip to content

Instantly share code, notes, and snippets.

@RickGriff
Created January 7, 2018 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickGriff/a92c1c9ef320bd63765a9e380ac31d25 to your computer and use it in GitHub Desktop.
Save RickGriff/a92c1c9ef320bd63765a9e380ac31d25 to your computer and use it in GitHub Desktop.
Codewars Challenge: Get the Middle Character
# You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.
# #Examples:
# runBF("test\0") should return "es"
# runBF("testing\0") should return "t"
# runBF("middle\0") should return "dd"
# runBF("A\0") should return "A"
# #Input
# A word (string) of length 0 < str < 200 For BF, all the input strings end with "\0". You do not need to test for this. This is only here to tell you that you do not need to worry about your solution timing out.
# #Output
# The middle character(s) of the word represented as a string.
-----
#My Solution:
def get_middle(s)
len = s.length
len % 2 == 0 ? s[(len/2 - 1)..len/2] : s[len/2]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment