Sitemap
jay.dez

jay.dez

no offense shortstack but you give me the creeps

Day 26 Learning Python & C - Practice Problems

8 min readDec 22, 2022

--

Press enter or click to view image in full size

Today’s completed practice problems on Codechef.com. I enjoy this site for practicing and learning code because I can see other other user’s write code and learn different ways to find the same answer.

I’ve done 250 problems in C and the same 250 Python so really I’m at 500 solved problems. Also, I was at a 44 day streak and since the site records IST (India time) there was one day I submitted a problem but it was outside of their IST so it restarted my streak counter. Lame.

Problem 1. Valid Pair

Chef has three socks in his drawer. Each sock has one of 10 possible colors, which are represented by integers between 1 and 10. Specifically, the colors of the socks are A, B, and C.

Chef has to wear two socks which have the same color. Help Chef find out if that is possible or not.

Input Format

The first and only line of the input contains three space-separated integers A, B and C.

Output Format

Print a single line containing the string "YES" if it is possible for Chef to wear two socks with the same color or "NO" if it is impossible (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).

In C ( and Python) I’m still doing basic coding, specifically math problems and I feel I’ve reached a point where I can look at most problems and fairly quickly visualize how the code will look — to the point where now I just set up the initial basic structure and then look at other user submissions to see if anyone wrote something that’s a little more advance or in an alternative way to how I would have done it. Then I’ll dissect that to see how they came up with that or just how it works and write it out myself after understanding it-even sometimes putting my own spin such as spacing things out, eliminating too many braces, etc.

#include <stdio.h>

int main(void)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a == b || b == c|| c == a)
printf("Yes\n");
else
printf("No\n");
return 0;
}

In Python, kept getting it wrong because I forgot to replace || with or. Also didn’t do a direct transcribe from C with if and else and print all on separate lines which is what like 90% of other users submitted. Instead I condensed it into the print function. Also saw another user do it with one line one code!

They used a ‘Set’ — Sets are used to store multiple items in a single variable.

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.

A set is a collection which is unordered, unchangeable*, and unindexed.

They also used len(). To determine how many items a set has, use the len() function.

#my code
a, b, c = map(int, input().split())
print('Yes' if a == b or c == b or a == c else 'No')
"""
Sets can't have duplicates. So if there's a duplicate,
it doesn't get added to the set which means the set is
less than 3 so it prints yes. The problem with this is
what if you wanted to verify the duplicate and scces it,
you can't because sets are unindexed and not allowed.
"""

print("Yes" if len(set(input().split())) < 3 else "No")

Problem 2. Make A and B Equal

Chef has two numbers A and B.

In one operation, Chef can choose either A or B and multiply it by 2.

Determine whether he can make both A and B equal after any number (possibly, zero) of moves.

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

Output Format

For each test case, output YES if Chef can make both numbers equal, NO otherwise.

Get jay.dez’s stories in your inbox

Join Medium for free to get updates from this writer.

Note that the checker is case-insensitive i.e. YES, Yes, yes, yES are all considered same.

