地址: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. iOS - instancetype

    OC是一门正在迅速发展的语言,ARC,object literals ,subscripting ,blocks,Auto Synthesis,让我们看到它惊人的改变.instancetype是cla ...

  2. LightOj_1321 Sending Packets

    题目链接 题意: 给一个数据大小为S的数据包, 每一次发送需要K秒(单向),现在要从节点0 发送到节点 n-1. 其中有n - 1条路径, 每条路径都有一个传输成功率. 问传输成功所需最小时间的期望. ...

  3. ajax 同步异步调用

  4. Hbase热点问题

    需求描述:扫描(查询)某个区间--->列用hbase多节点的资源,分布式扫描,加快速度==> 然后拼接到一起 如何打散数据 冠字号逆序,hash 并不一定数据连续就会造成热点,这个是由数据 ...

  5. 把 Eclipse 中的工程 Push 到 Github(适用 Windows 平台)

    今天发现一小技巧,关于如何把Eclipse的某一个Existing project push 到github服务器. Eclipse 应该是 JavaEE 版本. 在project 右键 team, ...

  6. ReactEurope Conf 参会感想

    React 带来的革命性创新是前端世界过去几年最激动人心的变化.自从接触 React 以来,我深信 React 会改变客户端开发者(包括前端.iOS 和 Android)的开发体验.这次在巴黎举办的  ...

  7. uva 1482 - Playing With Stones

    对于组合游戏的题: 首先把问题建模成NIM等经典的组合游戏模型: 然后打表找出,或者推出SG函数值: 最后再利用SG定理判断是否必胜必败状态: #include<cstdio> #defi ...

  8. Mongodb与关系型数据库

    MongoDB没有固定的关系约束 没有事务, 安全性不高 不一定保证数据的一致性. ACID不符合 NoSQL 放弃了传统关系型数据库严格的事务一致性和范式约束,采用弱一致性模型. http://os ...

  9. ubuntu修改grub2

    转自修改系统启动项 grub2配置的方法 ubuntu 在早期的Ubuntu中,使用Grub作为系统的启动引导程序,想修改系统启动项非常简单,只要用gedit打开系统菜单设定文件( sudo gedi ...

  10. 如何在C++中使用WebService

    gsoap主页 http://sourceforge.net/projects/gsoap2   使用gsoap生成所需的WebService 下载后的gsoap包为:(点击到我的资源中下载) 将他解 ...