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 ...
随机推荐
- while 、do...while 、for
1.while 特点:只有条件成立才会执行循环体. while陷阱: while(条件);即直接加分号 2.do while 特点:一定会执行一次循环体 3.for语句 l 初始化等可以是多句(把 ...
- linux之sed
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- 在Ubuntu 11.10工具栏上用数字显示网速、CPU负荷和内存占用量『译』
基本上照抄了<How To Display Network Upload / Download Speed On The Panel In Ubuntu 11.04>,只不过我的实践环境是 ...
- 当cpu飙升时,找出php中可能有问题的代码行
参考大牛: http://www.searchtb.com/2014/04/%E5%BD%93cpu%E9%A3%99%E5%8D%87%E6%97%B6%EF%BC%8C%E6%89%BE%E5%8 ...
- cocos2dx 字体BMFont,Atlas
为了更加个性化,系统提供的字体,有时候没法满足我们的要求,所以cocos2dx提供了自定义字体控件. 分别是CCLabelBMFont和CCLabelAtlas,先看BMFont的效果 CCLabel ...
- VM Agent 和扩展程序
VM Agent 和扩展程序 - 第 1 部分 Windows Azure基础结构服务最近宣布了一项新功能VM Agent.VMAgent是一个轻量级进程,用于启动由Microsoft或合作伙伴 ...
- C# DLL文件注册问题(涉及AxInterop.WMPLib.dll等)
近日遇到问题,给客户安装软件涉及视频等音影播放,安装软件启动过程遇到这样问题: 分析报错原因: 没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG ...
- distributor之Interrupt Set/Clear-Active Registers, GICD_IS/CACTIVERn
set active寄存器.顾名思义就是把一个中断置为active状态,clear active寄存器就是清除active状态,在这里我们有必要说明一下中断状态的一些概念: active状态:假设此时 ...
- 详解Objective-C中委托和协议
Objective-C委托和协议本没有任何关系,协议如前所述,就是起到C++中纯虚类的作用,对于“委托”则和协议没有关系,只是我们经常利用协议还实现委托的机制,其实不用协议也完全可以实现委托. AD: ...
- 注册表缺失导致Windows Server 2008 R2时钟服务W32time不能自启
参照@飘云 http://blog.csdn.net/piaoyunqing/article/details/6323647 的文章. 测试环境中有一台Windows Server 2008 R2的虚 ...