DEV Community

loading...
Cover image for Road to Genius - Python Series level 2

Road to Genius - Python Series level 2

codr profile image Ilya Nevolin ・5 min read

Welcome to the second level where we solve Python Challenges from Codr's Ranked mode. The goal is to reach the Genius level, so join me at Codr!


python coding challenge

We start off with quite hard challenge. Given the function name cross it seems to be the doing a cross product of two arrays. We have to fix two bugs; the first droplet bug is an easy guess, we can use the mathematical symmetry to confidently say it should be variable a because the calculations all are of fashion a * b.

The second rocket bug has to be a number, this is not something we can easily guess unless we calculate it:

a = [8, ?, 6]
b = [6, 2, 8]

cross(a,b):
   [ ?*8 - 6*6,
     6*6 - 8*8,
     8*2 - ?*6 ]

out = cross(a,b)
A = abs(out[2]) # -->  8*2 - ?*6
such that A == 32


32 = 8*2 - ?*6
? = (32-16)/-6
? = 2.6667
This number is not in the list of answers, something isn't right...

Notice that A = abs(out[2]), this means that out[2] could be negative, let's test:

-32 = 8*2 - ?*6
? = 8
Enter fullscreen mode Exit fullscreen mode

python coding challenge


python coding challenge

Here's a very simple challenge, it's sorting an array of numbers and asking for the value at the 3rd index.
The sorted array is: [3,6,7,7,8]

python coding challenge


python coding challenge

This challenge has four bugs for us to fix. The first moneybag bug checks if c is inside something. This something is is likely to be the MP dict, since it's used several times below with c being the key.

The devil bug is followed by the False value, and sits in an if condition, so it's likely a comparison operator (== or !=). By the logic of the code, it should check whether c is not in MP, so the bug should be ==.

The red heart should be c as index.

The panda can be tricky to figure out on first sight. But every key in MP is some character from T, so panda is likely to be T itself. This line basically gets the character from T and index 1, and checks how many times that character occurs in T (thanks to MP).

python coding challenge


python coding challenge

Function dot operates on two arrays, in what appears to be the dot-product. We need to find a value for panda such that dot returns 60.

If you look at the function, you'll see the crucial operation s += a[i] * b[i]. So every respective element in a and b is multiplied and summed:

a = [6,2,2]
b = [?,8,4]
s = 0
while:
  s += 6*?
  s += 2*8
  s += 2*4

Find ? such that s == 60

60 = 6*? + 2*8 + 2*4
? = 6
Enter fullscreen mode Exit fullscreen mode

python coding challenge


python coding challenge

That's a heck of a function right here. Unfortunately we don't have to fully analyze and understand it ;) We only have to fix panda's value, which is some identifier.

If you look for some patterns in this code you see this line appearing several times: merged .append(...) and our panda has been solved.

python coding challenge


python coding challenge

This function takes in one array and returns a boolean value. We only need to fix two bugs. The diamond bug should be the value False because True is already being returned lower down.

The red apple can be little tricky, but all that if-condition checks is whether the current element i is smaller or equal to element i - apple. The apple can be any number, however it should be a valid array index and not violate any boundaries. The most common uses of such code is comparing previous value to current value. Given the fact that A should result in False, and the input array is increasing with two consecutive equal numbers (= 5), this if-condition will be true, and the function then returns False.

python coding challenge


python coding challenge

We have to solve for R's value, thus we need to analyze the code.

Variable S is a string of numbers. The for loop iterates over each character in S. The variable mp is used to count the number of occurrences of every character in S. Eventually R = mp['0'] which basically asks how many times the character zero occurs in S.

python coding challenge


python coding challenge

To solve these four bugs we have to gain some insight on what this short algorithm is doing.

Given an array, and while the array is not empty it loops. It removes last (pop) from array into x. The if-condition then compares prev (~ previous) value with x. The diamond bug could likely be != to ensure only valid numbers are compared, and the green heart will then be prev. If only x is greater than prev will x be assigned to R.

Then we have panda and rocket on the same line. We know that prev is used to track the "previous" element of the array, so it's a good place to assign x to prev, since x has been processed: prev = x

python coding challenge


python coding challenge

The snowman should be a variable name, by looking at the variables used below we encounter people which hasn't been declared yet and makes sense given its contents.

The droplet bug should be 'name' to keep it consistent with the other present objects. And diamond should be a name of a person, you can peek in the list of possible choices. In the end this name doesn't matter, because the algorithm is only interested in ana.

python coding challenge


python coding challenge

We've encountered this algorithm above, it keeps track of the number of occurrences of each character in S. So we know that the red apple should be a string of characters. Panda should be the keyword for. The moneybag and droplet should both be mp.

python coding challenge


python coding challenge

To solve R we need to quickly analyze the algorithm. For each element in the array, it checks if the element is not equal to 3, if so R is set to True. Basically if the array has any element other than 3, it will return True.

python coding challenge


python coding challenge

Just like that we made it to the 3rd level (Advanced), stay tuned for that! If you enjoyed this post, make sure to join Codr, like and follow me on DEV <3!

Discussion (0)

pic
Editor guide