Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #requires
- import math
- import operator
- #
- # # Given the following formula,
- # #
- # # speed = ((STRIDE_LENGTH / LEG_LENGTH) - 1) * SQRT(LEG_LENGTH * g)
- # # Where g = 9.8 m/s^2 (gravitational constant)
- # #
- # # Write a program to print the names
- # # of only the bipedal dinosaurs from fastest to slowest.
- # # Do not print any other information.
- #
- # # $ cat dataset1.csv
- # # NAME,LEG_LENGTH,DIET
- # # Hadrosaurus,1.2,herbivore
- # # Struthiomimus,0.92,omnivore
- # # Velociraptor,1.0,carnivore
- # # Euoplocephalus,1.6,herbivore
- # # Stegosaurus,1.40,herbivore
- # # Tyrannosaurus Rex,2.5,carnivore
- #
- # # $ cat dataset2.csv
- # # NAME,STRIDE_LENGTH,STANCE
- # # Euoplocephalus,1.87,quadrupedal
- # # Stegosaurus,1.90,quadrupedal
- # # Tyrannosaurus Rex,5.76,bipedal
- # # Hadrosaurus,1.4,bipedal
- # # Struthiomimus,1.34,bipedal
- # # Velociraptor,2.72,bipedal
- #
- #name, leg, diet
- dataset1 = ("Hadrosaurus", 1.2, "herbivore"), \
- ("Struthiomimus", 0.92, "omnivore"), \
- ("Velociraptor", 1.0, "carnivore"), \
- ("Euoplocephalus", 1.6, "herbivore"), \
- ("Stegosaurus", 1.40, "herbivore"), \
- ("Tyrannosaurus Rex", 2.5, "carnivore")
- #name, stride, stance
- dataset2 = ("Euoplocephalus", 1.87, "quadrupedal"), \
- ("Stegosaurus", 1.90,"quadrupedal"), \
- ("Tyrannosaurus Rex", 5.76,"bipedal"), \
- ("Hadrosaurus", 1.4,"bipedal"), \
- ("Struthiomimus", 1.34, "bipedal"), \
- ("Velociraptor", 2.72, "bipedal")
- gravity = math.pow(9.8, 2)
- leg_dict = {}
- stride_dict = {}
- stance_dict = {}
- diet_dict = {}
- dino_speed = {}
- #Build dictionary set from first dataset
- for item in dataset1:
- name, leg_length, diet = item
- leg_dict[name] = leg_length
- diet_dict[name] = diet
- #Build dictionary set from second dataset
- for item2 in dataset2:
- name, stride_length, stance = item2
- stride_dict[name] = stride_length
- stance_dict[name] = stance
- #Build dictionary of dinosaur speed (could filter for bipedal here as well but dino_speed dict would be incomplete)
- for name, stance in stance_dict.items():
- stride_length = stride_dict[name]
- leg_length = leg_dict[name]
- speed = ((stride_length / leg_length) - 1) * math.sqrt(leg_length * gravity)
- dino_speed[name] = speed
- #Sort by speed
- sorted_dinospeeds = sorted(dino_speed.items(), key=operator.itemgetter(1), reverse=True)
- #filter for bipedal dinosaurs
- for name, speed in sorted_dinospeeds:
- if stance_dict[name] is "bipedal":
- print(name, speed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement