python 100题练习记录(四)

    科技2022-07-15  122

    Question 21 Level 3

    Question£º A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following: UP 5 DOWN 3 LEFT 3 RIGHT 2 ¡ The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer. Example: If the following tuples are given as input to the program: UP 5 DOWN 3 LEFT 3 RIGHT 2 Then, the output of the program should be: 2

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

    import math dic = {"len":0,"height":0} while True: s = input() if not s: break step = s.split(' ') if step[0] == "UP": dic['height'] += int(step[1]) elif step[0] == "DOWN": dic['height'] -= int(step[1]) elif step[0] == "LEFT": dic['len'] -= int(step[1]) elif step[0] == "RIGHT": dic['len'] += int(step[1]) else: pass dis = math.sqrt(dic['len']**2 + dic['height']**2) print(int(dis))

    Question 22 Level 3

    Question: Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. Suppose the following input is supplied to the program: New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3. Then, the output should be: 2:2 3.:1 3?:1 New:1 Python:5 Read:1 and:1 between:1 choosing:1 or:2 to:1

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

    freq = {} # frequency of words in text line = input("请输入内容:") for word in line.split(): freq[word] = freq.get(word,0)+1 words = list(freq.keys()) words.sort() for w in words: print("%s:%d" % (w,freq[w]))

    Question 23 level 1

    Question: Write a method which can calculate square value of number

    Hints: Using the ** operator

    方法一

    import operator i = int(input("请输入数值:")) r = operator.pow(i,2) print(r)

    方法二

    i = int(input("请输入数值:")) r = i ** 2 print(r)

    Question 24 Level 1

    Question: Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions. Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input() And add document for your own function

    Hints: The built-in document method is doc

    help(abs) help(int) help(input) def test(): '''This page is using for testing,Welcome to joker's room,hope you have fun with yourself''' return 2 help(test)

    Question 25 Level 1

    Question: Define a class, which have a class parameter and have a same instance parameter.

    Hints: Define a instance parameter, need add it in init method You can init a object with construct parameter or set the value later

    class Person(object): height = 170 def __init__(self,h): self.height = h joker = Person(175) print(joker.height)

    Question 26: Define a function which can compute the sum of two numbers.

    Hints: Define a function with two numbers as arguments. You can compute the sum in the function and return the value.

    def add(x,y): return x+y print(add(1,2))

    Question 27: Define a function that can convert a integer into a string and print it in console.

    Hints:

    Use str() to convert a number to string.

    def to_str(n): return str(n) print(to_str(3))

    Question 28: Define a function that can receive two integral numbers in string form and compute their sum and then print it in console.

    Hints:

    Use int() to convert a string to integer.

    def int_sun(a,b): return int(a) + int(b) print(int_sun('111','222'))

    Question 29: Define a function that can accept two strings as input and concatenate them and then print it in console.

    Hints:

    Use + to concatenate the strings

    def str_tail(a,b): return str(a) + str(b) print(str_tail(111,222))

    Question 30: Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print al l strings line by line.

    Hints:

    Use len() function to get the length of a string

    def match_str(str1,str2): if len(str1) == len(str2): print(str1) print(str2) elif len(str1) > len(str2): print(str1) elif len(str1) < len(str2): print(str2) else: pass match_str("abc","4156")
    Processed: 0.010, SQL: 8