摘要:一套全面的練習(xí),大家智慧的結(jié)晶大家好,好久不見,我最近在上發(fā)現(xiàn)了一個好東西,是關(guān)于夯實基礎(chǔ)的道題,原作者是在的時候創(chuàng)建的,閑來無事,非常適合像我一樣的小白來練習(xí)對于每一道題,解法都不唯一,我在這里僅僅是拋磚引玉,希望可以集合大家的智慧,如果
一套全面的練習(xí),大家智慧的結(jié)晶
大家好,好久不見,我最近在Github上發(fā)現(xiàn)了一個好東西,是關(guān)于夯實Python基礎(chǔ)的100道題,原作者是在Python2的時候創(chuàng)建的,閑來無事,非常適合像我一樣的小白來練習(xí)
對于每一道題,解法都不唯一,我在這里僅僅是拋磚引玉,希望可以集合大家的智慧,如果哪道題有其他解法,希望可以在評論中留下大家寶貴的意見!每次我會更新10道題,一共會更新10篇,這也算是對我之前的文章一個總結(jié)啦,如果沒有看到我之前有關(guān)Python的小白學(xué)習(xí)分享的同學(xué)們,可以戳下面連接查看哈:
Python 基礎(chǔ)起步,寫給同為小白的你
Python 進階之路
Python Pandas 之旅
如果大家想要和我聯(lián)系,可以訪問我的個人主頁:
我的個人主頁
好啦,閑話少說,讓我們開始今天的刷題之旅吧!
Question 1:Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.解法一
for i in range(2000,3201): if i%7 == 0 and i%5!=0: print(i,end=",") print("")解法二
numbers = [str(x) for x in range(2000,3201) if (x%7==0) and (x%5!=0)] print (",".join(numbers))Question 2:
*Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8解法一
Then, the output should be:40320*
def fact(x): if x == 0: return 1 return x * fact(x - 1) x=int(input()) print(fact(x))解法二
import math as ma x=int(input()) print(ma.factorial(x))解法三
from functools import reduce from operator import mul x=int(input()) print(reduce(mul,range(1,x+1)))Question 3:
With a given integral number n, write a program to generate a dictionary that contains (i, i x i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program: 8Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
n=int(input()) d=dict() for i in range(1,n+1): d[i]=i*i print(d)解法二
n=int(input()) d={x:x*x for x in range(1,n+1)} print(d)Question 4:
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
["34", "67", "55", "33", "12", "98"] ("34", "67", "55", "33", "12", "98")解法一
values=input() l=values.split(",") t=tuple(l) print(f"List of values : {l}") print(f"Tuple of values : {t}")Question 5:
Define a class which has at least two methods:
getString: to get a string from console input
printString: to print the string in upper case.
Also please include simple test function to test the class methods.
class InputOutString: def __init__(self): self.s = "" def getString(self): self.s = input() def printString(self): print(self.s.upper()) # Test a = InputOutString() a.getString() a.printString()Question 6:
Write a program that calculates and prints the value according to the given formula:Q = Square root of [(2 C D)/H]
Following are the fixed values of C and H:
C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.For example
Let us assume the following comma separated input sequence is given to the program:*
100,150,180
The output of the program should be:
18,22,24
import math c=50 h=30 value = [] items= [x for x in input("Input numbers comma-separated:").split(",")] for d in items: value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) print (",".join(value))Question 7:
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i * j.Note: i=0,1.., X-1; j=0,1,?-Y-1. Suppose the following inputs are given to the program: 3,5
Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
x,y = map(int,input().split(",")) lst = [] for i in range(x): tmp = [] for j in range(y): tmp.append(i*j) lst.append(tmp) print(lst)解法二
x,y = map(int,input().split(",")) lst = [[i*j for j in range(y)] for i in range(x)] print(lst)Question 8:
Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.Suppose the following input is supplied to the program:
without,hello,bag,world
Then, the output should be:
bag,hello,without,world
original_string = input("Input Text:") l = original_string.split(",") final_string = sorted(l,key=str) print(",".join(final_string))解法二
lst = input().split(",") lst.sort() print(",".join(lst))Question 9:
Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.Suppose the following input is supplied to the program:
Hello world Practice makes perfect
Then, the output should be:
HELLO WORLD PRACTICE MAKES PERFECT
lines = [] while True: s = input() if s: lines.append(s.upper()) else: break; for sentence in lines: print(sentence)Question 10:
Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.Suppose the following input is supplied to the program:
hello world and practice makes perfect and hello world again
Then, the output should be:
again and hello makes perfect practice world
word = input().split() for i in word: if word.count(i) > 1: #count function returns total repeatation of an element that is send as argument word.remove(i) # removes exactly one element per call word.sort() print(" ".join(word))解法二
s = input("Input Text:") words = [word for word in s.split(" ")] print (" ".join(sorted(list(set(words)))))源代碼下載
我把前十道題的代碼上傳到github上啦,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載:
Python 1-10題
我的運行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點:
raw_input()在Python3中是input()
print需要加括號
fstring 可以換成.format(), 或者%s,%d
謝謝大家,我們下期見!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評論里,我們一起進步!?。?/p>
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/43942.html
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題我的運行環(huán)境如果你 刷題繼續(xù) 昨天和大家分享了21-30題,今天繼續(xù)來刷31~40題 Question 31: Define a function which can pr...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下 刷題繼續(xù) 昨天和大家分享了61-70題,今天繼續(xù)來刷71~80題 Question 71: Please write a program to out...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題 刷題繼續(xù) 昨天和大家分享了51-60題,今天繼續(xù)來刷61~70題 Question 61: The Fibonacci Sequence is computed based o...
摘要:刷題繼續(xù)大家好,我又回來了,昨天和大家分享了題,今天繼續(xù)來看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題 刷題繼續(xù) 大家好,我又回來了,昨天和大家分享了31-40題,今天繼續(xù)來看41~50題 Question 41: Write a program whi...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載 刷題繼續(xù) 昨天和大家分享了41-50題,今天繼續(xù)來刷51~60題 Question 51: Write a function to compute 5/0 and use ...
閱讀 3459·2019-08-30 15:44
閱讀 800·2019-08-30 13:46
閱讀 2076·2019-08-30 11:05
閱讀 3338·2019-08-29 18:32
閱讀 2160·2019-08-29 13:56
閱讀 1298·2019-08-29 12:57
閱讀 764·2019-08-28 18:21
閱讀 1742·2019-08-26 12:16