The tenth day of learning Python --- Xiaolan (1)

1. Character sorting

Sort "WHERETHEREISAWILLTHEREISAWAY"
output as "AAAEEEEEEHHHIIILLRRRSSTTWWWY"

Writing method one:

s="WHERETHEREISAWILLTHEREISAWAY"
s=sorted(s)
print(*s,sep='')

Writing method two:

s = "WHERETHEREISAWILLTHEREISAWAY"
s = ''.join(sorted(s))
print(s)

2. Bill payment problem (greedy)

insert image description here
insert image description here
standard deviation
insert image description here

Writing method 1: Own writing method

n, s = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
avg=s/n
res = 0
for i in range(n):
    if a[i] * (n - i) >= s:#说明她钱够
        res+=(s/(n-i)-avg)**2*(n-i)
        break
    else:
        res+=(a[i]-avg)**2
        s-=a[i]
res=(res/n)**0.5
print("{:.4f}".format(res))

Writing method two:

from math import *
n,s=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
avg=s/n
sum=0
for i in range(n):
    if a[i]*(n-i)<s:
        sum+=pow(a[i]-avg,2)
        s-=a[i]
    else:
        cur_avg=s/(n-i)
        sum+=pow(cur_avg-avg,2)*(n-i)
        break
print("{:.4f}".format(sqrt(sum/n)))

time complexity

insert image description here
If the time complexity does not exceed 10^9, it may pass

3. How many lucky numbers (enumeration - permutation enumeration)

insert image description here

n=59084709587505
cnt=0
for i in range(50):#3^50一定超过了n
    for j in range(50):
        for z in range(50):
            a=3**i
            b=5**j
            c=7**z
            if a*b*c<=n:
                cnt+=1
print(cnt-1)#幸运数字不包括1

Composite enumeration

Let you randomly select m out of n. Cnm (the math formula one)

template:

chosen=[]
n,m=0,0

def calc(x):
    if len(chosen)>m:
        return
    if len(chosen)+n-x+1<m:#剩下的都选了都不够m个
        return
    if x==n+1:
        for i in chosen:
            print(i,end=' ')
        print('')
        return
    calc(x+1)
    chosen.append(x)
    calc(x+1)
if __name__=='_main_':
    tem=input().split()
    n=int(tem[0])
    m=int(tem[1])
    calc(1)

permutation enumeration

When the m of the cnm of the combined enumeration becomes n, it becomes a permutation enumeration

order=[0]*20
chosen=[0]*20
n=0
def calc(x):
    if x==n+1:
        for i in range(1,n+1):
            print(order[i],end=' ')
        print('')
        return
    for i in range(1,n+1):
        if (chosen[i]==1):
            continue
        order[x]=i
        chosen[i]=1
        calc(x+1)
        chosen[i]=0
        order[x]=0
if __name__=='_main':
    n=int(input())
    calc(1)

You can also use the function permutations()

insert image description here

from itertools import *
s=['a','b','c']
for element in permutations(s,2):#这里的2表示想要几个数
    a=element[0]+element[1]
    #或者可以这样子写 a=''.join(element)
    print(a)

4. Permutations

insert image description here
insert image description here
I made a mistake at the beginning, because I thought that the input characters, abcd, were fixed, but the result was not fixed, so I need to master this permutations function.

permutations(iterable, r=None) is a function in the itertools module of the Python standard library that returns all permutations of an iterable object, that is, all possible sequences.

iterable: iterable objects, such as strings, lists, tuples, etc.
r: the number of elements to be selected, the default is None, indicating that all elements are selected
permutations() generates an iterator, and the elements of the iterator are the arrangement of all elements in iterable Combinations, including every permutation. The sorting order is in lexicographical order, for example: the iterator returned by permutations('ABCD') is sorted in the order of 'ABCD', 'ABDC', 'ACBD', …. If r is set, only the selected number of permutations are returned.

from itertools import *
olds=input()
news=list(olds)
news.sort()
cnt=0
for element in permutations(news):
    a=''.join(element)
    if olds==a:
        print(cnt)
        break
    cnt+=1

5. The Martians

insert image description here
Python's permutations() function arranges the output according to the position of the element.
This code implements an algorithm for finding the next permutation in a complete permutation. The basic idea is: find the first adjacent ascending pair from back to front, that is, nums[i] < nums[i+1], and then find the minimum value in nums[i+1:] that is greater than nums[i] number, exchange them, and reverse the number in nums[i+1:] to get the next permutation.

Specifically, for a permutation of length n, the time complexity of finding adjacent ascending pairs for the first time is O(n), while the time complexity of the second exchange and reverse order is O(ni), so the entire algorithm The time complexity is O(n).

In this example, m operations are performed on nums individually, so the total time complexity is O(mn).

n=int(input())
m=int(input())
nums=list(map(int,input().split()))
def find_next(nums):
    for i in range(n-1,0,-1):
        if nums[i]>nums[i-1]:
            for j in range(n-1,i-1,-1):
                if nums[j]>nums[i-1]:
                    nums[j],nums[i-1]=nums[i-1],nums[j]
                    return nums[:i]+nums[:i-1:-1]
