자료구조에서 가장 큰 값을 찾는다.
class MaxAlgorithm:
def __init__(self, ns):
self.nums = ns
self.maxNum = 0
def getMaxNum(self):
self.maxNum = self.nums[0] # 0부터 시작하기 위해 0으로 초기화시킨다.
for n in self.nums:
if self.maxNum < n:
self.maxNum = n
return self.maxNum;
ma = MaxAlgorithm([-2, -4, 5, 7 ,10, 0, 8, 20, 110])
maxNum = ma.getMaxNum()
print(f'maxNum: {maxNum}')
class MaxAlgorithm:
def __init__(self,cs ):
self.chars = cs
self.maxChar = 0
def getMaxChar(self):
self.maxChar = self.chars[0]
for c in self.chars:
if ord(self.maxChar) < ord(c):
self.maxChar = c
return self.maxChar
chars = ['c', 'x', 'Q', 'A', 'e', 'P', 'p']
mc = MaxAlgorithm(chars)
maxChar = mc.getMaxChar()
print(f'maxChar: {maxChar}')
자료구조에서 가장 작은 값을 찾는다.
class MinAlgorithm:
def __init__(self,ns):
self.nums = ns
self.minNum = 0
def getMinNum(self):
self.minNum = self.nums[0]
for n in self.nums:
if self.minNum > n:
self.minNum = n
return self.minNum
ma = MinAlgorithm([-2, -4, 5, 7, 10, 0, 8, 20, -11])
minNum = ma.getMinNum()
print(f'minNum: {minNum}')
class MinAlgorithm:
def __init__(self,cs): # 객체를 초기화 해준다.
self.chars = cs
self.minNum = 0
def getMinNum(self):
self.minNum = self.chars[0]
for c in self.chars:
if ord(self.minNum) > ord(c):
self.minNum = c
return self.minNum
ma = MinAlgorithm(['c', 'x', 'Q', 'A', 'e', 'P', 'p'])
minNum = ma.getMinNum()
print(f'minNum: {minNum}')