Day 45 Learning C & Python: Practice Problems

jay.dez
6 min readJan 16, 2023

--

A view of Bergamo, Italy from Citta Alta. Two days ago.

I’ve been in Italy for almost a month now. In nine days I’ll return to my tour guide business in Las Vegas. A month oaway from that wasteland was very much needed, however, I’ve done less studying (computer science) here in Italy than I had hoped but I’ve still managed to do a couple practice problems every day. Here are today’s problems:

Problem 1. Chef & Hair Salon

Difficulty Rating: 895

Chef recently realized that he needs a haircut, and went to his favorite hair salon. At the salon, he found N customers waiting for their haircuts. From his past experience, Chef knows that the salon takes M minutes per customer. Only one person can get their haircut at a time.

For how many minutes will Chef have to wait before he can get his haircut?

Input Format

  • The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
  • The first and only line of each test case contains two space-separated integers N and M, as described in the problem.

Output Format

For each test case, print a single line containing one integer — the number of minutes that Chef will have to wait before he can get his haircut.

Classic C

Simple math, multiply the number of customers by minutes per customer. Can put that into the print function without creating a new variable.

#include <stdio.h>

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

Python

Simple math, multiply the number of customers by minutes per customer. Shortened the code by taking user input for test cases inside the for-loop. Like in C, can put the math into the print function without creating a new variable.

for t in range(int(input())):
n, m = map(int, input().split())
print(n * m)

Problem 2. Chef and Snackdown

Difficulty Rating: 895

Chef is interested in the history of SnackDown contests. He needs a program to verify if SnackDown was hosted in a given year.

SnackDown was hosted by CodeChef in the following years: 2010, 2015, 2016, 2017 and 2019.

Input Format

The first line contain the number of test-cases T. The first line of each test-case contains a single integer N.

Output Format

For each test case print a single line containing the string "HOSTED" if SnackDown was hosted in year N or "NOT HOSTED" otherwise (without quotes).

Classic C

Can also do this as a Switch/ Case function but I feel would be longer code.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d\n", &t);
while(t--)
{
int n;
scanf("%d", &n);
if(n == 2010 || n == 2015 || n == 2016 || n == 2017 || n == 2019)
printf("HOSTED\n");
else
printf("NOT HOSTED\n");
}

return 0;
}


Python

In Python, after our test case for-loop, we create a list named ‘years’ and assign the yearly values to it. Line 3 we use an if-statement to see if the user input is also inside of our years list. If so, print Hosted, else Not Hosted.

for t in range (int(input())):
years = [2010, 2015, 2016, 2017, 2019]
if int(input()) in years:
print("HOSTED")
else:
print("NOT HOSTED")

Problem 3. Overspeeding Fine

Difficulty Rating: 335

Chef was driving on a highway at a speed of X km/hour.

To avoid accidents, there are fine imposed on overspeeding as follows:

  • No fine if the speed of the car ≤70 km/hour.
  • Rs 500 fine if the speed of the car is strictly greater than 70 and ≤100.
  • Rs 2000 fine if the speed of the car is strictly greater than 100.

Determine the fine Chef needs to pay.

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 denoting the speed of Chef’s car.

Output Format

For each test case, output the fine paid by Chef.

Classic C

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x;
scanf("%d", &x);
if(x <= 70)
printf("0\n");
else if(x > 70 && x <= 100)
printf("500\n");
else if(x > 100)
printf("2000\n");
}
return 0;
}

Python

for t in range(int(input())):
x = int(input())
if(x <= 70):
print("0")
elif(x > 70 and x <= 100):
print("500")
elif(x > 100):
print("2000")

Problem 4. Negative Product

Difficulty Rating: 630

Chef is given three numbers A, B, and C.

He wants to find whether he can select exactly two numbers out of these such that the product of the selected numbers is negative.

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 integers A, B, and C the given numbers.

Output Format

For each test case, output YES if Chef can select exactly two numbers out of these such that the product of the selected numbers is negative, NO otherwise.

You may print each character in uppercase or lowercase. For example, the strings NO, no, No, and nO, are all considered identical.

Classic C

A negative number and a positive number multiplied together will produce another negative number (less than zero).

#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 < 0 || b * c < 0 || c * a < 0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

Python

Pretty much the same as C.

for t in range(int(input())):
a, b, c = map(int, input().split())
if(a * b < 0 or b * c < 0 or c * a < 0):
print("Yes")
else:
print("No")

Problem 5. Far From Origin

Difficulty Rating: 750

Alex, Bob, and Chef are standing on the coordinate plane. Chef is standing at the origin coordinates (0,0) while the location of Alex and Bob are (X1, Y1​) and (X2​, Y2​) respectively.

Amongst Alex and Bob, find out who is at a farther distance from Chef or determine if both are at the same distance from Chef.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • The first and only line of each test case contains four space-separated integers X1​, Y1​, X2​, and Y2​ — the coordinates of Alex and Bob.

Output Format

For each test case, output on a new line:

  • ALEX, if Alex is at a farther distance from Chef.
  • BOB, if Bob is at a farther distance from Chef.
  • EQUAL, if both are standing at the same distance from Chef.

You may print each character in uppercase or lowercase. For example, Bob, BOB, bob, and bOB, are all considered identical.

Classic C

First C code is wrong because I’m simply adding together the wrong variables and when I do add the correct ones with absolute value, I get the wrong output. The key is to square the each individual input number individually to make everything positive and then compare. Assigned it all to two new variables, alex and bob for readability.

// codechef says this is wrong even though it outputs the correct answer...
// well, first off... I'm adding together the wrong coordinates

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if(abs(x1 + x2) > abs(y1 + y2))
printf("Alex\n");
else if(abs(x1 + x2) < abs(y1 + y2))
printf("Bob\n");
else if(abs(x1 + x2) == abs(y1 + y2))
printf("Equal\n");
}
return 0;
}
// correct answer

#include <stdio.h>
#include <math.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
int alex = pow(x1, 2) + pow(y1, 2);
int bob = pow(x2, 2) + pow(y2, 2);

if(alex > bob)
printf("Alex\n");
else if(bob > alex)
printf("Bob\n");
else if(alex == bob)
printf("Equal\n");
}
return 0;
}

Python

Same code as C. Didn’t need to import math. Could have used in the header:
from math include sqrt
and then sqrt the variables. can also use:
math.hypot(x1, y1)
this will find the hypotenuse of a right angle, or base sqrd + height sqrd

for t in range(int(input())):
x1, y1, x2, y2 = map(int, input().split())
alex = pow(x1, 2) + pow(y1, 2)
bob = pow(x2, 2) + pow(y2, 2)

if(alex > bob):
print("Alex")
elif(bob > alex):
print("Bob")
elif(alex == bob):
print("Equal")

--

--