跳到主要内容

MS 关键词速查

数组/2D 数组

MS 原文代码要求
Declaration of (global) array with identifierglobal arr / arr = []
Initialising elements with null valuearr = [""] * 20
Looping through each array elementfor i in range(len(arr)):
2D array declarationarr = [[0]*cols for _ in range(rows)]

排序

MS 原文代码要求
Procedure header and loopingdef Sort(): + 外层循环
Working inner loop内层循环遍历
Comparison of elementsif arr[j] > arr[j+1]:
Swapping of elementsarr[j], arr[j+1] = arr[j+1], arr[j]
Recursive function with recursive call递归调用自身
Correct base case and returnif n <= 1: return arr
While loop controlwhile LoopAgain:

查找

MS 原文代码要求
Function header taking parameterdef Search(target):
Calculating the mid valuemid = (first + last) // 2
Checking if data at mid is parameterif arr[mid] == target:
Updating first/last with mid +/- 1last = mid - 1 / first = mid + 1
Returning -1 when not foundreturn -1

队列

MS 原文代码要求
Checks if queue is full and returns FALSEif Rear >= size: return False
Inserts data item and increments pointerQueue[Rear] = data; Rear += 1
Assigns Head to 0 when first elementif Head == -1: Head = 0
Returns TRUEreturn True
Check if queue empty and return "false"if Head < 0 or Head > Rear: return "false"

循环队列

MS 原文代码要求
NumberItems counter for full checkif NumberItems == size: return False
Wrap-around using MODTail = (Tail + 1) % size
Head wrap-aroundHead = (Head + 1) % size

MS 原文代码要求
Check if stack fullif TopOfStack == size-1: return -1
Store data and incrementStack[TopOfStack] = data; TopOfStack += 1
Check if stack emptyif TopOfStack == -1: return ""
Return top and decrementTopOfStack -= 1; return Stack[TopOfStack]

链表

MS 原文代码要求
Record type declarationclass Node: def __init__(self, data, next):
Array of nodes initialisedlinkedList = [Node(0, 0) for _ in range(10)]
Traversal by following pointerscurrent = startPointer; while current != -1:
Insert at endwhile linkedList[current].nextNode != -1:

二叉树

MS 原文代码要求
Class declaration with private attributesclass Node: + self.__Data
Constructor taking parameterdef __init__(self, data):
Get methods returning attributesdef GetData(self): return self.__Data
Checking if tree emptyif self.__NumberNodes == 0:
Comparing data for left/rightif data < current.GetData():
Updating pointer for parentself.__Tree[previous].SetLeft(index)

哈希表

MS 原文代码要求
Hash function using MODreturn key % 200
Check if slot is emptyif HashTable[h].GetKey() == -1:
Collision to Spare arraySpare[i] = record
Initialise all to emptykey = -1, item1 = -1, item2 = -1

OOP 继承

MS 原文代码要求
Child class inheriting from parentclass Child(Parent):
Constructor calling parentsuper().__init__(params)
Override methoddef Method(self): (same name)

文件处理

MS 原文代码要求
Appropriate use of exception handlingtry: ... except: ...
Opening text file to readopen("file.txt", 'r')
Reading each linef.read().splitlines()
Splitting each lineline.split(",")
Casting to integersint(value)

递归

MS 原文代码要求
Base case returnif n <= 1: return value
Recursive call with smaller inputreturn n * RecursiveFunc(n-1)
Iterative to recursive conversion循环体改为递归调用

字符串处理

MS 原文代码要求
MID function equivalents[start:start+len]
LENGTH functionlen(s)
Character comparisonif s[i] < s[j]: