Date:2018-05-08

1、Given: an array containing hashes of names

Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.

Example:

namelist([ {'name': 'Bart'}, {'name': 'Lisa'}, {'name': 'Maggie'} ])
# returns 'Bart, Lisa & Maggie'

namelist([ {'name': 'Bart'}, {'name': 'Lisa'} ])
# returns 'Bart & Lisa'

namelist([ {'name': 'Bart'} ])
# returns 'Bart'

namelist([])
# returns ''

Best Practices:

def namelist(names):
    if len(names) > 1:
        return '{} & {}'.format(', '.join(name['name'] for name in names[:-1]),
                                names[-1]['name'])
    elif names:
        return names[0]['name']
    else:
        return ''

My solutions:

def namelist(names):
    #your code here
    if len(names) > 1:
        first_name = ', '.join(tmp_name['name'] for tmp_name in names[:-1])
        last_name = names[-1]['name']

        return first_name + ' & ' + last_name
    elif names:
        return names[0]['name']
    else:
        return ''

2、Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.

Examples

to_camel_case("the-stealth-warrior") # returns "theStealthWarrior"

to_camel_case("The_Stealth_Warrior") # returns "TheStealthWarrior"

Best Practices:

def to_camel_case(s):
    return s[0] + s.title().translate(None, "-_")[1:] if s else s

My solutions:

import re
def to_camel_case(text):
    result = ''
    tmp_re = re.split('[-_]',text)

    for i in range(1,len(tmp_re)):
        result += tmp_re[i].capitalize()

    return tmp_re[0] + result

3、In a small town the population is p0 = 1000 at the beginning of a year. The population regularly increases by 2 percent per year and moreover 50 new inhabitants per year come to live in the town. How many years does the town need to see its population greater or equal to p = 1200 inhabitants?

At the end of the first year there will be:

1000 + 1000 * 0.02 + 50 => 1070 inhabitants

At the end of the 2nd year there will be:

1070 + 1070 * 0.02 + 50 => 1141 inhabitants (number of inhabitants is an integer)

At the end of the 3rd year there will be:

1141 + 1141 * 0.02 + 50 => 1213

It will need 3 entire years.

Best Practices:

def nb_year(population, percent, aug, target):
    year = 0
    while population < target:
        population += population * percent / 100. + aug
        year += 1
    return year

My solutions:

def nb_year(p0, percent, aug, p):
    re = p0
    year = 0
    while p0 < p:
        p0 = p0*(1+percent/100) + aug
        year += 1

    return year 

4、You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.

Examples

[2, 4, 0, 100, 4, 11, 2602, 36]

Should return: 11 (the only odd number)

[160, 3, 1719, 19, 11, 13, -21]

Should return: 160 (the only even number)

Best Practices:

def find_outlier(integers):
    parity = [n % 2 for n in integers]
    return integers[parity.index(1)] if sum(parity) == 1 else integers[parity.index(0)]

My solutions:

def find_outlier(intergers):
    ji = [x for x in intergers if x%2!=0]
    ou = [x for x in intergers if x%2==0]
    return ji[0] if len(ji)<len(ou) else ou[0]

Date:2018-05-010

1、There is an array with some numbers. All numbers are equal except for one. Try to find it!

findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2

findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55

Best Practices:

def find_uniq(arr):
    a, b = set(arr)
    return a if arr.count(a) == 1 else b

My solutions:

def find_uniq(arr):
    if arr[0] != arr[1]:
        for i in arr[2:]:
            if i == arr[0]:
                n = arr[1]
            else:
                n = arr[0]
    else:
        for i in arr[2:]:
            if i != arr[0]:
                n = i

    return n

Date:2018-05-16

1、Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.

For example:

unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']

unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']

unique_in_order([1,2,2,3,3]) == [1,2,3]

Best Practices:

unique_in_order = lambda l: [z for i, z in enumerate(l) if i == 0 or l[i - 1] != z]

My solutions:

def unique_in_order(iterable):
    result = []
    pre = ''
    for x in iterable[0:]:
        if x != pre:
            result.append(x)
            pre = x
    return result

Date:2018-05-17

1、You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.

Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:

likes [] // must be "no one likes this"

likes ["Peter"] // must be "Peter likes this"

likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"

likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"

likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"

Best Practices:

def likes(names):
    n = len(names)
    return {
        0: 'no one likes this',
        1: '{} likes this',
        2: '{} and {} like this',
        3: '{}, {} and {} like this',
        4: '{}, {} and {others} others like this'
    }[min(4, n)].format(*names[:3], others=n-2)

