Day 47 Learning C & Python: Practice Problems

jay.dez
5 min readJan 19, 2023

--

Today in Naples

Problem 1. Passing Marks

Difficulty Rating: 904

Recently, Chef’s College Examination has concluded. He was enrolled in 33 courses and he scored A, B, C in them, respectively. To pass the semester, he must score at least Amin​, Bmin​, Cmin​ marks in the respective subjects along with a cumulative score of at least Tmin​, i.e, A + B + C Tmin​.

Given seven integers Amin​, Bmin​, Cmin​, Tmin​, A, B, C, tell whether Chef passes the semester or not.

###Input:

  • The first line will contain T, number of testcases. Then the testcases follow.
  • Each testcase contains of a single line of input, seven integers Amin​, Bmin​, Cmin​, Tmin​, A, B, C each separated by a space.

###Output:
Output in a single line, the answer, which should be “YES” if Chef passes the semester and “NO” if not.

You may print each character of the string in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).

###Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ Amin​, Bmin​, Cmin​, A, B, C ≤ 100
  • Amin ​+ Bmin ​+ Cmin ​≤ Tmin ​≤ 300

Classic C

if statement — all grades a, b, c must be greater than or equal to their minimums (am, bm, cm) and total sum must be greater than or equal to their tm (tmin).

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int am, bm, cm, tm, a, b, c;
scanf("%d %d %d %d %d %d %d", &am, &bm, &cm, &tm, &a, &b, &c);
if(a + b + c >= tm && a >= am && b >= bm && c >= cm)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

Python

for t in range(int(input())):
am, bm, cm, tm, a, b, c = map(int, input().split())
print("Yes" if(a + b + c >= tm and a >= am and b >= bm and c >= cm) else "No")

Problem 2. Avoid Contact

Difficulty Rating: 907

A hostel has N rooms in a straight line. It has to accommodate X people. Unfortunately, out of these X people, Y of them are infected with chickenpox. Due to safety norms, the following precaution must be taken:

  • No person should occupy a room directly adjacent to a room occupied by a chickenpox-infected person. In particular, two chickenpox-infected people cannot occupy adjacent rooms.

For example, if room 4 has a chickenpox-infected person, then nobody should occupy rooms 3 and 5. Similarly, if room 1 has a chickenpox-infected person then nobody should occupy room 2.

What’s the minimum value of N for which all the people can be accommodated in the hostel, following the above condition?

Input Format

  • The first line of input contains a single integer T — the number of test cases. The description of T test cases follows.
  • The first and only line of each test case contains two integers X and Y — the total number of people and the number of chickenpox-infected people.

Output Format

For each test case, output on a new line a single integer — the minimum value of N for which all the people can be accommodated in the hostel.

Constraints

  • 1 ≤ T ≤ 200
  • 1 ≤ X ≤ 1000
  • 0 ≤ Y X

Classic C

Basically we need to find out how many rooms are needed if we have a certain number of infected and non-infected persons.

According to the problem, the infected(y) people can’t be in a room next to any other person (regardless of whether they are infected or not) which means: infected(y) people need to have a room for themselves and another room between them and whoever else is next to them hence the (y * 2). So, if the total number of people(x) is equal to the number of infected people(y) meaning everyone is infected, the equation y * 2 would produce enough rooms plus one extra empty room, more than we need so we subtract that one extra room.

Last, since the non-infected don’t need that extra room. Adding the total number of people(x) plus the number of infected(y) will end up giving us exactly enough rooms to accommodate everyone.

/* even though this gave me the same answer as in the problem,
overall it is wrong */


#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x, y;
scanf("%d %d", &x, &y);
if(x > y && (x + y)%2 == 0)
printf("%d\n", x + y);
else
printf("%d\n", (x + y) - 1);
}
return 0;
}
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x, y;
scanf("%d %d", &x, &y);
if(x == y)
printf("%d\n", (y * 2) - 1);
else
printf("%d\n", x + y);
}
return 0;
}

Python

for t in range(int(input())):
x, y = map(int, input().split())
if(x == y):
print((y * 2) - 1)
else:
print(x + y)

Problem 3. Mana Points

Difficulty Rating: 327

Chef is playing a mobile game. In the game, Chef’s character Chefario can perform special attacks. However, one special attack costs X mana points to Chefario.

If Chefario currently has Y mana points, determine the maximum number of special attacks he can perform.

Input Format

  • The first line contains a single integer T — the number of test cases. Then the test cases follow.
  • The first and only line of each test case contains two space-separated integers X and Y — the cost of one special attack and the number of mana points Chefario has initially.

Output Format

For each test case, output the maximum number of special attacks Chefario can perform.

Classic C

Simply divide and print how many times coins needed go into coins on hand.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", y/x);
}
return 0;
}

Python

# Use // to round down to avoid decimal output.

for t in range(int(input())):
x, y = map(int, input().split())
print(y//x)

--

--