lecture 2.1 简单算法

简介: (一)while循环1. Convert the following into code that uses a while loop.

(一)while循环

1. Convert the following into code that uses a while loop.
用一个while循环将下列转换成代码 
print  2
print  4
print  6
print  8
print  10
print  Goodbye!
t = 2
while t <= 10:
	print t
	t += 2
print 'Goodbye!'

2. Convert the following into code that uses a while loop.
用一个当型循环将下列转换成代码 
print  Hello!
print  10
print  8
print  6
print  4
print  2
print 'Hello!'
t = 10
while t >= 2:
	print t
	t -= 2

3. Write a while loop that sums the values 1 through  end , inclusive.  end  is a variable that we define for you. So, for example, if we define  end  to be 6, your code should print out the result:
编写一个从1加到end的当型循环。变量end的值由我们给出。所以,举个例子,假如我们把end的值定为6,那么你的代码输出的结果应该是21,也就是1+2+3+4+5+6的结果。 
21
which is 1 + 2 + 3 + 4 + 5 + 6.
For problems such as these, do not include  raw_input  statements or define the variable  end . Our automating testing will provide a value of  end  for you - so write your code in the following box assuming  end  is already defined.
对于这样的问题,请不要包含raw_input类型的语句或者给变量end赋值。我们的自动化测试将会帮你给end赋值-所以假定end已经被赋值,在下面的方框里写下你的代码。 
t = 1
s = 0
while t <= end:
	s += t
	t += 1
print s

(二)for循环

1. Convert the following code into code that uses a for loop. 将下列代码转换成使用for循环的代码。

 
        
print 2
print 4
print 6
print 8
print 10
print "Goodbye!"

t = 2
for x in range(5):
    print t
    t += 2 
print 'Goodbye!'

2. Convert the following code into code that uses a for loop. 将下列代码转换成使用for循环的代码。

print "Hello!"
print 10
print 8
print 6
print 4
print 2

t = 10
print 'Hello!'
for x in range(5):
    print t
    t -= 2 

3. Write a for loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:

编写一个从1加到end的for循环。变量end的值由我们给出。所以,举个例子,假如我们把end的值定为6,那么你的代码输出的结果应该是21,

21

which is 1 + 2 + 3 + 4 + 5 + 6. 也就是1+2+3+4+5+6的结果。 

For problems such as these, do not include raw_input statements or define the variable end. Our automating testing will provide a value of end for you - so write your code in the following box assuming end is already defined.

对于这样的问题,请不要包含raw_input类型的语句或者给变量end赋值。我们的自动化测试将会帮你给end赋值-所以假定end已经被赋值,在下面的方框里写下你的代码。 

以假定end已经被赋值,在下面的方框里写下你的代码。 

s = 0
for x in range(1,end+1):
	s += x
print s 

(三)综合运用
1   In this problem, you'll create a program that guesses a secret number!

The program works as follows: you (the user) thinks of an integer between 0 (inclusive) and 100 (not inclusive). The computer makes guesses, and you give it input - is its guess too high or too low? Using bisection search , the computer will guess the user's secret number!

Here is a transcript of an example session:

Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 75?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 87?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 81?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 84?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 82?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 83?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
Game over. Your secret number was: 83

Your program should use bisection search. So think carefully what that means. What will the first guess always be? How should you calculate subsequent guesses

Note: your program should be using raw_inputto obtain the user's input! Be sure to handle the case when the user's input is not one of hl, or c.

When the user enters something invalid, you should print out a message to the user explaining you did not understand their input. Then, you should re-ask the question, and prompt again for input. For example:

Is your secret number 91?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. y
Sorry, I did not understand your input.
Is your secret number 91?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c

print('Please think of a number between 0 and 100!')
r = ''
high = 100
low = 0
mid = 0
while  r != 'c':
	mid = (high + low)/2
	print('Is your secret number ' + str(mid)+ '?')
	r = str(raw_input('Enter \'h\' to indicate the guess is too high. Enter \'l\' to indicate the guess is too low. Enter \'c\' to indicate I guessed correctly.'))
	if r == 'h':
		high = mid
	elif r == 'l':
		low = mid
	elif r == 'c':
		print('Game over. Your secret number was: ' + str(mid))
	else:
		print("Sorry, I did not understand your input.")


目录
相关文章
|
6天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
17天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1327 7
|
5天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
300 129
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
4天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
16天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1396 87
|
4天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
5天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
298 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
5天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
232 82
2025年阿里云域名备案流程(新手图文详细流程)