Day 32 Learning C & Python - Practice Problems

jay.dez
6 min readDec 30, 2022

--

Did my first two challenges on Codechef where basically I challenge a random other user to completing incomplete code first. The first ended in a draw, neither of us got it. The second one I couldn’t figure out one of the exports and the other user ended up quitting so I won by default, lame. Anyway, You have to pay for a pro account in order to see the solutions which I’m not doing because I’m broke as fuck. But I’ve completed 10 code explanations so I got this dorky little bronge badge. Baically it’s just explanning my code.

Problem 1. Highest Divisor

Difficulty Rating: 860

You are given an integer N. Find the largest integer between 1 and 10 (inclusive) which divides N.

Input

The first and only line of the input contains a single integer N.

Output

Print a single line containing one integer ― the largest divisor of N between 1 and 10.

Constraints

  • 2 ≤ N ≤ 1,000

Subtasks

Subtask #1 (100 points): original constraints

I am proud I just logic’d my way through this one and didn’t look at other submissions… sort of… 90%. I kept getting a runtime error so I looked at a C++17 submissions and saw they had “i = 10; i > 1; i--” so immediately I remembered the constraints which originally I had i < 10 but also had some strange while(t) loop going and did bool t = true. Got rid of that, still runtime error, but the original constraints from the problem but instead I did: “int i = 0; i < 10; i++” and that worked.

#include <stdio.h>

int main(void)
{
int n;
int c = 0;
scanf("%d", &n);
for(int i = 1; i <= 10; i++)
{
if(n%i == 0)
{
c = i;
}
}
printf("%d\n", c);
return 0;
}
n = int(input())
i = 1
while i < 10:
if n % i == 0:
c = i
i+=1
print(c)

Problem 2. Car Choice

Difficulty Rating: 861

Chef is planning to buy a new car for his birthday. After a long search, he is left with 2 choices:

  • Car 1: Runs on diesel with a fuel economy of x1​ km/l
  • Car 2: Runs on petrol with a fuel economy of x2​ km/l

Chef also knows that

  • the current price of diesel is y1​ rupees per litre
  • the current price of petrol is y2​ rupees per litre

Assuming that both cars cost the same and that the price of fuel remains constant, which car will minimize Chef’s expenses?

Print your answer as a single integer in the following format

  • If it is better to choose Car 1, print -1
  • If both the cars will result in the same expenses, print 00
  • If it is better to choose Car 2, print 1

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.
  • Each test case consists of a single line containing 4 space-separated integers — x1​, x2​, y1​, y2​.

Output Format

For each test case, output in a single line the answer as explained earlier.

Constraints

  • 1 ≤ T ≤ 10000
  • 1 ≤ x1​, x2​, y1​, y2 ​≤ 50

First try got it wrong because my input variable were “ints” instead of “floats”. But basically dives the cost per litre by km/l to get the cost per km and compare car to car 2. Print the cheaper.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
{
while(t--)
{
float x1, x2, y1, y2;
scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
if(y1 / x1 < y2 / x2)
printf("-1\n");
else if(y2 / x2 < y1 / x1)
printf("1\n");
else
printf("0\n");
}
}
return 0;
}
for t in range(int(input())):
x1, x2, y1, y2 = map(float, input().split())
if(y1 / x1 < y2 / x2):
print("-1")
elif(y2 / x2 < y1 / x1):
print("1")
else:
print("0")

Problem 3. Total Expenses

Difficulty Rating: 861

While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000.
If the quantity and price per item are input, write a program to calculate the total expenses.

Input Format

The first line contains an integer T, total number of test cases. Then follow T lines, each line contains integers quantity and price.

Output Format

For each test case, output the total expenses while purchasing items, in a new line.

Constraints

1 T 1000 1 quantity, price 100000

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
float qty, pr;
scanf("%f %f", &qty, &pr);
if(qty > 1000)
printf("%f\n", (qty * pr) * .9);
else
printf("%f\n", qty * pr);
}
return 0;
}

Shortened the code in Python by three lines by creating a “total” variable for quantity/ price and by incorporating the “if/else” statements into the print-statement.

for t in range(int(input())):
qty, pr = map(float, input().split())
total = qty * pr
print(total * .9 if qty > 1000 else total)

Problem 4. Alternative Additions

Difficulty Rating: 863

Chef has 2 numbers A and B (A<B).

Chef will perform some operations on A.

In the ith operation:

  • Chef will add 1 to AA if i is odd.
  • Chef will add 2 to A if i is even.

Chef can stop at any instant. Can Chef make A equal to B?

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 A and B.

Output Format

For each test case, output YES if Chef can make A and B equal, NO otherwise.

Note that the checker is case-insensitive. So, YES, Yes, yEs are all considered same.

Constraints

  • 1 ≤ T ≤ 1000
  • 1 ≤ A < B ≤ 10 to the 9th

Even though this C code produces the correct answer, Codechef marks it wrong… which is annoying because I was proud to work my way through this and get the correct answer as oppose to getting hints from other submissions, but they were looking for some modulo 3 deal.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int a, b;
scanf("%d %d", &a, &b);
while(a < b)
{
if(a % 2 == 0)
a+=2;
else if(a % 2 != 0)
a+=1;
}
if(a == b)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
/*
see this is dumb because if you do a=8, b=9 and then
9 - 8 which equals 1... then divide by 3, the remainder isn't 2...
it's .333333.... So it'd print out 'Yes'*/


#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int a, b;
scanf("%d %d", &a, &b);
if(a < b)
if((b - a) % 3 == 2)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
for t in range(int(input())):
a, b = map(int, input().split())
if(a < b):
print("No" if (b - a) % 3 == 2 else "Yes")

Problem 5. Which Division

Difficulty Rating: 867

Given the rating R of a person, tell which division he belongs to. The rating range for each division are given below:

  • Division 1: 2000 ≤ Rating.
  • Division 2: 1600 ≤ Rating ≤ 2000
  • Division 3: Rating < 1600.

Input Format

  • The first line of the input contains T — the number of test cases. Then the test cases follow.
  • Each testcase contains a single line of input, which contains a single integer R.

Output Format

For each test case, output on a single line the answer: 1 if the person belongs to Division 1, 2 if the person belongs to Division 2, and 3 if the person belongs to Division 3.

Constraints

  • 1 ≤ T ≤ 1000
  • 1000 ≤ R ≤ 4500
#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int r;
scanf("%d", &r);
if(2000 <= r)
printf("1\n");
else if(1600 <= r && r <= 2000)
printf("2\n");
else if(r <= 1600)
printf("3\n");
}
return 0;
}
for t in range(int(input())):
r = int(input())
if 2000 <= r:
print("1")
elif 1600 <= r and r <= 2000:
print("2")
elif r <= 1600:
print("3")

--

--