前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 练习100题---No.(1-20)---附其他题目解答链接

Python 练习100题---No.(1-20)---附其他题目解答链接

作者头像
用户7886150
修改2021-01-28 10:05:15
1.2K0
修改2021-01-28 10:05:15
举报
文章被收录于专栏:bit哲学院

参考链接: Python程序查找可被另一个数整除的数字

github展示python100题 链接如下: https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt 以下为博主翻译后题目及解答,答案代码分为两个,第一条为博主个人解答(Python3),第二条为题目所提供答案(Python2) ……………………………………………………………………………… 本部分为题目1-20,等级难度1-3升序; 题目21-40链接:https://blog.csdn.net/weixin_41744624/article/details/103511139 题目41-60链接:https://blog.csdn.net/weixin_41744624/article/details/103575741 题目61-80链接: https://blog.csdn.net/weixin_41744624/article/details/103607992 题目81-98链接:https://blog.csdn.net/weixin_41744624/article/details/103646520 经检测题库去除重复只有98题啦(欢迎评论添加好题目)~ ……………………………………………………………………………… 1、问题: 

写一个程序,找出所有这些数字,可以被7整除,但不是5的倍数, 

2000至3200间(均包括在内)。 

获得的数字应以逗号分隔的顺序打印在一行上。 

ls =  []

for i in range(2000,3201):

    if (i%7 == 0)and (i%5 != 0):

        ls.append(str(i))

print (",".join(ls))

values=raw_input()

l=values.split(",")

t=tuple(l)

print l

print t

2、问题: 

写一个能计算给定 数的阶乘的程序。 

结果应以逗号分隔的顺序打印在一行上。 

假设向程序提供了以下输入: 

那么,输出应该是: 

40320 

a=int(input("input the number"))

JC =int(1)

while a!=0:

    JC=JC*a

    a=a-1

print (JC)

def fact(x):

    if x == 0:

        return 1

    return x * fact(x - 1)

x=int(raw_input())

print fact(x)

3、问题: 

对于给定的整数n,编写一个程序来生成一个字典,其中包含(i,i*i)这样一个介于1和n之间的整数(两者都包括在内)。然后程序应该打印字典。 

假设向程序提供了以下输入: 

那么,输出应该是: 

{1:1,2:4,3:9,4:16,5:25,6:36,7:49,8:64} 

a= {}

i=int(input("input the number"))

while i != 0:

    a[i]=i*i

    i=i-1

print (a)

n=int(raw_input())

d=dict()

for i in range(1,n+1):

    d[i]=i*i

print d

4、问题: 编写一个程序,从控制台接收一系列逗号分隔的数字,并生成一个列表和一个包含每个数字的元组。 

假设向程序提供了以下输入: 

34、67、55、33、12、98 

那么,输出应该是: 

[‘34’,‘67’,‘55’,‘33’,‘12’,‘98’] 

(‘34’、‘67’、‘55’、‘33’、‘12’、‘98’) 

x=input("please input")

a=x.split(",")

b=() 

for item in a: 

    b=b+(item,)

print (a)

print (b)

values=raw_input()

l=values.split(",")

t=tuple(l)

print l

print t

5、问题: 定义至少有两个方法的类: 

get string:从控制台输入获取字符串 

print string:以大写形式打印字符串。 

也请包含简单的测试函数来测试类方法。 

class test():

    def iput(self):

        self.i=input("please input:")

    def up(self):    

       self.j=self.i.upper()

    def pt(self):

       print(self.j)

x=test()

x.iput()

x.up()

x.pt()

class InputOutString(object):

    def __init__(self):

        self.s = ""

    def getString(self):

        self.s = raw_input()

    def printString(self):

        print self.s.upper()

strObj = InputOutString()

strObj.getString()

strObj.printString()

6、问题: 编写一个程序,根据给定的公式计算并打印值: 

Q= [(2 * C * D)/H]的平方根 

C和H的固定值如下: 

C是50。H是30。 

D是一个变量,其值应以逗号分隔的顺序输入到程序中。 

例子 

假设程序有以下逗号分隔的输入序列: 

