# python Week 11 (python learning problem set)

There are several difficult topics this week. Other topics are of normal difficulty. I will analyze more difficult topics (I want to try to record video analysis). I suggest doing other topics myself, which can still be used to consolidate the foundation.

# 6-1 JMU Python combined data type-1. Calculate the Euclidean distance of coordinate points (10 points)

Several points are read, and each point is put into a tuple. And print out the point information of all points, the type of points and the distance between the points and the origin.

### Function interface definition:

```readPoint() #Read the coordinates from a row of numbers separated by, put them in tuples and return
distance(point) #Calculate the distance between point and the origin and return to the function in math library```

### Example of referee test procedure:

```/* Please fill in the answer here */
n = int(input())
for i in range(n):
print('Point = {}, type = {}, distance = {:.3f}'.format(p,type(p),distance(p)))```

### Input format:

Enter n to enter the coordinates of N lines of points below. Coordinates are all integers.
The point coordinates x,y,z are separated by. Coordinates are all integers.

Note: the coordinates are separated by, and the corresponding positions may have no characters or contain multiple space characters. When reading in, it is processed as 0.

### Output format:

See output example

### Input example:

```5
1,1,1
,,
2,,1
3,1,3
5,,```

No blank lines at the end

### Output example:

```Point = (1, 1, 1), type = <class 'tuple'>, distance = 1.732
Point = (0, 0, 0), type = <class 'tuple'>, distance = 0.000
Point = (2, 0, 1), type = <class 'tuple'>, distance = 2.236
Point = (3, 1, 3), type = <class 'tuple'>, distance = 4.359
Point = (5, 0, 0), type = <class 'tuple'>, distance = 5.000```

No blank lines at the end

```def readPoint():  # Read the coordinates from a row of numbers separated by, put them in tuples and return
n = input()
n = n.split(',')
m = []
for i in n:
if i != '':
m.append(int(i))
else:
m.append(0)
m = tuple(m)
return m

def distance(point):  # Calculate the distance between point and the origin and return to the function in math library
sum1 = 0
for i in point:
sum1 += i ** 2
return sum1 ** 0.5
```

# 6-2 find the number that the last number in the list is twice the previous number (10 points)

This problem requires the implementation of a function doubles(), with a list of integers as input parameters. The output list is exactly twice the previous number, and each number occupies one line.

### Function interface definition:

`doubles(lst)`

lst is a list of integers

### Example of referee test procedure:

```#  Please fill in the answer here

def main():
mylist = [3, 0, 1, 2, 3, 6, 2, 4, 5, 6, 12]
doubles(mylist)

main()
```

### Input example:

No input data

No blank lines at the end

### Output example:

The corresponding output is given here. For example:

```2
6
4
12```

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 16:45
def doubles(lst: list):
for i in range(len(lst) - 1):
if lst[i] * 2 == lst[i + 1]:
print(lst[i + 1])
```

# 6-3 integer digit sum (higher education society, exercise 8-3 of fundamentals and applications of Python Programming) (4 points)

Write a function that accepts a positive integer as a parameter and returns the sum of the digits of the integer.

### Function interface definition:

`def digitSum(v)`

v is the input integer (positive integer); Function returns an integer whose value is the sum of the digits of v.

### Example of referee test procedure:

```a = int(input())
print(digitSum(a))```

### Input example:

`291`

No blank lines at the end

### Output example:

`12`

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 17:04
def digitSum(v):
lis = list(str(v))
lis = list(map(int, lis))
return sum(lis)
```

# 6-4 special number (10 points)

A special positive integer, which is a complete square after adding 150, and a complete square after adding 136. Enter an integer n from the keyboard to find the minimum number starting from n. Use function to judge whether a number is a complete square number.

### Function interface definition:

```The function interface is described here. For example:
def  Perfectsquare (x )```

### Example of referee test procedure:

