Python123 1-8 weeks programming answers

the first week

1. Conditional output of Hello World
Description
Obtain an integer entered by the user. Refer to the integer value and print out "Hello World". The requirements are: If the
input value is 0, direct output "Hello World" If the
input value is greater than 0, the output mode line of two characters "Hello World" (Space is also a character) ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‪‬‬‪‬‬‬‬‬‬ ‬‭‬‪ If the
input value is less than 0, output the "Hello World"
solution vertically :

n = eval(input())
if n == 0:
    print("Hello World")
elif n > 0:
    print("He\nll\no \nWo\nrl\nd")
else:
    for c in "Hello World":
        print(c)

the second week


A, turtle octagon draw
describe
the use of turtle library, draw a octagon. Note:
this is For an automatic review question, please add the content of the horizontal line in the "programming template", the horizontal line is not retained. output
example
octagonal The shape effect is as follows: ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‪‬‪‬‬‬‬‬‬‬‬

solution:

import turtle as t
t.pensize(2)
for i in range(8):
    t.fd(100)
    t.left(45)


Two, turtle graphics rendering star anise
describe
the use of turtle library, draw an octagonal shape. Note:
this is For an automatic review question, please add the content of the horizontal line in the "programming template", the horizontal line is not retained.
output
example
octagonal graphics follows: ‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‪‬‬‬‬‬‬‬‬‬‬‬

solution:

import turtle as t
t.pensize(2)
for i in range(8):
    t.fd(150)
    t.left(135)

The third week

1. Square root formatting
Description
Obtain an integer a entered by the user, calculate the square root of a, retain 3 decimal places, and print it out.
using the width of the output If the
result of more than 30 Characters, the width of the result shall prevail. example
input
output
input output
example 110 +++++++++++++++++++++++++++ 3.162
Solution:

a = input()
b = pow(eval(a),0.5)
if len(str(b)) <= 30:
    print('{:+>30.3f}'.format(b))
else:
    print(b)

Second, string segmentation combination
Description
Obtain an input string s, split s with the character minus sign (-), and combine the first and the last two segments with plus sign (+) to output. example input output input output example 1 Alice-Bob-Charis -David-Eric-Flurry Alice + Flurry solution:





s = input()
ls = s.split("-")
print("{}+{}".format(ls[0], ls[-1]))

the fourth week

One, four-digit rose number
description
rose is number four The self-power number refers to an n-digit number, and the sum of the n-th power of the digits in each digit is equal to itself. For example:
when n When it is 3, there are 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153, and 153 is an auto-power number when n is 3. The 3-digit auto-power number is called the daffodil number. Please
output of all 4 The four-digit rose number of digits, in order from small to large, each line is one. input
output example
output Only format, not right or wrong. input
output
example 1 No 111122223333
solution:

s = ""
for i in range(1000, 10000):
    t = str(i)
    if pow(eval(t[0]),4) + pow(eval(t[1]),4) + pow(eval(t[2]),4) + pow(eval(t[3]),4) == i :
        print(i)

Second, the sum of prime numbers within 100
Description
Find the sum of all prime numbers within 100 and output.
It refers to a prime Tip:
You can individually Determine whether each number within 100 is a prime number, and then sum.
the input format
of the subject Did not enter
input output Example
Input Output
Example 1 1234 (This is an example, not a real output)
Solution:

def is_prime(n):
    for i in range(2,n):
        if n%i == 0:
            return False
    return True
sum = 0
for i in range(2,100):
    if is_prime(i):
        sum += i
print(sum)

fifth week

A random password generator
described
complementary template programming code, the following functions:
17 is an integer Each password is output on a separate line. generated
using random password .randint () function.
example input output
input Output
Example 1 3 634524926
Solution:

import random
def genpwd(length):
    a = 10**(length-1)
    b = 10**length - 1
    return "{}".format(random.randint(a, b))