This one in C took some time. Not sure how long, I didn’t time it but at least 20 minutes. I fully understood the problem and how to get this outcome, in my head, but struggled with the setup. I had to look at other user submissions and I was very close but had things a little bakwards. Anyway, I figured it out and was able to write the code on my own and logic through it.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int a, b;
scanf("%d %d", &a, &b);
if(a == b)
printf("Yes\n");
else
if(a < b)
{
while(a < b)
a*=2;
if(a == b)
printf("Yes\n");
else
printf("No\n");
}
else
{
while(b < a)
b*=2;
if(a == b)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
/*
Another user use the <math.h> library to access the pow and log functions.

First they swap the larger numbers intil variables big or small. This
eliminates do if(a < b) else if (a > b) and going into the while-loops
I originally did.

Flag is just a counter/ trigger variable once a == b.

Used for-loop and variable imax for the i counter limit. This is defined
as larger of the two numbers divided by the smaller, log base 2 plus 1.

pow() function set at 2 power of whatever i is in the counter. 2 to the
power of 0 is 1. then 2 to the power of 1 is 2 and so on.

Keep multiplything that out by the smaller number until it's equal to the
larger. Once they are add one to flag if and break out of the loop.
(or i is no longer less than imax)
*/


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

int main(void) {
int T,A,B,small,big;
scanf("%d",&T);
while(T--){
scanf("%d %d",&A,&B);
small=A;big=B;
if(small>big){
small=B;
big=A;
}
int flag=0;
int imax=log2((big/small))+1;
for(int i=0;i<=imax;i++){
int a=small*pow(2,i);
if(a==big){
flag=1;
break;
}
}
if(flag==1){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}
"""
Kept getting all yes because i originally had a = min(a, b) and
b = max(a, b). Also forgot to reset my y variable to zero for each test
so why was always 1 after the first run.
"""


for t in range(int(input())):
a, b = map(int, input().split())
c = min(a, b)
e = max(a, b)
y = 0
if a == b:
print('Yes')
else:
while c < e:
c*=2
if c == e:
y = 1
else:
y = 0
print('Yes' if y == 1 else 'No')
"""
Here they imported math and used math.log and is_integer to check
if the log of base 2 is an integer (whole number, not decimal or fraction)
i.e 20/5 = 4 base 2 which is 2 * 2. But i.e 12/2 = 6 base 2 wouldn't
be a whole number. so log(value, base) is basically 2 to the what power
equal 4, or 6.

"""
import math
for t in range(int(input())):
a,b = map(int, input().split())
if math.log(a/b,2).is_integer() or math.log(b/a,2).is_integer():
print("YES")
else:
print("NO")

Problem 3. Summer Heat

Chefland has 2 different types of coconut, type A and type B. Type A contains only xa​ milliliters of coconut water and type B contains only xb​ grams of coconut pulp. Chef’s nutritionist has advised him to consume Xa​ milliliters of coconut water and Xb​ grams of coconut pulp every week in the summer. Find the total number of coconuts (type A + type B) that Chef should buy each week to keep himself active in the hot weather.

Input Format

  • The first line contains an integer T, the number of test cases. Then the test cases follow.
  • Each test case contains a single line of input, four integers xa​, xb​, Xa​, Xb​

Output Format

For each test case, output in a single line the answer to the problem.

Proud of myself! Haha did this one fairly quick but what I learned from the last problem from another user was just reassigning some variables earlier on to “small” and “big” variables since there isn’t a min/max function in C like Python. Then just dissected and figured out type and copy and pasted the code for “typeb” from “typea”.

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

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int xa, xb, XA, XB;
int small, big, typea, typeb;
scanf("%d %d %d %d", &xa, &xb, &XA, &XB);
small = xa;
big = XA;
if(small > big)
{
small = XA;
big = xa;
}
if(big % small == 0)
typea = big / small;
else
typea = ((big / small) + 1);

small = xb;
big = XB;
if(small > big)
{
small = XB;
big = xb;
}
if(big % small == 0)
typeb = big / small;
else
typeb = ((big / small) + 1);

printf("%d\n", typea + typeb);
}
return 0;
}
#don't need to impot math for min, max

for t in range(int(input())):
xa, xb, XA, XB = map(int, input().split())
small = min(xa, XA)
big = max(xa, XA)
typea = big // small

small = min(xb, XB)
big = max(xb, XB)
typeb = big // small
print(typea + typeb)

Actually, one of the constraints for the problem is that xa divides into XA and xb divides into XB so I guess I really don’t need to properly do “min, max” and assign them to other variables. I could just do:

for t in range(int(input())):
xa, xb, XA, XB = map(int, input().split())
print((XA//xa) + (XB//xb))

Problem 4. Equal Integers

Chef has two integers X and Y. Chef wants to perform some operations to make X and Y equal. In one operation, Chef can either:

  • set X:=X+1
  • or set Y:=Y+2

Find the minimum number of operations required to make X and Y equal.

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.

Output Format

For each test case, print the minimum number of operations required to make X and Y equal.

// lets just say I made this problem more difficutlt than it really was...

#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 - x);
else
if((x - y) % 2 == 0)
printf("%d\n", (x - y) / 2);
else
printf("%d\n", ((x - y) / 2) + 2);
}
return 0;
}
for t in range(int(input())):
x, y = map(int, input().split())
if(x <= y):
print(y - x)
else:
if((x - y) %2 == 0):
print(int((x - y) / 2))
else:
print(int((x - y) / 2) + 2)

--

--

jay.dez
jay.dez