When two arrays are given, the first element of each is compared, the winner is given 1 point, and the score of each is output.
Alice vs Bob. One point will be given to the winner. 0 points for a draw.
▼sample input
17 28 30
99 16 8
▼sampel output
2 1
▼my answer
python
def compareTriplets(a, b):
#Set variables for counting the number of wins and losses. Initial value 0
alice=0
bob=0
#Compare the elements of a given array one by one
for pair in zip(a,b):
if pair[0]>pair[1]:
alice+=1
elif pair[0]<pair[1]:
bob+=1
#Returns a return value
return (alice,bob)
#input processing
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
a = list(map(int, input().rstrip().split()))
b = list(map(int, input().rstrip().split()))
result = compareTriplets(a, b)
fptr.write(' '.join(map(str, result)))
fptr.write('\n')
fptr.close()
Use zip (array 1, array 2)
The type is zip.
The first element of array 1 and the first element of array 2 are set in a nested structure, and a new element is used as the first element.
python
a=[1,2,3]
b=[4,5,6]
print(list(zip(a, b)))
#output
[(1, 4), (2, 5), (3, 6)]
python
a=[1,2,3]
b=[4,5,6]
for pair in zip(a,b):
print(pair)
#output
(1, 4)
(2, 5)
(3, 6)
rstrip() Remove trailing whitespace
a=" a bcd efg "
a.rstrip()
#output
' a bcd efg'
.strip ()
Remove leading and trailing spaces
.lstrip ()
Remove leading whitespace
.rstrip ()
Remove trailing whitespace
python
letter=" a bcd efg "
s = letter.strip()
l = letter.lstrip()
r = letter.rstrip()
print(s)
print(l)
print(r)
#output
a bcd efg ← Up to here
a bcd efg ← Up to here
a bcd efg ← Up to here
.strip ("character string ")
Start and end
.lstrip ("character string ")
beginning
.rstrip ("character string ")
end
python
letter="a bad eaa"
s = letter.strip("a")
l = letter.lstrip("a")
r = letter.rstrip("a")
print(s)
print(l)
print(r)
#output
bad e
bad eaa
a bad e
①return If the call has fptr = open (os.environ ['OUTPUT_PATH'],'w'). The method is assigned to a variable. result = compareTriplets (a, b)
②print If you are executing a method. If the final output is compareTriplets (a, b), print () is used in the method.
Recommended Posts