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


目录
相关文章
UVa10484 - Divisibility of Factors(数论)
UVa10484 - Divisibility of Factors(数论)
71 1
|
机器学习/深度学习 存储 缓存
Lecture 3:动态规划
Lecture 3:动态规划
127 1
UVa668 - Parliament(贪心)
UVa668 - Parliament(贪心)
64 0
|
机器学习/深度学习 传感器 算法
能量谷算法Energy Valley Optimizer (EVO)附matlab代码
能量谷算法Energy Valley Optimizer (EVO)附matlab代码
|
Windows
German Collegiate Programming Contest 2019 H . Historical Maths (二分 大数)
German Collegiate Programming Contest 2019 H . Historical Maths (二分 大数)
89 0
German Collegiate Programming Contest 2019 H . Historical Maths (二分 大数)
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
158 0
|
人工智能 BI
[UVA 1599] Ideal Path | 细节最短路
Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci .
208 0
Algorithm之PrA:PrA之LP线性规划算法经典案例剖析+Matlab编程实现(一)
Algorithm之PrA:PrA之LP线性规划算法经典案例剖析+Matlab编程实现
Algorithm之PrA:PrA之LP线性规划算法经典案例剖析+Matlab编程实现(一)
Algorithm之PrA:PrA之LP线性规划算法经典案例剖析+Matlab编程实现(二)
Algorithm之PrA:PrA之LP线性规划算法经典案例剖析+Matlab编程实现
Algorithm之PrA:PrA之LP线性规划算法经典案例剖析+Matlab编程实现(二)
|
人工智能 BI 机器学习/深度学习

热门文章

最新文章