地址: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. VS2012 独占编辑 设置

    用VS2008建立的项目就有(禁止其他用户签出和签入) 为什么VS2012的项目就没有了呢??(管理员说他什么都没设置)VS2012: 两个项目同样是用VS2012打开的,而第一个是用VS2008创建 ...

  2. canvas图片的跨域问题

    科普文章from MDN 实践证明这篇里的回答对的: .起个服务器再在chrome里试一下,应该会跑通. .右键chrome,属性,在目标后面加上(有个空格) --allow-file-access- ...

  3. Serv-u FTP服务器

    它可以让我们通过http协议(web端)或者通过其他软件进行连接,从而可以操作服务器上的文件数据.

  4. Android 使用XmlPullParser解析xml

    这里我们假设要解析的xml文件名为:test.xml,我们将其放在assets路径中. xml文件内容为: <?xml version='1.0' encoding='utf-8' standa ...

  5. ANDROID_MARS学习笔记_S05_006_距离传感器

    import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import ...

  6. VC自动与Internet时间服务器同步更新

    在VCKBASE.CSDN里挖了许久的坟,才找到一些有点用的资料,最后自己整理出这样的个函数,方面VC实现时间同步,多的不说,自己看源码,根据自己的需要可以适当修改源码: #include <W ...

  7. Why Creating a Meaningful Morning Routine Will Make You More Successful

    https://medium.com/life-learning/how-creating-a-meaningful-morning-routine-will-make-you-more-succes ...

  8. 利用if else来运行咱们结婚吧

    static void Main(string[] args)        {            while (true)            {                string ...

  9. POJ_3176_Cow_Bowling_(数字三角形)_(动态规划)

    描述 http://poj.org/problem?id=3176 给出一个三角形,每个点可以走到它下面两个点,将所有经过的点的值加起来,问最大的和是多少. Cow Bowling Time Limi ...

  10. BZOJ_1202_狡猾的商人_(并查集)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1202 n 个月的账单,共 m 组数据,每一组数据包括 x , y , t ,表示从 x 月到 ...