Dev & Scripting » Coding Challenges » Clash of Code » 10 Clash of Code Challenges Explained #2
Let’s solve 10 clash of code challenges, shall we?
If you have any trouble understanding my solution, I suggest you to take a look at my Code Golf CheatSheet!
Table of contents
Problem #1 – Fastest Mode
Instructions:
Storyline:
You work in NASA, and must check if the rocket will reach Mars
The Problem:
You must create a program to calculate if rocket will reach Mars
Criterion of a Failing rocket:
- If velocity multiplied by the time is less than distance to Mars
- If fuel multiplied by the fuel consumption is less than distance
Notes:
If there is less time, print 'Failure, Not enough time'
If there is less fuel, print 'Failure, Not enough fuel'
If both only print 'Failure, Not enough time'
If rocket is successful, print 'Welcome to Mars'
time will be in hours
fuel will be in liters
fuel_consumption will be in distance per 1 liter of fuel
distance will be in kilometers
- Input:
time: Integer
fuel: Integer
fuel_consumption: Integer
distance: Integer
- Output:
answer: String
Constraints
0 < fuel, fuel_consumption, distance, velocity, time < 1001
Example
- Input:
1000
10
10
100
100
- Output:
Failure, Not enough time
Well this first challenge is pretty easy to start. My Python solution simply follows the instructions:
distance = int(input())
time = int(input())
velocity = int(input())
fuel = int(input())
fuel_consumption = int(input())
crit1 = (velocity * time) < distance
crit2 = (fuel * fuel_consumption) < distance
if crit1 and crit2:
print("Failure, Not enough time")
elif crit1:
print("Failure, Not enough time")
elif crit2:
print("Failure, Not enough fuel")
else:
print("Welcome to Mars")
Problem #2 – Shortest Mode
Interesting one:
What percentage of the days did the temperature go below freezing (0°C)?
Conversion from F to C: ("degree in F"- 32) x 5/9 = "degree in C"
- Input:
Line 1 The number of days your testing. n
Next n lines The day a string, the temp an integer, and the unit C or F representing Celsius or Fahrenheit.
- Output:
A percentage representing the days that go below freezing. (0°C)
Percentage rounded down to an integer.
Constraints
0<n<5
-40<temp<100
Example
- Input:
2
day-1 23 C
day-2 -2 F
- Output:
50%
Ok so at first I used this code:
n=int(input())
s=0
for i in "_"*n:
_=input().split()
t=int(_[1])
if _[2]=="F":t=(t-32)*5/9
s+=[0,1][t<0]
print(f"{s*100//n}%")
It stores in the variable s the amount of days where temperature in celcius was strictly less than 0. For each input, if it’s in Farenheit, it converts it to Celcius, then if it’s less than 0 it adds 1 to s.
Eventually I shortened the code to the best of my ability, for 116 characters:
I=input
n=int(I())
s=0
for i in"_"*n:_=I().split();t=int(_[1]);s+=int([t,5/9*(t-32)]["F"==_[2]]<0)
print(f"{s*100//n}%")
Problem #3 – Fastest Mode
Instructions:
In this puzzle, you will be given the current point that your timer is at and the point that your timer will stop. Your task is to output the amount of time remaining in the timer.
Input:
One Line: The current time position C; The position at which the timer stops S (m:ss)
Output:
The time remaining in m:ss
Example:
Input:
0:00 0:01
Output:
0:01
Pretty standard exercise, using the modulo operator with 60 for conversions:
c, s = input().split()
c1,c2 = map(int,c.split(':'))
s1,s2 = map(int,s.split(':'))
ctime = c1 * 60 + c2
stime = s1 * 60 + s2
ltime = stime - ctime
print(f"{ltime//60}:{ltime%60:02d}")
Problem #4 – Fastest Mode
Instructions:
You are given a list of space-separated integers which represent a binary string in terms of the number of consecutive 1s or 0s alternately, beginning with 1.
Your task is to return the decimal number.
Example:
Input is 3 2 1
Binary representation is 111001
Which is 57 in decimal.
Input:
Line 1: A single positive integers N.
Line 2: N space-separated integers.
Output:
Line 1: An integer representing the binary string converted to decimal.
Constraints
2 ≤ N ≤ 100
Example:
Input:
2
2 3
Output:
24
Simple solution, with a variable c that varies between 1 and 0:
n = int(input())
c = 0
s = ""
for i in input().split():
count = int(i)
s += ["1","0"][c] * count
c = [1,0][c]
print(int(s,2))
Problem #5 – Reverse Mode
Examples that were given:
| INPUT | OUTPUT |
| ROCK PAPER | PLAYER1 |
| ROCK ROCK | DRAW |
| SCISSORS ROCK | PLAYER2 |
Pretty easy to guess… here is my terribly long solution:
call1, call2 = input().split()
if call1 == "ROCK":
if call2 == "SCISSORS":
print("PLAYER1")
elif call2 == "PAPER":
print("PLAYER2")
else:
print("DRAW")
if call1 == "PAPER":
if call2 == "ROCK":
print("PLAYER1")
elif call2 == "SCISSORS":
print("PLAYER2")
else:
print("DRAW")
if call1 == "SCISSORS":
if call2 == "PAPER":
print("PLAYER1")
elif call2 == "ROCK":
print("PLAYER2")
else:
print("DRAW")
Problem #6 – Fastest Mode
Instructions (very original!):
Given a string, you must convert each character - using its ASCII code - in 8 bit long binary numbers and concatenate them from left to right.
Then you must sum bits whose indices are in the Fibonacci sequence (but do not include the same bit twice in this sum!).
The left-most bit of that binary sequence is considered to have index 0.
Input
Line 1: A string String (composed of uppercase and lowercase English letters, spaces and punctuation).
Output
Line 1: A number N for sum of bits.
Constraints
2 ≤ length(String) ≤ 1000
Example:
Input
testcase
Output
7
I thought it would be smarter to build the Fibonacci sequence along the sum:
s = input()
binascii = ''.join(list(map(lambda _: f'{ord(_):08b}', s)))
fib = [1, 1]
s = 0
for i in range(len(binascii)):
fib += [fib[-1] + fib[-2]]
if (i) in fib:
s += int(binascii[i])
print(s)
Problem #7 – Reverse Mode
This one was simple.
Examples given:
| INPUT | OUTPUT |
| string | 6 gnirts |
| variable | 8 elbairav |
| pointer | 7 retniop |
| function | 8 noitcnuf |
Obviously the output is the length of the input and the string in reverse:
i = input()
print(len(i), i[::-1])
Problem #8 – Reverse Mode again
This one was even more simple and a bit tricky:
| INPUT | OUTPUT |
| 2+1 | 3 |
| 2+2 | 3 |
| 2+1+2 | 5 |
| -5*1-1 | 6 |
| -6+6-1 | 6 |
The output is simply the length of the input:
print(len(input()))
Problem #9 – Reverse Mode again!
Examples given:
| INPUT | OUTPUT |
| 5 2 0 | 05 |
| AB 5 # | ###AB |
| 2025 4 ! | 2025 |
| 250 6 USD | USD250 |
| lol 9 TRY | TRYTRYlol |
| care 9 123 | 123123care |
| test 9 last_but_not_least | last_test |
Ok so it seems that the second input (the int) defines the final length of the output. And the output always ends with the first input (the first word). And the last input (the last word), is used as a padding before the first word in order to reach the desired length. As seen in the last example, if the padding word is longer than the length required for the padding, it must be cut.
This was my solution:
s = input()
length = int(input()) - len(s)
pad = input()
if len(pad) > length:
print(pad[:length] + s)
else:
print(pad * (length // len(pad)) + pad[:(length % len(pad))] +s)
If the padding was longer than needed, I printed the first part of the padding then the word.
If the padding should be repeated more than once, we need to calculate how many times it must be repeated entirely and then if for instance 2 characters are left while the padding is 5 characters long, then add the first 2 letters of the padding.
Probably not the best code…
Problem #10 – Fastest Mode
This one annoyed me:
Take a string s, and given mode (any combination of the space-separated items letters, numbers, symbols, or NONE) remove all characters satisfying mode from s. Symbols are all non-alphanumeric characters including whitespace.
If mode contains NONE, remove nothing, even if mode contains letters, numbers, or symbols.
Output the modified string, then, on another line, output the sum of all s's unique character's ASCII values.
Input
Line 1: a string mode containing any combination of the space-separated items letters, numbers, symbols, or NONE.
Line 2: a string s; the string to manipulate according to the statement.
Output
Line 1:: What remains of s, after taking out all characters that satisfy mode.
Line 2:: the sum of the modified s's unique char's ASCII values
Constraints
mode will always contain at least one item of letters, numbers, symbols, or NONE.
5 <= length of s <= 40
Example:
Input
letters
yo231
Output
231
150
Using the string methods in Python was very helpful:
mode = input()
s = input()
if "NONE" in mode:
print(s)
print(sum(map(ord, list(set(s)))))
else:
modified = ""
for l in s:
if l.isdigit() and "numbers" in mode:
continue
if l.isalpha() and "letters" in mode:
continue
if not(l.isalnum()) and "symbols" in mode:
continue
modified += l
print(modified)
print(sum(map(ord, list(set(modified)))))
As always, don’t forget to check the Code Golf CheatSheet to learn more and improve your problem-solving skills!