Problem 7: Double Ones (100pts)
Problem
Write a function that takes in a number and determines if the digits contain two adjacent 1s. (Reviewing this problem in Lab 01 might be helpful here!)
编写一个函数,接受一个数字作为参数,并判断其数字中是否包含两个相邻的 1。(回顾 Lab 01 中的 这个问题 可能会有所帮助!)
def double_ones(n):
"""Return true if n has two ones in a row.
>>> double_ones(1)
False
>>> double_ones(11)
True
>>> double_ones(2112)
True
>>> double_ones(110011)
True
>>> double_ones(12345)
False
>>> double_ones(10101010)
False
"""
"*** YOUR CODE HERE ***"
Hints
-
可以使用
str
逃课() -
大致思路:取最后两位看看是不是11,如果不是,去掉最后一位再判断
-
while
循环和递推都很好写