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.")


目录
相关文章
|
机器学习/深度学习 人工智能 自然语言处理
一文搞懂【知识蒸馏】【Knowledge Distillation】算法原理
一文搞懂【知识蒸馏】【Knowledge Distillation】算法原理
一文搞懂【知识蒸馏】【Knowledge Distillation】算法原理
|
3月前
|
算法 数据挖掘 芯片
Sentieon | 每周文献-Gene Editing-第二十八期
Sentieon | 每周文献-Gene Editing-第二十八期
18 0
|
9月前
|
机器学习/深度学习 存储 缓存
Lecture 3:动态规划
Lecture 3:动态规划
|
算法 决策智能 Windows
【论文阅读】(2017)The late acceptance Hill-Climbing heuristic
【论文阅读】(2017)The late acceptance Hill-Climbing heuristic
221 0
【论文阅读】(2017)The late acceptance Hill-Climbing heuristic
|
算法 Java C语言
practice1-基础算法
快速学习practice1-基础算法
|
机器学习/深度学习 算法 数据挖掘
Nat Commun&JAMA INTERN MED|浅析两篇LASSO+Logistic/Cox 套路文章
Nat Commun&JAMA INTERN MED|浅析两篇LASSO+Logistic/Cox 套路文章
264 0
Nat Commun&JAMA INTERN MED|浅析两篇LASSO+Logistic/Cox 套路文章
|
机器学习/深度学习 存储 分布式计算
ALS算法解析(上)
ALS算法解析(上)
377 0
ALS算法解析(上)
|
机器学习/深度学习 分布式计算 算法
ALS算法解析(下)
ALS算法解析(下)
288 0
ALS算法解析(下)
|
移动开发 索引 Python
动态规划法(五)钢条切割问题(rod cutting problem)
  继续讲故事~~   我们的主人公现在已经告别了生于斯,长于斯的故乡,来到了全国最大的城市S市。这座S市,位于国家的东南部,是全国的经济中心,工商业极为发达,是这个国家的人民所向往的城市。
1670 0