Current Events > My weekly programming help topic (Python)

Topic List
Page List: 1
LookANinja
11/09/18 11:02:08 PM
#1:


Assignment is to find max and min without using ANY built in functions. Using a given list. Two functions, one to find max, one to find min.

def topnumber(mylist):
hi_num = mylist[0]
for num in mylist:
if hi_num > num:
hi_num = num
return hi_num
def lownumber(mylist):
low_num = mylist[0]
for num in mylist:
if low_num < num:
low_num = num
return low_num
print("The max is :",topnumber([0, 3.14159, 5, 1, 9, 2.71828, -1]))
print("The min is :",lownumber([0, 3.14159, 5, 1, 9, 2.71828, -1]))

I can get the min, but the max keeps giving me 3.14159. Like it's not iterating through the whole list for some reason.
---
... Copied to Clipboard!
Frostshock
11/09/18 11:05:25 PM
#2:


Yeah...Python isn't the best choice to try and post here.
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
LookANinja
11/09/18 11:05:46 PM
#3:


Frostshock posted...
Yeah...Python isn't the best choice to try and post here.

I know those damn indents. =(
---
... Copied to Clipboard!
Frostshock
11/09/18 11:07:08 PM
#4:


Also, your code is totally fucked. The comparisons are completely backwards, I'm amazed it's giving any semblance of the correct answer.
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
kirbymuncher
11/09/18 11:07:33 PM
#6:


pastebin it

or at least put in in code tags I think those conserve whitespace
---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
REMercsChamp
11/09/18 11:08:18 PM
#7:


This is grade 11 shit

Get a job.
---
Come check out my community board for NEET discussions:
https://gamefaqs.gamespot.com/boards/1467-neet-discussions
... Copied to Clipboard!
LookANinja
11/09/18 11:09:08 PM
#8:


def topnumber(mylist):
hi_num = mylist[0]
for num in mylist:
if hi_num < num:
hi_num = num
return hi_num
def lownumber(mylist):
low_num = mylist[0]
for num in mylist:
if low_num > num:
low_num = num
return low_num
print("The max is :",topnumber([0, 3.14159, 5, 1, 9, 2.71828, -1]))
print("The min is :",lownumber([0, 3.14159, 5, 1, 9, 2.71828, -1]))

https://pastebin.com/WCuB0TcD

Fixed the backwards comparators (I was experimenting)

I feel like it's iterating, but it's reading the 3.14159 as like 3,14159 or something. instead of a decimal value.
---
... Copied to Clipboard!
Frostshock
11/09/18 11:10:51 PM
#9:


If your indentation is right it should work now that you fixed it.

But who knows if your indentation is right because you keep trying to post a whitespace scoped language to GameFAQs.
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
TheAnthraxBunny
11/09/18 11:11:20 PM
#10:


GFs code tag is worthless and doesnt allow indentation. Youre better off using Pastebin.
---
I am a bot, and this action was performed automatically
... Copied to Clipboard!
nexigrams
11/09/18 11:11:35 PM
#11:


Not sure I understand, but if I had to find a min without using any built in functions I would make a temp variable, iterate over the list, and each time check if temp < list[i]. If it is, set temp to list[i]. Return temp for your min.
---
... Copied to Clipboard!
Frostshock
11/09/18 11:12:05 PM
#12:


If you feel like your function is returning early, perhaps you should look at where you placed your return...
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
LookANinja
11/09/18 11:15:22 PM
#13:


Frostshock posted...
If you feel like your function is returning early, perhaps you should look at where you placed your return...

I don't know if this was actually a blatant hint, but from my understanding, the min function is correctly returning -1, which is the last element of the list, it shouldn't be returning early. And i wrote the max function first and copy pasted it for my min function. That might have been a mistake.
---
... Copied to Clipboard!
kirbymuncher
11/09/18 11:16:40 PM
#14:


LookANinja posted...
Frostshock posted...
If you feel like your function is returning early, perhaps you should look at where you placed your return...

I don't know if this was actually a blatant hint, but from my understanding, the min function is correctly returning -1, which is the last element of the list, it shouldn't be returning early. And i wrote the max function first and copy pasted it for my min function. That might have been a mistake.

try it on a different list

alternatively, step through one of your functions manually, yourself, and just consider what happens at each line of code
---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
Frostshock
11/09/18 11:16:51 PM
#15:


LookANinja posted...
the min function is correctly returning -1


Because you are using the only test case where your min function works.

Try putting a -2 after the -1.
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
TheAnthraxBunny
11/09/18 11:21:20 PM
#16:


I think your return statement wasn't lined up where it was supposed to be. Here's how your indentation should look:

GUvQdNo
---
I am a bot, and this action was performed automatically
... Copied to Clipboard!
Sativa_Rose
11/09/18 11:21:37 PM
#17:


One glance at the pastebin and I immediately saw the problem.

I want to explain it in a way that will teach the TC what he is doing wrong, though.

When do you want the functions to return TC? As someone else hinted at, are you sure you are returning when you mean to?
---
[This signature was deleted at the request of a moderator or administrator]
... Copied to Clipboard!
LookANinja
11/09/18 11:21:40 PM
#18:


Damn, you guys are right.

Fixed the damn indent with the return. That was a great hint. I appreciate the help guys.
---
... Copied to Clipboard!
LookANinja
11/09/18 11:22:20 PM
#19:


DAMMIT I SWEAR I DID IT BEFORE YOU GUYS POSTED ANYTHING. (except that one hint)
---
... Copied to Clipboard!
TheAnthraxBunny
11/09/18 11:28:16 PM
#20:


Also, there's a super condensed way of doing this using sorted() and negative indices. If you want to challenge yourself, you should be able to make this same program in no more than 3 lines.
---
I am a bot, and this action was performed automatically
... Copied to Clipboard!
1337toothbrush
11/09/18 11:31:31 PM
#21:


I love how Python's shitty indentation rule is called a good thing because it "forces developers into the good practice of properly indenting their code" yet types are a slapdick mess.
---
... Copied to Clipboard!
Sativa_Rose
11/09/18 11:32:27 PM
#22:


TheAnthraxBunny posted...
Also, there's a super condensed way of doing this using sorted() and negative indices. If you want to challenge yourself, you should be able to make this same program in no more than 3 lines.


I don't think they want him calling any libraries.
---
[This signature was deleted at the request of a moderator or administrator]
... Copied to Clipboard!
Sativa_Rose
11/09/18 11:34:08 PM
#23:


1337toothbrush posted...
I love how Python's shitty indentation rule is called a good thing because it "forces developers into the good practice of properly indenting their code" yet types are a slapdick mess.


The types are still there under the hood, you can use the type(variable) operator to check them. But yeah, when I first went from Java to Python the lack of strict typing or w/e was something I was very skeptical of.
---
[This signature was deleted at the request of a moderator or administrator]
... Copied to Clipboard!
LookANinja
11/09/18 11:34:23 PM
#24:


TheAnthraxBunny posted...
Also, there's a super condensed way of doing this using sorted() and negative indices. If you want to challenge yourself, you should be able to make this same program in no more than 3 lines.

I think i can kinda conceptualize it in my head. Let me know if i'm on the right track.

Basically, sort the list in either ascending or descending order
print list[0] for max (if descending)
print list[-1] for min (if descending)
---
... Copied to Clipboard!
1337toothbrush
11/09/18 11:37:53 PM
#25:


Sativa_Rose posted...
1337toothbrush posted...
I love how Python's shitty indentation rule is called a good thing because it "forces developers into the good practice of properly indenting their code" yet types are a slapdick mess.


The types are still there under the hood, you can use the type(variable) operator to check them. But yeah, when I first went from Java to Python the lack of strict typing or w/e was something I was very skeptical of.

Yeah, the types are there, which makes all the implicit declaration all the more baffling. What benefit is there to committing this information from the text? Instead of being able to reason about what a function expects simply from the function signature, I have to rely on the documentation or looking at the function's code to figure things out. This is awful for poorly-documented code.
---
... Copied to Clipboard!
TheAnthraxBunny
11/09/18 11:38:58 PM
#26:


Sativa_Rose posted...
I don't think they want him calling any libraries.

Yeah, I saw that. I just thought I'd let him know it's possible in case he wants to try it.

LookANinja posted...
TheAnthraxBunny posted...
Also, there's a super condensed way of doing this using sorted() and negative indices. If you want to challenge yourself, you should be able to make this same program in no more than 3 lines.

I think i can kinda conceptualize it in my head. Let me know if i'm on the right track.

Basically, sort the list in either ascending or descending order
print list[0] for max (if descending)
print list[-1] for min (if descending)

Yeah, that's pretty much it.
---
I am a bot, and this action was performed automatically
... Copied to Clipboard!
LookANinja
11/09/18 11:47:33 PM
#27:


mylist = [0, 3.14159, 5, 1, 9, 2.71828, -1]
mylist.sort()
print("min is :",mylist[0])
print("max is :",mylist[-1])

I did it in 4 lines. =/ can i set and sort the list in the same line somehow?
---
... Copied to Clipboard!
Sativa_Rose
11/09/18 11:52:46 PM
#28:


LookANinja posted...
I did it in 4 lines. =/ can i set and sort the list in the same line somehow?


Try adding .sort() after the closing ] in the list. I am not 100% sure it will work, but I think it will.
---
[This signature was deleted at the request of a moderator or administrator]
... Copied to Clipboard!
Frostshock
11/09/18 11:54:37 PM
#29:


