当前位置: 首页 > article >正文

100+Python挑战性编程练习系列 -- day 2

Question 4

编写一个程序,从控制台接收一个逗号分隔的数字序列,并生成一个列表和一个包含每个数字的元组。假设向程序提供以下输入:34,67,55,33,12,98 然后,输出应为:[‘34’, ‘67’, ‘55’, ‘33’, ‘12’, ‘98’]
(‘34’, ‘67’, ‘55’, ‘33’, ‘12’, ‘98’)

lst = input().split(',')  # the input is being taken as string and as it is string it has a built in
                          # method name split. ',' inside split function does split where it finds any ','
                          # and save the input as list in lst variable

tpl = tuple(lst)          # tuple method converts list to tuple

print(lst)
print(tpl)

Question 5

定义一个至少有两个方法的类:
getString:从控制台输入获取字符串
printString:以大写打印字符串。
也请包含简单的测试函数来测试类方法。

class IOstring():
    def get_string(self):
        self.s = input()

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

xx = IOstring()
xx.get_string()
xx.print_string()

Question 6

编写一个程序,根据给定的公式计算并打印值:
Q =[(2 × C × D)/ H]的平方根
以下是C和H的固定值:
C是50。H是30。
D是一个变量,它的值应该以逗号分隔的顺序输入到程序中。例如,让我们假设以下逗号分隔的输入序列被赋予程序: 100,150,180 程序的输出应为: 18,22,24

解法1:

from math import sqrt

C,H = 50,30

def calc(D):
    return sqrt((2*C*D)/H)

D = input().split(',')                     # splits in comma position and set up in list
D = [str(round(calc(int(i)))) for i in D]  # using comprehension method. It works in order of the previous code
print(",".join(D))

解法2:

from math import sqrt
C, H = 50, 30
mylist = input().split(',')
print(*(round(sqrt(2*C*int(D)/H)) for D in mylist), sep=",")

解法3:

my_list = [int(x) for x in input('').split(',')]
C, H, x = 50, 30, []

for D in my_list:
    Q = ((2*C*D)/H)**(1/2)
    x.append(round(Q))

print(','.join(map(str, x)))

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

方法1:

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)

方法2:

x,y = map(int,input().split(','))
lst = [[i*j for j in range(y)] for i in range(x)]
print(lst)

Question 8

编写一个程序,接受逗号分隔的单词序列作为输入,并在按字母顺序排序后以逗号分隔的顺序打印单词。
假设向程序提供以下输入: without,hello,bag,world
然后,输出应为: bag,hello,without,world

lst = input().split(',')
lst.sort()
print(",".join(lst))

Question 9

写一个程序,接受一系列的行作为输入,并在将句子中的所有字符大写后打印这些行。
假设向程序提供以下输入:
Hello world
Practice makes perfect
然后,输出应为:
HELLO WORLD
PRACTICE MAKES PERFECT

解法1:

lst = []

while True:
    x = input()
    if len(x)==0:
        break
    lst.append(x.upper())

for line in lst:
    print(line)

解法2:

def inputs():
    while True:
        string = input()
        if not string:
            return
        yield string

print(*(line.upper() for line in inputs()),sep='\n')

http://www.kler.cn/news/16555.html

相关文章:

  • python基于轻量级YOLOv5的生猪检测+状态识别分析系统
  • 读研读博不emo
  • 数字化医院PACS影像系统 三维影像后处理技术应用
  • 100篇帮小白入门——什么是嵌入式系统?
  • CANOE入门到精通——CANOE系列教程记录2
  • 【Python】芜湖市空气质量指数可视化(散点图、分类散点图、单变量分布图、线性回归拟合图、相关性热力图)
  • Linux常见的网络命令
  • ChatGPT技术原理 第五章:GPT模型
  • 《Effective Python 编写高质量Python代码的59个有效方法》学习笔记5
  • mybatis generator自定义model的代码注释
  • 测牛学堂:2023软件测试入门学习指南之测试方法完结总结
  • EMC VPLEX VS2 FRU故障备件更换基本流程
  • JAVA开发——常用的注解
  • 十一、通过六个因素对织物起球等级进行预测
  • 【第十一届泰迪杯B题】最终版:问题一的实现(含源代码)
  • 26从零开始学Java之如何对数组进行排序与二分查找?
  • scratch比大小 中国电子学会图形化编程 少儿编程 scratch编程等级考试三级真题和答案解析2023年3月
  • gRPC入门教程
  • 【Java笔试强训 25】
  • 时序预测 | MATLAB实现BO-CNN-BiLSTM贝叶斯优化卷积双向长短期记忆网络时间序列预测
  • Java 基础入门篇(四)—— 方法的重载与参数传递机制
  • 11.string,stringbuilder,stringbuffer的区别和联系。
  • 5. 操作系统基础
  • windows下定时备份mysql数据库
  • 第 02 章 OSPF实验
  • 【AI前沿】chatgpt还有哪些不足?
  • Python操作PostgreSQL数据库
  • 【学习视频】阅读开源工业软件和工业智能实战上线B站
  • 数字设计小思 - D触发器与死缠烂打的亚稳态
  • 五种最危险的新兴网络攻击技术