length = eval(input())
random.seed(17)
for i in range(3):
    print(genpwd(length))

Two consecutive primes calculation
described
complementary template programming code, the following functions: obtaining
user input number N, calculates and outputs the five prime numbers starting from N, single line output, with commas between the prime number divided. Note: You
need to consider The number N entered by the user may be a floating point number, and the input should be an integer; no comma is needed after the last output. example
input
output
input output
example 112 13, 17, 19, 23, 29
solutions:

def prime(m):
    for i in range(2,m):
        if m % i == 0:
            return False
    return True

n = eval(input())
a = int(n)
a = a+1 if a < n else a
count = 5
while count > 0:
    if prime(a):
        if count > 1:
            print(a, end=",")
        else:
            print(a, end="")
        count -= 1 
    a += 1

6th week

1. Sum of different numbers of numbers
Description
Obtain an integer N input by the user, and output the sum of different numbers appearing in N. For example:
user input 123123123, where the different numbers appearing are: 1, 2, 3, and the sum of these numbers is 6.
example input output
input Output
Example 1 123123123 6
solutions:

print("请输入一个整数N:")
num = eval(input())
num2 = str(num)
num3 = set()
for j in num2:
    num3.add(j)
sum = 0
for i in num3:
    k = int(i)
    sum += k
print("该整数不重复各位数之和是:", sum)

2. Statistics of the maximum number of names
Description
A string is given in the programming template, which contains the names of people with duplicates. Please directly output the names of the names that appear the most.
input
output example
here is an example to show The output format is not the result. input
output
example 1 Wu Huangrong
Solution:
s = '' 'Shuang'er Hong Qigong Zhao Min Zhao Min Xiao Yaozi Ao Bai worship Yin Tianzheng Jin Fafa Wang Qiao Feng Yang Guohong Qi Gong Guo Jing
Yang Xiao Ao Bai Yin Tianzheng Duan Yu Yang Xiao Murong Fu A Zi Murong Fu Guo Fuqiao Feng Guo Fu Linghu
Kingland King Murong Fu Yang Guo Maid Limo Chou Mei Chaofeng Mei Chaofeng Yang Xiao Zhang Wuji Qigong
Aobai Kingland Duan Yu Yue Buqun I buy Huang Rong Qiao Feng Qiao Feng Chi Master King Kublai Khan
A Zi Qiaofeng Golden Wheel Falun Yuan Guannan Zhang Wuji Guo Xiang Huang Rong Li Mochou Zhao Min Zhao Min Guo Fu Zhang Sanfeng
Qiao Feng Zhao Minmei Chaofeng Shuang'er Aochen Chen Jialuo Yuan Guannan Guo Fu Guo Fu Yang Xiao Zhao Min Golden Chakra
Hu Bilie Murong Fu Zhang Sanfeng Zhao Min Yang Xiaoling Hu Chong Yellow Pharmacist Yuan Guannan Yang Xiaowan Yan Honglie Yan Tianzheng
Li Mochou A Zi Xiaoyao Zi Qiao Feng Xiao Yaozi Wan Yanhonglie Guo Fu Yang Xiao Zhang Wuji Yang Guo Murong Fu
Xiaoyao Zi Xuzhu Shuang'er Qiao Feng Guo Fu Huang Rong Li Mochou Chen Jialuo Yang Guohuublie Aowang Wang Yuyan
Hong Qigong Wei Xiaobao A Zhumei Chaofeng Duan Yu Yueshan Shan Yan Yanlie Li Qiao Feng Duan Yu Yang Guo Yang Guo Murong Fu
Huang Rong Yang Guo A
Ziyang Xiao Zhang Sanfeng Zhang Sanfeng Zhao Min Zhang Sanfeng Yang Xiao Huang Rong Jin Fawang Guo Xiang Zhang Sanfeng Linghu Chong Zhao Min Guo Fu Wei Xiaobao Huang Pharmacist A
Ziwei Xiaobao Golden Wheel King Yang Xiaolinghu Chong A Zi Hong Qigong Yuan Guannan Shuang'er Guo Jingao worship Xie Xun A Zi Guo Xiangmei Chao Feng Zhang Wuji Duan Yu Kublai
Wan Yan Honglie Shuang Er Xiaoyao Xie Xun Wan Yan Honglie Yin Tianzheng Golden Chakra Zhang Sanfeng Shuang'er Guo Xiang A Zhu
Guo Xiang Shuang'er Li Mochou Guo Xiang Kublai King
Jin Fawang Zhang Wuji Ao Bai Kublai Guo Xiang Linghu Chong Xie Xunmei Chao Feng Yin Tianzheng Duan Yu Yuan Guannan Zhang Sanfeng Wang Yuyan A Zi Xie Xun Yang Guo Guo Huang Rong Shuanger Extinction Teacher
Tai Duan Yu Zhang Wuji Chen Jialuo Huang Rong Aobai I buy Happy son Kublai Zhao
Xiao King Jinlun sub Wanyanhonglie Shuanger Aobai Qigong Zhao Guo Xiang Guo Fu '' '

