Python Forum
John Guttag Book - Finger Exercise 4 - need help to make the code better
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
John Guttag Book - Finger Exercise 4 - need help to make the code better
#1
Hi,

I am learning python using John Guttag's book - "Introduction to Computation and Programming Using Python: With Application to Understanding Data (MIT Press) second edition".

There's a finger exercise in the book that states the following:
Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.

I cannot use a for loop or range function or arrays because I am supposed to write the solution using the keywords, functions and concepts that are discussed in the book just before this exercise. So that includes a while loop, if/elif/else, == != True/False and or etc. Basically nothing except the stuff I have used in the script below.

Here's the solution I have come up with:

counter = 0
even_count = 0
biggest_odd = False
while counter <= 10:
    counter = counter + 1
    x = int(input("Enter a number: "))
    if counter == 1 or (x % 2 != 0 and x > biggest_odd) :
        biggest_odd = x

        #The line below helped me debug the script.
        #print("biggest odd in the while loop is now " , biggest_odd)

    elif x % 2 == 0:
        even_count = even_count + 1

#if I dont do 
#if biggest_odd % 2 != 0, 
# the script says "No odd number entered"
#if one of the numbers is a negative odd number
#and the rest are zeros.
if biggest_odd % 2 != 0:
    print (biggest_odd, " is the biggest odd")

elif even_count == 10:
    print("No odd numbers entered")
Problem is, if I don't do the "if biggest_odd % 2 != 0", the script says "No odd number entered" if, lets say, the first number entered is -3 or -1 (a negative odd number), and then for the rest of the 9 times, I enter a zero.

Is there a better way to write this? Please let me know.

Note:- I am an office going fellow who gets only an hour or so a day (if possible) to work on this. I've been scratching my head to come up with the solutions since quite a while, and this is the best that I have done so far, so I have really reached my thinking limit with this one. :)
Reply
#2
Can you use the standard math "abs()" ?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Where's the rest of this post?
Reply
#4
Looks like it's here: https://python-forum.io/Thread-Anyone-el...d-to-crack
Please don't post more than one thread for same topic
Reply
#5
Hi All,

First of all, apologies for reverting the thread and posting multiple times.

The earlier script had an issue. If I put a negative integer after inputting 0 in succession, the script would fail.

Here's the latest version, which I think covers all the corner cases.

Re quoting from the original post for reference:
Exercise: Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.

I cannot use a for loop or range function or arrays because I am supposed to write the solution using the keywords, functions and concepts that are discussed in the book just before this exercise. So that includes a while loop, if/elif/else, == != True/False and or etc. Basically nothing except the stuff I have used in the script below.


counter = 1 
largest_odd = 0
even_count = 0
while counter <= 10:
    x = int(input("Enter an integer: "))
    if (x % 2 != 0 and x > largest_odd) or (x % 2 != 0 and x <= 0 and largest_odd == 0):
        largest_odd = x
        #print(largest_odd, " is the largest odd number so far")
    elif x % 2 == 0:
        even_count = even_count + 1
        #print(x , " is an even number", " even count: ", even_count)
    counter = counter + 1
if even_count == 10:
    print("No odd numbers were input")
else:
    print(largest_odd, " is the largest odd number so far")
I would be thankful if someone could test and let me know if it really covers all the corner cases.
The way I tested it is executed the script, and tried to cover all possible cases.

Is there some kind of tool available that allows me to cover all possible cases where the script could fail? It will save me a lot of time, and rather than spending my time covering all the possibilities manually, the tool can tell me something like "Ran the script x times, with xyz combinations, and it passed/failed when first number is set to 0 and next number is set to a negative integer and rest are all zeros".

Now on a more generic note, here's some issues I have observed with my programming technique/method. I would be thankful if someone could let me know how to circumvent these issues:

1) Except for some simple scripts, I cannot write a script perfectly from start to end. I always have to execute it, fix the errors, run the script again.

2) If the above method does not work, I have to resort to using a lot of print statements to figure out what my own script is doing....which I know sounds weird, but that's how it is. I cannot see it in my head....I've gone to the extent of putting the print statement before and after every variable declaration/loop/statement...

