Third week of Python study notes (2)

Source: Internet
Author: User

Select Sort:
    • Time complexity O (n**2)
    • There is no way to know whether the current wheel has reached the sorting requirements, but it is possible to know whether the extremum is at the target index position
    • Traversal count 1,..., n-1 N (n-1)/2
    • Contrast Bubble method: Reduce the number of exchanges, improve efficiency, slightly better performance
    • Method three or four actually reduces the average time complexity
Method One:
nums = [1, 2, 6, 7, 8, 9, 3, 4, 5]for i in range(len(nums)):    maxindex = i    for j in range(i + 1, len(nums)):        if nums[j] < nums[maxindex]:            maxindex = j    if i != maxindex:        nums[i], nums[maxindex] = nums[maxindex], nums[i]print(nums)       
Method two (based on optimization of method one):
    • Optimization point: Internal loop one-time traversal simultaneously fetch maximum, minimum value at both ends
      nums = [7, 19, 15, 12, 15, 5, 13, 14, 8, 15]  length = len(nums)for i in range(length // 2):maxindex = iminindex = length - i - 1minorigin = length - i - 1for j in range(i + 1, length - i):    if nums[j] > nums[maxindex]:        maxindex = j    if nums[j-1] < nums[minindex]:#正序比较 或者逆序比较都可以 length - j - 1        minindex = j - 1if i != maxindex:    nums[i], nums[maxindex] = nums[maxindex], nums[i]if i == minindex: #如果i就是最小值索引,则执行此if,因为上一行索引i的值与最大值已经进行了交换    minindex = maxindexif minorigin != minindex:    
Method Three (based on the optimization of Method two):
    • Optimization point: If the maximum value of one iteration is equal to the minimum value, the remaining element values are the same, that is, sorted
      nums = [7, 19, 15, 12, 15, 5, 13, 14, 8, 15] length = len(nums)for i in range(length // 2):maxindex = iminindex = length - i - 1minorigin = length - i - 1for j in range(i + 1, length - 1):    if nums[j] > nums[maxindex]:        maxindex = j    if nums[length - j - 1] < nums[minindex]:        minindex = length - j - 1if minindex == maxindex: #!    breakif i != maxindex:    nums[i], nums[maxindex] = nums[maxindex], nums[i]if i == minindex:    minindex = maxindexif minorigin != minindex:    nums[minorigin], nums[minindex] = nums[minindex], nums[minorigin]print(nums)
Method Four (based on Method three optimization):
    • If the list is the case below, the minimum value does not need to be exchanged
      nums =[1, 1, 1, 1, 1, 1, 1, 1, 2]length = len(nums)for i in range(length // 2):maxindex = iminindex = length - i - 1minorigin = length - i - 1for j in range(i + 1, length - 1):    if nums[j] > nums[maxindex]:        maxindex = j    if nums[length - j - 1] < nums[minindex]:        minindex = length - j - 1if minindex == maxindex: #!    breakif i != maxindex:    nums[i], nums[maxindex] = nums[maxindex], nums[i]if i == minindex:    minindex = maxindexif minorigin != minindex and nums[minorigin] != nums[minindex]: #!    nums[minorigin], nums[minindex] = nums[minindex], nums[minorigin]print(nums)
Problem solving 1. User enters a number
    • print each digit and its number of repetitions method one:
      num = input(‘>>>‘)d = {}for c in num:if not d.get(c):    d[c] = 1    continued[c] += 1print(d)
Method Two:
d ={}for c in num:    if c not in d.keys():        d[c] = 1    else:        d[c] += 1print(d)
2. Digital Repeat Statistics
    • Randomly generates 100 integers
    • range of numbers [-1000, 1000]
    • Output all the different numbers in ascending order and the number of repetitions
      import randomn = 100nums = [0] * nfor i in range(n):nums[i] = random.randint(-1000, 1000)print(nums)    t = nums.copy()t.sort()print(t)d = {}for x in nums:if x not in d.keys():    d[x] = 1else:    d[x] += 1print(d)d1 = sorted(d.items())print(d1)
3. String Repetition statistics
    • Character ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
    • Randomly select 2 letters to form a string and select 100
    • Output all different strings in descending order and the number of repetitions
      import randomalphabet = ‘abcdefghijklmnopqrstuvwxyz‘words = []for _  in range(100):words.append(‘‘.join(random.choice(alphabet) for _ in range(2)))d = {}for x in words:d[x] = d.get(x, 0) + 1print(d)d1 = sorted(d.items(), reverse = True)print(d1)
4. Returns a list of 1-10 squares
[i ** 2 for i in range(1,11)]
5. There is a list of LST = [1,4,9,16,2,5,10,15], which generates a new list that requires the new list element to be a lst adjacent to 2 items and
lst = [1,4,9,16,2,5,10,15][lst[i] + lst[i + 1] for i in range(len(lst) - 1)]
6. Print 99 multiplication table
[print(‘{}*{}={:<{}}{}‘.format(j, i, j * i,2 if j ==1 else 3, ‘‘ if i != j else ‘\n‘),end = ‘‘)for i in range(1,10) for j in range(1,i + 1)]
7. "0001.abadicddws" is the ID format, requires ID format is divided by Dot, the left is 4 bits from 1, the right is a 10-bit random lowercase English letter. Please generate a list of the first 100 IDs in turn
import random[(‘{:>04}‘.format(i) + ‘.‘ + ‘‘.join(‘abcdefghijklmnopqrstuvwxyz‘[random.randint(0,25)] for _ in range(10))) for i in range(1,101)]

Third week of Python study notes (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.