My solutions:

def likes(names):
    if names:
        if 1 == len(names):
            return names[0] + ' likes this'
        elif 2 == len(names):
            return names[0] + ' and ' + names[1] + ' like this'
        elif 3 == len(names):
            return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this'
        else:
            l = len(names) - 2
            return names[0] + ', ' + names[1] + ' and ' + str(l) + ' others like this'
    else:
        return  'no one likes this'

Python题库的更多相关文章

  1. 『Python题库 - 填空题』151道Python笔试填空题

    『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...

  2. 『Python题库 - 简答题』 Python中的基本概念 (121道)

    ## 『Python题库 - 简答题』 Python中的基本概念 1. Python和Java.PHP.C.C#.C++等其他语言的对比? 2. 简述解释型和编译型编程语言? 3. 代码中要修改不可变 ...

  3. Python题库系列分享一(17道)

    1.1  到Python官方网站下载并安装Python解释器环境.1.2  到Anaconda官方网站下载并安装最新的Anaconda3开发环境.1.3  Python程序的__name__的作用是什 ...

  4. python 题库1

    1. 生成一个1到50的大字符串,每个数字之间有个空格,例如1 2 3 4 ……50 解题思路: (1)声明一个空字符串变量用于保存生成的字符串 (2)使用for循环+range()函数生成一个1到5 ...

  5. Python解答力扣网站题库简单版----第三讲

    1041. 困于环中的机器人 题库链接: 1041. 困于环中的机器人. 题干 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G" ...

  6. 小学生噩梦——四则运算题库(python 全功能实现)

    Github: https://github.com/holidaysss 小组:龙天尧(代码实现),林毓植(浮点转分数函数,代码审查) PSP2.1 Personal Software Proces ...

  7. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  8. Python 标准库一览(Python进阶学习)

    转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...

  9. Python turtle库详解

    Python turtle库详解 Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在 ...

随机推荐

  1. C 打印格式小记

    转自:http://blog.csdn.net/fivedoumi/article/details/7077504 d,lx,ld,,lu,这几个都是输出32位的 hd,hx,hu,这几个都是输出16 ...

  2. C语言之可变参实现scanf函数

    既然有printf函数可变参实现,那就一定有scanf函数的可变参实现.废话不多说,源码奉上: 本源码不过多分析,如要明白原理,请翻本博客以往的文章看说明. 欢迎关注新浪微博:http://weibo ...

  3. javascript、ruby和C性能一瞥(3) :上汇编

    在博文(1)和(2)里分别用了4中方式写一个素数筛选的算法,分别是javascript in browser.node.js.ruby和c:最终的结果是c最快,node.js其次,js in b虽然也 ...

  4. ubuntu下无法编译ruby-2.1.5提示something wrong with CFLAGS -arch x86_64

    在Mac OS X10.10下以下语句运行没有问题: ./configure -prefix=/Users/apple/src/ruby_src/ruby2.1.5_installed --with- ...

  5. ELF 动态链接 so的动态符号表(.dynsym)

    静态链接中有一个专门的段叫符号表 -- ".symtab"(Symbol Table), 里面保存了所有关于该目标文件的符号的定义和引用. 动态链接中同样有一个段叫 动态符号表 - ...

  6. Qt Creator 更改默认构建目录到工程目录下

    Qt Creator 更改默认构建目录到工程目录下 步骤 工具->选项->构建和运行->概要->Default build directory->去掉第一个". ...

  7. 万水千山ABP - 弹出对话框禁用回车

    模态对话框中禁用回车 ABP Zero 中,使用弹出对话框进行实体编辑,回车时会自动保存并关闭对话框.那么如何禁用这个回车功能 ? 查看实体列表视图 index.cshtml 所对应加载的脚本文件 i ...

  8. MySQL中的行级锁,表级锁,页级锁

    在计算机科学中,锁是在执行多线程时用于强行限制资源访问的同步机制,即用于在并发控制中保证对互斥要求的满足. 在数据库的锁机制中介绍过,在DBMS中,可以按照锁的粒度把数据库锁分为行级锁(INNODB引 ...

  9. generate parentheses(生成括号)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  10. Spring2.5整合Ibatis入门级开发实例

      分类: SPRING IBATIS2010-11-22 20:19 1170人阅读 评论(0) 收藏 举报 ibatisspringstringpropertiesclassuser 最近一直在看 ...