ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter¶
animal_crackers('Levelheaded Llama') --> True
animal_crackers('Crazy Kangaroo') --> False
def animal_crackers(text):
wordlist = text.split()
return wordlist[0][0] == wordlist[1][0]
这里用了split 来分隔字符串、后面return 接一个bool判别
MAKES TWENTY: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. If not, return False
makes_twenty(20,10) --> True
makes_twenty(12,8) --> True
makes_twenty(2,3) --> False
def makes_twenty(n1,n2):
return (n1+n2)==20 or n1==20 or n2==20
return式子好于if条件判断
OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name
old_macdonald('macdonald') --> MacDonald
Note: 'macdonald'.capitalize()
returns 'Macdonald'
def old_macdonald(name):
if len(name) > 3:
return name[:3].capitalize() + name[3:].capitalize()
else:
return 'Name is too short!'
这里首先要判断单词是否够数 然后 capitalize函数 大写字符串首字母
MASTER YODA: Given a sentence, return a sentence with the words reversed
master_yoda('I am home') --> 'home am I'
master_yoda('We are ready') --> 'ready are We'
def master_yoda(text):
return ' '.join(text.split()[::-1])
空白加join可以连接 这里只是单词倒序 没有字母倒序 所以可以使用split
关于翻转
[::-1] 顺序相反操作
[-1] 读取倒数第一个元素
[3::-1] 从下标为3(从0开始)的元素开始翻转读取
同样适用于字符串
eg:
a=[1,2,3,4,5]
b=a[::-1]
1
2
[5, 4, 3, 2, 1]
b=a[-1]
1
5
b=a[3::-1]
1
[4, 3, 2, 1]
ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200
almost_there(90) --> True
almost_there(104) --> True
almost_there(150) --> False
almost_there(209) --> True
:
def almost_there(n):
return ((abs(100 - n) <= 10) or (abs(200 - n) <= 10))
多利用return
Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.
has_33([1, 3, 3]) → True
has_33([1, 3, 1, 3]) → False
has_33([3, 1, 3]) → False
def has_33(nums):
for i in range(0, len(nums)-1):
# nicer looking alternative in commented code
#if nums[i] == 3 and nums[i+1] == 3:
if nums[i:i+2] == [3,3]:
return True
return False
这里利用 范围取值 右边的是开区间 并且利用列表相等
PAPER DOLL: Given a string, return a string where for every character in the original there are three characters¶
paper_doll('Hello') --> 'HHHeeellllllooo'
paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'
def paper_doll(text):
result = ''
for char in text:
result += char * 3
return result
构建空字符串 然后累加
SUMMER OF '69: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.
summer_69([1, 3, 5]) --> 9
summer_69([4, 5, 6, 7, 8, 9]) --> 9
summer_69([2, 1, 6, 9, 11]) --> 14
def summer_69(arr):
total = 0
add = True
for num in arr:
while add:
if num != 6:
total += num
break
else:
add = False
while not add:
if num != 9:
break
else:
add = True
break
return total
这是一个非常非常体现python特色的循环判断
for 循环已经确保覆盖所有元素
提前生成一个bool变量 来当做条件控制阀门
while 来做循环 同时每个条件下都有break 来控制
break将返回上层
SPY GAME: Write a function that takes in a list of integers and returns True if it contains 007 in order
spy_game([1,2,4,0,0,7,5]) --> True
spy_game([1,0,2,4,0,5,7]) --> True
spy_game([1,7,2,0,4,5,0]) --> False
def spy_game(nums):
code = [0,0,7,'x']
for num in nums:
if num == code[0]:
code.pop(0) # code.remove(num) also works
return len(code) == 1
列表加x 是为了不被清空
因为可以间断 所有可以不断删除 反向达到目的
COUNT PRIMES: Write a function that returns the number of prime numbers that exist up to and including a given number
count_primes(100) --> 25
def count_primes(num):
primes = [2]
x = 3
if num < 2: # for the case of num = 0 or 1
return 0
while x <= num:
for y in range(3,x,2): # test all odd factors up to x-1
if x%y == 0:
x += 2
break
else:
primes.append(x)
x += 2
print(primes)
return len(primes)
PRINT BIG: Write a function that takes in a single letter, and returns a 5x5 representation of that letter¶
print_big('a')
out: *
* *
*****
* *
* *
HINT: Consider making a dictionary of possible patterns, and mapping the alphabet to specific 5-line combinations of patterns.
For purposes of this exercise, it's ok if your dictionary stops at "E".
def print_big(letter):
patterns = {1:' * ',2:' * * ',3:'* *',4:'*****',5:'**** ',6:' * ',7:' * ',8:'* * ',9:'* '}
alphabet = {'A':[1,2,4,3,3],'B':[5,3,5,3,5],'C':[4,9,9,9,4],'D':[5,3,3,3,5],'E':[4,9,4,9,4]}
for pattern in alphabet[letter.upper()]:
print(patterns[pattern])
upper转化为大写
最后的print 表达了字典多项输出