Location>code7788 >text

Must-practice questions for basic python! !

Popularity:539 ℃/2025-04-25 20:52:19

Narcissus

Narcissus
 info = 3
 While info:
     # User input number
     try:
         print(f"Please enter the number, you have {info} chances!!")
         num = int(input("Please enter the number 100-999:"))
         if num<100 or num>999:
             info -= 1
             print(f"Please enter a legal number!!! You have {info} chances!!!")
             Continue continue
         b = num //100 # Take a hundred
         s = num //10%10 # Take ten
         g = num % 10 # Take single bits
         if b**3+s**3+g**3 == num:
             print(f"{num}: This number is the daffodil number")
             break
         else:
             print(f"{num}: This number is not a daffodil")
             info -= 1
     except:
         print(f"Please enter the number!!! You have {info} chances!!!")
         info -= 1

Insert sort

Insert sort
 # Define list
 nums = [87,38,77,2,33,98,73,22,12]
 for i in range(len(nums)):
     # Determine whether <right element> is smaller than <left element>
     # If it is less than, then the two elements are exchanged and the position becomes "small on the left and big on the right"
     while nums[i] < nums[i-1] and i>=1:
         nums[i],nums[i-1] = nums[i-1],nums[i]
         i -= 1

Bubble sort

Bubble sort
 # Define list
 nums = [87,99,17,2,33,68,73,22,12]
 # The number of traversals is the list length minus one
 # It's equivalent to how many rounds of bubbles
 for i in range(len(nums)-1):
     # The number of bubbles per round,
     # -i is because: the number of large numbers already ranked behind in the last round, so the following ones will no longer participate
     for j in range(len(nums)-1-i):
         if nums[j] > nums[j+1] :
             # Compare the elements of the left ‘nums[j]’ and the right ‘nums[j+1]’ Transposition
             nums[j],nums[j+1] = nums[j+1],nums[j]
 print(nums)

Hundred chickens and hundreds of dollars

Hundred chickens and hundreds of dollars
 Title: Male 5, female 3, small 1/3
 Rooster x Hen y Chicken z Total 100
 5x+3y+z/3=100
 ---------------------------------------
 count = 0
 for x in range(1,20):
     # Because the number of roosters will not exceed 20
     for y in range(1,33):
     # The number of hens should not exceed 33
         # Number of chicks
         z = 100 - x - y
         if z != 0 and 5*x+3*y+z/3 == 100:
             count += 1 # Add one for each method record
             print(f"{count} method: {x} rooster, {y} hen, {z} chick")

Minimum common multiple

Minimum common multiple

 num1 = int(input("Please enter the number 1:"))
 num2 = int(input("Please enter the number 2:"))
 if num1 < num2 :
     num1,num2 = num2,num1
 else:
     num1,num2 = num1,num2
 for i in range(num1,num1*num2+1):
     if i%num1==0 and i%num2==0:
         print(f"{i} is the least common multiple of {num1} and {num2}")

Binary search

The primary condition: The sequence being queried must be ordered.

target: target

target = n
 left = 0 # lower left/index
 right = len(lis)-1 # lower right mark/index
 while left <= right:
     # Find the middle index of the list
     mid = (left+right)//2
     if lis[mid] > target:
         # If the intermediate value is greater than the target value, the target value is on the left side of the intermediate value.
         # So you need to modify the lower right mark.
         right = mid-1
     elif lis[mid] < target:
         # If the intermediate value is less than the target value, the target value is to the right of the intermediate value.
         # So you need to modify the lower left mark.
         left = mid + 1
     else:
         print(f"The subscript of this number is {mid}")
         break
 else:
	 print("This target is not in the sequence...")

Guess the age - how old is my sister

Xiao Ming took his two sisters to the * Festival. When someone asked them how old they were, they said mischievously: "The age of the two of us is six times the sum of our age." Xiao Ming added: "They are not twins, and the age difference is no more than 8 years old." Please write out the age of the younger sister?

Implementation ideas:
	 1. The two sisters are not twins, so they are definitely different in age.
	 2. Age amount is six times that of age sum
for i in range(1,100): # Assume your sister's age
     for j in range(1,i): # Assume the age of the sister
         if i*j==6*(i+j) and i-j<8: # Sisters' age conditions
             print(f"Sister {i} is old, sister {j} is old")

Ask for a good string?

Question requirements:
     Substrings with three lengths and different characters
     If a string does not contain any duplicate characters, we call this string a good string.
     Give you - a string s, please return the number of good substrings of length 3 in s.
     Note that if the same good substring appears multiple times, each time should be recorded in the answer.
     A substring is a continuous sequence of characters in a string.
str_i = input("Please enter more than 3 characters:")
 counts = 0
 for i in range(len(str_i)-2): # In case the index below is out of range
     if str_i[i] != str_i[i+1] and str_i[i] != str_i[i+2] and str_i[i+1] != str_i[i+2]:
         counts += 1
         print(f"Good characters are: {str_i[i]}{str_i[i+1]}{str_i[i+2]}")
 print(f"There are {counts} good characters in total")