Quiz 6b Question 7————An Introduction to Interactive Programming in Python
Question 7
Convert the following English description into code.
- Initialize
n
to
be 1000. Initialize numbers
to
be a list of numbers from 2 to n, but not including n
.
- With
results
starting
as the empty list, repeat the following as long as numbers
contains
any numbers.
- Add the first number in
numbers
to
the end of results
.
- Remove every number in
numbers
that
is evenly divisible by (has no remainder when divided by) the number that you had just added to results
.
How long is results
?
To test your code, when n
is
instead 100, the length of results
is
25.
代码:
lis = list(range(2,1000))
print lis[0]
res =[]
print len(res)
res.append(lis[0])
while len(lis)>1:
for i in lis:
if i % res[-1]==0:
lis.remove(i)
res.append(lis[0])
print lis
print len(res)
Convert the following English description into code.
- Initialize
n
to
be 1000. Initializenumbers
to
be a list of numbers from 2 to n, but not includingn
. - With
results
starting
as the empty list, repeat the following as long asnumbers
contains
any numbers.- Add the first number in
numbers
to
the end ofresults
. - Remove every number in
numbers
that
is evenly divisible by (has no remainder when divided by) the number that you had just added toresults
.
- Add the first number in
How long is results
?
To test your code, when n
is
instead 100, the length of results
is
25.
代码:
lis = list(range(2,1000))
print lis[0]
res =[]
print len(res)
res.append(lis[0])
while len(lis)>1:
for i in lis:
if i % res[-1]==0:
lis.remove(i)
res.append(lis[0])
print lis print len(res)
Quiz 6b Question 7————An Introduction to Interactive Programming in Python的更多相关文章
- Quiz 6b Question 8————An Introduction to Interactive Programming in Python
Question 8 We can use loops to simulate natural processes over time. Write a program that calcula ...
- Quiz 6a Question 7————An Introduction to Interactive Programming in Python
First, complete the following class definition: class BankAccount: def __init__(self, initial_bal ...
- An Introduction to Interactive Programming in Python
这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习
Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...
- Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...
- 【python】An Introduction to Interactive Programming in Python(week two)
This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_2 练习
#Practice Exercises for Logic and Conditionals # Solve each of the practice exercises below. # 1.Wri ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习
# Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python ...
- Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"
Mini-project #4 - "Pong" In this project, we will build a version of Pong, one of the firs ...
随机推荐
- The error indicates that IIS is in 32 bit mode, while this application is a 64 b it application and thus not compatible.
I was trying to install a new WSS v3 Sharepoint on a 64 bit Windows 2003 server today but the instal ...
- Data Mining
数据探索: 数据探索有助于选择合适的数据处理与数据分析技术.它甚至可以解决一些数据挖掘问题. 1.汇总统计:量化用单个数或数据的集合展示数据的特性: 如集合的平均值.方差. 2.可视化技术:以图形或表 ...
- USB創意讀卡機設計特別獎
- Echoprint系列--Android编译与调用
在Echoprint系列--编译中编译了源代码,这次将Echoprint移植到Android平台并測试识别歌曲功能. 一.编译库 1.环境准备 Android NDK,我的是android-ndk-r ...
- cocos2d-x-3.1 Text Labels(官方正式译文)
介绍 cocos2d支持(true type字体)标签,和纹理地图集标签. LabelTTF 标签的优缺点: 全部 TTF 字体的长处: 随意大小,支持调整字距. 易于使用. 不须要外部编辑器. 创建 ...
- DEV GridControl 小结(持续添加)
一.属性: 1.Views OptionsBehavior=>Editable:False 列表不可编辑 OptionsSelection=>EnableAppearanceFocuse ...
- VIJOS 1512SuperBrother打鼹鼠(二维BIT)
呵呵.. 二维树状数组,第二维和第一维基本一样. --------------------------------------------------------------------------- ...
- InputStream和OutputStream 何时使用
原文引自:http://blog.csdn.net/fyxxq/article/details/7071978 记得刚学习程序流一章的时候,就是搞不清楚In和Out,不知道什么时候用in什么时候用ou ...
- Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化
一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...
- javascript Node操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...