DEV Community

Cover image for Code that replicates itself(not recursion)
Shuvo
Shuvo

Posted on

Code that replicates itself(not recursion)

This thing called virus is crazy as hell, right? Like some dude out of nowhere got infected by COVID-19 virus. Now the world is going through this huge pandemic. That is due to their ability to replicate.
Computer virus

The situation is similar with digital virus aka computer virus. If somehow just one virus can enter your computer, in right condition it will create millions of copies of itself.
So what is a computer virus? A computer is just some code that has the ability to replicate itself. Code that can output itself is called Quine.
However a code that simply just reads itself is not a Quine.

#not a quine
print(open(__file__).read())
Enter fullscreen mode Exit fullscreen mode

Code for a quine is made out of two parts:

  1. code used to do the printing
  2. data representing textual form of the code

Here's an example of quine in Python

q='q=%r;print (q%%q)';print (q%q)

#output: q='q=%r;print (q%%q)';print (q%q)
Enter fullscreen mode Exit fullscreen mode

And in JS:

($=_=>`($=${$})()`)()

//output in console: ($=_=>`($=${$})()`)()
Enter fullscreen mode Exit fullscreen mode

You can find more examples of quine by following these links: JavaScript Quines, Python Quines

That's all for now. Make sure you check out my other articles and YT tutorials.

0shuvo0 image




Discussion (0)