Common Words

Let's continue examining words. You are given two string with words separated by commas. Try to find what is common between these strings. The words are not repeated in the same string.

Your function should find all of the words that appear in both strings. The result must be represented as a string of words separated by commas in alphabetic order.

Tips: You can easily solve this task with several useful functions:str.splitstr.join and sorted. Also try using the built-in type -- set.

Input: Two arguments as strings.

Output: The common words as a string.

题目大义:给出两个字符串,找出在两个字符串中同时出现的单词,并按字典序输出

当然使用了str.split方法,然后想到set有in方法,于是乎得到这么一段代码

 1 def checkio(first, second):
2 first_str = first.split(',')
3 words_set = set(first_str)
4
5 second_str = second.split(',')
6
7 result = []
8
9 for each in second_str:
10 if each in words_set:
11 result.append(each)
12 else:
13 words_set.add(each)
14
15 result.sort()
16
17
18 return ','.join(result);

接下来看了别人的解答,发现其实可以把两个字符串split后,转化为set,再使用set的intersection方法即可得到相同单词

Common Words的更多相关文章

  1. Socket聊天程序——Common

    写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...

  2. angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)

    common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...

  3. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  4. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  5. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  6. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  7. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  8. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  9. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  10. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

随机推荐

  1. Android 获取文件大小

    android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位 FileSizeUtil public class FileSizeUtil { ;//获取文件大小单位为B的double值 ; ...

  2. TestNG基本注解(注释)

    传统的方式来表示JUnit3中的测试方法是测试自己的名字前缀.标记一个类中的某些方法,具有特殊的意义,这是一个非常有效的方法,但命名不很好的扩展(如果我们想添加更多标签为不同的框架?),而非缺乏灵活性 ...

  3. 【转】android 开发 命名规范

    原文网址:http://www.cnblogs.com/ycxyyzw/p/4103284.html 标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外, ...

  4. 手游与App测试如何快速转型? —— 过来人科普手游与App测试四大区别

    随着智能设备的普及和移动互联网的兴起,各家互联网巨头纷纷在往移动端布局和转型,同时初创的移动互联网公司也都盯着这个市场希望分一杯羹.在这个大环境下,互联网的重心已经慢慢从Web端转向了移动端,而移动端 ...

  5. Bring it on

    I am going to open a whole new English Blog here. Most blogs here would be computer technologies, in ...

  6. jQuery 各种选择器 $.()用法

    jQuery 元素选择器jQuery 使用 CSS 选择器来选取 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") 选 ...

  7. 文件夹65ad47d7-2e27-4a5c-b238-26643fdaeb98

    这几天发现电脑中毒了,本地开的服务预览页面时,页面会被插入Html代码.我用360扫描之后发现有木马病毒(c:\programData有个65ad47d7-2e27-4a5c-b238-26643fd ...

  8. WebGIS在行业中应用的演变

    结合我本身的项目及WebGIS在公检法行业中的应用,对此作了一个演变过程的总结:         第一阶段:GIS基本功能的应用:Data Show(数据展示):Search(搜索):Search b ...

  9. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  10. [小技巧] 把虚拟机中的Linux系统安装到U盘中

    出于各种需求,很多用户可能经常会在Windows系统中安装虚拟机,然后在虚拟机中安装Linux系统.使用虚拟机的优点是可以同时使用多个系统,而缺点也是显然的,也就是程序运行效率较差.   而实际上,L ...