ls = s.split()
d = {}
for i in ls:
    d[i] = d.get(i, 0) + 1
max_name, max_cnt = "", 0
for k in d:
    if d[k] > max_cnt:
        max_name, max_cnt = k, d[k]
print(max_name)

Seventh week:

1. The average number of columns in the text
Describe the average number of columns in the
printed output attachment file, calculated as follows: (1)
refers to effective row line containing at least one character, not counting the blank lines; (2)
the number of columns per row for the effective number of characters; ‬‪‬‮‬‫‬‮‬‪‬‪‬‪ (3)
Average number of columns is the average number of effective lines of the columns, using rounded carry rounded off.
example O
only Examples of output formats are given, incorrect answers. input
output
example 1 No 123
solutions:

f=open("latex.log")
tot=0
line=0
for i in f:
    i=i.strip("\n")
    if(i==""):
        continue
    tot+=len(i)
    line+=1
print(round(tot/line))

Two, cleaning and CSV format conversion
described
accessory is a CSV format file, extract data format conversion as follows: (1)
row by reverse order; (2)
each row of data in reverse order; ‪‬‪‬‪‬‮‬‫‬‭‬‪ (3)
a semicolon (;) instead of a comma (,) divided data, no spaces; according to
the above output data after conversion required. input
output example
less It is a format example, not the final result. input
output
Example 1 (the following is in the file) 1, 2, 34, 5, 67, 8, 9 9; 8; 76; 5; 43; 2; 1
Solution:

f=open("data.csv")
line=f.readlines()
line.reverse()
for i in line:
    i=i.replace('\n','')
    i=i.replace(' ','')
    a=i.split(",")
    a.reverse()
    print(";".join(a))

Eighth week

1. Robust input of English characters
Description
Obtain any possible input from the user and print out the English characters in it without error.
Input and output example
Input and output
example 1 * & ^ 123abc0e abce
solution:

ls = []
for i in range(26):
    ls.append(chr(ord('a') + i))
    ls.append(chr(ord('A') + i))
s = input()
for c in s:
    if c in ls:
        print(c, end="")

2. Robust input of numbers
Description
Obtain a number entered by the user, which may be a floating point number or a complex number. If it is an integer, only the decimal form is accepted, and it can only be a number. Square the input numbers and output the result.
requirements: (1)
regardless of the user input what kind of content, program error-free; (2)
If you make a mistake, please output "was entered incorrectly."
example input output
input Output
Example 1 1 + 2j (-3 + 4j)

s = input()
try:
    if complex(s) == complex(eval(s)):
        print(eval(s)**2)
except:
    print("输入有误")
Published 3 original articles · received 1 · views 50

Guess you like

Origin blog.csdn.net/hq0523/article/details/105603275