100,150,180 

程序的输出应为: 

18,22,24 

C = 50

H = 30

TOLQ = []

TOL = [x for x in input('input').split(',') ]

for D in TOL:

      TOLQ.append(str(int(round((((2 * C * float(D))/H)**1/2)))))

print (','.join(TOLQ))

#!/usr/bin/env python

import math

c=50

h=30

value = []

items=[x for x in raw_input().split(',')]

for d in items:

    value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))

print ','.join(value)

7、问题: 编写一个以2位X,Y为输入,生成二维数组的程序。数组的第i行和第j列中的元素值应为i*j。 

注:i=0,1…,X-1;j=0,1,…Y-1。 

例子 

假设程序有以下输入: 

3,5 

那么,程序的输出应该是: 

[[0,0,0,0,0],[0,1,2,3,4],[0,2,4,6,8]] 

x = input ("x:")

y = input ("y:")

num_list = [[0]*(int(y)) for n in range(int(x))]

i=0

j=0

for i in range(0,int(x)) :

    for j in range(0,int(y)) :

       num_list[i][j]=i*j

       j +=1

    i +=1

print (num_list)

input_str = raw_input()

dimensions=[int(x) for x in input_str.split(',')]

rowNum=dimensions[0]

colNum=dimensions[1]

multilist = [[0 for col in range(colNum)] for row in range(rowNum)]

for row in range(rowNum):

    for col in range(colNum):

        multilist[row][col]= row*col

print multilist

8、问题: 编写一个程序,接受以逗号分隔的单词序列作为输入,并在按字母顺序排序后以逗号分隔的序列打印单词。 

假设向程序提供了以下输入: 

without,hello,bag,world 

那么,输出应该是: 

bag,hello,without,world 

x = input ("x:")

a = x.split(',')

i=0

for i in range(0,len(a)-1):

    for j in range(i+1,len(a)):

        string1 = a[i]

        string2 = a[i+1]

        if string1[0] > string2[0]:

            b=a[i]

            a[i]=a[j]

            a[j]=b

        i+=1

print (a)

items=[x for x in raw_input().split(',')]

items.sort()

print ','.join(items)

9、问题: 编写一个程序,接受行序列作为输入,并在将句子中的所有字符大写后打印行。 假设向程序提供了以下输入: Hello world Practice makes perfect 那么,输出应该是: HELLO WORLD PRACTICE MAKES PERFECT 

ls = []

end = 'end'

print ("输入,并以end结束")

for i in iter(input,end):

    ls.append(i.upper())

print (ls)

lines = []

while True:

    s = raw_input()

    if s:

        lines.append(s.upper())

    else:

        break;

for sentence in lines:

    print sentence

10、问题: 编写一个程序,接受一系列空格分隔的单词作为输入,并在删除所有重复的单词并按字母数字顺序排序后打印这些单词。 

假设向程序提供了以下输入: 

hello world and practice makes perfect and hello world again 

那么,输出应该是: 

again and hello makes perfect practice world 

list0=input('please input').split()

list3=list(set(list0))

list3.sort()

print(list3)

s = raw_input()

words = [word for word in s.split(" ")]

print " ".join(sorted(list(set(words))))

11、问题: 编写一个程序,它接受一系列以逗号分隔的4位二进制数作为输入,然后检查它们是否可被5整除。可被5整除的数字将按逗号分隔的顺序打印。 

例子: 

0100,0011,1010,1001 

那么输出应该是: 

1010 

注意:假设数据是由控制台输入的。 

ls = input("please input").split(",")

ls2 = []

ls3 = []

for i in ls:

    j=int(i[0])*(2**3)+int(i[1])*(2**2)+int(i[2])*(2**1)+ int(i[3])*(2**0) 

    if (j%5 ==0):

        ls2.append(j)

        ls3.append(i)

print (ls2)

print (ls3)

value = []

items=[x for x in raw_input().split(',')]

for p in items:

    intp = int(p, 2)

    if not intp%5:

        value.append(p)

print ','.join(value)

12、问题: 

编写一个程序,找出1000到3000之间的所有数字(包括这两个数字),使每个数字都是偶数。 