```Here is an example of a function being called for testing. For example:

n  = int(input())
while True:
if Perfectsquare(n+150):
if Perfectsquare(n+136+150):
print(n)
break
n = n+1```

### Input example:

A set of inputs is given here. For example:

`10`

No blank lines at the end

### Output example:

The corresponding output is given here. For example:

`75`

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 17:13
def Perfectsquare(x):
if int(x ** 0.5) == x ** 0.5:
return True
else:
return False```

# 7-1 JMU Java & Python count the number of words in the text and sort them according to the number of occurrences (25 points)

Now you need to count the number of words in several paragraphs of text (English), and you also need to count the number of occurrences of each word.

Note 1: words are separated by spaces (one or more spaces).
Note 2: ignore blank lines or blank lines.

Basic Edition:
Statistics are case sensitive and do not delete the specified punctuation marks.

1. Before statistics, you need to delete the specified punctuation marks!,: *?. Note: the so-called deletion is to replace the corresponding character with a space.
2. You need to ignore the case of words when counting words.

### Enter description

Several lines of English, finally with!!!!! To end.

### Output description

Number of words
The top 10 words in the number of occurrences (the number of occurrences is sorted in descending order, and if the number of occurrences is the same, it is sorted in ascending alphabetical order of the key value) and the number of occurrences.

### Input sample 1

```failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta

when you are wondering whether new money it has laid
background because of you then at the heart of the

most lax alert and most low awareness and left it

godsend failed
!!!!!
```

### Output sample 1

```46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2```

No blank lines at the end

### Input sample 2

```Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief when You
are thinking how to. spend several hard-won lepta.

when yoU are? wondering whether new money it has laid
background Because of: yOu?, then at the heart of the
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it

godsend failed
!!!!!```

No blank lines at the end

### Output sample 2

```54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2```

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 17:17
dict1 = {}
sum1 = 0
while True:
str = input()
lis = list(str.lower().split())
if len(lis) == 0:
continue
else:
if "!!!!!" in lis:
break
for i in lis:
for q in "!.,:*?":
i = i.replace(q, '')
if i.lower() in dict1:
dict1[i.lower()] += 1
else:
dict1[i.lower()] = 1
sum1 = len(dict1)
print(sum1)
sum1 = 0
for i in sorted(dict1.items(), key=lambda x: (-x, x)):
if sum1 == 10:
break
print("%s=%s" % (i, i))
sum1 += 1
```

# 7-2 analyze the valve status in the workshop (higher education society, exercise 5-4 of fundamentals and applications of Python Programming) (4 points)

The CPU reads 1 byte through an 8-bit IO port and now stores it in a bytes object, for example: b'\x45'; These 8 bits respectively represent the current status of 8 valves in the workshop. 1 indicates that the valve is on and 0 indicates that the valve is off. Please design a program to parse the current state of 8 valves from the bytes object. True means on and False means off. These eight states shall be organized in a list, where the ith element corresponds to the ith bit of the input byte.

Example of output format: [True, False, False, True, True,True,False,False]

### Input format:

Single byte bytes in the form of b'\x45'. (note hexadecimal)

### Output format:

A list of 8 Boolean values. The i-th element represents the i-th bit of the input byte (0 ~ 7 bits respectively from low to high).

[True, False, True, False, False, False, True, False]

### Input example:

`b'\x01'`

No blank lines at the end

### Output example:

