Leave these fields empty (spam trap):
Name
You can leave this blank to post anonymously, or you can create a Tripcode by using the format Name#Password
Comment
[i]Italic Text[/i]
[b]Bold Text[/b]
[spoiler]Spoiler Text[/spoiler]
>Highlight/Quote Text
[pre]Preformatted & Monospace Text[/pre]
[super]Superset Text[/super]
[sub]Subset Text[/sub]
1. Numbered lists become ordered lists
* Bulleted lists become unordered lists
File

Sandwich


Discord Now Fully Linked With 420chan IRC

Would someone mind checking my work?

Reply
!!dPPr4Oxe - Mon, 09 Apr 2018 22:15:27 EST PRg+vC3B No.37504
File: 1523326527669.jpg -(1821032B / 1.74MB, 4128x2322) Thumbnail displayed, click image for full size. Would someone mind checking my work?
I'm using the site PracticePython.org to learn some python and one of the challenges is list overlap comprehension. My solution seems WAY different than what they have but it also seems to be working which seems, wrong. Would one of you kind people mind having a look and telling me if this is ferkakt? Here's a picture of my Jack Russel Terrier as a show of my gratitude.

import random

al = random.randint(5, 25)
bl = random.randint(5, 25)
a = random.sample(range(100), al)
b = random.sample(range(100), bl)
c = []

if len(a) > len(b):
c = [i for i in a if i in b and not c]
if len(b) > len(a):
c = [i for i in b if i in a and not c]

print(a)
print(b)
print(c)
>>
Isaac Yankem D.D.S. !!dPPr4Oxe - Mon, 09 Apr 2018 22:17:51 EST PRg+vC3B No.37505 Reply
Here's the challenge itself if that helps you understand what I was trying to do with it: (https://www.practicepython.org/solution/2014/04/16/10-list-overlap-comprehensions-solutions.html)

This week’s exercise is going to be revisiting an old exercise (see Exercise 5), except require the solution in a different way.

Take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. Write this using at least one list comprehension. (Hint: Remember list comprehensions from Exercise 7).

Extra:

Randomly generate two lists to test this
>>
Nathaniel Pittwell - Tue, 10 Apr 2018 01:19:26 EST k++VoPQ4 No.37506 Reply
  1. Stop using bullshit variable names. Is c common? Then call it common. Names matter.
  2. It doesn't matter which list is longer or which list you iterate over. If you were trying to optimize, all you did was waste time and create a bug.
  3. List comprehensions are always introduced as being the same thing as a list appending loop, but it isn't true. You can't reference the resulting list from within the comprehension so what you're trying to do will never work. To get unique items, either make the input or output unique.
  4. Randomly generating lists and manually checking the result is a bad joke. Instead, organize code into small functions and write test cases:

def make_common_list(a, b):
common = [i for i in a if i in b]
return make_unique_list(common)

def make_unique_list(seq):
unique_list = []
for i in seq:
if i not in unique_list:
unique_list.append(i)
return unique_list

def test_make_common_list():
assert make_common_list([1,1,2], [1,2,5]) == [1, 2]
assert make_common_list([1,2,5], [1,1,2]) == [1, 2]
assert make_common_list([], [1,2,5]) == []
>>
Isaac Yankem D.D.S. !!dPPr4Oxe - Tue, 10 Apr 2018 22:00:49 EST PRg+vC3B No.37512 Reply
>>37506

Firstly, thinks for responding and giving advice. I appreciate it immensely.

Let me address each of your comments individually to give and get clarification.

  1. The list names are used from the exercise given. Also, I didn't think they would matter much because they aren't being used in any real world code. Just as an exercise to learn how things work. I get that if I were building something that would be used for anything other than completing this coding exercise naming the lists something useful would be important. I have a little tiny bit of knowledge of JavaScript and have dicked around with creating things in it and I know to always make sure an array has a useful name so that I know what data it's storing.

2. Ok. I was thinking that because the lists weren't sorted in my code (Remember they're generated randomly) that I would always want to check the longer one against the shorter one so it wouldn't get to the end of the short one and stop. That wasn't made clear in the source material I was reading that it wouldn't be a problem. In fact, they made it seem like it would be a problem if one was longer than the other and you didn't do something for it.

3. I'm not completely sure I understand but IF I do, you're saying that I cannot check against the list I'm creating inside of the list comprehension? That makes sense sort of and working around that is definitely something I'll keep in mind. TBH: I kind of like using the for loops more than list comprehension. The whole things feels like voodoo to me and the for loops make more sense in my head.

4. I see. Randomly generating and then testing it is dumb because who knows what kind of results you'll get if they're random. I should have started with known lists, made sure it works and then generated them randomly.

Report Post
Reason
Note
Please be descriptive with report notes,
this helps staff resolve issues quicker.