The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 313 Weeks Ago, 5 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I couldn't figure out the sort_by_name fucntion for this python file, could you help me?
"""
vet.py (15 points)
=====
This file will contain functions that will help you write a program in
another file for managing the animals that are coming into a pet clinic.
All animals coming in the clinic have to check in with the following
information:
* a name ('jane clawston')
* a type / kind ('cat')
* a number representing urgency - 0-100, with the most urgent! (10 - not very
 urgent)
All of the functions in this file assume that all of the animals that are
checked-in to the clinic are stored in a specific format - a list of lists.
Each animal is represented by a 3 element list:
* name will be at index 0
* type will be at index 1
* urgency will be at index 2
For example, a cat named 'jane clawston' that does not have an urgent issue
can be reprsented as:
['jane clawston', 'cat', 10]
Of course, this means that storing all checked-in animals will be a list of
lists:
#Â Â Â Â Â Â Â Â Â Â Â Â animal 1 ------+Â Â Â Â Â Â Â Â Â Â Â animal 2 ------+Â Â Â Â Â Â Â Â Â animal 3 ------+
#Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
var animals = [['jane clawston', 'cat', 10], ['franz catka', 'cat', 2], ['bark twain', 'dog', 20]]
Write the following functions AND TEST THEM USING ASSERTIONS.
find_by_name:
-----
Returns the animal with the name specified from a list of animals.
* parameters: 2 - a string (the name to search for), and a list of animals
* processing:
   1. go through every animal in the list
   2. checks the name
   3. if the name matches the supplied name, give back that animal
* return value: a single animal (a list) or None if the animal is not found
examples:
animal_list = [['sam', 'snake', 4], ['gertrude', 'goat', 99]]
print(find_by_name('gertrude', animal_list))
# will print out ['gertrude', 'goat', 99]
sort_by_name:
-----
This function will return a new list of animals, sorted by the name of the
the animal (that is, the first element in the list that represents a single
animal), based on the original list of animals given to the function. The
result will be a new list of animals sorted alphabetically by name.
* parameters: a list of animals (a list of lists)
* processing:
   1. DO NOT USE THE LIST'S SORT METHOD, instead, follow this process
   2. go through every animal in the list positionally (that is, look at the
      first animal, the second, third, etc...)
   3. compare the name of the current animal with the name of the next animal
   4. if the name of the current animal goes after the name of th next animal,
      swap animals
   5. remember, the idiomatic way of swapping in python is a, b = b, a (but
      be careful, as you'll have to swap two elements in a list!)
   6. of course, once you're at the last element, there's no next element to
      check
   7. once you've gone through all of the animals... REPEAT the whole process
      if you've swapped at least once
   8. stop repeating the process when no swaps occur after going through the
      entire list
   9. here's an example of how it might work:
      element 1: swap with next
      [['zim', 'dog', 10], ['bev', 'dog', 10], ['ari', 'cat', 10]]
      element 2: swap with next
      [['bev', 'dog', 10], ['zim', 'dog', 10], ['ari', 'cat', 10]]
      element 3: last element, do not compare!
      [['bev', 'dog', 10], ['ari', 'cat', 10], ['zim', 'dog', 10]]
      element 1: swap with next
      [['bev', 'dog', 10], ['ari', 'cat', 10], ['zim', 'dog', 10]]
      element 2: do not swap!
      [['ari', 'cat', 10], ['bev', 'dog', 10], ['zim', 'dog', 10]]
      element 3: last element, do not compare!
      [['ari', 'cat', 10], ['bev', 'dog', 10], ['zim', 'dog', 10]]
      element 1: do not swap!
      [['ari', 'cat', 10], ['bev', 'dog', 10], ['zim', 'dog', 10]]
      element 2: do not swap!
      [['ari', 'cat', 10], ['bev', 'dog', 10], ['zim', 'dog', 10]]
      element 3: last element, do not compare!
      [['ari', 'cat', 10], ['bev', 'dog', 10], ['zim', 'dog', 10]]
      No swaps occurred... so stop repeating process.
* return value: a list of animals, alphabetically sorted by name
examples:
animal_list = [['sam', 'snake', 4], ['gertrude', 'goat', 99], ['ang', 'unicorn', 50]]
sorted_animal_list = sort_by_name(animal_list)
print(sorted_animal_list)
# will print out [['ang', 'unicorn', 50], ['gertrude', 'goat', 99], ['sam', 'snake', 4]]
print_animals:
-----
Prints out the list of animals using their name, kind, and the position they're in
with in the list (using 1 as the first animal, rather than 0).
* parameters: a list of animals
* processing:
   1. go through every animal in the list
   2. print out the animal's position, name and type
* return value: does not return anything
examples:
animal_list = [['sam', 'snake', 4], ['gertrude', 'goat', 99], ['ang', 'unicorn', 50]]
print_animals(animal_list)
prints out...
1 - sam, snake
2 - gertrude, goat
3 - ang, unicorn
get_most_urgent:
-----
This function will find the animal that has the most urgent issue in a list of animals.
* parameters: a list of animals (a list of lists)
* processing:
   1. iterates through entire list of animals
   2. checks the urgency value of each animal
   3. finds the animal with the highest urgency value
   4. if urgency values are the same, choose any animal (it's easiet to
      just choose the last one with the highest value)
* return value: a single animal (a list)
examples:
animal_list = [['sam', 'snake', 4], ['gertrude', 'goat', 99]]
print(get_most_urgent(animal_list))
# will print out ['gertrude', 'goat', 99]
get_least_urgent:
-----
This function will find the animal that has the least urgent issue in a list
of animals.
* parameters: a list of animals (a list of lists)
* processing:
   1. iterates through entire list of animals
   2. checks the urgency value of each animal
   3. finds the animal with the least urgency value
   4. if urgency values are the same, choose any animal (it's easiet to
      just choose the last one with the least value)
* return value: a single animal (a list)
examples:
animal_list = [['sam', 'snake', 4], ['gertrude', 'goat', 99]]
print(get_most_urgent(animal_list))
# will print out ['sam', 'snake', 4]
allowed_animal:
-----
This function will determine whether or not the kind of animal that is
brought in to the clinic can be treated by the vet.
* parameters: a string, the kind of animal (such as 'dog' or 'alligator')
* processing:
   1. this function will maintain a list of allowed animals
   2. these animals are 'dog', 'cat', 'snake', 'goat', 'pig', 'duck',
      'narwhale', and 'unicorn'
   2. it will determine if the string passed in, regardless of casing, is
      within the list of allowed animals
 return value: a boolean (True if the animal is allowed, False otherwise)
examples:
  Â
print(allowed_animal('alligator'))
# False
print(allowed_animal('dog'))
# will print out ['sam', 'snake', 4]
# True
"""
# part 1
# making functions that can accept a list of animals
def find_by_name(name, animals):
   # determine if animal with name exist in animal
   for a in animals:
       if name == a[0]:
           return a
   return none
