Finding third largest element in the list without using len() and sort()
Here is the code which I used to find the third largest element in the list without using any built in functions like max,sort,len.
list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
#list = [35,10,45,9,8,5]
largest_1 = list[0]
largest_2 = list[0]
largest_3 = list[0]
print (largest_1)
print (largest_2)
print (largest_3)
for each in list:
print ('Each element Before if Loop --->',each)
if each > largest_1:
print ('Each element inside if loop --->',each)
largest_1 = each
print('largest_1 element---->',largest_1)
elif largest_2 != largest_1 and largest_2 < each:
print ('Each element inside if loop --->',each)
largest_2 = each
print ('Largest_1 element is ---->',largest_1)
print ('Largest_2 element is ---->',largest_2)
elif largest_3 != largest_2 and largest_3 < each:
print ('Each element inside if loop --->',each)
largest_3 = each
print ('Largest_2 element is ---->',largest_2)
print ('Largest_3 element is ---->',largest_3)
print (largest_1)
print (largest_2)
print (largest_3)
The same code is not working for the
list = [35,10,45,9,8,5]
I am not getting what mistake I have done. How can I fix this?
python list
|
show 1 more comment
Here is the code which I used to find the third largest element in the list without using any built in functions like max,sort,len.
list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
#list = [35,10,45,9,8,5]
largest_1 = list[0]
largest_2 = list[0]
largest_3 = list[0]
print (largest_1)
print (largest_2)
print (largest_3)
for each in list:
print ('Each element Before if Loop --->',each)
if each > largest_1:
print ('Each element inside if loop --->',each)
largest_1 = each
print('largest_1 element---->',largest_1)
elif largest_2 != largest_1 and largest_2 < each:
print ('Each element inside if loop --->',each)
largest_2 = each
print ('Largest_1 element is ---->',largest_1)
print ('Largest_2 element is ---->',largest_2)
elif largest_3 != largest_2 and largest_3 < each:
print ('Each element inside if loop --->',each)
largest_3 = each
print ('Largest_2 element is ---->',largest_2)
print ('Largest_3 element is ---->',largest_3)
print (largest_1)
print (largest_2)
print (largest_3)
The same code is not working for the
list = [35,10,45,9,8,5]
I am not getting what mistake I have done. How can I fix this?
python list
1
Possible duplicate of Finding the Kth Largest element in a Python List using recursion
– Ganesh K
Nov 25 '18 at 4:47
@GaneshK, Hey buddy i dont want to use any max in my code. Its not a duplicate
– venkat
Nov 25 '18 at 4:49
Your question does not state that anywhere.
– Ganesh K
Nov 25 '18 at 4:52
@venkat You can't use any libraries?
– RoadRunner
Nov 25 '18 at 4:54
1
@venkat put yourlargest_2 and largest_3
equal to0
and you're done!
– Sandesh34
Nov 25 '18 at 4:58
|
show 1 more comment
Here is the code which I used to find the third largest element in the list without using any built in functions like max,sort,len.
list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
#list = [35,10,45,9,8,5]
largest_1 = list[0]
largest_2 = list[0]
largest_3 = list[0]
print (largest_1)
print (largest_2)
print (largest_3)
for each in list:
print ('Each element Before if Loop --->',each)
if each > largest_1:
print ('Each element inside if loop --->',each)
largest_1 = each
print('largest_1 element---->',largest_1)
elif largest_2 != largest_1 and largest_2 < each:
print ('Each element inside if loop --->',each)
largest_2 = each
print ('Largest_1 element is ---->',largest_1)
print ('Largest_2 element is ---->',largest_2)
elif largest_3 != largest_2 and largest_3 < each:
print ('Each element inside if loop --->',each)
largest_3 = each
print ('Largest_2 element is ---->',largest_2)
print ('Largest_3 element is ---->',largest_3)
print (largest_1)
print (largest_2)
print (largest_3)
The same code is not working for the
list = [35,10,45,9,8,5]
I am not getting what mistake I have done. How can I fix this?
python list
Here is the code which I used to find the third largest element in the list without using any built in functions like max,sort,len.
list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
#list = [35,10,45,9,8,5]
largest_1 = list[0]
largest_2 = list[0]
largest_3 = list[0]
print (largest_1)
print (largest_2)
print (largest_3)
for each in list:
print ('Each element Before if Loop --->',each)
if each > largest_1:
print ('Each element inside if loop --->',each)
largest_1 = each
print('largest_1 element---->',largest_1)
elif largest_2 != largest_1 and largest_2 < each:
print ('Each element inside if loop --->',each)
largest_2 = each
print ('Largest_1 element is ---->',largest_1)
print ('Largest_2 element is ---->',largest_2)
elif largest_3 != largest_2 and largest_3 < each:
print ('Each element inside if loop --->',each)
largest_3 = each
print ('Largest_2 element is ---->',largest_2)
print ('Largest_3 element is ---->',largest_3)
print (largest_1)
print (largest_2)
print (largest_3)
The same code is not working for the
list = [35,10,45,9,8,5]
I am not getting what mistake I have done. How can I fix this?
python list
python list
edited Dec 7 '18 at 20:31
halfer
14.6k758113
14.6k758113
asked Nov 25 '18 at 4:45
venkatvenkat
3181220
3181220
1
Possible duplicate of Finding the Kth Largest element in a Python List using recursion
– Ganesh K
Nov 25 '18 at 4:47
@GaneshK, Hey buddy i dont want to use any max in my code. Its not a duplicate
– venkat
Nov 25 '18 at 4:49
Your question does not state that anywhere.
– Ganesh K
Nov 25 '18 at 4:52
@venkat You can't use any libraries?
– RoadRunner
Nov 25 '18 at 4:54
1
@venkat put yourlargest_2 and largest_3
equal to0
and you're done!
– Sandesh34
Nov 25 '18 at 4:58
|
show 1 more comment
1
Possible duplicate of Finding the Kth Largest element in a Python List using recursion
– Ganesh K
Nov 25 '18 at 4:47
@GaneshK, Hey buddy i dont want to use any max in my code. Its not a duplicate
– venkat
Nov 25 '18 at 4:49
Your question does not state that anywhere.
– Ganesh K
Nov 25 '18 at 4:52
@venkat You can't use any libraries?
– RoadRunner
Nov 25 '18 at 4:54
1
@venkat put yourlargest_2 and largest_3
equal to0
and you're done!
– Sandesh34
Nov 25 '18 at 4:58
1
1
Possible duplicate of Finding the Kth Largest element in a Python List using recursion
– Ganesh K
Nov 25 '18 at 4:47
Possible duplicate of Finding the Kth Largest element in a Python List using recursion
– Ganesh K
Nov 25 '18 at 4:47
@GaneshK, Hey buddy i dont want to use any max in my code. Its not a duplicate
– venkat
Nov 25 '18 at 4:49
@GaneshK, Hey buddy i dont want to use any max in my code. Its not a duplicate
– venkat
Nov 25 '18 at 4:49
Your question does not state that anywhere.
– Ganesh K
Nov 25 '18 at 4:52
Your question does not state that anywhere.
– Ganesh K
Nov 25 '18 at 4:52
@venkat You can't use any libraries?
– RoadRunner
Nov 25 '18 at 4:54
@venkat You can't use any libraries?
– RoadRunner
Nov 25 '18 at 4:54
1
1
@venkat put your
largest_2 and largest_3
equal to 0
and you're done!– Sandesh34
Nov 25 '18 at 4:58
@venkat put your
largest_2 and largest_3
equal to 0
and you're done!– Sandesh34
Nov 25 '18 at 4:58
|
show 1 more comment
3 Answers
3
active
oldest
votes
I don't know why you wouldn't want to use len()
, or max()
- they're literally built-in functions, not part of any library, and there's no practical reason not to use them. That said, if you really want to do it otherwise, here's another approach:
Take three variables, assign them largest
, second_largest
, and third_largest
, and walk through the list.
largest = 0
second_largest = 0
third_largest = 0
for each in list:
if each >= largest:
# assign the new largest, and push the rest of them back down the chain
# we use >= instead of > to ensure that duplicate maximums still work.
#
largest, second_largest, third_largest = each, largest, second_largest
elif each >= second_largest:
second_largest, third_largest = each, second_largest
elif each > third_largest:
third_largest = each
print(third_largest)
add a comment |
@venkat: Here another proposal to get the third largest number out of your list without using len()
and sort()
.
def find_largest(alist):
"""
Find the largest number in a list.
Return the largest number found and it index
"""
largest = alist[0]
for item in alist[1:]:
if item > largest:
largest = item
idx = alist.index(largest)
return (idx, largest)
#--
def get_third_largest(alist):
"""
Return the third largest number in a list.
"""
# Let make a copy of the input list so that any change to it may not affect the
# original data.
thisList = alist.copy()
index, largest = 0, 0
for item in range(3):
index, largest = find_largest(thisList)
if item != 2:
# delete the first two largest from the List
del thisList[index]
return largest
# Test of the algorithm
if __name__ == "__main__":
List = [12, 45, 2, 41, 31, 10, 8, 6, 4]
third = get_third_largest(List)
# print("Initial list: ", List)
# print("The third largest item in the list:")
print("tExpected: 31")
print("tResult: %d" % third);
# --- Output---
# Expected: 31
# Result: 31
add a comment |
You could also store your top 3 max numbers in a dictionary, then print out the third largest one:
largest = {"first": 0, "second": 0, "third": 0}
lst = [12, 45, 2, 41, 31, 10, 8, 6, 4]
for number in lst:
if number > largest["first"]:
largest["third"] = largest["second"]
largest["second"] = largest["first"]
largest["first"] = number
elif number > largest["second"]:
largest["third"] = largest["second"]
largest["second"] = number
elif number > largest["third"]:
largest["third"] = number
print(largest)
# {'first': 45, 'second': 41, 'third': 31}
print(largest["third"])
# 31
add a comment |
Your Answer
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
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%2fstackoverflow.com%2fquestions%2f53464722%2ffinding-third-largest-element-in-the-list-without-using-len-and-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't know why you wouldn't want to use len()
, or max()
- they're literally built-in functions, not part of any library, and there's no practical reason not to use them. That said, if you really want to do it otherwise, here's another approach:
Take three variables, assign them largest
, second_largest
, and third_largest
, and walk through the list.
largest = 0
second_largest = 0
third_largest = 0
for each in list:
if each >= largest:
# assign the new largest, and push the rest of them back down the chain
# we use >= instead of > to ensure that duplicate maximums still work.
#
largest, second_largest, third_largest = each, largest, second_largest
elif each >= second_largest:
second_largest, third_largest = each, second_largest
elif each > third_largest:
third_largest = each
print(third_largest)
add a comment |
I don't know why you wouldn't want to use len()
, or max()
- they're literally built-in functions, not part of any library, and there's no practical reason not to use them. That said, if you really want to do it otherwise, here's another approach:
Take three variables, assign them largest
, second_largest
, and third_largest
, and walk through the list.
largest = 0
second_largest = 0
third_largest = 0
for each in list:
if each >= largest:
# assign the new largest, and push the rest of them back down the chain
# we use >= instead of > to ensure that duplicate maximums still work.
#
largest, second_largest, third_largest = each, largest, second_largest
elif each >= second_largest:
second_largest, third_largest = each, second_largest
elif each > third_largest:
third_largest = each
print(third_largest)
add a comment |
I don't know why you wouldn't want to use len()
, or max()
- they're literally built-in functions, not part of any library, and there's no practical reason not to use them. That said, if you really want to do it otherwise, here's another approach:
Take three variables, assign them largest
, second_largest
, and third_largest
, and walk through the list.
largest = 0
second_largest = 0
third_largest = 0
for each in list:
if each >= largest:
# assign the new largest, and push the rest of them back down the chain
# we use >= instead of > to ensure that duplicate maximums still work.
#
largest, second_largest, third_largest = each, largest, second_largest
elif each >= second_largest:
second_largest, third_largest = each, second_largest
elif each > third_largest:
third_largest = each
print(third_largest)
I don't know why you wouldn't want to use len()
, or max()
- they're literally built-in functions, not part of any library, and there's no practical reason not to use them. That said, if you really want to do it otherwise, here's another approach:
Take three variables, assign them largest
, second_largest
, and third_largest
, and walk through the list.
largest = 0
second_largest = 0
third_largest = 0
for each in list:
if each >= largest:
# assign the new largest, and push the rest of them back down the chain
# we use >= instead of > to ensure that duplicate maximums still work.
#
largest, second_largest, third_largest = each, largest, second_largest
elif each >= second_largest:
second_largest, third_largest = each, second_largest
elif each > third_largest:
third_largest = each
print(third_largest)
answered Nov 25 '18 at 4:58
Green Cloak GuyGreen Cloak Guy
2,8941720
2,8941720
add a comment |
add a comment |
@venkat: Here another proposal to get the third largest number out of your list without using len()
and sort()
.
def find_largest(alist):
"""
Find the largest number in a list.
Return the largest number found and it index
"""
largest = alist[0]
for item in alist[1:]:
if item > largest:
largest = item
idx = alist.index(largest)
return (idx, largest)
#--
def get_third_largest(alist):
"""
Return the third largest number in a list.
"""
# Let make a copy of the input list so that any change to it may not affect the
# original data.
thisList = alist.copy()
index, largest = 0, 0
for item in range(3):
index, largest = find_largest(thisList)
if item != 2:
# delete the first two largest from the List
del thisList[index]
return largest
# Test of the algorithm
if __name__ == "__main__":
List = [12, 45, 2, 41, 31, 10, 8, 6, 4]
third = get_third_largest(List)
# print("Initial list: ", List)
# print("The third largest item in the list:")
print("tExpected: 31")
print("tResult: %d" % third);
# --- Output---
# Expected: 31
# Result: 31
add a comment |
@venkat: Here another proposal to get the third largest number out of your list without using len()
and sort()
.
def find_largest(alist):
"""
Find the largest number in a list.
Return the largest number found and it index
"""
largest = alist[0]
for item in alist[1:]:
if item > largest:
largest = item
idx = alist.index(largest)
return (idx, largest)
#--
def get_third_largest(alist):
"""
Return the third largest number in a list.
"""
# Let make a copy of the input list so that any change to it may not affect the
# original data.
thisList = alist.copy()
index, largest = 0, 0
for item in range(3):
index, largest = find_largest(thisList)
if item != 2:
# delete the first two largest from the List
del thisList[index]
return largest
# Test of the algorithm
if __name__ == "__main__":
List = [12, 45, 2, 41, 31, 10, 8, 6, 4]
third = get_third_largest(List)
# print("Initial list: ", List)
# print("The third largest item in the list:")
print("tExpected: 31")
print("tResult: %d" % third);
# --- Output---
# Expected: 31
# Result: 31
add a comment |
@venkat: Here another proposal to get the third largest number out of your list without using len()
and sort()
.
def find_largest(alist):
"""
Find the largest number in a list.
Return the largest number found and it index
"""
largest = alist[0]
for item in alist[1:]:
if item > largest:
largest = item
idx = alist.index(largest)
return (idx, largest)
#--
def get_third_largest(alist):
"""
Return the third largest number in a list.
"""
# Let make a copy of the input list so that any change to it may not affect the
# original data.
thisList = alist.copy()
index, largest = 0, 0
for item in range(3):
index, largest = find_largest(thisList)
if item != 2:
# delete the first two largest from the List
del thisList[index]
return largest
# Test of the algorithm
if __name__ == "__main__":
List = [12, 45, 2, 41, 31, 10, 8, 6, 4]
third = get_third_largest(List)
# print("Initial list: ", List)
# print("The third largest item in the list:")
print("tExpected: 31")
print("tResult: %d" % third);
# --- Output---
# Expected: 31
# Result: 31
@venkat: Here another proposal to get the third largest number out of your list without using len()
and sort()
.
def find_largest(alist):
"""
Find the largest number in a list.
Return the largest number found and it index
"""
largest = alist[0]
for item in alist[1:]:
if item > largest:
largest = item
idx = alist.index(largest)
return (idx, largest)
#--
def get_third_largest(alist):
"""
Return the third largest number in a list.
"""
# Let make a copy of the input list so that any change to it may not affect the
# original data.
thisList = alist.copy()
index, largest = 0, 0
for item in range(3):
index, largest = find_largest(thisList)
if item != 2:
# delete the first two largest from the List
del thisList[index]
return largest
# Test of the algorithm
if __name__ == "__main__":
List = [12, 45, 2, 41, 31, 10, 8, 6, 4]
third = get_third_largest(List)
# print("Initial list: ", List)
# print("The third largest item in the list:")
print("tExpected: 31")
print("tResult: %d" % third);
# --- Output---
# Expected: 31
# Result: 31
answered Nov 25 '18 at 5:39
eapetchoeapetcho
42927
42927
add a comment |
add a comment |
You could also store your top 3 max numbers in a dictionary, then print out the third largest one:
largest = {"first": 0, "second": 0, "third": 0}
lst = [12, 45, 2, 41, 31, 10, 8, 6, 4]
for number in lst:
if number > largest["first"]:
largest["third"] = largest["second"]
largest["second"] = largest["first"]
largest["first"] = number
elif number > largest["second"]:
largest["third"] = largest["second"]
largest["second"] = number
elif number > largest["third"]:
largest["third"] = number
print(largest)
# {'first': 45, 'second': 41, 'third': 31}
print(largest["third"])
# 31
add a comment |
You could also store your top 3 max numbers in a dictionary, then print out the third largest one:
largest = {"first": 0, "second": 0, "third": 0}
lst = [12, 45, 2, 41, 31, 10, 8, 6, 4]
for number in lst:
if number > largest["first"]:
largest["third"] = largest["second"]
largest["second"] = largest["first"]
largest["first"] = number
elif number > largest["second"]:
largest["third"] = largest["second"]
largest["second"] = number
elif number > largest["third"]:
largest["third"] = number
print(largest)
# {'first': 45, 'second': 41, 'third': 31}
print(largest["third"])
# 31
add a comment |
You could also store your top 3 max numbers in a dictionary, then print out the third largest one:
largest = {"first": 0, "second": 0, "third": 0}
lst = [12, 45, 2, 41, 31, 10, 8, 6, 4]
for number in lst:
if number > largest["first"]:
largest["third"] = largest["second"]
largest["second"] = largest["first"]
largest["first"] = number
elif number > largest["second"]:
largest["third"] = largest["second"]
largest["second"] = number
elif number > largest["third"]:
largest["third"] = number
print(largest)
# {'first': 45, 'second': 41, 'third': 31}
print(largest["third"])
# 31
You could also store your top 3 max numbers in a dictionary, then print out the third largest one:
largest = {"first": 0, "second": 0, "third": 0}
lst = [12, 45, 2, 41, 31, 10, 8, 6, 4]
for number in lst:
if number > largest["first"]:
largest["third"] = largest["second"]
largest["second"] = largest["first"]
largest["first"] = number
elif number > largest["second"]:
largest["third"] = largest["second"]
largest["second"] = number
elif number > largest["third"]:
largest["third"] = number
print(largest)
# {'first': 45, 'second': 41, 'third': 31}
print(largest["third"])
# 31
edited Nov 25 '18 at 5:25
answered Nov 25 '18 at 5:16
RoadRunnerRoadRunner
11.2k31340
11.2k31340
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
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%2fstackoverflow.com%2fquestions%2f53464722%2ffinding-third-largest-element-in-the-list-without-using-len-and-sort%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');
});
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');
});
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');
});
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
1
Possible duplicate of Finding the Kth Largest element in a Python List using recursion
– Ganesh K
Nov 25 '18 at 4:47
@GaneshK, Hey buddy i dont want to use any max in my code. Its not a duplicate
– venkat
Nov 25 '18 at 4:49
Your question does not state that anywhere.
– Ganesh K
Nov 25 '18 at 4:52
@venkat You can't use any libraries?
– RoadRunner
Nov 25 '18 at 4:54
1
@venkat put your
largest_2 and largest_3
equal to0
and you're done!– Sandesh34
Nov 25 '18 at 4:58