2

One code liner in Python 🐍

 3 years ago
source link: https://dev.to/willnode/one-code-liner-in-python-1k3l
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Cover image for One code liner in Python 🐍

One code liner in Python 🐍

Sep 8 Originally published at blog.wellosoft.net

・3 min read

During college, my lecturer gave me a task to find the shortest code to create this string using python:

****
***
**
*
**
***
****
Enter fullscreen modeExit fullscreen mode

So how you would do that? Here's a typical code in Python we end up:

for i in range(4, 1, -1):
    print('*' * i)
for i in range(1, 5):
    print('*' * i)
Enter fullscreen modeExit fullscreen mode

For those who wonder, range(a, b, c) creates an iterator loop from a to b by c. the a is inclusive but b is not. If c is 1, you can omit it since it's the default anyway. Thus range(4, 1, -1) generates [4,3,2] while range(1, 5) generates [1,2,3,4].

And then we take that i to multiply with '*' because python multiplication with string causes that string to be repeated i times, e.g. '*' * 3 generates '***'

This is already short in fact 😅 that's really awesome about Python, isn't it?

But wait, there's more! We can reduce our python code into one single for loop:

for i in range(1, 8):
  if (i < 4):
    print('*' * (5 - i))
  else:
    print('*' * (i - 3))
Enter fullscreen modeExit fullscreen mode

What is code does? It transforms [1,2,3,4,5,6,7] to [4,3,2,1,2,3,4] so then we took these values to get multiplied by *. If it takes a while to grasp why I put these values on, I'll try to explain here:

# i < 4
i = 1 => 5 - 1 = 4
i = 2 => 5 - 2 = 3
i = 3 => 5 - 3 = 2
# else (i >= 4)
i = 4 => 4 - 3 = 1
i = 5 => 5 - 3 = 2
i = 6 => 6 - 3 = 3
i = 7 => 7 - 3 = 4
Enter fullscreen modeExit fullscreen mode

Okay, but why do we do this? It seems didn't make our code shorter, right?

That's why we need to do some math here. Enter the absolute function:

How does this absolute function help us? Well, with some fitting on the equation, we get this graph:

(click the image to see interactive version)

This graph does what we want, transforms [1,2,3,4,5,6,7] to [4,3,2,1,2,3,4] with a single equation. Math is powerful đŸ’Ș.

This is the code now looks like after we implement the equation:

for i in range(1, 8):
  print('*' * (abs(i - 4) + 1))
Enter fullscreen modeExit fullscreen mode

Luckily python has abs() built-in without importing any modules. This reduces our code lines from 5 to 2 đŸ’Ș.

But does this can get further decreased to 1 line? Yes đŸ˜±

But before that, we have to put the print() function outside the loop. This can be done with a temporary variable that holds the whole string in a loop:

output = []
for i in range(1, 8):
  output.append('*' * (abs(i - 4) + 1))
print('\n'.join(output))
Enter fullscreen modeExit fullscreen mode

Now we hold the loop value to a variable named output and use that to print output once. Note that we use '\n'.join(output) which converts the array to string with \n between values. \n simply means a new line, for each string, we add during for loop.

But why this makes our code more verbose? Enter Python's killer feature: List Comprehension.

List Comprehension simply means doing a loop within a single line. Effectively makes a loop became a python expression. What this means for a layman is they allow us to convert this code:

variable = []
for item in list:
  variable.append(expression(item))
Enter fullscreen modeExit fullscreen mode
variable = [expression(item) for item in list]
Enter fullscreen modeExit fullscreen mode

Three lines into one! Doesn't that awesome? 😎

This is our final code after the list comprehension:

print('\n'.join(['*' * (abs(i - 4) + 1) for i in range(1, 8)]))
Enter fullscreen modeExit fullscreen mode

That's really short, isn't it? Of course, this code is not readable as we are beginning with 😅 but hey, we do this for fun đŸ’Ș also these pieces of stuff are also useful for machine code optimization (if you're into that).

Anyway, if you're familiar with code golfing, you can get the lowest of all codes within 55 characters in Python by removing unneeded whitespace:

print('\n'.join(['*'*(abs(i-4)+1)for i in range(1,8)]))
Enter fullscreen modeExit fullscreen mode

Anyone can beat that? 😅 Hope this useful đŸ’Ș


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK