100题练习记录(七)

Question 61:
Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.

Hints:

Use unicode() function to convert.

s = input("")
u = s.encode("utf-8")
print(u)

Question 62:

Write a special comment to indicate a Python source code file is in unicode.

Hints:

# -*- coding: utf-8 -*-

Question 63:

Write a program to compute 1/2+2/3+3/4+…+n/n+1 with a given n input by console (n>0).

Example:
If the following n is given as input to the program:

5

Then, the output of the program should be:

3.55

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:
Use float() to convert an integer to a float

def float(n):
    if n == 1:
        return 1 / 2
    else:
        return float(n-1)+n/(n+1)
print(float(5))

Question 64:

Write a program to compute:

f(n)=f(n-1)+100 when n>0
and f(0)=1

with a given n input by console (n>0).

Example:
If the following n is given as input to the program:

5

Then, the output of the program should be:

500

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:
We can define recursive function in Python.
感觉题目有问题,5的时候应该是501

def compute(n):
    if n == 0 :
        return 1
    elif n > 0:
        return compute(n-1)+100
print(compute(5))

Question 65:

The Fibonacci Sequence is computed based on the following formula:

f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1

Please write a program to compute the value of f(n) with a given n input by console.

Example:
If the following n is given as input to the program:

7

Then, the output of the program should be:

13

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:
We can define recursive function in Python.

def f(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    elif n>1:
        return f(n-1)+f(n-2)
    else:
        pass
print(f(7))

Question 66:

The Fibonacci Sequence is computed based on the following formula:

f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1

Please write a program using list comprehension to print the Fibonacci Sequence in comma separated form with a given n input by console.

Example:
If the following n is given as input to the program:

7

Then, the output of the program should be:

0,1,1,2,3,5,8,13

Hints:
We can define recursive function in Python.
Use list comprehension to generate a list from an existing list.
Use string.join() to join a list of strings.

In case of input data being supplied to the question, it should be assumed to be a console input.

def f(n):
    if n == 0:
         return 0
    elif n == 1:
        return 1
    elif n>1:
        return f(n-1)+f(n-2)
    else:
        pass
n = int(input())
l = [str(f(x)) for x in range(0,n+1)]
print(",".join(l))

Question 67:

Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console.

Example:
If the following n is given as input to the program:

10

Then, the output of the program should be:

0,2,4,6,8,10

Hints:
Use yield to produce the next value in generator.

In case of input data being supplied to the question, it should be assumed to be a console input.

def EvenGenerator(n):
    i=0
    while i<=n:
        if i%2==0:
            yield i
        i+=1

n=int(input())
values = []
for i in EvenGenerator(n):
    values.append(str(i))

print(",".join(values))

Question 68:

Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console.

Example:
If the following n is given as input to the program:

100

Then, the output of the program should be:

0,35,70

Hints:
Use yield to produce the next value in generator.

In case of input data being supplied to the question, it should be assumed to be a console input.

def divgenerator(n):
    i = 0
    while i < n+1:
        if (i%5==0) and (i%7==0):
            yield i
        i += 1
x = int(input("请输入:"))
l = []
for j in divgenerator(x):
    l.append(str(j))
print(",".join(l))

Question 69:

Please write assert statements to verify that every number in the list [2,4,6,8] is even.

Hints:
Use “assert expression” to make assertion.

list = [2,4,6,8]
for i in list:
    assert i%2==0

Question 70:

Please write a program which accepts basic mathematic expression from console and print the evaluation result.

Example:
If the following string is given as input to the program:

35+3

Then, the output of the program should be:

38

Hints:
Use eval() to evaluate an expression.

s = eval(input("请输入数学表达式:"))
print(s)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值