for i in range(m):nums=find_next(nums)
print(" ".join([str(i) for i in nums]))

6. Palindrome judgment (double pointer)

insert image description here

Writing method 1: double pointer

There is also the usage of while...else

s=input()
i=0
j=len(s)-1
if i==j:
  print("Y")
else:
  while s[i]==s[j]:
    i+=1
    j-=1
    if j<i:
      print('Y')
      break
  else:
    print("N")

Writing method two: slice

s=input()
ss=s[::-1]
if s==ss:
  print("Y")
else:
  print("N")

7. Beautiful interval (ruler method)

insert image description here
Very direct sliding window, find the interval sum in the window, and satisfy the minimum length greater than S.
The i pointer is in the front, and the j pointer is in the back. Calculate the interval sum between the two pointers. When the i pointer reaches the end, the calculation ends.
Computational complexity is O(n)

n,s=map(int,input().split())
a=list(map(int,input().split()))
i,j,sum,ans=0,0,0,100000
while i<len(a):
  if sum<s:
    sum+=a[i]
    i+=1
  else:
    ans=min(ans,i-j)
    sum-=a[j]
    j+=1
print(ans)

Eight, approximate number

insert image description here

Writing method 1: Violent law

n=1200000
res=0
for i in range(1,n+1):
  if n%i==0:
    res+=1
print(res)

Writing method two: pruning

se = set()
for i in range(1,int(1200000**0.5)+1):
  if 1200000%i==0:
    se.add(i)
    se.add(1200000//i)
print(len(se))

9. Special time (simulation)

insert image description here

day_per_month=[0,31,28,31,30,31,30,31,31,30,31,30,31]
def check_D(D):
  month =D//100
  day=D%100
  if month <1 or month>12:
    return 0
  if day<1 or day>day_per_month[month]:
    return 0
  return 1

def check_H(H):
  h=H//100
  m=H%100
  if h<0 or h>23:
    return 0
  if m<0 or m>59:
    return 0
  return 1
ans=0
for a in range(10):
  for b in range(10):
    if a==b:
      continue
    N_Y,N_D,N_H=4,0,0#年份都满足要求
    A=[a,a,a,a]
    for i in range(4):
      A[i]=b
      number=0
      for j in A:
        number=number*10+j
      N_D+=check_D(number)
      N_H+=check_H(number)
      A[i]=a
    ans+=N_Y*N_D*N_H
print(ans)

Ten, recursion and recursion

recursion

insert image description here
insert image description here

A single query does not need to be stored, but multiple queries must be stored (memory search).

Fibonacci sequence

Inefficient code:

cnt=0
def fib(n):
  global cnt
  cnt+=1
  if n==1 or n==2:
    return 1
  return fib(n-1)+fib(n-2)

print(fib(20))
print(cnt)

insert image description here

memory

data=[0 for i in range(25)]
cnt=0
def fib(n):
  global cnt
  cnt+=1
  if data[n]!=0:#记忆化搜索,如果算了,就不用再算了,直接返回答案
    return data[n]
  if n==1 or n==2:
    data[n]=1
    return data[n]
  data[n]=fib(n-1)+fib(n-2)
  return data[n]

print(fib(20))
print(cnt)

number triangle

insert image description here
insert image description here
Counting from the back to the front, the number of choices will be reduced in this way, so that the optimal one can be derived. If there is no
condition that the difference between the left and right sides does not exceed 1, the code can be written like this! ! ! Note that there is no restriction on the difference in the number of times between the left and right sides.

a=[[0]*101]*101
if __name__=='__main__':
  n=int(input())
  for i in range(1,n+1):
    a[i]=input().split()
    a[i]=list(map(int,a[i]))

  for i in range(n-1,0,-1):
    for j in range(0,i):
      if a[i+1][j]>=a[i+1][j+1]:
        a[i][j]+=a[i+1][j]
      else:
        a[i][j]+=a[i+1][j+1]
print(a[1][0])

In this question, the number of times to go to the left and to the right cannot exceed 1, so it must be in the middle at the end

h = int(input())  # 输入数据
W = [list(map(int, input().split())) for i in range(h)]
# 循环遍历计算到每一行的和的最大值
for i in range(1, h):
    for j in range(0, i + 1):
        if j == 0:  # 最左边元素只能由右上方得到
            W[i][j] += W[i - 1][j]
        elif j == i:  # 最右边元素只能由左上方得到
            W[i][j] += W[i - 1][j - 1]
        else:  # 其余元素由上方较大值得到
            W[i][j] += max(W[i - 1][j - 1: j + 1])
if h & 1:  # 如果是奇数行,则返回最中间值
    print(W[-1][h // 2])
else:  # 偶数行则返回中间较大值
    print(max(W[-1][h // 2 - 1], W[-1][h // 2]))

Guess you like

Origin blog.csdn.net/qq_51408826/article/details/129639505