3) This is specific to the above script - It took several hours for me to come out with a solution and that too I am not sure if it still covers all the cases. A real programmer would have done this in minutes. I would be very interested to know the path to reach to that level.

Are there others here who have same issues as I have? How did you improve?
Reply
#6
There's a tool that helps, unittest: https://docs.python.org/3.6/library/unittest.html
but I don't know of any tool that's smart enough to test all cases. If you can identify all possible
inputs, and know all possible outputs, a truth table works well.
There is also a coverage tool: https://pypi.python.org/pypi/coverage/4.5.1
and others: https://pypi.python.org/pypi/coverage/4.5.1
Reply
#7
(Feb-13-2018, 10:26 AM)pritesh Wrote: Now on a more generic note, here's some issues I have observed with my programming technique/method. I would be thankful if someone could let me know how to circumvent these issues

I don't think you're ever going to reach a point where you circumvent these issues. I know I go through your 3 steps all the time. In some cases, I spend more time trying to break my code than I spent creating it. Case in point, when I asked you above if you could use the abs() function, I thought I had the perfect solution, then this morning I thought "wait, that was to easy, what did I miss", so this morning I tried the one sequence I hadn't thought of last night, and sure enough I broke the code. By the way, I had 4 'print' functions not necessary for the script, but just so I could see what the 'program' saw. The 'print' function is probably one of the best tools the programmer has.

Since you seem satisfied, I'll go ahead and post this link Finger Excersise 2, you will have to scroll down almost to the end (a post by Archery1234), which also highlights the fact that there can be more than one solution to a problem.

Congratulations on solving the exercise. Onward and upward.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
(Feb-13-2018, 12:15 PM)Larz60+ Wrote: I don't know of any tool that's smart enough to test all cases.
Automatic program correctness verification is an undecidable problem. Halting problem and all that.
Reply
#9
(Feb-13-2018, 03:01 PM)sparkz_alot Wrote:
(Feb-13-2018, 10:26 AM)pritesh Wrote: Now on a more generic note, here's some issues I have observed with my programming technique/method. I would be thankful if someone could let me know how to circumvent these issues

I don't think you're ever going to reach a point where you circumvent these issues. I know I go through your 3 steps all the time. In some cases, I spend more time trying to break my code than I spent creating it. Case in point, when I asked you above if you could use the abs() function, I thought I had the perfect solution, then this morning I thought "wait, that was to easy, what did I miss", so this morning I tried the one sequence I hadn't thought of last night, and sure enough I broke the code. By the way, I had 4 'print' functions not necessary for the script, but just so I could see what the 'program' saw. The 'print' function is probably one of the best tools the programmer has.

Since you seem satisfied, I'll go ahead and post this link Finger Excersise 2, you will have to scroll down almost to the end (a post by Archery1234), which also highlights the fact that there can be more than one solution to a problem.

Congratulations on solving the exercise. Onward and upward.

Hi,
Glad to know I am not the only one who is facing these 3 issues. :)

As for the code solution by Archery1234, it's far more elegant and succinct than mine. One day, I will write something as good as that, but till then, I will have to live with my own overly verbose code. Big Grin
Reply
#10
(Feb-13-2018, 07:33 PM)Mekire Wrote:
(Feb-13-2018, 12:15 PM)Larz60+ Wrote: I don't know of any tool that's smart enough to test all cases.
Automatic program correctness verification is an undecidable problem. Halting problem and all that.
Thanks for the informative link.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  py4e book exercise not working when compiled adriand 11 8,794 Jul-01-2020, 08:42 AM
Last Post: tawhidbd1248
  [split] help me make this code better please (basic) Rustam 2 2,228 Jun-19-2020, 01:27 PM
Last Post: Rustam
  How to make this dictionary-generating code more efficient? Mark17 4 2,317 Oct-08-2019, 07:42 PM
Last Post: Mark17
  Book exercise in lists sonedap 17 6,849 Feb-03-2019, 08:37 PM
Last Post: ichabod801
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,407 Mar-05-2018, 03:48 PM
Last Post: mpd
  How to make faster this code Ace 1 2,928 Oct-23-2017, 12:11 PM
Last Post: Larz60+
  How can I make faster that code BlueEva00 1 2,655 Oct-18-2017, 03:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020