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. JFrame编程

    最基础的窗口 public test1() { setTitle("test1"); setSize(300,300); //设置窗口大小 setLocation(300,300) ...

  2. 在线服装零售商Betabrand获得650万美元风投 - 投资风向 - 创业邦

    在线服装零售商Betabrand获得650万美元风投 - 投资风向 - 创业邦 在线服装零售商Betabrand获得650万美元风投

  3. 关于cvAbs的那些事

    void cvAbs(const  CvArr* src, const   CvArr*    dst); cvAbs :计算数组中所有的元素的绝对值 // cvAbs函数的使用.cpp : 定义控制 ...

  4. STL中序列式容器的共性

    代码如下: /* * vector_1.cpp * * Created on: 2013年8月6日 * Author: Administrator */ #include <iostream&g ...

  5. c语言中break continue goto return和exit的区别 联系(筛选奇数和goto求和)

    break 一般你是用于循环和switch语句中,执行break,退出循环,如果是多层循环,那么退出的当前的循环. 如果循环结构中有switch语句,而switch语句中有break 那么不会直接退出 ...

  6. c语言学习之结构篇代码演示样例-输入n个同学的姓名,数学英语成绩,依照平均分从低到高排序并输出

    #include<stdio.h> void main(){ const int count = 5;//定义数量 struct student{ char name[80]; float ...

  7. Linux RAR 安装和使用详细说明

    描述:Linux默认自带ZIP压缩,最大支持4GB压缩,RAR的压缩比大于4GB.  流程:下载 >安装 > 使用  ----------------------------------- ...

  8. 将samba加入到windows域《转载》

    将samba加入到windows域 那什么是域呢? 一台Windows计算机,它要么隶属于工作组,要么隶属于域.所以说到域,我们就不得不提一下工作组,工作组是MS的概念,一般的普遍称谓是对等网. 工作 ...

  9. OC特有语法-分类(category)

    本文转载Keefo. Objective-C的Object-oriented programming特性提供subclass和category这2个比较非常重要的部分.subclass应该反复被各种编 ...

  10. UIActivityIndicatorView活动控制器的大小改变

    self.activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicat ...