Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh Programmer named Tim Posts: 11 Threads: 5 Joined: Jan 2018 Reputation: 1 Likes received: 2 #1 Feb-12-2018, 07:04 PM (This post was last modified: Feb-12-2018, 08:24 PM by Mekire. Edited 5 times in total.) 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. :) Mekire wrote Feb-12-2018, 08:24 PM:Post reverted. Please don't do that. sparkz_alot Posts: 1,296 Threads: 36 Joined: Sep 2016 Reputation: 30 Likes received: 266 #2 Feb-13-2018, 01:59 AM 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 Larz60+ aetate et sapientia Posts: 4,902 Threads: 202 Joined: Sep 2016 Reputation: 169 Likes received: 730 #3 Feb-13-2018, 02:23 AM Where's the rest of this post? Larz60+ aetate et sapientia Posts: 4,902 Threads: 202 Joined: Sep 2016 Reputation: 169 Likes received: 730 #4 Feb-13-2018, 02:24 AM 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 pritesh Programmer named Tim Posts: 11 Threads: 5 Joined: Jan 2018 Reputation: 1 Likes received: 2 #5 Feb-13-2018, 10:26 AM 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? Mekire likes this post Larz60+ aetate et sapientia Posts: 4,902 Threads: 202 Joined: Sep 2016 Reputation: 169 Likes received: 730 #6 Feb-13-2018, 12:15 PM 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 pritesh likes this post sparkz_alot Posts: 1,296 Threads: 36 Joined: Sep 2016 Reputation: 30 Likes received: 266 #7 Feb-13-2018, 03:01 PM (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. pritesh likes this post 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 Mekire Posts: 508 Threads: 18 Joined: Sep 2016 Reputation: 37 Likes received: 321 #8 Feb-13-2018, 07:33 PM (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. pritesh likes this post pritesh Programmer named Tim Posts: 11 Threads: 5 Joined: Jan 2018 Reputation: 1 Likes received: 2 #9 Feb-13-2018, 09:20 PM (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. pritesh Programmer named Tim Posts: 11 Threads: 5 Joined: Jan 2018 Reputation: 1 Likes received: 2 #10 Feb-13-2018, 10:31 PM (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. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post py4e book exercise not working when compiled adriand 10 277 Jun-13-2018, 01:01 PM Last Post: adriand Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 220 Mar-05-2018, 03:48 PM Last Post: mpd Help with test exercise lm777 3 278 Feb-21-2018, 10:42 PM Last Post: nilamo How to make faster this code Ace 1 349 Oct-23-2017, 12:11 PM Last Post: Larz60+ How can I make faster that code BlueEva00 1 332 Oct-18-2017, 03:51 PM Last Post: ichabod801 This is an open book home assignment but i really need your help Samiha 3 541 Oct-04-2017, 05:39 PM Last Post: nilamo Simple exercise - how to write in python tomtom55 6 966 Sep-28-2017, 07:18 PM Last Post: nilamo Help with an exercise Cerobug 8 578 Sep-20-2017, 09:10 PM Last Post: nilamo The Python Book altered rock paper scissors Python_Noob 0 405 Sep-18-2017, 06:13 AM Last Post: Python_Noob Trouble with LPtHW exercise 21 pyth31 6 765 Sep-09-2017, 07:46 PM Last Post: pyth31

Forum Jump:

Users browsing this thread: 1 Guest(s)