获得的数字应以逗号分隔的顺序打印在一行上。 

ls = []

for i in range(1000,3001):

    if (i%2==0):

        ls.append(i)

print (ls)

values = []

for i in range(1000, 3001):

    s = str(i)

    if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0):

        values.append(s)

print ",".join(values)

13、问题: 编写一个接受句子并计算字母和数字的程序。 

假设向程序提供了以下输入: 

hello world! 123 

那么,输出应该是: 

LETTERS 10 DIGITS 3 

ls = input("please input:")

digit = 0

alpha = 0

for i in ls:

    if i.isdigit():

        digit += 1

    elif i.isalpha():

        alpha += 1

print ("LETTERS "+str(alpha))

print ("DIGITS "+str(digit))

s = raw_input()

d={"DIGITS":0, "LETTERS":0}

for c in s:

    if c.isdigit():

        d["DIGITS"]+=1

    elif c.isalpha():

        d["LETTERS"]+=1

    else:

        pass

print "LETTERS", d["LETTERS"]

print "DIGITS", d["DIGITS"]

14、问题: 编写一个接受句子的程序,计算大写字母和小写字母的数目。 

假设向程序提供了以下输入: 

Hello world! 那么,输出应该是: 

UPPER CASE 1 LOWER CASE 9 

ls = input("please input:")

up = 0

low = 0

for i in ls:    

    if i.isupper():  

        up += 1

    elif i.islower():

        low += 1

    else: 

        pass

print ("UPPER CASE " + str(up))

print ("LOWER CASE " + str(low))

s = raw_input()

d={"UPPER CASE":0, "LOWER CASE":0}

for c in s:

    if c.isupper():

        d["UPPER CASE"]+=1

    elif c.islower():

        d["LOWER CASE"]+=1

    else:

        pass

print "UPPER CASE", d["UPPER CASE"]

print "LOWER CASE", d["LOWER CASE"]

15、问题: 编写一个程序,用一个给定的数字作为a的值来计算a+aa+aaa+aaaa的值。 

假设向程序提供了以下输入: 

那么,输出应该是: 

11106 

a=int(input("input a:"))

b=a*10 +a

c=a*100+b

d=a*1000+c

e=a+b+c+d

print (e)

a = raw_input()

n1 = int( "%s" % a )

n2 = int( "%s%s" % (a,a) )

n3 = int( "%s%s%s" % (a,a,a) )

n4 = int( "%s%s%s%s" % (a,a,a,a) )

print n1+n2+n3+n4

16、问题: 使用列表理解将列表中的每个奇数平方。列表由一系列逗号分隔的数字输入。 

假设向程序提供了以下输入: 

1,2,3,4,5,6,7,8,9 

那么,输出应该是: 

1,3,5,7,9 

ls = (input("please input:")).split(',')

ls2 = []

for i in ls:

    if (int(i)%2 != 0):

        ls2.append(i)

print (ls2)

values = raw_input()

numbers = [x for x in values.split(",") if int(x)%2!=0]

print ",".join(numbers)

17、问题: 编写一个程序,根据控制台输入的事务日志计算银行帐户的净额。事务日志格式如下: 

D 100 W 200 

D表示存款,W表示取款。 

假设向程序提供了以下输入: 

D 300 D 300 W 200 D 100 

那么,输出应该是: 

500 

ls = []

digit = []

alpha = []

end = 'end'

print ("输入,并以end结束")

for i in iter(input,end):

    ls.append(i.split())

for h in ls:

    for j in h: 

        if j.isdigit():

            digit.append(j)

        if j.isalpha():

            alpha.append(j)

print (digit)

print (alpha)

x =0

SUM1 = 0

for x in range(0,len(digit)):

    if alpha[x]=='D' :

        SUM1 = SUM1 + int(digit[x])

    elif alpha[x]=='W' :

        SUM1 = SUM1 - int(digit[x])

    else:

        pass

print (SUM1)

netAmount = 0

while True:

    s = raw_input()

    if not s:

        break

    values = s.split(" ")

    operation = values[0]

    amount = int(values[1])

    if operation=="D":

        netAmount+=amount

    elif operation=="W":

        netAmount-=amount

    else:

        pass

