Day 43 Learning C && Python: Practice Problems

jay.dez
4 min readJan 15, 2023

--

Yong into Naples last night from Bergamo

Problem 1. Chef Plays Ludo

Difficulty Rating: 260

Chef is playing Ludo. According to the rules of Ludo, a player can enter a new token into the play only when he rolls a 66 on the die.

In the current turn, Chef rolled the number X on the die. Determine if Chef can enter a new token into the play in the current turn or not.

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 one integer X — the number rolled by the Chef on the die.

Output Format

For each test case, output YES if the Chef can enter a new token in the game. Otherwise, output NO.

You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical).

Classic C

Easy problem. Use double == for equals. Just testing if variable is equal to number six. If so, print yes, if not, print no.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int x;
scanf("%d", &x);
if(x == 6)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

Python

Nothing to it. Jammed everything into a single print function.

for t in range(int(input())):
print("Yes" if int(input()) == 6 else "No")

Problem 2. Multivitamin Tablets

Difficulty Rating: 376

The doctor prescribed Chef to take a multivitamin tablet 3 times a day for the next X days.

Chef already has Y multivitamin tablets.

Determine if Chef has enough tablets for these X days or not.

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 number of days Chef needs to take tablets and the number of tablets Chef already has.

Output Format

For each test case, output YES if Chef has enough tablets for these X days. Otherwise, output NO.

You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical).

Classic C

Fairly straight forward simple problem. If the total amount of vitamins chef needs is more than what he has, print no, else print yes.

#include <stdio.h>

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

Python

Incorporated the math and if/else statement into the print function to shorten code length.

for t in range(int(input())):
x, y = map(int, input().split())
print("Yes" if (y >= x * 3) else "No")

Problem 3. Lucy Numbers

Difficulty Rating: 655

Chef calls a number lucky if it contains the digit 7 at least once.

Given a number X, determine if it is a lucky number or not.

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 an integer X — the number mentioned in the problem statement.

Output Format

For each test case, output YES if X is a lucky number. Otherwise, output NO.

You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical).

Classic C

I wanted to loop through each number to see if there is a 7. Once found, stop the loop and print yes. To do that, I took input as a string.

#include <stdio.h>

int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int count = 0;
char x[100];
scanf("%s", x);
for(int i = 0; x[i]!='\0'; i++)
{
if(x[i]=='7')
{
count++;
break;
}
}
if(count >= 1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

Python

Fuck Python is so much easier, once you know it of course, put look at all that code in C versus Python. Now, the first two Python codes I’ve copy and posted here are from two other users, I felt I could simplify it even more so. The third code is mine.

# I don't understand the purpose of int(d) but I get the string input
# but he's got this for-loop within the list, looping through the input

t = int(input())
for _ in range(0,t):
times = [int(d) for d in str(input())]
if(7 in times):
print("Yes")
else:
print("No")
#this guys made it even more simple

for i in range(int(input())):
n=(input())
if '7' in n:
print('yes')
else:
print('no')
for t in range(int(input())):
print('Yes' if '7' in input() else 'No')

--

--