Skip to main content
Code Review

Return to Answer

Code quote identifiers
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325

A few quick comments:

  • Indeed, your solution doesn’t require \$N\$N to be odd. You’ve solved the slightly more general problem of finding a lonely integer in an array where every other element occurs at least twice, but could occur more often than that.

    Likewise, the restriction that \$N \leq 100\$N ≤ 100 isn’t used, but I can’t think of how that could be used in a solution.

  • I don’t know why you’ve defined a histogram()histogram() function, when you could swap it out for Counter()Counter(). That will make your code a little easier to read, because when I read Counter()Counter(), I immediately know what it does; if I see histogram()histogram() then I have to check I’ve understood the definition.

  • When you iterate over the histogram, you should use .iteritems() instead of .items(); in Python 2.7 the latter is slower and more memory-expensive.

  • You don’t need an explicit return None; Python will automatically return NoneNone if it drops out the end of a function.

  • Rather than iterating over the tuples in Counter(array).items() and picking out the element/frequency by numeric index, it would be better to use tuple unpacking in the forfor loop:

     for elem, frequency in histogram(ary).items():
     if frequency == 1:
     return elem
    

    You could also just use the most_common() method on Counter(), and take the last element of that – but at that point you’re barely doing any work, so that might not be in the spirit of the problem.

You could also just use the most_common() method of Counter, and take the last element of that – but at that point you’re barely doing any work, so that might not be in the spirit of the problem.

A few quick comments:

  • Indeed, your solution doesn’t require \$N\$ to be odd. You’ve solved the slightly more general problem of finding a lonely integer in an array where every other element occurs at least twice, but could occur more often than that.

    Likewise, the restriction that \$N \leq 100\$ isn’t used, but I can’t think of how that could be used in a solution.

  • I don’t know why you’ve defined a histogram() function, when you could swap it out for Counter(). That will make your code a little easier to read, because when I read Counter(), I immediately know what it does; if I see histogram() then I have to check I’ve understood the definition.

  • When you iterate over the histogram, you should use .iteritems() instead of .items(); in Python 2.7 the latter is slower and more memory-expensive.

  • You don’t need an explicit return None; Python will automatically return None if it drops out the end of a function.

  • Rather than iterating over the tuples in Counter(array).items() and picking out the element/frequency by numeric index, it would be better to use tuple unpacking in the for loop:

     for elem, frequency in histogram(ary).items():
     if frequency == 1:
     return elem
    

    You could also just use the most_common() method on Counter(), and take the last element of that – but at that point you’re barely doing any work, so that might not be in the spirit of the problem.

A few quick comments:

  • Indeed, your solution doesn’t require N to be odd. You’ve solved the slightly more general problem of finding a lonely integer in an array where every other element occurs at least twice, but could occur more often than that.

    Likewise, the restriction that N ≤ 100 isn’t used, but I can’t think of how that could be used in a solution.

  • I don’t know why you’ve defined a histogram() function, when you could swap it out for Counter(). That will make your code a little easier to read, because when I read Counter(), I immediately know what it does; if I see histogram() then I have to check I’ve understood the definition.

  • When you iterate over the histogram, you should use .iteritems() instead of .items(); in Python 2.7 the latter is slower and more memory-expensive.

  • You don’t need an explicit return None; Python will automatically return None if it drops out the end of a function.

  • Rather than iterating over the tuples in Counter(array).items() and picking out the element/frequency by numeric index, it would be better to use tuple unpacking in the for loop:

     for elem, frequency in histogram(ary).items():
     if frequency == 1:
     return elem
    

You could also just use the most_common() method of Counter, and take the last element of that – but at that point you’re barely doing any work, so that might not be in the spirit of the problem.

Source Link
alexwlchan
  • 8.7k
  • 1
  • 23
  • 55

A few quick comments:

  • Indeed, your solution doesn’t require \$N\$ to be odd. You’ve solved the slightly more general problem of finding a lonely integer in an array where every other element occurs at least twice, but could occur more often than that.

    Likewise, the restriction that \$N \leq 100\$ isn’t used, but I can’t think of how that could be used in a solution.

  • I don’t know why you’ve defined a histogram() function, when you could swap it out for Counter(). That will make your code a little easier to read, because when I read Counter(), I immediately know what it does; if I see histogram() then I have to check I’ve understood the definition.

  • When you iterate over the histogram, you should use .iteritems() instead of .items(); in Python 2.7 the latter is slower and more memory-expensive.

  • You don’t need an explicit return None; Python will automatically return None if it drops out the end of a function.

  • Rather than iterating over the tuples in Counter(array).items() and picking out the element/frequency by numeric index, it would be better to use tuple unpacking in the for loop:

     for elem, frequency in histogram(ary).items():
     if frequency == 1:
     return elem
    

    You could also just use the most_common() method on Counter(), and take the last element of that – but at that point you’re barely doing any work, so that might not be in the spirit of the problem.

lang-py

AltStyle によって変換されたページ (->オリジナル) /