Contents
- ./admissions3.py
- ./admissions.py
- ./search_recursive.py
./admissions3.py 1/3
[top][prev][next]
"""
Saint Bonaventure University Programming Contest
Problem #1 - Getting Into College
SOLUTION
"""
def main():
criteria = input()
criteriaList = criteria.split()
ap = int(criteriaList[0])
intangibles = int(criteriaList[1])
gpa = int(criteriaList[2])
sat = int(criteriaList[3])
score = ap*2 + intangibles*3 + gpa*0.25 + sat*0.02
if score >= 70:
print("ADMIT")
else:
print("DENY")
main()
./admissions.py 2/3
[top][prev][next]
"""
Saint Bonaventure University Programming Contest
Problem #1 - Getting Into College
SOLUTION
"""
def main():
criteria = raw_input()
criteriaList = criteria.split()
ap = int(criteriaList[0])
intangibles = int(criteriaList[1])
gpa = int(criteriaList[2])
sat = int(criteriaList[3])
score = ap*2 + intangibles*3 + gpa*0.25 + sat*0.02
if score >= 70:
print "ADMIT"
else:
print "DENY"
main()
./search_recursive.py 3/3
[top][prev][next]
# Recursive Binary Search Implementation
# By Sara Sprenkle
# represents that binarySearch did not find the key
NOT_FOUND=-1
def main():
integers = range(1,20,2)
print("The list to search: ", integers)
print()
findMeList = [1, 4, 15, 16, 17]
for key in findMeList:
print("Search for", key)
# binarySearch returns the position the number was found, or -1 if it was
# not found. Translate the result from binarySearch to a True or False
pos = recBinarySearch(integers, key)
binFound = pos != NOT_FOUND
print("Binary: Found?", binFound)
print()
print("Repeast search, using a different recursive implementation")
for key in findMeList:
print("Search for", key)
# binarySearch returns the position the number was found, or -1 if it was
# not found. Translate the result from binarySearch to a True or False
pos = recBinarySearchWithIndices(integers, key, 0, len(integers))
binFound = pos != NOT_FOUND
print("Binary: Found?", binFound)
print()
def recBinarySearch(searchlist, key):
"""Literally divide the list in half. Issues: creates multiple lists.
Each call to the function requires another list (of ~half the size of
the original)."""
# Base case: ran out of elements in the list
if len(searchlist) == 0:
return NOT_FOUND
mid = len(searchlist)//2
valueAtMid = searchlist[mid]
if valueAtMid == key:
return mid
if searchlist[mid] < key: # search upper half
return recBinarySearch(searchlist[mid+1:], key)
else: # search lower half
return recBinarySearch(searchlist[:mid], key)
def recBinarySearchWithIndices(searchlist, key, low, high):
"""Recursive search using the indices to
cut the search space in half each time.
"""
# Base case: ran out of elements in the list
if low > high:
return NOT_FOUND
mid = (low+high)//2
valueAtMid = searchlist[mid]
if valueAtMid == key:
return mid
if searchlist[mid] < key: # search upper half
return recBinarySearchWithIndices(searchlist, key, mid+1, high)
else: # search lower half
return recBinarySearchWithIndices(searchlist, key, low, mid-1)
main()
Generated by GNU Enscript 1.6.5.90.