亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Python基礎(chǔ)練習(xí)100題 ( 21~ 30)

jeffrey_up / 3555人閱讀

摘要:刷題繼續(xù)昨天和大家分享了前道題,今天繼續(xù)來刷解法一解法一解法二解法三解法一解法二解法一解法一解法二解法一解法二解法一解法二解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊

刷題繼續(xù)

昨天和大家分享了前10道題,今天繼續(xù)來刷21~30

Question 21:
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

解法一
import  math

x,y = 0,0
while True:
    s = input().split()
    if not s:
        break
    if s[0]=="UP":                  # s[0] indicates command
        x-=int(s[1])                # s[1] indicates unit of move
    if s[0]=="DOWN":
        x+=int(s[1])
    if s[0]=="LEFT":
        y-=int(s[1])
    if s[0]=="RIGHT":
        y+=int(s[1])
                                    # N**P means N^P
dist = round(math.sqrt(x**2 + y**2))  # euclidean distance = square root of (x^2+y^2) and rounding it to nearest integer
print(dist)
Question 22:
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

解法一
ss = input().split()
word = sorted(set(ss))     # split words are stored and sorted as a set

for i in word:
    print("{0}:{1}".format(i,ss.count(i)))
解法二
ss = input().split()
dict = {}
for i in ss:
    i = dict.setdefault(i,ss.count(i))   

dict = sorted(dict.items())            
                                         
for i in dict:
    print("%s:%d"%(i[0],i[1]))
解法三
from collections import Counter

ss = input().split()
ss = Counter(ss)         # returns key & frequency as a dictionary
ss = sorted(ss.items())  # returns as a tuple list

for i in ss:
    print("%s:%d"%(i[0],i[1]))
Question 23:
Write a method which can calculate square value of number
解法一
def square(num):
    return num ** 2

print(square(2))
print(square(3))
解法二
n=int(input())
print(n**2)
Question 24:
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

解法一
print (abs.__doc__)
print (int.__doc__)

def square(num):
    """
 Return the square value of the input number.
 The input number must be integer.
 """
    return num ** 2

print (square(2))
print (square.__doc__)
Question 25:
Define a class, which have a class parameter and have a same instance parameter.
解法一
class Car:
    name = "Car"

    def __init__(self,name = None):
        self.name = name

honda=Car("Honda")
print("%s name is %s"%(Car.name,honda.name))

toyota=Car()
toyota.name="Toyota"
print("%s name is %s"%(Car.name,toyota.name))
解法二
class Person:
    # Define the class parameter "name"
    name = "Person"
    
    def __init__(self, name = None):
        # self.name is the instance parameter
        self.name = name

jeffrey = Person("Jeffrey")
print ("{0} name is {1}".format(Person.name, jeffrey.name))

nico = Person()
nico.name = "Nico"
print (f"{Person.name} name is {nico.name}")
Question 26:
Define a function which can compute the sum of two numbers.
解法一
sum = lambda n1,n2 : n1 + n2      # here lambda is use to define little function as sum
print(sum(1,2))
解法二
def SumFunction(number1, number2):
    return number1 + number2

print SumFunction(1,2)
Question 27:
Define a function that can convert a integer into a string and print it in console.
解法一
def printValue(n):
    print (str(n))

printValue(3)
解法二
conv = lambda x : str(x)
n = conv(10)
print(n)
print(type(n))  
Question 28:
Define a function that can receive two integer numbers in string form and compute their sum and then print it in console.
解法一
def printValue(s1,s2):
    print int(s1) + int(s2)
printValue("3","4") #7
解法二
sum = lambda s1,s2 : int(s1) + int(s2)
print(sum("10","45"))      # 55
Question 29:
Define a function that can accept two strings as input and concatenate them and then print it in console.
解法一
def printValue(s1,s2):
    print s1 + s2

printValue("3","4") #34
解法二
sum = lambda s1,s2 : s1 + s2
print(sum("10","45"))        # 1045
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 all strings line by line.
解法一
def printVal(s1,s2):
    len1 = len(s1)
    len2 = len(s2)
    if len1 > len2:
        print(s1)
    elif len1 < len2:
        print(s2)
    else:
        print(s1)
        print(s2)

s1,s2=input().split()
printVal(s1,s2)
源代碼下載

這十道題的代碼在我的github上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載:

Python 21-30題

我的運(yùn)行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點(diǎn):

raw_input()在Python3中是input()

print需要加括號(hào)

fstring可以換成.format(),或者%s,%d

謝謝大家,我們下期見!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評(píng)論里,我們一起進(jìn)步?。?!

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/43955.html

相關(guān)文章

  • Python基礎(chǔ)練習(xí)100 ( 31~ 40)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn)行環(huán)境如果你 刷題繼續(xù) 昨天和大家分享了21-30題,今天繼續(xù)來刷31~40題 Question 31: Define a function which can pr...

    miracledan 評(píng)論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 41~ 50)

    摘要:刷題繼續(xù)大家好,我又回來了,昨天和大家分享了題,今天繼續(xù)來看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題 刷題繼續(xù) 大家好,我又回來了,昨天和大家分享了31-40題,今天繼續(xù)來看41~50題 Question 41: Write a program whi...

    mochixuan 評(píng)論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 1~ 10)

    摘要:一套全面的練習(xí),大家智慧的結(jié)晶大家好,好久不見,我最近在上發(fā)現(xiàn)了一個(gè)好東西,是關(guān)于夯實(shí)基礎(chǔ)的道題,原作者是在的時(shí)候創(chuàng)建的,閑來無事,非常適合像我一樣的小白來練習(xí)對(duì)于每一道題,解法都不唯一,我在這里僅僅是拋磚引玉,希望可以集合大家的智慧,如果 一套全面的練習(xí),大家智慧的結(jié)晶 大家好,好久不見,我最近在Github上發(fā)現(xiàn)了一個(gè)好東西,是關(guān)于夯實(shí)Python基礎(chǔ)的100道題,原作者是在Pyt...

    Java3y 評(píng)論0 收藏0
  • 測(cè)試開發(fā)必看:《笨辦法學(xué)Python3》PDF中文高清版,豆瓣高分8.0

    摘要:笨辦法學(xué)第版結(jié)構(gòu)非常簡(jiǎn)單,共包括個(gè)習(xí)題,其中個(gè)覆蓋了輸入輸出變量和函數(shù)三個(gè)主題,另外個(gè)覆蓋了一些比較高級(jí)的話題,如條件判斷循環(huán)類和對(duì)象代碼測(cè)試及項(xiàng)目的實(shí)現(xiàn)等。最后只想說,學(xué)習(xí)不會(huì)辜負(fù)任何人,笨辦法學(xué) 內(nèi)容簡(jiǎn)介   《笨辦法學(xué)Python(第3版)》是一本Python入門書籍,適合對(duì)計(jì)...

    不知名網(wǎng)友 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<