Week 4 Learning Summary

1. Print the numbers from 1 to 10 and the square, geometric series and factorial of each number

from math import factorial

def main():
print('%-10s%-10s%-10s%-10s' % ('number', 'square', 'geometric series', 'factorial'))
for num in range(1, 11 ):
print('%-12d%-12d%-12d%-12d' % (num, num * 2, 2 * num, factorial(num)))

if name == ‘main‘:
main()

2. Design a function to generate a verification code of a specified length (a random string composed of numbers and uppercase and lowercase English letters)

from random import randrange

def generate_code(length = 4):
all_chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST0123456789’
all_chars_length = len(all_chars)
code = ”
for _ in range(length):
index = randrange(all_chars_length)
code += all_chars[index]
return code

def main():
for _ in range(1, 11):
print(generate_code(4))

if name == ‘main‘:
main()

3. Design a function to count the number of occurrences of English letters and numbers in the string

def count_number(string):
letter_count = 0
digital_number = 0
for ch in string:
if ‘a’ <= ch <= ‘z’ or ‘A’ <= ch <= ‘Z’:
letter_count += 1
elif ‘0’ <= ch <= ‘9’:
digital_number += 1
return letter_count,digital_number

def main():
print(count_number(‘abgdggd6666’))
print(count_number(‘fssgs535353ddd’))

if name == ‘main‘:
main()

4. Design a function to judge whether the elements in the incoming integer list (the number of elements is required to be greater than 2) can constitute an arithmetic sequence

def foo(list):
list_length = len(list)
assert list_length >2
new_list = sorted(list)
for index in range(2,list_length):
if list[index] - list[index-1]!=list[index-1]-list[index-2]:
return False
return True

def main():
list1=[1,4,5,6,7,9]
list2=[100,300,400,500,600,700,900]
print(foo(list1))
print(foo(list2))
if name==’main‘:
main()

5. Design a function that calculates the sum of all sequences of numbers in a string

from re import findall
def find_number(my_string):
total=0
my_number = map(int, findall(‘\d+’, my_string))
for number in my_number:
total += number
return total

def main():
print(find_number(‘abc3456gh’))
print(find_number(‘db583883’))

if name==’main‘:
main()

6. Design a function to encrypt the incoming string (assuming that the string contains only lowercase letters and spaces). The encryption rules are a to d, b to e, c to f,..., x to a , y changes to b, z changes to c, spaces remain unchanged, return the encrypted string

def caesar_encrypt(string):
base = ord(‘a’)
encrypted_string = ”
for ch in string:
if ch != ’ ‘:
curr = ord(ch)
diff = (curr - base + 3) % 26
ch = chr(base + diff)
encrypted_string += ch
return encrypted_string

def main():
print(caesar_encrypt(‘attack at dawn’)) # dwwdfn dw gdzq
print(caesar_encrypt(‘dinner is on me’)) # glqqhu lv rq ph

if name == ‘main‘:
main()

PS: I found that a good way to learn programming is to make a draft before writing code. What does it mean? Just figure out your own ideas first, you can write a logical relationship diagram on paper, and then write the code, continue to summarize tomorrow night, good night

(1)count

The count method is used to count the number of occurrences of a character in a string.
str.count(sub, start=0, end=len(string))] sub —the subcharacter of
the search
start—the position where the string starts to search, the default first character, index 0
end—the position where the string ends the search , the index of the first character in the character is 0, which defaults to the last position of the string.
Example: str='aaabbbbccccc'
sub = 'a'
print(str.count(str))#The default value of the starting position, the search range is the entire string
Result : 3

(2)counter

1. Find the number of occurrences of each element in the array
a = [1,4,2,3,2,3,4,2]
from collections import Counter
print (Counter(a))
print result:
Counter({2: 3 , 3: 2, 4: 2, 1: 1})

The result shows: element 2 appears 3 times; element 3 appears 2 times; element 4 appears 2 times; element 1 appears 1 time.

2. Find the element with the most occurrences in the array
from collections import Counter
a = [1,4,2,3,2,3,4,2]
countNumber = Counter(a) #Print the number of occurrences of each element in a
mostNumber = countNumber .most_commo(2)) #most_comm() function prints the element with the most occurrences in the array, 2 means: output several elements with the most occurrences
Running result:
[(2, 3), (4, 2)]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325505165&siteId=291194637