###JavaScript
###JavaScript
Capitalize the First Letter of Words in a String (JavaScript, Python)
###Problem
Write a function that accepts a string, capitalizes the first letter of each word in the string, and returns the capitalized string. ###Examples
function('a short sentence') => 'A Short Sentence'
function('a lazy fox') => 'A Lazy Fox'
function('look, it is working!') => 'Look, It Is Working!'
###Code
I've solved the above problem using a few methods. If you'd like to review the codes and provide any change/improvement recommendations please do so, and I'd really appreciate that.
###Python
def capitalize_built_in_title(sentence: str) -> str:
"""Capitalizes the first letters and preserves the spaces"""
return sentence.title()
def capitalize_naive_one(sentence: str) -> str:
"""Capitalizes the first letters and doesn't preserve the spaces"""
words = sentence.split()
for index_word, word in enumerate(words):
word_list = split_char_by_char_list(word)
for index_letter, letter in enumerate(word_list):
if index_letter == 0:
word_list[index_letter] = letter.upper()
word = "".join(word_list)
break
words[index_word] = word
return " ".join(words)
def capitalize_naive_two(sentence: str) -> str:
"""Capitalizes the first letters and doesn't preserve the spaces"""
words = sentence.split()
for index_word, word in enumerate(words):
words[index_word] = word[:1].upper() + word[1:]
return " ".join(words)
def capitalize_naive_three(sentence: str) -> str:
"""Capitalizes the first letters and preserves the spaces"""
import regex as re
split_on_first_letter = list(
re.splititer(r'(?i)(?<=^|\s)([a-z])', sentence))
for index, item in enumerate(split_on_first_letter):
if item is not None and len(item) == 1:
split_on_first_letter[index] = item.upper()
return "".join(split_on_first_letter)
def split_char_by_char_re(string: str):
import re
return re.findall(r'.', string)
def split_char_by_char_regex(string: str):
import regex as re
return re.findall(r'.', string)
def split_char_by_char_list(string: str):
return list(string)
if __name__ == '__main__':
# ---------------------------- TEST ---------------------------
DIVIDER_DASH_LINE = '-' * 50
GREEN_APPLE = '\U0001F34F'
RED_APPLE = '\U0001F34E'
test_sentences = ['hey there, how was your day?',
' good day! ', ' it waS GreaT!']
capitalized_sentences = [
'Hey There, How Was Your Day?', ' Good Day! ', ' It WaS GreaT!']
# ---------------------- DONT REPEAT YOURSELF -------------------------------
for index, sentence in enumerate(test_sentences):
print(DIVIDER_DASH_LINE)
if capitalized_sentences[index] == capitalize_built_in_title(sentence):
print(f'{GREEN_APPLE} "{sentence}" => "{capitalize_built_in_title(sentence)}"')
else:
print(f'{RED_APPLE} "{sentence}" => "{capitalize_built_in_title(sentence)}"')
if capitalized_sentences[index] == capitalize_naive_one(sentence):
print(f'{GREEN_APPLE} "{sentence}" => "{capitalize_naive_one(sentence)}"')
else:
print(f'{RED_APPLE} "{sentence}" => "{capitalize_naive_one(sentence)}"')
if capitalized_sentences[index] == capitalize_naive_two(sentence):
print(f'{GREEN_APPLE} "{sentence}" => "{capitalize_naive_two(sentence)}"')
else:
print(f'{RED_APPLE} "{sentence}" => "{capitalize_naive_two(sentence)}"')
if capitalized_sentences[index] == capitalize_naive_three(sentence):
print(f'{GREEN_APPLE} "{sentence}" => "{capitalize_naive_three(sentence)}"')
else:
print(f'{RED_APPLE} "{sentence}" => "{capitalize_naive_three(sentence)}"')
###Output
--------------------------------------------------
π "hey there, how was your day?" => "Hey There, How Was Your Day?"
π "hey there, how was your day?" => "Hey There, How Was Your Day?"
π "hey there, how was your day?" => "Hey There, How Was Your Day?"
π "hey there, how was your day?" => "Hey There, How Was Your Day?"
--------------------------------------------------
π " good day! " => " Good Day! "
π " good day! " => "Good Day!"
π " good day! " => "Good Day!"
π " good day! " => " Good Day! "
--------------------------------------------------
π " it waS GreaT!" => " It Was Great!"
π " it waS GreaT!" => "It WaS GreaT!"
π " it waS GreaT!" => "It WaS GreaT!"
π " it waS GreaT!" => " It WaS GreaT!"
// --- Problem
// Write a function that accepts a string. The function should
// capitalize the first letter of each word in the string then
// return the capitalized string.
// --- Examples
// function('a short sentence') => 'A Short Sentence'
// function('a lazy fox') => 'A Lazy Fox'
// function('look, it is working!') => 'Look, It Is Working!'
function capitalize_naive_one(sentence) {
words = [];
for (let word of sentence.split(' ')) {
if (word != '') {
word = (word[0].toUpperCase() + word.slice(1));
}
words.push(word);
}
return words.join(' ');
}
function capitalize_naive_two(sentence) {
capitalized_sentence = sentence[0].toUpperCase();
for (let i = 1; i < sentence.length; i++) {
if (sentence[i - 1] === ' ') {
capitalized_sentence += sentence[i].toUpperCase();
} else {
capitalized_sentence += sentence[i];
}
}
return capitalized_sentence;
}
test_sentences = ['hey there, how was your day?', ' good day! ', ' it waS GreaT!'];
capitalized_sentences = ['Hey There, How Was Your Day?', ' Good Day! ', ' It WaS GreaT!'];
count = 0;
for (let sentence of test_sentences) {
if (capitalized_sentences[count] === capitalize_naive_one(sentence) && capitalized_sentences[count] === capitalize_naive_two(sentence)) {
console.log('π "'.concat(sentence, '" => "', capitalized_sentences[count], '"'));
}
count++;
}
lang-py