Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

##Remarks on the Code

Remarks on the Code

Alternative Implemenation

##Alternative Implemenation TheThe problem can be solved with the following code utilizing Python's Standard Library. There is no guarantee that the code demonstrates what the question was designed to measure.

##Semi-Relevant Rambling

Semi-Relevant Rambling

Remarks on Interview Coding Questions

###Remarks on Interview Coding Questions Interview coding questions are typically selected for purposes unique to interviews:

find_index as an interview question

###find_index as an interview question BecauseBecause the specification of find_index allows for a range of implementations it is probably not a FizzBuzz. It is most likely a How much relevant technical experience does the candidate have? type question.

##Remarks on the Code

##Alternative Implemenation The problem can be solved with the following code utilizing Python's Standard Library. There is no guarantee that the code demonstrates what the question was designed to measure.

##Semi-Relevant Rambling

###Remarks on Interview Coding Questions Interview coding questions are typically selected for purposes unique to interviews:

###find_index as an interview question Because the specification of find_index allows for a range of implementations it is probably not a FizzBuzz. It is most likely a How much relevant technical experience does the candidate have? type question.

Remarks on the Code

Alternative Implemenation

The problem can be solved with the following code utilizing Python's Standard Library. There is no guarantee that the code demonstrates what the question was designed to measure.

Semi-Relevant Rambling

Remarks on Interview Coding Questions

Interview coding questions are typically selected for purposes unique to interviews:

find_index as an interview question

Because the specification of find_index allows for a range of implementations it is probably not a FizzBuzz. It is most likely a How much relevant technical experience does the candidate have? type question.

Bounty Awarded with 50 reputation awarded by NinjaG
Post Undeleted by ben rudgers
added 270 characters in body
Source Link
ben rudgers
  • 2.7k
  • 13
  • 23

##Remarks on the Code

[] The use of assert demonstrates the understanding data validation is important. It is a clear strength of the code.

[] The line if pattern == '': is a bit weaker. In part because if pattern: is equivalent. In part because if the reason for using pattern == '' is to express intent, then pattern == '' or pattern == "" more clearly expresses that intent.

[] The code phrase:

if pattern == '':
 return 0
text_index = 0

is probably better as

text_index = 0
if pattern == '':
 return text_index

[] Because the specification of the problem does not define what should happen in when pattern == '', providing an explicit behavior may be a problem. Comments might help people notice that there is an explicit behavior or that the explicit behavior is different from what they expect.

[] A docstring would be a reasonable (and probably better) place to describe the behavior when pattern == '' because docstrings are accessible without examining the source code and ordinarily used by automated documentation tools.

[] Although it is possible to iterate through Python strings as if they were arrays, it is generally better to use the methods of <class str> when possible. Iterating through a string as if it were an array makes Python code look like **old C **code (old because strstr has been part of the C Standard Library since ANSI C or C89).

[] Having unit tests for code that you have written is a strong point of the code. It reflects positively on your approach to programming.

##Alternative Implemenation The problem can be solved with the following code utilizing Python's Standard Library. There is no guarantee that the code demonstrates what the question was designed to measure.

1: def find_index(text, pattern):
2: output = None
3: try:
4: output = text.index(pattern)
5: except ValueError:
6: pass
7: return output

2: The use of output provides a clear name for the role of the variable within the function. Assigning it to None makes the default behavior explicit.

