python100道练习题(11--15)

昨天因为家里有点事,只能匆匆回来,半夜补发一下昨天的内容。

11 lv2

这道题用第一种方法是无法得到题目要求的结果的,大家可以去试一下,我用第一种办法旨在说明一些进制转换函数。

转换到十进制

  • int( , )第一部分填写数字,第二部分填写被转换数的进制。
    如,二进制转十进制就是
    int(‘0100’,2)

转换到十六进制

  • 十进制转十六进制:
    hex(1033)
  • 其余进制数转换为十六进制可以先转换为十进制,再转换为十六进制
    如:
    hex(int(‘17’,8))

转换到二进制

  • 十进制转二进制:
    bin(1000)
  • 其余进制数转换方法同十六进制。

转换到八进制

  • oct(),可以将任意数直接转换
# question:
# Write a program which accepts a sequence of comma separated 4 digit binary numbers as
# its input and then check whether they are divisible by 5 or not.
# The numbers that are divisible by 5 are to be printed in a comma separated sequence.
# Example:
# 0100,0011,1010,1001
# Then the output should be:
# 1010
# Notes: Assume the data is input by console.
#写一个接受用逗号隔开的二进制数作为程序的输入并且检测它是否可以被5整除。可以被5整除的那个数
#用以逗号隔开的形式输出,例:
#输入:
# 0100,0011,1010,1001
# 输出:
# 1010
values=[x for x in input().split(',')]
items=[]
for value in values:
    value=int(f'{value}',2)
    if value % 5 == 0 :
        value=bin(value)
        items.append(value)
print(','.join(items))

values=[x for x in input().split(',')]
items=[]
for value in values:
    x=int(value,2)
    if not x % 5 :
        items.append(value)
print(','.join(items))

12 lv2

# Question:
# Write a program, which will find all such numbers between 1000 and 3000 (both included)
# such that each digit of the number is an even number.
# The numbers obtained should be printed in a comma-separated sequence on a single line.
# 找出1000到3000之间的所有偶数,并且把它们用以逗号隔开的形式单行输出
even_nums=[x for x in range(1000,3001)]
save=[]
for even_num in even_nums:
    if even_num % 2 == 0:
       save.append(str(even_num))
print(','.join(save))

13 lv2

# Question:
# Write a program that accepts a sentence and calculate the number of letters and digits.
# Suppose the following input is supplied to the program:
# hello world! 123
# Then, the output should be:
# LETTERS 10
# DIGITS 3
# 写一个这样的程序:接收一个句子并且计算其中的字母数和数字的数量,
# 输入格式如下:
# hello world! 123
# 输出格式如下:
# LETTERS 10
# DIGITS 3
sentence = input()
sentence.lower()
l_count = 0
d_count = 0
for i in range(len(sentence)):
    if 'a' <= sentence[i] <= 'z':
        l_count += 1
    elif '0' <= sentence[i] <= '9':
        d_count += 1
print('LETTERS %d'%l_count)
print('DIGITS %d'%d_count)

s = 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 lv2

# Write a program that accepts a sentence and calculate the number of upper case letters
# and lower case letters.
# Suppose the following input is supplied to the program:
# Hello world!
# Then, the output should be:
# UPPER CASE 1
# LOWER CASE 9
# 写一个统计该句子中大写字母和小写字母数目的代码
# 输入应该是类似于:
# Hello world!
# 输出:
# UPPER CASE 1
# LOWER CASE 9
sentence=str(input())
d={
     'UPPER CASE':0 , 'LOWER CASE':0}
for letter in sentence:
    if letter.isupper():
        d['UPPER CASE']+=1
    elif letter.islower():
        d['LOWER CASE']+=1
print('UPPER CASE',d['UPPER CASE'])
print('LOWER CASE',d['LOWER CASE'])

15 lv2

# Question:
# Write a program that computes the value of a+aa+aaa+aaaa with a given digit
# as the value of a.
# Suppose the following input is supplied to the program:
# 9
# Then, the output should be:
# 11106
# 输入一个数字a,计算a+aa+aaa+aaaa
# 输入:
# 9
# 输出:
# 11106
a=str(input())
c=a
count = 1
sums = 0
while count < 5:
    b = int(a)
    sums =sums+b
    a=a+c
    count+=1
print(sums)

a = 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)

今天就先到这儿,等我睡醒咱们继续解题!如果这篇文章对你有帮助的话可以点个赞再走哦。

你可能感兴趣的:(python)