The code duplication reducing in the linked list implementationImplement LinkedList class in pythonOptimizing iteration through linked listsSingly linked list implementationLinked list C implementationPython Linked List implementationPython Singly Linked List ImplementationDoubly-linked list implementationLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)Linked List implementation in PythonImplementation of Singly Linked ListLRUCache for integers using dict + linkedlist
What to do when eye contact makes your subordinate uncomfortable?
This is why we puzzle
Fear of getting stuck on one programming language / technology that is not used in my country
Biological Blimps: Propulsion
How could a planet have erratic days?
Probability that THHT occurs in a sequence of 10 coin tosses
A binary search solution to 3Sum
How can I write humor as character trait?
When were female captains banned from Starfleet?
Why does the Sun have different day lengths, but not the gas giants?
Are these expressions not equal? Mathematica output is ambiguous
Add big quotation marks inside my colorbox
How to align my equation to left?
X marks the what?
Pre-mixing cryogenic fuels and using only one fuel tank
Non-trope happy ending?
Mimic lecturing on blackboard, facing audience
What is Cash Advance APR?
Temporarily disable WLAN internet access for children, but allow it for adults
How do you make your own symbol when Detexify fails?
Did arcade monitors have same pixel aspect ratio as TV sets?
How to get directions in deep space?
What is the English pronunciation of "pain au chocolat"?
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
The code duplication reducing in the linked list implementation
Implement LinkedList class in pythonOptimizing iteration through linked listsSingly linked list implementationLinked list C implementationPython Linked List implementationPython Singly Linked List ImplementationDoubly-linked list implementationLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)Linked List implementation in PythonImplementation of Singly Linked ListLRUCache for integers using dict + linkedlist
$begingroup$
I am writing the linked list data structure, using this question's requirements (the methods set).
I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.
The question:
- Do you see another way to duplication elimination?
- I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?
Before refactoring
def insert_before_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr # differs
prev.next = new_node # differs
else:
new_node.next = self.head # differs
self.head = new_node # differs
def insert_after_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr.next # differs
curr.next = new_node # differs
else:
new_node.next = self.head.next # differs
self.head.next = new_node # differs
After refactoring
# Common part was moved to this function
def insert(self, key, value, with_prev, non_prev):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
with_prev(prev, curr, new_node)
else:
non_prev(prev, curr, new_node)
# put two little chunks of code to the nested functions
# and pass them to the "insert" function, that implement the logic
def insert_before_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr
prev.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head
self.head = new_node
self.insert(key, value, with_prev, non_prev)
def insert_after_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr.next
curr.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head.next
self.head.next = new_node
self.insert(key, value, with_prev, non_prev)
python-3.x linked-list
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I am writing the linked list data structure, using this question's requirements (the methods set).
I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.
The question:
- Do you see another way to duplication elimination?
- I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?
Before refactoring
def insert_before_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr # differs
prev.next = new_node # differs
else:
new_node.next = self.head # differs
self.head = new_node # differs
def insert_after_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr.next # differs
curr.next = new_node # differs
else:
new_node.next = self.head.next # differs
self.head.next = new_node # differs
After refactoring
# Common part was moved to this function
def insert(self, key, value, with_prev, non_prev):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
with_prev(prev, curr, new_node)
else:
non_prev(prev, curr, new_node)
# put two little chunks of code to the nested functions
# and pass them to the "insert" function, that implement the logic
def insert_before_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr
prev.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head
self.head = new_node
self.insert(key, value, with_prev, non_prev)
def insert_after_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr.next
curr.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head.next
self.head.next = new_node
self.insert(key, value, with_prev, non_prev)
python-3.x linked-list
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I am writing the linked list data structure, using this question's requirements (the methods set).
I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.
The question:
- Do you see another way to duplication elimination?
- I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?
Before refactoring
def insert_before_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr # differs
prev.next = new_node # differs
else:
new_node.next = self.head # differs
self.head = new_node # differs
def insert_after_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr.next # differs
curr.next = new_node # differs
else:
new_node.next = self.head.next # differs
self.head.next = new_node # differs
After refactoring
# Common part was moved to this function
def insert(self, key, value, with_prev, non_prev):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
with_prev(prev, curr, new_node)
else:
non_prev(prev, curr, new_node)
# put two little chunks of code to the nested functions
# and pass them to the "insert" function, that implement the logic
def insert_before_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr
prev.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head
self.head = new_node
self.insert(key, value, with_prev, non_prev)
def insert_after_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr.next
curr.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head.next
self.head.next = new_node
self.insert(key, value, with_prev, non_prev)
python-3.x linked-list
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I am writing the linked list data structure, using this question's requirements (the methods set).
I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.
The question:
- Do you see another way to duplication elimination?
- I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?
Before refactoring
def insert_before_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr # differs
prev.next = new_node # differs
else:
new_node.next = self.head # differs
self.head = new_node # differs
def insert_after_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
new_node.next = curr.next # differs
curr.next = new_node # differs
else:
new_node.next = self.head.next # differs
self.head.next = new_node # differs
After refactoring
# Common part was moved to this function
def insert(self, key, value, with_prev, non_prev):
new_node = Node(value)
prev, curr = self._find_key(key)
if curr:
if prev:
with_prev(prev, curr, new_node)
else:
non_prev(prev, curr, new_node)
# put two little chunks of code to the nested functions
# and pass them to the "insert" function, that implement the logic
def insert_before_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr
prev.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head
self.head = new_node
self.insert(key, value, with_prev, non_prev)
def insert_after_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr.next
curr.next = new_node
def non_prev(prev, curr, new_node):
new_node.next = self.head.next
self.head.next = new_node
self.insert(key, value, with_prev, non_prev)
python-3.x linked-list
python-3.x linked-list
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 5 mins ago
MiniMaxMiniMax
1463
1463
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216018%2fthe-code-duplication-reducing-in-the-linked-list-implementation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216018%2fthe-code-duplication-reducing-in-the-linked-list-implementation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown