#!/usr/bin/env python
# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# Name: lab01.py
# Description:
# Author: handa
# Date: 2022/4/14
#-------------------------------------------------------------------------------
def falling(n, k):
"""Compute the falling factorial of n to depth k.
>>> falling(6, 3) # 6 * 5 * 4
120
>>> falling(4, 3) # 4 * 3 * 2
24
>>> falling(4, 1) # 4
4
>>> falling(4, 0)
1
"""
"*** YOUR CODE HERE ***"
if k == 0:
return 1
else:
return n * falling(n - 1, k - 1)
res = falling(6,3)
print(res)
def sum_digits(y):
"""Sum all the digits of y.
>>> sum_digits(10) # 1 + 0 = 1
1
>>> sum_digits(4224) # 4 + 2 + 2 + 4 = 12
12
>>> sum_digits(1234567890)
45
>>> a = sum_digits(123) # make sure that you are using return rather than print
>>> a
6
"""
"*** YOUR CODE HERE ***"
sum = 0
for i in str(y):
sum += int(i)
return sum
res = sum_digits(4224)
print(res)
'''
只有string,list,tuple,dict才是可迭代对象,所以把int转成str,但同时要转回int才能进行整型比较运算
'''
def double_eights(n):
"""Return true if n has two eights in a row.
>>> double_eights(8)
False
>>> double_eights(88)
True
>>> double_eights(2882)
True
>>> double_eights(880088)
True
>>> double_eights(12345)
False
>>> double_eights(80808080)
False
"""
"*** YOUR CODE HERE ***"
count_eights = 0
for i in str(n):
if count_eights >=2:
return True
if int(i) == 8:
count_eights += 1
else:
if count_eights > 0:
count_eights -= 1
if count_eights >= 2:
return True
else:
return False
'''
这道题主要是结束的边界条件,要连续两个8就可以结束,非连续两个8不符合条件,所以这时你就会写成遇到8,count +=1,非8 count -=1
但是这会导致还没有遇到8时先减成负数,所以边界两件是两个
一个是遇到8才开始count + 或-,二是满足2个就成立返回
'''
以下是官方解法
def falling(n, k):
"""Compute the falling factorial of n to depth k.
>>> falling(6, 3) # 6 * 5 * 4
120
>>> falling(4, 3) # 4 * 3 * 2
24
>>> falling(4, 1) # 4
4
>>> falling(4, 0)
1
"""
total, stop = 1, n - k
while n > stop:
total, n = total * n, n - 1
return total
def sum_digits(y):
"""Sum all the digits of y.
>>> sum_digits(10) # 1 + 0 = 1
1
>>> sum_digits(4224) # 4 + 2 + 2 + 4 = 12
12
>>> sum_digits(1234567890)
45
>>> a = sum_digits(123) # make sure that you are using return rather than print
>>> a
6
"""
total = 0
while y > 0:
total, y = total + y % 10, y // 10
return total
'''
依次 %除模,求余数,再每次除10,最终累加
比如123
>>> 123 % 10
3
>>> 123 // 10
12
>>> 12 % 10
2
>>> 12 // 10
1
>>> 1 % 10
1
两个操作,取余获取低位余数,除10,去除取余的低位数
'''
def double_eights(n):
"""Return true if n has two eights in a row.
>>> double_eights(8)
False
>>> double_eights(88)
True
>>> double_eights(2882)
True
>>> double_eights(880088)
True
>>> double_eights(12345)
False
>>> double_eights(80808080)
False
"""
prev_eight = False
while n > 0:
last_digit = n % 10
if last_digit == 8 and prev_eight:
return True
elif last_digit == 8:
prev_eight = True
else:
prev_eight = False
n = n // 10
return False
def k_in_num(k, num):
"""
Complete k_in_num, a function which returns True if num has the digit k and
returns False if num does not have the digit k. 0 is considered to have no
digits.
>>> k_in_num(3, 123) # .Case 1
True
>>> k_in_num(2, 123) # .Case 2
True
>>> k_in_num(5, 123) # .Case 3
False
>>> k_in_num(0, 0) # .Case 4
False
"""
"*** YOUR CODE HERE ***"
while num > 0:
if num % k == 0:
return True
else:
num = num // 10
return False
from turtle import st
def add_in_range(start, stop):
"""
>>> add_in_range(3, 5) # .Case 1
12
>>> add_in_range(1, 10) # .Case 2
55
"""
"*** YOUR CODE HERE ***"
sum = 0
for i in range(start,stop+1):
sum += i
return sum
def digit_pos_match(n, k):
"""
>>> digit_pos_match(980, 0) # .Case 1
True
>>> digit_pos_match(980, 2) # .Case 2
False
>>> digit_pos_match(98276, 2) # .Case 3
True
>>> digit_pos_match(98276, 3) # .Case 4
False
"""
"*** YOUR CODE HERE ***"
while n>0:
x = n % 10
if x == k:
return True
else:
n = n // 10
return False
def k_occurrence(k, num):
"""
>>> k_occurrence(5, 10) # .Case 1
0
>>> k_occurrence(5, 5115) # .Case 2
2
>>> k_occurrence(0, 100) # .Case 3
2
>>> k_occurrence(0, 0) # .Case 4
0
"""
"*** YOUR CODE HERE ***"
if num == 0:
return 0;
countk = 0
while num :
if num % 10 == k:
countk +=1
num = num // 10
return countk
$ /d/Python310/python.exe ok --submit
=====================================================================
Assignment: Lab 1
OK, version v1.18.1
=====================================================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
20 test cases passed! No cases failed.
Submit... 0.0% complete
Could not submit: Late Submission of cal/cs61a/sp22/lab01
OK is up to date
学习笔记
运算符相关
Notice that Python outputs `ZeroDivisionError` for certain cases. We will go over this later in this lab under [Error Messages](https://cs61a.org/lab/lab01/#error-messages).
One useful technique involving the `%` operator is to check whether a number `x` is divisible by another number `y`:
x % y == 0
For example, in order to check if `x` is an even number:
x % 2 == 0
以上是官方的注释,大致意思 是%取模可以用来验证 x % y,y 是不是可以被完整y完全整除
123 % 10,所以按照上面的模式来算,每次个位数不可能被 10 % 个位整除,所以每次拿到个位数,又 // 10 除10,去掉低位数,不断迭代, // 10 相当于指针偏移量,不断把数往高位移,移动位取决于 除以基数。
逻辑条件短路
Short-circuiting happens when the operator reaches an operand that allows them to make a conclusion about the expression. For example, and will short-circuit as soon as it reaches the first false value because it then knows that not all the values are true.
If and and or do not short-circuit, they just return the last value; another way to remember this is that and and or always return the last thing they evaluate, whether they short circuit or not. Keep in mind that and and or don't always return booleans when using values other than True and False.
如果AND条件,遇到false条件则短路中止执行 如果or条件,遇到True条件则短信中止执行