#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Strings and Arrays/Lists. Please help!


    Write a program that continuously prompts for a string to be entered from the keyboard until one of "y", "yes", "oui", "n", "no", or "non" (in lower or upper case) is entered, then outputs "You entered 'yes'" or "You entered 'no'", as appropriate.

    Use an array containing each of the test cases, as follows:

    yesNo = ["y", "yes", "oui", "n", "no", "non"]

    The program should loop through each of the test cases to determine if one of those cases was chosen.

    Please help!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    I like this pattern which takes care of your loop problem.

    Code:
    unfinished = True
    while unfinished:
        #changes that can influence unfinished
    Python has a set data type,
    set('y yes oui n no non'.split())

    You could test
    OBJECT in ACCEPTABLE_SET

    The input part of your problem may depend on your python version.

    Finally, observe that strings have a method to make characters lower case.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Code:
    yesNo = ["y", "yes", "oui", "n", "no", "non"]
    
    STATUS = True 
    
    while STATUS:
        RES = input()
        if RES in yesNo:
            print('You entered' + response)
            STATUS = False  # while loop finishes
    Good luck,

    Yegers
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    The comparisons should be faster with set data type (they're probably implemented with red-black trees whereas the list is a linear search.)

    Also, I dislike
    while STATUS:

    there's always a status, whether it be yeah, neah, or 'gold'.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    "No matter what type of code they create, all programmers/coders have their own ways of doing their jobs"

    I tried making the code a bit explicit...

    about the STATUS thing, well, that everyone has his/her own personal taste of what is good and what not ^.^'
    that's what makes people unique!

    No doubt you could do it better...

    Comments on this post

    • b49P23TIvg agrees : Nope, looks like my code was worse.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    If I thought your code wouldn't work I'd have used the site reputation feature.

    As for "all programmers/coders have their own ways of doing their jobs", choosing among various algorithms for the same task distinguishes programmers.

    red-black tree runs in O(log(n)) time, while the list search runs in O(n).

    OK, for short lists or for just a single look up if you already have a list but not a set the list search is faster. The set search for 100 thousand elements is ten thousand times faster than list search. If I have to create the entire set anew (corresponds to a single look up) then the set search is 10 times slower than list search. For a list length 8 similar to this thread the list search is faster. Twice as fast.
    Code:
    >>> import timeit
    >>> timeit.timeit('L[len(L)//2] in L',setup='import random;L=[random.random()for i in range(99999)]',number=100)
    0.14156699180603027
    >>> timeit.timeit('L[len(L)//2] in S',setup='import random;L=[random.random()for i in range(99999)];S=set(L)',number=100)
    3.218650817871094e-05
    >>> timeit.timeit('L[len(L)//2] in S',setup='import random;L=[random.random()for i in range(99999)];S=set(L)',number=100)
    2.9087066650390625e-05
    >>> timeit.timeit('L[len(L)//2] in L',setup='import random;L=[random.random()for i in range(99999)]',number=100)
    0.23739981651306152
    >>> timeit.timeit('L[len(L)//2] in L',setup='import random;L=[random.random()for i in range(8)]',number=100)
    3.314018249511719e-05
    >>> timeit.timeit('L[len(L)//2] in S',setup='import random;L=[random.random()for i in range(8)];S=set(L)',number=100)
    6.318092346191406e-05
    >>> timeit.timeit('S=set(L);L[len(L)//2] in S',setup='import random;L=[random.random()for i in range(99999)]',number=100)
    2.93165922164917
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Convinced ^^'

    (never said that you suggested that my code didn't work? I know when it does and when not, speed is another matter.)

    Using more advanced methods, is not always the best solution if a question like this is asked...

    But I'll look into this "red-black tree"

    Thanks!
    Yegers

IMN logo majestic logo threadwatch logo seochat tools logo