Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit 4 lesson 2 contrived examples considered harmful. #406

Open
Dan-TEALS opened this issue Jun 27, 2022 Discussed in #365 · 0 comments
Open

Unit 4 lesson 2 contrived examples considered harmful. #406

Dan-TEALS opened this issue Jun 27, 2022 Discussed in #365 · 0 comments
Assignees
Labels
Bug IntroCS.2 Medium Severity Makes the lesson hard to use but has a workaround

Comments

@Dan-TEALS
Copy link
Contributor

Discussed in #365

Originally posted by jdonwells March 6, 2022
There are two problems with contrived examples. The first is code that the students can't understand because it simply makes no sense. The second is most of these contrived examples are badly written Python. We want to show the students well written Python only.

I would even go so far as to say if you can't think of a well written Python example maybe you should skip what you want to show them entirely. Showing the students how to traverse a list using for i in range(0, len(list)) is probably not something to show them at all. There are no good examples because no one would do that.

In Unit 4 lesson 2 we show them this code:

for i in range(len(numbers)):
    numbers[i] = numbers[i] * 2

The correct way to do that is this:

doubled_numbers = [n * 2 for n in numbers]

We also show them this code:

def is_reverse(word1, word2):
    if len(word1) != len(word2):
        return False
    i = 0
    j = len(word2)
    while j > 0:
        if word1[i] != word2[j]:
            return False
        i = i + 1
        j = j - 1
    return True
print(is_reverse('pots', 'stop'))

The correct way to do that is this:

def is_reverse(word_a, word_b):
    return word_a == word_b[::-1]


print(is_reverse('pots', 'stop'))

Here are my new examples. I am going to show 4. One each for the major traversals (map, filter, reduce) and one for using range.

Traversing a list to transform it. (map)

def strings(list):
    all_strings = []
    for item in list:
        all_strings.append(str(item))
    return all_strings

Traversing a list to summarize the items. (reduce)

def sum(numbers):
    """Return a total of the numbers"""
    total = 0
    for number in numbers:
        total = total + number
    return total

Using a traversal to filter a list. (filter)

def only_primes(numbers):
    """Return a list of the primes in the numbers list"""
    primes = []
    for n in numbers:
        if is_prime(n):
            primes.append(n)
    return primes

Using range() to generate a list for traversal.

def is_prime(n):
    for divisor in range(2, n):
        if n % divisor == 0:
            return False
    return True
```</div>
@Dan-TEALS Dan-TEALS added Bug IntroCS.2 Medium Severity Makes the lesson hard to use but has a workaround labels Jun 27, 2022
@Dan-TEALS Dan-TEALS self-assigned this Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IntroCS.2 Medium Severity Makes the lesson hard to use but has a workaround
Projects
None yet
Development

No branches or pull requests

1 participant