`[True, False, False, False, False, False, False, False]`

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 18:11
bit = eval(input())
lis1 = list(bin(int.from_bytes(bit, byteorder='little', signed=False)).replace('0b', ''))
lis2 = list(reversed(lis1))
lis3 = []
if len(lis2) < 8:
while True:
lis2.append('0')
if len(lis2) == 8:
break
for i in range(8):
if lis2[i] == '1':
lis3.append(True)
else:
lis3.append(False)
print(lis3)
```

# 7-3 count the number of words in the input string and the average length of words (higher education society, exercise 7-7 of fundamentals and applications of Python Programming) (3 points)

Write a program to accept a line of English sentences input by the user (assuming that the sentence is only composed of English words and spaces, excluding commas and other symbols), count and output the number of words contained in the line of sentences and the average length of words.

### Input format:

Word 1 word 2... Word n

### Output format:

Number of words, average length of words (keep two decimal places)

### Input example:

`aaa bbb ccccccccccc`

No blank lines at the end

### Output example:

`3,5.67`

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 18:50
n = list(input().split())
m = len(n)
sum1 = 0
for i in n:
sum1 += len(i)
print("%d,%.2f" % (m, sum1 / m))
```

# 7-4 list sorting and reverse order (10 points)

It is known that the list elements are [12,3,48,6,79,63,89,7], and the list is output in reverse order, ascending order and reverse order.

nothing

### Output format:

The list reverse order result is: [7, 89, 63, 79, 6, 48, 3, 12] the list ascending sort result is: [3, 6, 7, 12, 48, 63, 79, 89] the list descending sort result is: [89, 79, 63, 48, 12, 7, 6, 3]

### Input example:

A set of inputs is given here. For example:

No blank lines at the end

### Output example:

The corresponding output is given here. For example:

```The reverse order result of the list is:[7, 89, 63, 79, 6, 48, 3, 12]
The ascending sorting result of the list is:[3, 6, 7, 12, 48, 63, 79, 89]
The descending sorting result of the list is:[89, 79, 63, 48, 12, 7, 6, 3]```

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 18:54
lis1 = [12, 3, 48, 6, 79, 63, 89, 7]
lis2 = list(reversed(lis1))
lis3 = sorted(lis1, reverse=False)
lis4 = sorted(lis1, reverse=True)
print("The reverse order result of the list is:%s\n The ascending sorting result of the list is:%s\n The descending sorting result of the list is:%s" % (lis2, lis3, lis4))
```

# 7-5 list insertion (10 points)

Enter a string s and a non negative integer i, list ls = ['2', '3', '0', '1', '5'], and insert the user entered string s at the specified position i and at the end of the list respectively. When i > = 5, it is equivalent to inserting the string s twice at the end of the list.

### Input format:

Enter a string on the first line

Enter a nonnegative integer on the second line

### Output format:

List after inserting new data

### Input example:

A set of inputs is given here. For example:

```a
2```

No blank lines at the end

### Output example:

The corresponding output is given here. For example:

`['2', '3', 'a', '0', '1', '5', 'a']`

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 19:01
ls = ['2', '3', '0', '1', '5']
n = input()
i = int(input())
if i < 5:
ls.insert(i, n)
else:
ls.append(n)
ls.append(n)
print(ls)```

# 7-6 odd four digits (10 points)

A four digit number. The numbers are different from each other. The sum of all numbers is 6, and this number is a multiple of 11. How many four digits meet this requirement? What are each?

### Input format:

The title is not entered

### Output format:

The first line outputs the number of qualified numbers

The second line outputs all four digits that meet the conditions in the form of a list, and the list elements are arranged in order from small to large

### Input example:

A set of inputs is given here. For example:

No blank lines at the end

### Output example:

The corresponding output is given here. For example:

```6
[1023, 1320, 2013, 2310, 3102, 3201]```

No blank lines at the end

```# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/11/10 19:17
def digitSum(v):                    #Integer digit sum
lis = list(str(v))
lis = list(map(int, lis))
return sum(lis)

def digitsame(v):                   #Determine whether there are the same numbers
lis = list(str(v))
lis = list(map(int, lis))
lis1 = list(set(lis))
if len(lis1) == 4:
return True
return False

lis = []
for i in range(1000, 10000):
if digitSum(i) == 6 and i % 11 == 0 and digitsame(i):
lis.append(i)
print(6)
print(lis)
```

Tags: Python pta

Posted on Wed, 10 Nov 2021 18:52:03 -0500 by 7awaka