Sativa_Rose posted...
LookANinja posted...
I did it in 4 lines. =/ can i set and sort the list in the same line somehow?


Try adding .sort() after the closing ] in the list. I am not 100% sure it will work, but I think it will.


It would work, but sort() modifies the literal list object, then returns None.

Wrapping the list literal in sorted() is the correct way to do it.
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
TheAnthraxBunny
11/09/18 11:55:09 PM
#30:


Oh, .sort() is different than sorted().

mylist.sort() sorts the original list without returning anything (or rather, it returns "None").

sorted(mylist) just returns a sorted version of it. The original is kept in tact.
---
I am a bot, and this action was performed automatically
... Copied to Clipboard!
LookANinja
11/10/18 12:07:35 AM
#31:


Still can't get it done in less than 4 lines. =/ trying to combine the list and sorted keeps giving me "mylist is not defined"
---
... Copied to Clipboard!
Sativa_Rose
11/10/18 12:08:42 AM
#32:


mylist = sorted([0, 3.14159, 5, 1, 9, 2.71828, -1])
---
[This signature was deleted at the request of a moderator or administrator]
... Copied to Clipboard!
TheAnthraxBunny
11/10/18 12:11:21 AM
#33:


Sativa_Rose posted...
mylist = sorted([0, 3.14159, 5, 1, 9, 2.71828, -1])

That or this:

K0NG5v4
---
I am a bot, and this action was performed automatically
... Copied to Clipboard!
LookANinja
11/10/18 12:11:36 AM
#34:


Sativa_Rose posted...
mylist = sorted([0, 3.14159, 5, 1, 9, 2.71828, -1])

doh. I had it absolutely mentally stupidly idiotically written like this
mylist(sorted([0, 3.14159, 5, 1, 9, 2.71828, -1]))

I swear the word sort has lost all meaning to me now.
---
... Copied to Clipboard!
Topic List
Page List: 1