python编程100+题1—10(第一天)

**

python编程100题1—10(第一天)

自学python提高编程能力(第一天)

以下为python编程100题,来自github,原文链接如下:
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
文章的最后是一些知识点总结和提示
完成这些题目的基础上,进行了一些题目要求的翻译理解;添加了自己理解的注释和出过的错误,请同样学习的朋友们指正!

QUESTION1

'''
Question 1
Level 1

Question:
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.
{找到2000到3200之间(包括左右边界)所有能被7整除,并且不是5的倍数的数字}
Hints: 
Consider use range(#begin, #end) method
{提示用range来实现}
'''
代码如下:

l=[]  #定义空列表
for i in range(2000,3200):
    if (i%7==0 and i%5!=0): #同时也在学习C,Python逻辑运算符是and而不是&&;
        l.append(str(i))   #.append把后面的字符加到l数组的后面;
                           #str(i)返回了一个string格式数字

print(','.join(l)) #.join(l)表示用,来连接字符,成为一个新的列表

输出如下:
= RESTART: C:/Users/14632/OneDrive/桌面/toachieveit/python profile/100/Q1.py
2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198,2212,2219,2226,2233,2247,2254,2261,2268,2282,2289,2296,2303,2317,2324,2331,2338,2352,2359,2366,2373,2387,2394,2401,2408,2422,2429,2436,2443,2457,2464,2471,2478,2492,2499,2506,2513,2527,2534,2541,2548,2562,2569,2576,2583,2597,2604,2611,2618,2632,2639,2646,2653,2667,2674,2681,2688,2702,2709,2716,2723,2737,2744,2751,2758,2772,2779,2786,2793,2807,2814,2821,2828,2842,2849,2856,2863,2877,2884,2891,2898,2912,2919,2926,2933,2947,2954,2961,2968,2982,2989,2996,3003,3017,3024,3031,3038,3052,3059,3066,3073,3087,3094,3101,3108,3122,3129,3136,3143,3157,3164,3171,3178,3192,3199

QUESTION2

'''
Question 2
Level 1

Question:
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
{求出给定数字的阶乘}

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
{需要通过控制台输入数据}
'''
def factorial(x):  #factorial翻译为阶乘
    if x==0:       #认为0时值为1,即递归的最后一个乘数为1
        return 1   
    return x*factorial(x-1)    #通过递归求阶乘

x=int(input())     #将input的数值转化为了int类型,因为input函数的输入默认为str类型,在函数中的计算就无法进行了
print(factorial(x))

QUESTION3

'''
Level 1

Question:
With a given integral number n, write a program to generate a dictionary that contains (i, i*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:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
{输入为一个正整数,输出(i:i*i)的字典形式}
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
Consider use dict()
'''
n=int(input())
d=dict() #创建了一个空字典
for i in range(1,n+1): #range是从1到(n+1)-1
    d[i]=i*i  #定义字典中的元素,i对应i*i
    
print(d)

QUESTION4

'''
Question 4
Level 1

Question:
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')
{输入一串数字,先按列表输出,再按元组输出}

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
tuple() method can convert list to tuple
'''

values=input()  #将输入的一整串存储下来为string
l=values.split(",") #用,来打断string,存入列表中
t=tuple(l)     #将列表直接转为元组
print (l)
print (t)

QUESTION5

'''
Question 5
Level 1

Question:
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.
{定义一个类,这个类中有功能:getstring printstring}
Hints:
Use __init__ method to construct some parameters
'''
class InOutputString:
    def _init_(self):
        self.s=""

    def getString(self):
        self.s=input()

    def printString(self):
        print (self.s.upper())

strObj=InOutputString()  #初始化strObj为类
strObj.getString()    #这里的功能块是可以对内部参数进行传递的(相当于在类之内进行操作)
strObj.printString()
    

QUESTION6

'''
Question 6
Level 2

Question:
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,输出对应计算式的结果}
D is the variable whose values should be input to your program in a comma-separated sequence.
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

Hints:
If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26)
In case of input data being supplied to the question, it should be assumed to be a console input.
'''
import math
c=50
h=30
value=[]
items=[x for x in input().split(',')]  #输入的数字是以都好隔开的,所以打断逗号存入列表总
for d in items:
    value.append(str(int(math.sqrt(2*c*float(d)/h)))) #数据类型的转换很重要
     #d是items中的str类型,转为float进行除法运算;math运算结束后,进行就近取整;再转为str存入列表中

print(",".join(value))  #用,连接进行列表输出

QUESTION7

'''
Question 7
Level 2

Question:
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.
{输入行数x,列数y,输出二维列表,元素值为i*j}
Example
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]] 

Hints:
Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.
'''

row,column=input().split(',')  #输入用,划分分别输入,为string类型
rowNum=int(row)                
columnNum=int(column)       #转int,这样才能进行range运算

multilist=[[0 for col in range(columnNum)] for row in range(rowNum)]  #建立一个确定宽长的二维列表

for i in range(rowNum):
    for j in range(columnNum):
        multilist[i][j]=i*j
print(multilist)
        

QUESTION8

'''
Question 8
Level 2

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

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

items=[x for x in input().split(",")] #逗号分割输入的常用输入格式,存入一个列表中
items.sort()                         #该函数的作用即为排序
print(",".join(items))             #.join表示将将前面的符号作为分隔符进行输出

QUESTION9

'''
Question 9
Level 2

Question£º
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

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
'''
lines=[]
while True:
    s=input()
    if s: #这里的含义即是,第一次按回车,输入下一个字符,判断继续,
        #但如果再按一次回车,即输入了一个空字符,这时停止输入程序(python中将回车视为空字符)
        lines.append(s.upper())
    else:
        break;

for sentence in lines:
    print(sentence) #这里因为要输出的不能带[],也不能带,所以要分开输出

Question 10

'''
Question 10
Level 2

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

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
We use set container to remove duplicated data automatically and then use sorted() to sort the data.
'''
array=[x for x in input().split(' ')] #用空格分割,不要忘了input()表示一个输入的string列表
array=sorted(set(array))              #set函数,把array变成一个不重复、无顺序的列表
print(' '.join(array))    

问题总结:

  1. python中的input 和raw_input的区别:
    raw_input是python2的输入函数,可以用来接受任意类型的输入,目前已被删除。在python3中,input是常用的输入函数,须注意的是input函数的输入始终是一个string类型。
    演示:python3利用input输入具有任意性,并且为string类型
a=input("input:")
input:123456
type(a)
<class 'str'>
//输入数字

a=input("input:")
input:"asfvx"
type(a)
<class 'str'>
a
'"asfvx"'
//输入字符

a=input("input:")
input:[1,2,3,4,5,6]
a
'[1,2,3,4,5,6]'
//输入列表

2.对列表进行操作的函数小结:
(list表示列表,seq表示列表或元素序列,index表示索引号,obj表示列表内元素)
末尾添加:list.append(‘what?’)
删除元素:del list[2]
列表长度:len(list)
比较列表:cmp(list1, list2)
(将元组)转换为列表:list(seq)
找最大:max(list)
找最小:min(list)
元素出现个数:list.count(obj)
列表扩展:list.extend(seq)
移除元素并返回该值:list.pop([index=-1]) //括号内为元素序号
将元素插入:list.insert(index, obj)
移除第一个匹配项:list.remove(obj)
反序:list.reverse()
列表排序:list.sort(cmp=None, key=None, reverse=False)//默认字母表顺序

3.Python转义字符:
来自BUNOOB.COM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值