Skip to content

Commit

Permalink
Example solutions from OPEI2019
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopapais committed Jan 5, 2021
1 parent 8530be4 commit 112afc2
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 0 deletions.
10 changes: 10 additions & 0 deletions opei/2019/cifra-de-cesar/cifra-de-cesar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
shift = int(input())
message = str(input())
result = []

for index, char in enumerate(message):
result.append(chr(ord(message[index]) - shift))

resultParsed = ''.join(char for char in result)
print(resultParsed)

61 changes: 61 additions & 0 deletions opei/2019/colheita-de-milho/colheita-de-milho.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# matriz: N x M (N = altura, M = largura; N = y, M = x)
height, width = [int(number) for number in input().split()]
matrix = []
mainSum = 0

# Handle input
for i in range(height):
matrix.append(input().split())
for number in matrix[i]:
number = int(number)

# Auxiliary function
def getRightLineSum(y, x):
sum = 0
for i in range(x, width):
sum += int(matrix[y][i])
return sum

# Auxiliary function
def getDownColumnSum(y, x):
sum = 0
for i in range(y, height):
sum += int(matrix[i][x])
return sum

# Main recursive function to find turns potential to increase mainSum
def findPotential(y, x):
global mainSum

turnRightPotential = 0
turnDownPotential = 0

turnRightCellsCount = 0
turnDownCellsCount = 0

if (y < height) and (x < width):

mainSum += int(matrix[y][x])
# print(matrix[y][x])

# get turn right potential (x + 1)
for i in range(y, height):
turnRightPotential += getRightLineSum(i, x + 1)
turnRightCellsCount += (width - x)

# get turn down potential (y + 1)
for i in range(y + 1, height):
turnDownPotential += getRightLineSum(i, x)
turnDownCellsCount += (width - x - 1)

turnDownAvrgPotential = (turnDownPotential / turnDownCellsCount) if (turnDownCellsCount > 0) else (turnDownPotential)
turnRightAvrgPotential = (turnRightPotential / turnRightCellsCount) if (turnRightCellsCount > 0) else (turnRightPotential)

if turnRightAvrgPotential >= turnDownAvrgPotential:
findPotential(y, x + 1)
else:
findPotential(y + 1, x)

findPotential(0, 0)

print(mainSum)
30 changes: 30 additions & 0 deletions opei/2019/hotel-maluco/hotel-maluco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
roomCount = int(input())
peopleName = []
peopleFound = []

# handle input
for i in range(roomCount):
peopleName.append(str(input()))

peopleName = sorted(peopleName)

currentPerson = ''

def findMiddleAndCompare(newList):
middleIndex = int(len(newList) - 1) // 2

currentPerson = peopleName[middleIndex]
if (currentPerson != 'Henrique'):
if ord('H') > ord(list(currentPerson)[0]):
findMiddleAndCompare(peopleName[middleIndex:(len(newList) - 1)])
else:
findMiddleAndCompare(peopleName[0:middleIndex])
peopleFound.append(currentPerson)


findMiddleAndCompare(peopleName)

# print in reverse
# can use reverse(peopleFound) too
for i in range((len(peopleFound) - 1), -1, -1):
print(peopleFound[i])
13 changes: 13 additions & 0 deletions opei/2019/mesa-pra-quantos/mesa-pra-quantos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
peopleCount = int(input())

sumDesks = 0

while peopleCount > 0:
if sumDesks > 0:
# when you add another desk, you earn 3 new places but lose one (3 -1 = 2)
peopleCount -= 2
else:
peopleCount -= 4
sumDesks += 1

print(sumDesks)
24 changes: 24 additions & 0 deletions opei/2019/paciente-zero/paciente-zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pacientsCount = int(input())

badPacients = []
infectedPacients = []

# handle input
for i in range(pacientsCount):
inputLine = input().split()

currentBadPacientName = inputLine[0]
if currentBadPacientName not in badPacients:
badPacients.append(currentBadPacientName)

infectedCount = int(inputLine[1])
if (infectedCount > 0):
# good for handling not-known inline inputs
currentInfectedPacients = inputLine[2:len(inputLine)]
for pacient in currentInfectedPacients:
if pacient not in infectedPacients:
infectedPacients.append(pacient)

for pacient in badPacients:
if pacient not in infectedPacients:
print(pacient)
26 changes: 26 additions & 0 deletions opei/2019/tudo-palindromo/tudo-palindromo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
listString = list(input().replace(" ", ""))
# made convertion with list() typecasting
# alternatively we could do:
# listString = char for char in word
# also removed all whitespace with replace() method

ocurrences = {}
# add ocurrences in dict
for char in listString:
if char not in ocurrences:
ocurrences[char] = 1
else:
ocurrences[char] += 1


removes = 0
# to iterate an object (dict)
for i in ocurrences.keys():
if (ocurrences[i] % 2) != 0:
removes += 1

# Check if it's possible to have just only one char in the middle of string
if (len(listString) - removes + 1) % 2 != 0:
removes -= 1

print(removes)
11 changes: 11 additions & 0 deletions opei/2019/usuarios-unicos/usuarios-unicos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
idCount = int(input())
idList = []

# handle input
for i in range(idCount):
currentId = int(input())

if currentId not in idList:
idList.append(currentId)

print(len(idList))
30 changes: 30 additions & 0 deletions opei/2019/validacao-de-cartao/validacao-de-cartao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
numero_do_cartão = [int(charNumber) for charNumber in list(input())]

def getNumberDigitsSum(number):
sum = 0
while number != 0:
sum = sum + (number % 10)
number = number // 10
return sum

alternate = False
i = len(numero_do_cartão) - 1

while i >= 0:
if alternate == True:
new_number = numero_do_cartão[i] * 2
if new_number >= 10:
new_number = getNumberDigitsSum(new_number)
numero_do_cartão[i] = new_number
alternate = not alternate
i -= 1

sum = 0
for number in numero_do_cartão:
sum += number

if (sum % 10) == 0:
print("SIM")
else:
print("NAO")

0 comments on commit 112afc2

Please sign in to comment.