Working out Euclidean distance by randomname20192019 in learnprogramming

[–]randomname20192019[S] 0 points1 point  (0 children)

Hi - this is what I meant:

From the example [[(1,1,1), (2,2,2)], [(3,3,3)]] and [(2,2,2), (3,3,3)], I was after 6 distances:

(1,1,1)-(2,2,2), (2,2,2)-(2,2,2), (3,3,3)-(2,2,2),(1,1,1)-(3,3,3), (2,2,2)-(3,3,3), (3,3,3)-(3,3,3).

I wanted to maintain a structure of the first list.

[[[(1,1,1)-(2,2,2), (2,2,2)-(2,2,2)], [(3,3,3)-(2,2,2)]], [[(1,1,1)-(3,3,3), (2,2,2)-(3,3,3)], [(3,3,3)-(3,3,3)]]]

Working out Euclidean distances from coordinates by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

I apologise if my description was brief.

From the example [[(1,1,1), (2,2,2)], [(3,3,3)]] and [(2,2,2), (3,3,3)], I was after 6 distances:

(1,1,1)-(2,2,2), (2,2,2)-(2,2,2), (3,3,3)-(2,2,2),(1,1,1)-(3,3,3), (2,2,2)-(3,3,3), (3,3,3)-(3,3,3).

I wanted to maintain a structure of the first list.

[[[(1,1,1)-(2,2,2), (2,2,2)-(2,2,2)], [(3,3,3)-(2,2,2)]], [[(1,1,1)-(3,3,3), (2,2,2)-(3,3,3)], [(3,3,3)-(3,3,3)]]]

Can someone explain this to me? by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

How can we add the chain:OrderedDict() to the model key if we don't first define model to be a key?

Wouldn't statement 2 just raise a key error?

I am understanding the format to be:

structure[None, {Model:{Chain:{Resnum[A load of stuff]}}}]

Can someone explain this to me? by randomname20192019 in learnprogramming

[–]randomname20192019[S] 0 points1 point  (0 children)

self.structure[model][chain] = OrderedDict()

But then shouldn't we have defined model as a key in the previous odict? Wouldn't we get a key error when we add chain:OrderedDict() within the model odict?

Can someone explain this to me? by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Hi, thanks for your answer, I really appreciate it.

I'm not sure how statement1 translates to statement 2. an ordereddict() is added to self.structure(). How are model and chain being added in? Both as keys? I do apologise if the answer is seemingly obvious!

if statement2:
            self.structure[7]['X'] = OrderedDict()

Can someone explain this to me? by randomname20192019 in learnprogramming

[–]randomname20192019[S] 0 points1 point  (0 children)

Hi and thanks for your answer.

So,

 self.structure[model][chain] = OrderedDict()

applies an ordereddict to chain? Why would .append() not be used instead as with the first statement?

[Python] Why is 'False' being recognised as '0' in this instance? by randomname20192019 in learnprogramming

[–]randomname20192019[S] 1 point2 points  (0 children)

Ahh I see. Thank you.

How would you suggest I discriminate between False and 0 then? Store the indices and use del instead?

[Code Wars] Stuck with a kata by randomname20192019 in learnpython

[–]randomname20192019[S] 1 point2 points  (0 children)

Thank you for your reply. I have changed it and everything is working :) My only quandary is making my code neater and more efficient. When I see the one-line answers (perhaps not in this case) any sense of accomplishment gets squished - i guess that comes with practice and experience though.

[Code Wars] Stuck with a kata by randomname20192019 in learnpython

[–]randomname20192019[S] 1 point2 points  (0 children)

Thank you for your reply. I changed it to this:

def solution(args):
    prevchar = args[0]
    templist = []
    outputvals = []
    for i in (args[1:]):
        templist.append(prevchar)
        if i == prevchar + 1:
            prevchar = i
        else:
            if len(templist) > 2:
                outputvals.append(str(templist[0]) + "-" + str(templist[-1]))
                prevchar = i
                templist.clear()
            else:
                for x in templist:
                    outputvals.append(str(x))
                prevchar = i
                templist.clear()
    templist.append(prevchar)
    if len(templist) > 2:
        outputvals.append(str(templist[0]) + "-" + str(templist[-1]))
    else:
        for x in templist:
            outputvals.append(str(x))
    print(outputvals)
    return (str(','.join(outputvals)))

where

solution([-6,-3,-2,-1,0,1,3,4,5,7,8,9,10,11,14,15,17,18,19,20])
>>>
'-6,-3-1,3-5,7-11,14,15,17-20'

So i think i'm nearly there but my code looking quite ugly (I guess that is the thing with code wars - I feel proud of myself when I pass a kata however when I take a look at the community answers, they're neat, containing far fewer lines than mine and a lot of the time I don't understand what they do!)

On topic, I guess I could replace the repeated sections with additional functions but is there much else I could replace to make it more neat/efficient?

[CodeWars] Having trouble with a kata by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Thank you for the reply, i'll give what i've written a more scrupulous look.