3: A try:except block is "the trick" to using the Python Standard Library and returning None if the value is not found. Using try:except blocks is not guaranteed to be what the question was designed to ask (for example if when the question's purpose is to determine ability to iterate on a string).

5: The only way to know that index raises ValueError when the substring is not found is from the documentation/experiment. Note that this handles no other exception (example: TypeError raised by 'abc'.index(1)). Additional errors can be handled similarly.

7: The function only returns in one location. That location is at the end, which is at least as good as another location in the code.

##Semi-Relevant Rambling

###Remarks on Interview Coding Questions Interview coding questions are typically selected for purposes unique to interviews:

###Remarks on this###find_index as an interview question Because the specification of find_index allows for a range of implementations it is probably not a FizzBuzz. It is most likely a How much relevant technical experience does the candidate have? type question.

###Remarks on the Code

[] The useOne ofassert demonstrates the understanding data validation is important. It is a clear strengthfeatures of the code.

[] The line if pattern == '': is a bit weaker. In part because if pattern:find_index is equivalent. In part because ifthat it will reflect the reason for usingcandidate's knowledge of the Python pattern == '' is to express intent, thenbuilt-in functions much as pattern == '' or pattern == "" more clearly expresses that intenta similar question in C might reflect a candidate's knowledge of C's standard library.

[] The code phrase:

if pattern == '':
 return 0
text_index = 0

is probably better as

text_index = 0
if pattern == '':
 return text_index

[] BecauseSome candidates will start solving the specification ofproblem by writing code. Some candidates will start solving the problem does not define what should happen in whenby looking at pattern == '', providing an explicit behavior may be a problemPython's string module. Comments might help people notice that there is an explicit behavior or thatDepending on what the explicit behaviorinterviewer is different from what they expect.

[] A docstring wouldlooking for, either approach might be an even better place to describe the behavior whenpreferred. For example, pattern == ''find_index because docstrings are accessible without examining the source codemight be a FizzBuzz type question on iteration in a whiteboard context and ordinarily used by automated documentation toolsa "relevant technical experience" question for Python's Standard Library in a take-home context.

###Alternative Implemenation The code can be simplified to:

1: def find_index(text, pattern):
2: output = None
3: try:
4: output = text.index(pattern)
5: except ValueError:
6: pass
7: return output

###Remarks on Interview Coding Questions Interview coding questions are typically selected for purposes unique to interviews:

###Remarks on this question Because the specification of find_index allows for a range of implementations it is probably not a FizzBuzz. It is most likely a How much relevant technical experience does the candidate have? type question.

###Remarks on the Code

[] The use ofassert demonstrates the understanding data validation is important. It is a clear strength of the code.

[] The line if pattern == '': is a bit weaker. In part because if pattern: is equivalent. In part because if the reason for using pattern == '' is to express intent, then pattern == '' or pattern == "" more clearly expresses that intent.

[] The code phrase:

if pattern == '':
 return 0
text_index = 0

is probably better as

text_index = 0
if pattern == '':
 return text_index

[] Because the specification of the problem does not define what should happen in when pattern == '', providing an explicit behavior may be a problem. Comments might help people notice that there is an explicit behavior or that the explicit behavior is different from what they expect.

[] A docstring would be an even better place to describe the behavior when pattern == '' because docstrings are accessible without examining the source code and ordinarily used by automated documentation tools.

###Alternative Implemenation The code can be simplified to:

1: def find_index(text, pattern):
2: output = None
3: try:
4: output = text.index(pattern)
5: except ValueError:
6: pass
7: return output

##Remarks on the Code

[] The use of assert demonstrates the understanding data validation is important. It is a clear strength of the code.

[] The line if pattern == '': is a bit weaker. In part because if pattern: is equivalent. In part because if the reason for using pattern == '' is to express intent, then pattern == '' or pattern == "" more clearly expresses that intent.

[] The code phrase:

if pattern == '':
 return 0
text_index = 0

is probably better as

text_index = 0
if pattern == '':
 return text_index

[] Because the specification of the problem does not define what should happen in when pattern == '', providing an explicit behavior may be a problem. Comments might help people notice that there is an explicit behavior or that the explicit behavior is different from what they expect.

[] A docstring would be a reasonable (and probably better) place to describe the behavior when pattern == '' because docstrings are accessible without examining the source code and ordinarily used by automated documentation tools.

[] Although it is possible to iterate through Python strings as if they were arrays, it is generally better to use the methods of <class str> when possible. Iterating through a string as if it were an array makes Python code look like **old C **code (old because strstr has been part of the C Standard Library since ANSI C or C89).

[] Having unit tests for code that you have written is a strong point of the code. It reflects positively on your approach to programming.

##Alternative Implemenation The problem can be solved with the following code utilizing Python's Standard Library. There is no guarantee that the code demonstrates what the question was designed to measure.

1: def find_index(text, pattern):
2: output = None
3: try:
4: output = text.index(pattern)
5: except ValueError:
6: pass
7: return output

2: The use of output provides a clear name for the role of the variable within the function. Assigning it to None makes the default behavior explicit.

3: A try:except block is "the trick" to using the Python Standard Library and returning None if the value is not found. Using try:except blocks is not guaranteed to be what the question was designed to ask (for example if when the question's purpose is to determine ability to iterate on a string).

5: The only way to know that index raises ValueError when the substring is not found is from the documentation/experiment. Note that this handles no other exception (example: TypeError raised by 'abc'.index(1)). Additional errors can be handled similarly.

7: The function only returns in one location. That location is at the end, which is at least as good as another location in the code.

##Semi-Relevant Rambling

###Remarks on Interview Coding Questions Interview coding questions are typically selected for purposes unique to interviews:

###find_index as an interview question Because the specification of find_index allows for a range of implementations it is probably not a FizzBuzz. It is most likely a How much relevant technical experience does the candidate have? type question.

One of the features of find_index is that it will reflect the candidate's knowledge of the Python built-in functions much as a similar question in C might reflect a candidate's knowledge of C's standard library.

Some candidates will start solving the problem by writing code. Some candidates will start solving the problem by looking at Python's string module. Depending on what the interviewer is looking for, either approach might be preferred. For example, find_index might be a FizzBuzz type question on iteration in a whiteboard context and a "relevant technical experience" question for Python's Standard Library in a take-home context.

added 270 characters in body
Source Link
ben rudgers
  • 2.7k
  • 13
  • 23
  • The use of assert demonstrates the understanding data validation is important. It is a clear strength of the code.

  • The line if pattern == '': is a bit weaker. In part because if pattern: is equivalent. In part because if the reason for using pattern == '' is to express intent, then pattern == '' or pattern == "" more clearly expresses that intent.

  • The code:

    return 0text_index = 0

[] The use of assert demonstrates the understanding data validation is important. It is a clear strength of the code.

[] The line if pattern == '': is a bit weaker. In part because if pattern: is equivalent. In part because if the reason for using pattern == '' is to express intent, then pattern == '' or pattern == "" more clearly expresses that intent.

[] The code phrase:

if pattern == '':
 return 0
text_index = 0

is probably better as

text_index = 0
if pattern == '':
 return text_index

[] Because the specification of the problem does not define what should happen in when pattern == '', providing an explicit behavior may be a problem. Comments might help people notice that there is an explicit behavior or that the explicit behavior is different from what they expect.

[] A docstring would be an even better place to describe the behavior when pattern == '' because docstrings are accessible without examining the source code and ordinarily used by automated documentation tools.

  • The use of assert demonstrates the understanding data validation is important. It is a clear strength of the code.

  • The line if pattern == '': is a bit weaker. In part because if pattern: is equivalent. In part because if the reason for using pattern == '' is to express intent, then pattern == '' or pattern == "" more clearly expresses that intent.

  • The code:

    return 0text_index = 0

[] The use of assert demonstrates the understanding data validation is important. It is a clear strength of the code.

[] The line if pattern == '': is a bit weaker. In part because if pattern: is equivalent. In part because if the reason for using pattern == '' is to express intent, then pattern == '' or pattern == "" more clearly expresses that intent.

[] The code phrase:

if pattern == '':
 return 0
text_index = 0

is probably better as

text_index = 0
if pattern == '':
 return text_index

[] Because the specification of the problem does not define what should happen in when pattern == '', providing an explicit behavior may be a problem. Comments might help people notice that there is an explicit behavior or that the explicit behavior is different from what they expect.

[] A docstring would be an even better place to describe the behavior when pattern == '' because docstrings are accessible without examining the source code and ordinarily used by automated documentation tools.

added 270 characters in body
Source Link
ben rudgers
  • 2.7k
  • 13
  • 23
Loading
added 270 characters in body
Source Link
ben rudgers
  • 2.7k
  • 13
  • 23
Loading
added 270 characters in body
Source Link
ben rudgers
  • 2.7k
  • 13
  • 23
Loading
Post Deleted by ben rudgers
Source Link
ben rudgers
  • 2.7k
  • 13
  • 23
Loading
lang-py

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