地址:http://www.codewars.com/kata/5202ef17a402dd033c000009/train/python

题目:

A string is considered to be in title case if each word in the string is either (a) capitalised (that is, only the first letter of the word is in upper case) or (b) considered to be an exception and put entirely into lower case unless it is the first word, which is always capitalised.

Write a function that will convert a string into title case, given an optional list of exceptions (minor words). The list of minor words will be given as a string with each word separated by a space. Your function should ignore the case of the minor words string -- it should behave in the same way even if the case of the minor word string is changed.

Example:

title_case('a clash of KINGS', 'a an the of') # should return: 'A Clash of Kings'

title_case('THE WIND IN THE WILLOWS', 'The In') # should return: 'The Wind in the Willows'

title_case('the quick brown fox') # should return: 'The Quick Brown Fox'

代码:

def title_case(title,minor_words=""):
ans = title.title()
titleList = ans.split(" ")
words = minor_words.title()
wordsList = words.split(" ") if ans == "":
return ""
else:
for i in range(1,len(titleList)):
if titleList[i] in wordsList:
titleList[i] = titleList[i][0].lower() + titleList[i][1:] return " ".join(titleList)

  

Title Case的更多相关文章

  1. Title Case a Sentence-freecodecamp算法题目

    Title Case a Sentence(中单词首字母大写) 要求 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 思路 将句子小写化后用.split(& ...

  2. Title Case a Sentence

    解决思路 将字符串转换成小写 把字符串分割成字符串数组 循环数组将每一个单词首字母大写 把数组所有的元素转换成一个字符串 第一种方法 function titleCase(str) { str=str ...

  3. freeCodeCamp:Title Case a Sentence

    确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. /*思路 将字符串转为小写.toLowerCase() 分割字符串以单词形式组成数组myarr 确保数组中的 ...

  4. FreeCodeCamp:Title Case a Sentence

    要求: 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 结果: titleCase("I'm a little tea pot") 应该 ...

  5. FCC JS基础算法题(4):Title Case a Sentence(句中单词首字母大写)

    题目描述: 确保字符串的每个单词首字母都大写,其余部分小写.像'the'和'of'这样的连接符同理. 算法: function titleCase(str) { // 转小写及分割成数组 var st ...

  6. CASE WHEN 及 SELECT CASE WHEN的用法(转)

    Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END   ...

  7. [Upper case conversion ] 每个单词的首小写字母转换为对应的大写字母

    Given a string , write a program to title case every first letter of words in string. Input:The firs ...

  8. CASE WHEN 批量更新

    单个值: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHE ...

  9. Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例

    如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 复制代码 代码如下: UPDATE mytable SET myfield = CASE i ...

随机推荐

  1. Quartz2D学习笔记(1)

    ********************************** 简介 *************************************** Quartz2D是⼀个二维绘图引擎,同时支持 ...

  2. 如何利用SecureCRT连接Ubuntu12.0.4

    环境描述:虚拟机网络选择NAT连接方式,Ubuntu的版本是Ubuntu12.0.4 1. 先做一个测试,假设现在系统还没有装ssh,用secureCRT连接Ubuntu是出现下面的界面. 实际上,这 ...

  3. 将页面中指定表格的数据导入到Excel中

    function AutoExcel(){   var oXL = new ActiveXObject("Excel.Application"); //创建应该对象   var o ...

  4. 23个.NET开源项目

    Castle是.NET里走过了三年的开源框架,下载地址如:http://www.castleproject.org/index.html ,当然如果你是从事过JAVA开发并用过spring,hiber ...

  5. 意犹未尽而来的第一篇Android 逆向

    游戏:咕噜王国大冒险 平台:android 目标: 1. 去除乱七八糟提示(本篇目标) 2. 去除google弹窗 3. 破解“all stages” 破文开始: 1. 使用APKIDE反编译:搜索字 ...

  6. uva 11168 - Airport

    凸包+一点直线的知识: #include <cstdio> #include <cmath> #include <cstring> #include <alg ...

  7. Spring In Action 第4版笔记-第一章-001架构

    1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...

  8. 调试技巧 —— 如何利用windbg + dump + map分析程序异常

    调试技巧 —— 如何利用windbg + dump + map分析程序异常 逗比汪星人2011-09-04上传   调试技巧 —— 如何利用windbg + dump + map分析程序异常 http ...

  9. Datetime中yyyy-MM-dd-hh-mm-ss的格式

    namespace yyyy_MM_dd_hh_mm{    class Program    {        static void Main(string[] args)        { wh ...

  10. Java程序员从笨鸟到菜鸟全部博客目录

    本文来自:曹胜欢博客专栏.转载请注明出处:http://blog.csdn.net/csh624366188 大学上了一年半,接触java也一年半了,虽然中间也有其他东西的学习,但是还是以java为主 ...