Nerd For Tech
Published in

Nerd For Tech

Write Up Code Wars — Gravity Flip Solutions Python

Code Wars is best platform for learning algorithm from programming language, here I want to write the solution for ‘Gravity Flip’ challenge using Python. When it was written, the challenge is in kyu 8.

Well, don’t fell disappointed about the kyu 8. Last year, I was really love JavaScript and still love it. But, somehow I feel that Data Science will have bigger chance in 5 years to go. So, I turned my sail into Python language. As an intro into Data Science.

The recent I share this write up, because this challenge give me very basic concept from Python, but yeah it’s important and also I had a motto like this.

You never understand something until you can teach it into a child

So let’s get into it!


Bob is bored during his physics lessons so he's built himself a toy box to help pass the time. The box is special because it has the ability to change gravity.There are some columns of toy cubes in the box arranged in a line. The i-th column contains a_i cubes. At first, the gravity in the box is pulling the cubes downwards. When Bob switches the gravity, it begins to pull all the cubes to a certain side of the box, d, which can be either 'L' or 'R' (left or right). Below is an example of what a box of cubes might look like before and after switching gravity.
Given the initial configuration of the cubes in the box, find out how many cubes are in each of the n columns after Bob switches the gravity.


flip('R', [3, 2, 1, 2])     =>  [1, 2, 2, 3]
flip('L', [1, 4, 5, 3, 5]) => [5, 5, 4, 3, 1]


Yep, it just need two lines of code to solve it. My understanding in JavaScript help a lot in the concept. But somehow the syntax, sometimes annoying me, but no problem — I learn a lot.

As you can see, there are two basic concept that I used in the program, the sorting algorithm and ternary operator.

Sorting Algorithm

By solving this kyu, I just understand, that there are two function to sorting a list in Python. It’s sort() and sorted(). What makes this two different is that sort() changes the list directly without returning any value, while sorted() doesn’t change the list but returns the sorted list.

So when you using sort(), like the picture below

The output will be none until you call it. Because the process is lies in the backend (system). But when you using sorted(), like the picture below

The output will be printed directly after the command executed, but the original list will never being affected.

FYI, both of the function have two parameter that maybe will useful in your project.

  • reverse — If the value is True, the sorted list will be reversed (Descending order)
  • key — It will useful for sorting big information like JSON. Because it works as a keyword.

Ternary Operator

Ask me one years ago, about this and I will said, what is that? 😂. But well, six month had been passed since I learn JavaScript. I really love this, because ternary make the function more simple. Instead of build really big nested loop that takes many lines. You can finish doing it in one line only.

Ternary operator is a conditional operators that evaluate somethings, whether it’s True or False. What makes it special, it takes only one line. Trust me, it will make you look smarter. In Python the syntax is different with JavaScript, but it still understandable

[True_statement] if [expression] else [False_statement]

As you can see, my answer is written like this

return sorted(arr) if text == 'R' else sorted(arr, reverse=True)

So, in long answer it will look like this

if text == 'R':
return sorted(arr)
return sorted(arr, reverse=True)

As far as I know, we can’t return any ‘If condition’, but we can do it in ternary operator. So instead got four lines, I finish the job in one line only.


Here we come into the conclusion, we had written simple program that contain sorting algorithm and ternary operator. I know it was really simple, but did you know above sky is still a sky, so I found more clean answer, and vote it as the Best Practices.

def flip(d,a):
return sorted(a, reverse=d=='L')

It more simpler right?, thanks for reading until the end.

Have a Nice Code ✌

Link for my Python Write-Up Collection



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Handhika Yanuar Pratama

Handhika Yanuar Pratama

A Stoic Developer || Junior Data Engineer || Dreamer