-3
def shortenPlus(s):
    # Yer Code 'Ere Mate!
    # Aye Aye cap'
    new = ''
    prior = ''
    for x in s:
        if not (x in 'aeiou' and prior.isalpha()):
            new += x
        prior = x
    return new

print(shortenPlus("I've information vegetable, animal and mineral"))

So here's the code that I found from this thread. I am having trouble understanding how the "if not" part of the for loop works, and why we have new and prior statements.

I understand that we take a variable from the string, and if this variable is NOT in 'aeiou', and the prior container does NOT have anything from the alphabet, then you add this variable to new. But if prior is I, and x is v, it doesn't satisfy the prior criteria, and yet it still adds it to new.

This is how I'm understanding it so far. Please let me know what I'm misunderstanding!

Nakx
  • 1,460
  • 1
  • 23
  • 32
Kappadar
  • 1
  • 1
  • 2
    Welcome to stack overflow! One great way to understand what's happening is to insert `print()` statements to show you what exactly `new` and `prior` are after each iteration. Another thing to do after that is to play with the conditions. Remove one or the other from the `if`, or remove the `not` and see what prints then – G. Anderson Feb 06 '20 at 23:36
  • 1
    Also: [How to step through python code to debug issues](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) – G. Anderson Feb 06 '20 at 23:38
  • Useful relations (deMorgans): (1) not A and not B == not (A or B) ............(2) not A or not B == not (A and B). Rewrite your code using those, to see if it's more understandable. – neutrino_logic Feb 06 '20 at 23:44
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – AMC Feb 07 '20 at 01:43
  • I would recommend learning more Python, and about programming in general. Stack Overflow is not a substitute for guides and tutorials. – AMC Feb 07 '20 at 01:44

4 Answers4

1

Let's expand

if not(x in 'aeiou' and prior.isalpha()): ...

to

inner = x in 'aeiou' and prior.isalpha()
if not inner:
    ...

for clarity. Now, inner will be True if and only if both conditions are true:

  • x is a vowel
  • The previous character is a letter

If either of those is not true, then inner will be False. So, the original test:

not(x in 'aeiou' and prior.isalpha())

negates that, and will be False if both of those conditions are met. If either (or both!) are not met, then it will be True, and the if-statement will be evaluated. Therefore, the if-statement will be evaluated if:

  • x is not a value
  • and/or the previous character is not a letter.
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
1

I am having trouble understanding [...] why we have new and prior statements.

new and prior are variables, not statements. The code is using new to build up a new string, and prior to store the character from the previous iteration of the for-loop.

if this variable is NOT in 'aeiou', and the prior container does NOT have anything from the alphabet

Your understanding of the logic isn't quite right. The new += x line is executed if both x in 'aeiou' AND prior.isalpha() evaluate to false -- which (per De Morgan's theorem) is equivalent to saying, the new += x line is executed if either x not in 'aeiou' or prior.isalpha()==False (or both).

So, the code will add a character to the string as long as either it's not a vowel, or the previous character was not a letter, or both.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
1

You've got a slight logical misunderstanding. The code specifies NOT (A AND B); this is equivalent to NOT A OR NOT B. In other words:

if this variable is NOT in 'aeiou', OR the prior container does NOT have anything from the alphabet, then you add this variable to new

So, continuing your hypothetical, if prior is I, and x is v, then:

  • x in 'aeiou' is false
  • prior.isalpha() is true

therefore:

  • x in 'aeiou' and prior.isalpha() is false

therefore:

  • not(x in 'aeiou' and prior.isalpha()) is true.
Hamms
  • 5,016
  • 21
  • 28
0

'new' there is the new string which is generated by the for.

'prior' serves to make the condition, which depends on the previous character in the original string it goes thru.

As a comment mentions, adding prints to every step is a good way to see what's going on.

antont
  • 2,676
  • 1
  • 19
  • 21