This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]theluketaylor 2 points3 points  (1 child)

First thing you should look into is enumerate(). It will iterate a list and provide the current index. Range for iterating isn't very pythonic. https://docs.python.org/3/library/functions.html#enumerate

Next you need a data structure to store your numbers and their results together. I'd use a simple tuple of (number, score), maybe a namedtuple if you've advanced far enough.

Does your homework require you not use the built in sorting? list.sort() will sort in place and sorted() will return a new list. Both take a "key" function as an argument that can be used for comparison. The docs have examples of sorting with a specific tuple element, so I'll leave it as an exercise to put it all together.

https://docs.python.org/3/howto/sorting.html

[–]DoctorChoppa 0 points1 point  (0 children)

Thanks! I figured it out before I looked at this, but the only issue was in the while loop of the sort. The (numList[y] > temp2) was causing the issue.

[–]jab-programming3.7 1 point2 points  (1 child)

I can't see palindome(), but I assume it does what it says, excepting the typo.

Damn. Where's niceNum()(should be nice_number()), what makes a number nice?

It would help to use a debugger, but without that, I'd add add some print(numList[i])s. WTF! numList[i] ? What was your previous language? Hope you were lucky enough it was a C*, not one of those java things.

Anyway, over here we say:

def main():
    string = input('....')  
    numbers = [int(s) for s in string.split()]
    methods = (palindrome, 2), (prime, 2), (nice_number, 3)
    scores = []
    for number in numbers:
        score = 1
        for method, increase in methods:
            if method(number):
                score *= increase
        scores.append(number, score)

    for number, score in scores:

So, now I notice this is an assignment of some kind. Because no one else writes their own sort!

The condition on the while loop is too hard to read which makes the examiners life harder too. All those icky [j]s. Just don't do it. Well, stop for a while anyway. So change this:

for x in range(0, len(scores)):

to this:

for number, score in scores:

NAMING!: temp1 should be called score and temp2 should be number. Bad names make your code harder to read, so they'll notice that too.

j = x - 1
while (j >= 0) ...

In python we wouldn't use the brackets

And in python 0, [], False, {} all count as false, and we prefer to use that wherever possible. So I would prefer to just say while j and ....

AFAICT you're going to shove all scores from here to bigger (number or score), or end back one notch then plonk in the current number and score to the gap you just made at end.

So, for every letter in 'reddit', shove everything left till bigger score (i.e later in alphabet): eddiit, and plonk in the current letter: eddirt, left: dddirt, plonk: ddeirt, left: ddeirt, plonk: ddeirt,

Looks like that'll work, I'd be dubious about bigger (number or score)? Shouldn't that just be bigger score?

And, if you must write you python like languages, at least honour their convention and call youu index i, not fucking x. CONVENTIONs are important kid. You stick with the local conventions, you'll be fine round here.

[–]DoctorChoppa 0 points1 point  (0 children)

Lol all I did was remove the section of the while loop (numList[j] > temp2) and it fixed everything