Sitemap

Day 16 Learning C & Python

5 min readDec 9, 2022

--

Press enter or click to view image in full size
Some cool code screenshot I found on Google

Problem 1. Second Largest

I had a feeling there was another way to do this in Python. The idea is to print out the second largest number of the three numbers. My code is the last one, which is just translated from C to Python, however, there are other ways:

"""map the splitted input into a list called a. Sort function to sort
the values in numerical order. Print indiex 1, which is the middle num.
This one seems like the more practical and short method rather than
eliminating numbers liek the next code below."""

n=int(input())
for i in range(n):
a=list(map(int,input().split()))
a.sort()
print(a[1])
"""So first there this and open array assigned to a variable lis. 
Then, append adds items (variable values) to the end of the list
They use remove to eliminate the max value.
Finally, print the max value of the remaining two values."""

for i in range(int(input())):
a,b,c=map(int,input().split())
lis=[]
lis.append(a)
lis.append(b)
lis.append(c)
lis.remove(max(lis))
print(max(lis))

# my python code

for t in range(int(input())):
a, b, c = map(int, input().split())
if(a > b and b > c or c > b and b > a):
print(b)
elif(b > a and a > c or c > a and a > b):
print(a)
elif(a > c and c > b or b > c and c > a):
print(c)
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t-->0)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);

if(a > b && b > c || c > b && b > a)
printf("%d\n", b);
else if(b > a && a > c || c > a && a > b)
printf("%d\n", a);
else if(a > c && c > b || b > c && c > a)
printf("%d\n", c);
}
return 0;
}

Problem 2. Pass or Fail

for t in range(int(input())):
n, x, p = map(int, input().split())
if((x * 3) - (n - x) >= p):
print("PASS")
else:
print("FAIL")
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t-->0)
{
int n, x, p;
scanf("%d%d%d", &n, &x, &p);
if((x * 3) - (n - x) >= p)
printf("PASS\n");
else
printf("FAIL\n");
}
return 0;
}

Problem 3. Cyclic Quadrilateral

for t in range(int(input())):
a, b, c, d = map(int, input().split())
if(a + c == 180 and b + d == 180):
print("YES")
else:
print("NO")
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t-->0)
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if(a + c == 180 && b + d == 180)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

Problem 4. Too Many Items

for t in range(int(input())):
n = int(input())
if(n % 10 != 0):
print(int(n / 10) + 1)
else:
print(int(n / 10))
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t-->0)
{
int n;
scanf("%d", &n);
if(n % 10 != 0)
printf("%d\n", (n / 10) + 1);
else
printf("%d\n", n / 10);
}
return 0;
}

Problem 5. Count the AC’s

for t in range(int(input())):
p = int(input())
if p >= 0 and p <= 0:
print(p)
elif (p // 100) + (p % 100) > 10:
print(-1)
else:
print((p // 100) + (p % 100))
#include <stdio.h>

int main(void)
{
int t, p;
scanf("%d", &t);
while(t-->0)
{
scanf("%d", &p);
if(p % 100 == 0)
printf("%d\n", p / 100);
else if(p % 100 <= 10 && (p % 100) + (p / 100) <= 10)
printf("%d\n", (p % 100) + (p / 100));
else
printf("-1\n");
}
return 0;
}

Problem 6. Building Race

Get jay.dez’s stories in your inbox

Join Medium for free to get updates from this writer.

Note* map must have two arguments — map( , )

for t in range(int(input())):
a, b, x, y = map(float, input().split())
if((a / x) < (b / y)):
print("Chef")
elif((a / x) > (b / y)):
print("Chefina")
else:
print("Both")
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
float a, b, x, y;
scanf("%f %f %f %f", &a, &b, &x, &y);
if((a / x) < (b / y))
printf("Chef\n");
else if((a / x) > (b / y))
printf("Chefina\n");
else
printf("Both\n");
}
return 0;
}

Problem 7. Chef and Races

for t in range(int(input())):
x, y, a, b = map(int, input().split())
count = 0
if(x != a and x != b):
count+=1
if(y != a and y != b):
count+=1
print(count)
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t-->0)
{
int x, y, a, b;
int count = 0;
scanf("%d%d%d%d", &x, &y, &a, &b);

if(x != a && x != b)
count++;
if(y != a && y != b)
count++;

printf("%d\n", count);
}
return 0;
}

Problem 8. Black Cells in a Chessboard
Iiiinnterresstiinngg! If I do just (n * n) / 2 without the int. I get 32.0 which would be wrong because the anser is just 32. So I add some extra parenthesis and convert to an int to get 32. However, if I use // that will perform the division but round down so I just get a flat 32 instead of 32.0. Cool.

n = int(input())
if n%2 == 0:
print(int((n * n) / 2))
n = int(input())
if n%2 == 0:
print((n * n) // 2)
#include <stdio.h>

int main(void)
{
int n;
scanf("%d", &n);
if(n%2 == 0)
printf("%d\n", (n * n) / 2);
return 0;
}

Problem 9. Sum or Difference

The problem specifically asks for two integers on seperate lines. If you put N1 and N2 on the same line (in Python) and use the map(int, input().split()) function… it gives you a nasty runtime error.

N1 = int(input())
N2 = int(input())
if(N1 > N2):
print(abs(N1 - N2))
else:
print(N1 + N2)
#include <stdio.h>

int main(void)
{
int N1, N2;
scanf("%d\n", &N1);
scanf("%d\n", &N2);
if(N1 > N2)
printf("%d\n", abs(N1 - N2));
else
printf("%d\n", N1 + N2);
return 0;
}

Problem 10. Valid Triangles

for t in range(int(input())):
a, b, c = map(int, input().split())
if(a + b + c == 180):
print("YES")
else:
print("NO")
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a + b + c == 180)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

--

--

jay.dez
jay.dez

Written by jay.dez

no offense shortstack but you give me the creeps