Dev & Scripting » Coding Challenges » Clash of Code » 10 Clash of Code Challenges Explained #1
Let’s solve 10 clash of code challenges, shall we?
Table of contents
Problem #1 – Reverse
For this first problem, we don’t have instructions, only examples of input/output:
| Input | Expected output |
| ACGT | TGCA |
| A | T |
| C | G |
| G | C |
| T | A |
| GATTACA | CTAATGT |
Well it seems that the instructions are pretty simple: A must be replaced by T, C must be replaced by G, G by C and T by A.
Python solution:
c = {
"A": "T",
"C": "G",
"G": "C",
"T": "A"
}
i = input()
for l in i:
print(c[l], end='')
Really straightforward.
Problem #2 – Reverse
Another reverse challenge:
| Input | Expected output |
| 2 -2 | 1 |
| 1 -5 | 0 |
| 5 5 | 0 |
| 111101 -111101 | 1 |
| 2 2 | 0 |
Weird. The output seems to be binary, probably 1 for a true condition and 0 for a false condition. But what’s that condition?
Well from those examples, it seems that this condition is the following: both numbers must be different, but identical when the “-” sign is removed from one of them.
2 and -2 are different, but 2 and 2 are the same.
1 and -5 are different, however 1 and 5 are also different.
So basically both numbers must be opposite (positive and negative value of the same number).
Python solution (input() always returns a string):
a = input()
b = input()
if a.replace("-", "") == b.replace("-", "") and a!=b:
print(1)
else:
print(0)
Probably not the best solution, but it was 100% working for the tests 🙂
Problem #3 – Shortest mode
Instructions:
Johnny and Bobby are both hungry for a small snack after their soccer practice at school. They have a snack that contains n number of chips, but they will only eat the snack if both of them get the same amount of chips each.
However, before they eat the snack, Dobby also came from basketball practice and also wants to eat the snack, and they will only eat the snack if all three of them have the same amount of chips.
Help Johnny, Bobby, and Dobby figure out how many people can eat the snack, as Dobby will just go home if only 2 people can eat the snack.
Input
Line 1: An integer n representing the number of chips.
Output
Line 1: A string depending on the following condition:
• If all three can eat the snack: All three can eat the snack
• If only Johnny and Bobby can eat the snack: Johnny and Bobby can eat the snack
• If none can eat the snack: No one can eat the snack
My Python solution in 141 characters:
n=int(input())
print("All three can eat the snack"if n%3==0 else "Johnny and Bobby can eat the snack"if n%2==0 else"No one can eat the snack")
Explanation:
print("all three...") if n is divisible by 3
otherwise: print("johnny...") if n is divisible by 2, otherwise print("no one...")
My solution wasn’t the shortest. Thinking about it, the following code is shorter, and we can maybe come up with a shorter code:
n=int(input())
print([["Johnny and Bobby can eat the snack","No one can eat the snack"][n%2],"All three can eat the snack"][n%3==0])
If you don’t understand this final code, make sure to check my Code Golf CheatSheet in the Python section 😉
Problem #4 – Fastest mode
Instructions:
Your program must print the N first numbers of an arithmetic sequence of common difference R and started by 0.
An arithmetic sequence is a sequence of numbers such that the next term can be computed by adding a constant value R.
INPUT:
Two space separated integers N and R.
OUTPUT:
The first N integers of the arithmetic sequence of common difference R, starting with 0.
CONSTRAINTS:
0 < N < 100
0 ≤ R ≤ 100
EXAMPLE:
Input
5 2
Output
0 2 4 6 8
Quite simple. My solution in Python:
n, r = [int(i) for i in input().split()]
seq = [0]
i = 1
while i < n:
seq.append(seq[-1] + r)
i += 1
print(" ".join(list(map(str, seq))))
My solution has multiple useless instructions and can be shortened, but it works.
Problem #5 – Fastest mode
Instructions:
To study the thickness of a layer of rock before a new layer of rock is deposited on top (layers settle with weight), this past thickness is calculated as a function of the evolution of the rock's porosity, according to the following formula:
initial_thickness = current_thickness / ( 1-(log₁₀(current_porosity)-log₁₀(initial_porosity)))
You have to calculate this initial thickness of a given rock layer.
Input
Line 1 : An integer CT current thickness
Line 2 : A float CP current porosity
Line 3 : A float IP initial porosity
Output
Line 1 : An integer IT truncated initial thickness
Constraints
100 ≤ CT ≤ 100000
0.01<CP<1.0
0.01<IP<1.0
Example
Input
500
0.4
0.2
Output
715
Also very straightforward, we must simply use the given formula. The log10 function is available through math.log10().
Here is my Python solution:
import sys
import math
ct = int(input())
cp = float(input())
ip = float(input())
initial_thickness = ct / ( 1-(math.log10(cp)-math.log10(ip)))
print(math.floor(initial_thickness))
Problem #6 – Fastest mode
Instructions (long):
You are a race engineer. Based on tire telemetry, you must output a specific status code representing the driver's priority at the end of the lap.
The priorities are defined as follows (checked in this order):
1. If tireWear is strictly greater than 70, output EMERGENCY_PIT.
2. If the remaining laps are greater than the estimatedLife, output SCHEDULED_PIT.
3. If rivalGap is less than 1.5 and tireWear is greater than 30, output UNDERCUT_DEFENSE.
4. If rivalGap is less than 5.0 but tireWear is less than or equal to 30, output PUSH_HARD.
In all other cases, output MAINTAIN_GAP.
Input
Line 1: An integer tireWear (0-100).
Line 2: An integer laps remaining.
Line 3: An integer estimatedLife of the current tires.
Line 4: A float rivalGap to the car behind.
Output
Line 1: One of the following strings: EMERGENCY_PIT, SCHEDULED_PIT, UNDERCUT_DEFENSE, PUSH_HARD, or MAINTAIN_GAP.
Constraints
- 0 ≤ tireWear ≤ 100
- 1 ≤ laps, estimatedLife ≤ 80
- 0.0 ≤ rivalGap ≤ 30.0
Example
Input
80
5
10
15.0
Output
EMERGENCY_PIT
Well, this one is also pretty simple: I’m just going to translate each of the 4 priorities in the same order inside my Python code:
import sys
tire_wear = int(input())
laps = int(input())
estimated_life = int(input())
rival_gap = float(input())
if tire_wear > 70:
print("EMERGENCY_PIT")
sys.exit()
if laps > estimated_life:
print("SCHEDULED_PIT")
sys.exit()
if rival_gap < 1.5 and tire_wear > 30:
print("UNDERCUT_DEFENSE")
sys.exit()
if rival_gap < 5 and tire_wear <= 30:
print("PUSH_HARD")
sys.exit()
print("MAINTAIN_GAP")
Code is trivial, and the repeated sys.exit() makes it very ugly, but whatever…
Problem #7 – Shortest mode
Instructions:
Given N non-negative integers, output the largest odd number among them.
If none of the numbers are odd, output 0.
Input
Line 1: An integer N, the count of numbers.
Next N lines: non-negative integers, one integer per line
Output
Line 1: The largest odd number in the list, or 0 if no odd number exists.
Constraints
1 ≤ N ≤ 10
Each number is a non-negative integer ≤ 1000
Example
Input
4
4
7
10
3
Output
7
Well at first I did a one liner solution in Python, with 84 characters:
print(max(sorted(map(lambda _:[0,_][_%2],[int(input())for i in "a"*int(input())]))))
But then I realised that inserting each number inside a list then sorting the list and printing its max wasn’t the best solution. Instead, I could store the max odd number and compare each odd number with the max. If the odd number was greater than the max, then it would become the new max.
Final Python solution (71 characters):
r=0
for i in "a"*int(input()):_=int(input());r=max(r,[0,_][_%2])
print(r)
Problem #8 – Fastest mode
Instructions:
Repeat the given string N times and output the result to the console
Input
lines 1:N - number of times to repeat
lines 2: the line to be repeated
Output
string repeated N times, but if N=0 output 'empty'
Constraints
0<=N<=10
1<=string length <=10
Example
Input
1
string
Output
string
What a dumb example lol.
Here is my solution in Python:
n=int(input())
print(input()*n or "empty")
Problem #9 – Fastest mode
Instructions:
In this Clash, you will be given two lists of integers that are already sorted. You must merge those lists into a single sorted list. If both lists are empty print Empty.
Input
Line 1 : An integer n1 which is the length of the first list
Line 2 : An integer n2 which is the length of the second list
Next n1 lines : An integer in each line which is an element of L1 (the first list)
Next n2 lines : An integer in each line which is an element of L2 (the second list)
Output
L : the sorted list of integers seperated by commas or Empty
Constraints
0<=n1<=50
0<=n2<=50
All integers are between 0 and 1000.
Example
Input
3
3
1
3
5
2
4
6
Output
1,2,3,4,5,6
This is pretty simple with built-in Python list functions:
l1 = []
l2 = []
n1 = int(input())
n2 = int(input())
for i in range(n1):
l1+=[int(input())]
for i in range(n2):
l2+=[int(input())]
print(",".join(map(str,sorted(list([*l2,*l1]))))or"Empty")
Problem #10 – Reverse mode
Instructions:
| Input | Expected output |
| 3 ROCK PAPER ROCK PAPER ROCK SCISSORS | 2-1 |
| 2 PAPER SCISSORS SCISSORS ROCK | 0-2 |
| 5 SCISSORS SCISSORS SCISSORS ROCK PAPER PAPER SCISSORS ROCK ROCK SCISSORS | 1-2 |
| 10 ROCK ROCK PAPER SCISSORS ROCK ROCK PAPER PAPER ROCK SCISSORS ROCK ROCK SCISSORS PAPER ROCK ROCK PAPER PAPER ROCK PAPER | 2-1 |
Well, of course it’s rock paper scissors, and the output is the final score between the two players (player 1 is the first line of rock/paper/scissors and player 2 is the second line). My Python solution was pretty straightforward:
n = int(input())
p1 = input().split(' ')
p2 = input().split(' ')
s1 = 0
s2 = 0
for i in range(n):
if p1[i] == "ROCK":
if p2[i] == "PAPER":
s2 +=1
elif p2[i] == "SCISSORS":
s1 += 1
if p1[i] == "PAPER":
if p2[i] == "SCISSORS":
s2 +=1
elif p2[i] == "ROCK":
s1 += 1
if p1[i] == "SCISSORS":
if p2[i] == "ROCK":
s2 +=1
elif p2[i] == "PAPER":
s1 += 1
print("{}-{}".format(s1,s2)
Don’t forget to check the Code Golf CheatSheet to better understand my code, write better code yourself and learn tips for your favorite programming language!