You're framing your question as a test of whether the resulting code is an original composition or a derivative work for copyright violation purposes. It may be that less than or greater than 4 bars for a piece of music would be taken as an indication in some legal jurisdictions, but I'm not aware of any such guidelines for software, or indeed written work generally.
However, in the software world it's usually much more clear cut because of licensing. Most software will carry a copyright header that refers to the license or be bundled with it. And unlike music, there's an awful lot of software that is explicitly licensed to allow you to use it as you wish, including changing it to suit your purposes (usually requiring only that you credit those who went before).
Rather than trying to walk a non-existent tightrope looking for tests that, even if they exist in Sweden, could still leave you open to legal action from other jurisdictions, just check the license and if it say it's OK to use it do. If it says it's not, base your work on something that you are allowed to use this way.