Title Case
地址: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的更多相关文章
- Title Case a Sentence-freecodecamp算法题目
Title Case a Sentence(中单词首字母大写) 要求 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 思路 将句子小写化后用.split(& ...
- Title Case a Sentence
解决思路 将字符串转换成小写 把字符串分割成字符串数组 循环数组将每一个单词首字母大写 把数组所有的元素转换成一个字符串 第一种方法 function titleCase(str) { str=str ...
- freeCodeCamp:Title Case a Sentence
确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. /*思路 将字符串转为小写.toLowerCase() 分割字符串以单词形式组成数组myarr 确保数组中的 ...
- FreeCodeCamp:Title Case a Sentence
要求: 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 结果: titleCase("I'm a little tea pot") 应该 ...
- FCC JS基础算法题(4):Title Case a Sentence(句中单词首字母大写)
题目描述: 确保字符串的每个单词首字母都大写,其余部分小写.像'the'和'of'这样的连接符同理. 算法: function titleCase(str) { // 转小写及分割成数组 var st ...
- CASE WHEN 及 SELECT CASE WHEN的用法(转)
Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- [Upper case conversion ] 每个单词的首小写字母转换为对应的大写字母
Given a string , write a program to title case every first letter of words in string. Input:The firs ...
- CASE WHEN 批量更新
单个值: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHE ...
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 复制代码 代码如下: UPDATE mytable SET myfield = CASE i ...
随机推荐
- POJ 1837 Balance 01背包
题目: http://poj.org/problem?id=1837 感觉dp的题目都很难做,这道题如果不看题解不知道憋到毕业能不能做出来,转化成了01背包问题,很神奇.. #include < ...
- bzoj 1209: [HNOI2004]最佳包裹 三维凸包
1209: [HNOI2004]最佳包裹 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 160 Solved: 58[Submit][Status] ...
- bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形 容斥
1914: [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 272 Sol ...
- 开源网站管理工具—Altman
0×00前言 之前用过几款webshell工具,有B/C的也有C/S的,有的只能用于php或者aspx,当然个人用得最多.觉得用得舒服的也只有菜刀了. 但是毕竟菜刀是好几年之前的产物了,而且也已经停止 ...
- 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)
[题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...
- 【UVA 11183】 Teen Girl Squad (定根MDST)
[题意] 输入三元组(X,Y,C),有向图,定根0,输出MDST. InputThe first line of input gives the number of cases, N (N < ...
- 了解php的session_start的工作原理
一.php使用session_start开启SESSION 二.浏览器访问该php脚本时,将产生两个可能: 1.(客户端的提交的cookie没有找到PHPSESSID的键) 或 (在服务器端没有找到P ...
- 一台机器上运行多个ActiveMq
由于业务需要一台机器上运行多个ActiveMq,这里主要说一下有什么地方不重复: 1.brokerName名称不能重复 2.端口号不能重复uri = tcp://localhost:50509 3.k ...
- 转自 x_x的百度空间
空华人生 by 淡漠的心情 昨天,又昨天. 今天,又今天. 明天,又明天. 日历渐渐稀薄,忽然发现,那是时间的痕迹. 似乎,总是在麻木的等待. 何时,才能历尽. 再算算,我又还有多少天 ...
- Android 应用页面延缓载入
1.新建一个线程,使用handle的延缓运行线程 new Handler().postDelayed(new Runnable() { // 为了减少代码使用匿名Handler创建一个延时的调用 pu ...