Idiom: Search
Sue Inn Chng
The University of taken from:
https://www.independent.co.uk/arts-entertainment/books/news/where-s-the-brains-behind-wally-6261459.html https://mobile.twitter.com/whereswally/photo
Copyright By PowCoder代写 加微信 powcoder
Application: Search lists with loops
– A search requires reading each element of the collection until the search criteria has been satisfied.
– Manyreasonstoperformasearch: – Tolearnifitemexists
– Tolearnwhereitis
– Tolearnhowmanyexist
– Sameprinciplesassearchingeachcharacterinastring! See the example in Lab 5.
The University of 2
– Consideralistofanimals:
animals = [ “armadillo”, “bear”, “kangaroo”, “okapi”, “squirrel”, “bear”]
– Let’swriteaprogramtodetermine: – Isthereabearinthelist?
– Whereisthebear?Whatistheindex? – Howmanybearsarepresentinthelist?
The University of 3
Program Design for Bear Hunt
– Traverse each element of the list – Count-controlledloop:
• Start: 0
• End condition: End of list – Actionstoberepeated?
• Access the element • Isitabear?
» Display the location of the bear. » Increment count for bear by 1.
– No: Do nothing.
• Modification of control: + 1
– ImplementatEdLesson. The University of 4
Limitations of Bear Hunt
– Willtheprogramworkforotheranimalse.g.‘armadillo’?
– Wecangetinputsfromtheuserfromstandardinputusing ____. Can the user provide the search term before the execution of the program?
– Didyouknowwecangetinputsfromtheuserviathecommand line?
$ python3 demo.py INFO1110 COMP9001 INFO1910
– Why do I want to do this?
– Allowstheprogrammertoexecutetheprogramondifferentsettings.
The University of 5
Command Line Arguments
– Inputs are passed in through the command line instead of using input() function.
– Command line argument are stored in a variable (an array of string objects) called argv in module sys.
– First element will always be the script name.
– Go to Ed Lesson and run the program.
The University of 6
Animal Hunt
Let’s modify the bear_hunt.py program to find the locations and occurrences of each search item given by the user through command line arguments.
1. Retrieve command line arguments and store into a list. This will be the list of items to search.
2. Searchtheanimallistforeachiteminthelistofitemsto search. Store and increment count accordingly if found.
3. Displayresultofthehunt.
Example for Step 1 and Step 2 on Ed Lesson. Can you do Step 3 on your own?
The University of 7
– Anotherwayofgettinginputsfromusers–commandline arguments.
– Thefirstcommandlineargumentisalwaysthescriptname.
– Create our very own search program to locate location and count occurrences of a search term obtained from command line arguments.
– You must be able to construct codes to traverse the elements in a list to perform actions on all or specific elements.
The University of 8
Reading This Week
– Chapter1.4.Sedgewick,R.,Wayne,K.,&Dondero,R.(2015). Introduction to programming in Python: An interdisciplinary approach. Addison- .
The University of 9
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com