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月前
|
算法 数据挖掘 芯片
Sentieon | 每周文献-Gene Editing-第二十八期
Sentieon | 每周文献-Gene Editing-第二十八期
37 0
|
机器学习/深度学习 存储 自然语言处理
机器学习面试笔试知识点-贝叶斯网络(Bayesian Network) 、马尔科夫(Markov) 和主题模型(T M)1
机器学习面试笔试知识点-贝叶斯网络(Bayesian Network) 、马尔科夫(Markov) 和主题模型(T M)
179 0
机器学习面试笔试知识点-贝叶斯网络(Bayesian Network) 、马尔科夫(Markov) 和主题模型(T M)1
|
机器学习/深度学习 自然语言处理 算法
机器学习面试笔试知识点-贝叶斯网络(Bayesian Network) 、马尔科夫(Markov) 和主题模型(T M)2
机器学习面试笔试知识点-贝叶斯网络(Bayesian Network) 、马尔科夫(Markov) 和主题模型(T M)
67 0
|
算法 决策智能 Windows
【论文阅读】(2017)The late acceptance Hill-Climbing heuristic
【论文阅读】(2017)The late acceptance Hill-Climbing heuristic
305 0
【论文阅读】(2017)The late acceptance Hill-Climbing heuristic
|
机器学习/深度学习 传感器 算法
海马算法 (Sea-Horse optimizer ,SHO)附matlab代码
海马算法 (Sea-Horse optimizer ,SHO)附matlab代码
|
机器学习/深度学习 算法 数据挖掘
Nat Commun&JAMA INTERN MED|浅析两篇LASSO+Logistic/Cox 套路文章
Nat Commun&JAMA INTERN MED|浅析两篇LASSO+Logistic/Cox 套路文章
302 0
Nat Commun&JAMA INTERN MED|浅析两篇LASSO+Logistic/Cox 套路文章
Mad Scientist (纯模拟题)
Mad Scientist 题目描述 Farmer John’s cousin Ben happens to be a mad scientist. Normally, this creates a good bit of friction at family gatherings, but it can occasionally be helpful, especially when Farmer John finds himself facing unique and unusual problems with his cows.
138 0
|
机器学习/深度学习 算法 前端开发
三个臭皮匠赛过诸葛亮!白话Blending和Bagging
本文将主要介绍Aggregation Models,也就是把多个模型集合起来,利用集体的智慧得到最佳模型。
302 0
三个臭皮匠赛过诸葛亮!白话Blending和Bagging
|
机器学习/深度学习 存储 分布式计算
ALS算法解析(上)
ALS算法解析(上)
512 0
ALS算法解析(上)
|
机器学习/深度学习 分布式计算 算法
ALS算法解析(下)
ALS算法解析(下)
363 0
ALS算法解析(下)