First: Five basic algorithmic questions
1. Armstrong Number
If an n-bit positive integer equals the sum of the n-th power of its digits, it is called an Armstrong number. Determine whether the number entered by the user is Armstrong.
(1) Topic analysis: Here we need to first get the number of digits, and then cut out the number of each digit, the sum of the n-th power of each digit and the number together can be judged.
(2) algorithm analysis: in python, len() function can get the length of a string, so it is necessary to convert a positive integer into a positive integer string. Then intercept from high to low (or vice versa). Or efficient algorithms use for loop slicing.
From high to low: The quotient obtained by dividing positive integers by the n-th power of 10 is the high number, and the remainder is the number of the next cycle.
From low to high: Divide the positive integer by 10, and the remainder is the low number, and the quotient is the number of the next cycle.
For loop: Use for loop to get each digit in turn. Iterable objects are displayed sequentially.
(3) Used python grammar: while loop, for loop, if statement, function.
(4) Blogger Answer Code:
From high to low:
def judge(num): mysum = 0 n = len(str(num)) - 1 m = n + 1 firstNum = num while num > 0: quotient = num // (10**n) remainder = num % (10**n) mysum += quotient ** m num = remainder n -= 1 if mysum == firstNum: print('This number is Armstrong number.') else: print('This number is not Armstrong number.') num = int(input('Please enter an integer:')) judge(num)
From low to high:
def judge(num): mysum = 0 n = len(str(num)) - 1 m = n + 1 firstNum = num while num > 0: quotient = num // 10 remainder = num % 10 mysum += remainder ** m num = quotient n -= 1 if mysum == firstNum: print('This number is Armstrong number.') else: print('This number is not Armstrong number.') num = int(input('Please enter an integer:')) judge(num)
(5) Efficient methods:
for cycle:
def judge(num): n = len(num) sum = 0 for i in num: sum += int(i) ** n if sum == int(num): print('This number is Armstrong number.') else: print('This number is not Armstrong number.') num = input('Please enter an integer:') judge(num)
2. Integer arrays
Given an array of integers, determine whether duplicate elements exist.
(1) Topic analysis: Using list's built-i n function count to calculate the number of each element will take a lot of time. The built-in function list.count(i) has a time complexity of O(n) with a layer of loops nested outside. The total time is O(n^2), which is not an efficient algorithm.
The adjacent elements can be sorted to determine whether they are equal. Another method is to use the set() feature for judgment.
(2) Algorithmic analysis: According to the above topic analysis, show the more efficient algorithm.
(3) The python grammar used:
(4) Blogger Answer Code:
def judgeInt(num): this_set = set(num) if len(this_set) == len(num): print('No repetition') else: print('Repeat') my_num = input('Please enter an integer:') judgeInt(my_num)
3. palindrome number
Determine whether an integer is a palindrome number.
(1) Topic analysis: Palindrome numbers refer to integers with the same reading order (from left to right) and reverse order (from right to left).
(2) Algorithmic analysis: python slices can be used to solve this problem easily, but if it is in other languages, there is no slice. Therefore, a general approach needs to be considered.
The algorithm analysis is as follows:
As you can see, we can get the results by analyzing two different situations.
(3) python grammar: if judgment statement, slice, function.
(4) Blogger Answer Code:
def judge(x): this_str = str(x) if len(this_str) % 2 != 0: mid = int((len(this_str) + 1 ) / 2 - 1) left = mid - 1 right = mid if this_str[0:left+1] == this_str[-1:right:-1]: return True else: return False if len(this_str) % 2 == 0: mid = int(len(this_str)/2) - 1 if this_str[0:mid+1] == this_str[-1:mid:-1]: return True else: return False num = input('Please enter an integer:') if judge(num): print('{0}Palindrome number'.format(num)) else: print('{0}Not palindromes'.format(num))
(5) Efficient methods:
def judge(x): return str(x) == str(x)[::-1] num = input('Please enter an integer:') if judge(num): print('{0}Palindrome number'.format(num)) else: print('{0}Not palindromes'.format(num))
It takes only one line of code to judge, which is the benefit of slicing.
Is it very simple?
4. Palindrome number progressive algorithm, unlimited conversion to strings
Is there any way that you don't need to convert to strings first? There are also. We can use the previous Amsterdam numbers to get two lists or dictionaries from high to low and from low to high to compare. We will not analyze them here, but code them directly.
def judge(num1): if '-' in str(num1): return False if num1 >= 0 and num1 < 10 : return True list1 = [];list2 = [] num2 = num1 n1 = len(str(num1)) - 1 n2 = len(str(num2)) - 1 while num1 > 0: quotient1 = num1 // (10**n1) remainder1 = num1 % (10**n1) list1.append(quotient1) num1 = remainder1 n1 -= 1 while num2 > 0: quotient2 = num2 // 10 remainder2 = num2 % 10 list2.append(remainder2) num2 = quotient2 n2 -= 1 num = 0 for i in range(0,len(list1)): if list2[i] == list1[i]: num += 1 if num == len(list1): return True else: return False num = int(input('Please enter an integer:')) if judge(num): print('{0}Palindrome number'.format(num)) else: print('{0}Not palindromes'.format(num))
It's really inefficient.
5. Insertion sort
For unsorted arrays, scan from front to back or from back to front in the sorted sequence to find the corresponding position and insert it.
(1) Topic analysis: This is a simple algorithm, just need to compare each element with the adjacent elements in turn.
(2) Algorithmic analysis: you want to mark the traversal elements with a variable, and then there are two ways.
Previously, the element is compared with the element on the left. If it is smaller than the element on the left, it is interchanged and stops when the number of the element on the left is - 1.
In the past, the element is compared with the element on the right. If it is larger than the element on the right, it is interchanged and stops when the number of the element on the right is reduced by one of the length of the array.
(3) Used python grammar: while loop, function, data exchange.
(4) Blogger Answer Code:
def insert(arr): for i in range(1,len(arr)): j = i while j > 0: if arr[j] < arr[j-1]: arr[j-1],arr[j] = arr[j],arr[j-1] j -= 1 my_arr = list(map(int,input('Please enter an array:').split(','))) insert(my_arr) print(my_arr)
(5) Efficient code
Sorting can be easily done with python's list sorting function sort().
Two: Difficult Algorithmic Question 1
These will be explained in detail in the next blog.
1. Strings of all words in series
Given a string s and some words of the same length. Find out the starting position of the substring in s that happens to be formed by all the words in words in series.
Note that the substrings should match the words in words perfectly. There should be no other characters in the substrings, but there is no need to consider the order in which words are concatenated.
2. solution independence
Write a program to solve the Sudoku problem by filling in the blanks.
The blank space is denoted by'.'.
The more difficult algorithm problems will be explained in detail in the blog.