Code Flashcards
(15 cards)
To find max
Max = var[0]
For i in range (len(array)):
If var[i] > Max:
Max = var[i]
Display Max
Code for finding Min
Min = var[0]
For i in range (len(array)):
If var[i] < Min:
Min = var[i]
Display Min
Linear search code
Values = []
Searchvalue =
Index = 0
Found = false
While found == False and not end of list:
If values[i] == Searchvalue
Found = true
Else:
Add i to index
Count occurrences
Numbers = []
Criteria = 0
For i in range (len(array)):
If number[i] > criteria
Add 1 to criteria
Modulus
Code to find the remainder
Answer = a%b
Code to start a function/ procedure
def getData():
return …
result = getData()
Advantages of modular programming
Less code to be written - sub programs can be used multiple times
Improves readability
Splits programs into manageable chunks
To repeat for length of array
For i in range (len(array)):
Code round
(round(array))
Round up
Round down
(math.ceil(array))
(math.floor(array))
Substrings
Print (variable[0:4])
(Up to fourth integer)
To make a class
Class = className:
Val1 = 0
Val2 = “”
Val3 = 0.0
Code to multiply the class template
List = [className()]*12
How to read from file
File = open(“filename.txt”,”r”)
File.close()
How to write into file
File = open(“fileName.txt”,”w”)
File.write (texttowrite)
File.close()