Day 27 Learning C & Python— Practice Problems

jay.dez
4 min readDec 23, 2022

Problem 1. Kitchen Timings

The working hours of Chef’s kitchen are from X pm to Y pm (1≤X<Y≤12).

Find the number of hours Chef works.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two space-separated integers X and Y — the starting and ending time of working hours respectively.

Output Format

For each test case, output on a new line, the number of hours Chef works.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ X < Y ≤ 12

In C, I originally used this like nested while-loop but it would just print out a bunch of one’s. Changed the second while-loop to an if-statement and now I get the correct answer. Also, most of these problems I don’t incorporate the “constraints” that are list on the original problems and just write the code. Except this one I incorporated the constraints into the code.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x, y;
scanf("%d %d", &x, &y);
if (1 <= x && x < y && y <= 12)
{
printf("%d\n", y - x);
}
}
return 0;
}
for t in range(int(input())):
x, y = map(int, input().split())
if (1 <= x and x < y and y <= 12)
print(y - x)

Problem

In Chefland, precipitation is measured using a rain gauge in millimeter per hour.

Chef categorizes rainfall as:

  • LIGHT, if rainfall is less than 3 millimetre per hour.
  • MODERATE, if rainfall is greater than equal to 3 millimeter per hour and less than 7 millimeter per hour.
  • HEAVY if rainfall is greater than equal to 7 millimeter per hour.

Given that it rains at X millimeter per hour on a day, find whether the rain is LIGHT, MODERATE, or HEAVY.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of a single integer X — the rate of rainfall in millimeter per hour.

Output Format

For each test case, output on a new line, whether the rain is LIGHT, MODERATE, or HEAVY.

You may print each character in lowercase or uppercase. For example, LIGHT, light, Light, and liGHT, are all identical.

Constraints

  • 1 ≤ T ≤ 20
  • 1 ≤ X ≤ 20
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x;
scanf("%d", &x);
if(x < 3)
printf("Light\n");
else if(3 <= x && x < 7)
printf("Moderate\n");
else
printf("Heavy\n");
}
return 0;
}
for t in range(int(input())):
x = int(input())
if(x < 3):
print("Light")
elif(3 <= x and x < 7):
print("Moderate")
else:
print("Heavy")

Problem 3. Spice Level

Each item in Chef’s menu is assigned a spice level from 11 to 1010. Based on the spice level, the item is categorized as:

  • MILD: If the spice level is less than 4.
  • MEDIUM: If the spice level is greater than equal to 4 but less than 7.
  • HOT: If the spice level is greater than equal to 7.

Given that the spice level of an item is X, find the category it lies in.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of an integer X — the spice level of the item.

Output Format

For each test case, output on a new line, the category that the item lies in.

You may print each character in uppercase or lowercase. For example, HOT, hot, Hot, and hOT are all considered the same.

Constraints

  • 1 ≤ T ≤ 1000
  • 1 ≤ X ≤ 10
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x;
scanf("%d", &x);
if(x < 4)
printf("Mild\n");
else if(4 <= x && x < 7)
printf("Medium\n");
else
printf("Hot\n");
}
return 0;
}
for t in range(int(input())):
x = int(input())
if(x < 4):
print("Mild")
elif(4 <= x and x < 7):
print("Medium")
else:
print("Hot")

Problem 4. Scalene Triangle

Given A,B, and C as the sides of a triangle, find whether the triangle is scalene.

Note:

  • A triangle is said to be scalene if all three sides of the triangle are distinct.
  • It is guaranteed that the sides represent a valid triangle.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of three space-separated integers A, B, and C — the length of the three sides of the triangle.1

Output Format

For each test case, output on a new line, YES, if the triangle is scalene, and NO otherwise.

You may print each character of the string in uppercase or lowercase. For example, YES, yes, Yes, and yEs are all considered identical.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ A B C ≤ 10
  • C < (A + B)
#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 && b != c && c < a + b)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
for t in range(int(input())):
a, b, c = map(int, input().split())
if(a != b and b != c and c < a + b):
print("Yes")
else:
print("No")
# another way to write this

for t in range(int(input())):
a, b, c = map(int,input().split())
if a == b or b == c or a == c: print("NO")
else: print("YES")

--

--

jay.dez

no offense shortstack but you give me the creeps