Which of the following five Python scripts have been derived from the same source?
Common themes:
- Iterating over
itertools.combinations()
: all files.
- Passing the generator
range(n)
as the iterable: all files.
- Data structure of returned value:
- List:
file1.py
-- more or less unique.
- Set:
file2.py
, file3.py
, file4.py
, file5.py
- Input sanitization:
file3.py
, file4.py
- Improper input sanitization:
file4.py
: checking if both parameters n
and k
are None
? A bit silly.
From this, we suspect both file1.py
and file3.py
to be original.
Which files should include an acknowledgment?
I suspect file2.py
, file4.py
, and maybe file5.py
should include an acknowledgment. Their implementations are very similar to file3.py
's and, given how many ways the method could have been written, their edit distances are way too close.
What form should an acknowledgment take? Provide an example.
If, for example, file4.py
didn't know how to properly sanitize input, they could have written:
def bits(n=None, k=None):
# NOTE: I had difficulty sanitizing valid input values, and received some assistance
# from the code at https://github.com/nyu-cds/quiz/blob/master/file3.py.
#First, validate the input values
# if k or n is not an integer, return error message
if int(n) != n or int(k) != k:
raise ValueError("k and n should be integers")
# if n <= 0, return error message
if n <= 0:
raise ValueError("n should not be negative or zero")
# if k < 0, return error message
if k < 0:
raise ValueError("k should not be negative")
res = set()
# use combinations to get the positions of "0"
for tuple in combinations(range(n), k):
temp = ["1"] * n
for i in tuple:
temp[i] = "0"
res.add("".join(temp))
return res
which is a full acknowledgment of (a) issue the user faced and (b) proper credit to the source.
Note: none of these zbits
programs are particularly well-done and, at a high level, they are all candidates of deriving from the same source. They all use itertools.combinations
, a list of length n
of 1's, etc — not particularly unique. A better understanding of source derivatives would be to use timestamps to see commit history relative to one-another.