print netAmount

18、问题: 网站要求用户输入用户名和密码才能注册。编写程序检查用户输入密码的有效性。 以下是检查密码的条件: 1。[a-z]之间至少有一个字母 2。在[0-9]之间至少有一个数字 1。[A-Z]之间至少有一个字母 3。[$#@]中至少有一个字符 4。交易密码最短长度:6 5。交易密码的最大长度:12 您的程序应该接受一系列逗号分隔的密码,并将根据上述标准进行检查。 将打印符合条件的密码,每个密码用逗号分隔。 例子 如果以下密码作为程序的输入: ABd1234@1,a F1#,2w3E*,2We3345 那么,程序的输出应该是: ABd1234@1 注:本题博主认为自己的方法是符合题意的,也就是程序输出应该不是题中所给的,叙述和结果不一致,仅作参考。 

ls = input("input your password:")

tol = []

zm=sz=zm1=fh1=d=c = 0

for i in ls:

    if i.isalpha():

        if ("a"<=str(i) and str(i)<="z"):

            if (zm == 0):

                zm = 1      

            tol.append(i)    

        elif ("A"<=str(i) and str(i)<="Z"):

            if (zm1 == 0):

                zm1 = 1

            tol.append(i)

    elif i.isdigit():       

        if (0<int(i) & int(i)<10 ):

            if (sz == 0):

                sz = 1

            tol.append(i)

    elif str(i) in ('#','$','@'):

        if (fh1 == 0):    

            fh1 = 1

        tol.append(i)

    else :

        print ("存在规定外的字符"+str(i)) 

    if (len(ls)>=6 & len(ls)<=12):

        if (d == 0):

            d = 1      

    else :

        print ("超出范围")

if (zm == 1 and zm1==1 and sz ==1 and fh1==1 and d==1):

    print ("密码正确:"+str(tol))

else:

    print ("请输入规范密码")

    print ("当前符合规范的密码:"+str(tol))

import re

value = []

items=[x for x in raw_input().split(',')]

for p in items:

    if len(p)<6 or len(p)>12:

        continue

    else:

        pass

    if not re.search("[a-z]",p):

        continue

    elif not re.search("[0-9]",p):

        continue

    elif not re.search("[A-Z]",p):

        continue

    elif not re.search("[$#@]",p):

        continue

    elif re.search("\s",p):

        continue

    else:

        pass

    value.append(p)

print ",".join(value)

19、问题: 您需要编写一个程序来按升序对(name,age,height)元组进行排序,其中name是string,age和height是数字。元组由控制台输入。排序条件为: 

1:按名称排序; 

2:然后按年龄排序; 

3:然后按分数排序。 

优先考虑的是名字>年龄>分数。 

如果将下列元组作为程序的输入: 

Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85 

那么,程序的输出应该是: 

[(‘John’, ‘20’, ‘90’), (‘Jony’, ‘17’, ‘91’), (‘Jony’, ‘17’, ‘93’), (‘Json’, ‘21’, ‘85’), (‘Tom’, ‘19’, ‘80’)] 

import operator

ls = []

while True:

    x = input("please input:")

    if not x:

        break

    ls.append(tuple(x.split(',')))

print (sorted(ls,key=operator.itemgetter(0,2,1)))

l = []

while True:

    s = raw_input()

    if not s:

        break

    l.append(tuple(s.split(",")))

print sorted(l, key=itemgetter(0,1,2))

20、问题: 使用生成器定义一个类,该生成器可以在给定范围0和n之间迭代可被7整除的数字。 

def putNumbers(n):

    i = 0

    while i<n:

        j=i

        i=i+1

        if j%7==0:

            yield j

x= input("please input 0-?:")

for i in putNumbers(int(x)):

    print (i)

def putNumbers(n):

    i = 0

    while i<n:

        j=i

        i=i+1

        if j%7==0:

            yield j

for i in reverse(100):

    print i

推荐文章: MySql练习题–50题–第一弹 MySql练习题–45题–第二弹

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档