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
nto
be 1000. Initializenumbersto
be a list of numbers from 2 to n, but not includingn. - With
resultsstarting
as the empty list, repeat the following as long asnumberscontains
any numbers.- Add the first number in
numbersto
the end ofresults. - Remove every number in
numbersthat
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 ...
随机推荐
- 如何使ListView具有像ios一样的弹性
ListView 是我们在开发过程中经常使用的控件之一,通常情况下,当我们没有对它进行自定义或者给添加headerview 或者footerView的时候,他都没有一个很好的反馈效果,但是相比较而言, ...
- Spring 上下文
Spring 上下文WebApplicationContext.是服务器启动的时候加载ContextLoaderListener 的时候存在 ServletContext 中 servletConte ...
- Python3.2官方文档翻译--作用域和命名空间
6.2 Python作用域和命名空间 在介绍类之前.首先我想告诉你一些关于python作用域的规则. 类的定义很巧妙地运用了命名空间,你须要知道范围和命名空间的工作原理以能全面了解接下来发生的. 顺便 ...
- 扩大或缩小undo表空间
***********************************************声明*************************************************** ...
- ANDROID对文件的操作
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...
- [翻译]Go语言调度器
Go语言调度器 译序 本文翻译 Daniel Morsing 的博文 The Go scheduler.个人认为这篇文章把Go Routine和调度器的知识讲的浅显易懂.作为一篇介绍性的文章.非常不错 ...
- Css Rest 方法
在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...
- hdu 5104 Primes Problem(prime 将三重循环化两重)
//宁用大量的二维不用量小的三维 #include <iostream> #include<cstdio> #include<cstring> using name ...
- HDU2023-求平均成绩
描述: 假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量. 输入数据有多个测试实例,每个测试 ...
- OpenGL: 环境配置和图元的绘制
前言 距离上一篇博客已经过去一个半月了,这段时间过得确实充实,虽然一大段时间泡在图书馆复习,但至少也能学到点东西.跨年晚和元旦一整天,全身心投入图形学小课设的编程,终于实现了老师要求的所有功能,回想起 ...