animal_list = [['sam', 'snake', 4], ['gertrude', 'goat', 99]]
print(find_by_name('gertrude', animal_list))
# part 2 # need revising
def sort_by_name(animals):
   # determining if swap has occured
   swaps = True
   while swaps:
       # start out by sying we haven't swap anything yet
       swap = False
       for a in animals:
           for i in range(len(animals)):
               if ord(a[i-1][0]) >= ord(a[i][0]):
                   a[i-1],a[i] = a[i], a[i-1]
               return a
animals = [['sam', 'snake', 4], ['gertrude', 'goat', 99], ['ang', 'unicorn', 50]]
sorted_animals = sort_by_name(animals)
print(sorted_animals)
               swap
               my_list(indexq, mylist[index2], m)
               swaps = 0
              Â
# part 3Â Â Â Â Â Â Â
def print_animals(animals):
   count = 1
   for a in animals:
       print(count,"-", a[0], ",", a[1])
       count += 1
animals = [['sam', 'snake', 4], ['gertrude', 'goat', 99], ['ang', 'unicorn', 50]]
print_animals(animals)
# part 4
def get_most_urgent(animals):
   animal_urgent = []
   most_urgent = []
   for i in range(0, len(animals)):
       animal_urgent.append(animals[i][2])
       most_urgent = max(animal_urgent)
   for a in animals:
       if most_urgent == a[2]:
           return a
   return None
Â
animals = [['sam', 'snake', 4], ['gertrude', 'goat', 99], ['ang', 'unicorn', 50]]
print(get_most_urgent(animals))
# part 5Â
def get_least_urgent(animals):
   animal_urgent = []
   least_urgent = []
   for i in range(0, len(animals)):
       animal_urgent.append(animals[i][2])
       least_urgent = min(animal_urgent)
   for a in animals:
       if least_urgent == a[2]:
           return a
   return None
Â
animals = [['sam', 'snake', 4], ['gertrude', 'goat', 99], ['ang', 'unicorn', 50]]
print(get_least_urgent(animals))
# part 6
def allowed_animal(animal_kind):
   for a in animals:
       if a[1] != 'dog' or 'cat' or 'snake' or 'goat' or 'pig' or 'duck' or 'narwhale' or 'unicorn':
           return False
       else:
           return True
      Â
print(allowed_animal('shark'))
Â
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly