小白的自我救赎:今日份学习

今天出了一些事~到现在才开始看
题目:
[
’ * ',
’ *** ',
‘*****’
]
让我们以列表的形式输出:

def bulid_tower(n):
	output = []
	for i in range(n):
		str = ''
		for _ in range(n - i - 1):
			str += ' '
		for _ in range(2 * i + 1):
			str +='*'
		for _ in range(n - i - 1):
			str += ' '
		output.append(str)
	return output

大神代码:

def build_tower(n):
	return [('*'*(2*i+1)).center(2*n - 1) for i in range(0,n)]

题目:
Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

就是给你一个整数,把它变成二进制数,然后统计1的个数

我的代码:

def countBits(n):
    n = bin(n)
    n = str(n)
    count = 0
    for i in n:
        if i == '1':
            count += 1
    return count

别人的代码:

def countBits(n):
    count = 0
    while n >0:
        if n & 1 == 1:
            count += 1
        n >>= 1
    return count

注意移位运算符 >>

题目:
Return the number (count) of vowels in the given string. We will consider a, e, i, o, and u as vowels for this Kata.The input string will only consist of lower case letters and/or spaces.
要求我们返回给定字符串中元音字母的个数
很简单的一道编程题
我的代码:

def getCount(inputStr):
    s = 'aeiou'
    count = 0
    for i in inputStr:
        if i in s:
            count += 1
    return count

高亮代码:

def getCount(inputStr):
    return sum(1 for let in inputStr if let in 'aeiou')

sum(iterable, [, start])函数
iterable:可迭代的对象,列表、元组、集合等。
start:指定相加的参数,若缺省则为0
满足if条件时生成一个1加入序列,最终生成一个包含多个1的序列作为sum的函数,求和可得1的个数。

题目:
A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10).
给我们一个数字,让我们判断它是不是一个自幂数,所谓自幂数,就是它本身各个位的n次方相加等于本身,这里面的n是这个数的位数

思路:
先判断这个数字是几位数,再判断是不是自幂数

我的代码:

def narcissistic( value ):
    n = len(str(value))
    newvalue = str(value)
    temp = 0
    for i in newvalue:
        temp += int(i)**n
    if temp == value:
        return True
    else:
        return False

大神代码:

def narcissistic(value):
	return value == sum(int(x)**len(str(value)) for x in str(value))

今日最后一题:
Alice and Bob were on a holiday. Both of them took many pictures of the places they’ve been, and now they want to show Charlie their entire collection. However, Charlie doesn’t like this sessions, since the motive usually repeats. He isn’t fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order?

给我们一个列表,要求相同的元素不能出现n次(n给定)

我的代码:

def delete_nth(order,max_e):
    output = []
    for i in order:
        if output.count(i) < max_e:
            output.append(i)
    return output

难得写这么好一次(主要是以前做过类似的)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值