Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. #requires
  2. import math
  3. import operator
  4.  
  5. #
  6. # # Given the following formula,
  7. # #
  8. # # speed = ((STRIDE_LENGTH / LEG_LENGTH) - 1) * SQRT(LEG_LENGTH * g)
  9. # # Where g = 9.8 m/s^2 (gravitational constant)
  10. # #
  11. # # Write a program to  print the names
  12. # # of only the bipedal dinosaurs from fastest to slowest.
  13. # # Do not print any other information.
  14. #
  15. # # $ cat dataset1.csv
  16. # # NAME,LEG_LENGTH,DIET
  17. # # Hadrosaurus,1.2,herbivore
  18. # # Struthiomimus,0.92,omnivore
  19. # # Velociraptor,1.0,carnivore
  20. # # Euoplocephalus,1.6,herbivore
  21. # # Stegosaurus,1.40,herbivore
  22. # # Tyrannosaurus Rex,2.5,carnivore
  23. #
  24. # # $ cat dataset2.csv
  25. # # NAME,STRIDE_LENGTH,STANCE
  26. # # Euoplocephalus,1.87,quadrupedal
  27. # # Stegosaurus,1.90,quadrupedal
  28. # # Tyrannosaurus Rex,5.76,bipedal
  29. # # Hadrosaurus,1.4,bipedal
  30. # # Struthiomimus,1.34,bipedal
  31. # # Velociraptor,2.72,bipedal
  32. #
  33.  
  34. #name, leg, diet
  35. dataset1 = ("Hadrosaurus", 1.2, "herbivore"), \
  36.            ("Struthiomimus", 0.92, "omnivore"), \
  37.            ("Velociraptor", 1.0, "carnivore"), \
  38.            ("Euoplocephalus", 1.6, "herbivore"), \
  39.            ("Stegosaurus", 1.40, "herbivore"), \
  40.            ("Tyrannosaurus Rex", 2.5, "carnivore")
  41.  
  42. #name, stride, stance
  43. dataset2 = ("Euoplocephalus", 1.87, "quadrupedal"), \
  44.             ("Stegosaurus", 1.90,"quadrupedal"), \
  45.             ("Tyrannosaurus Rex", 5.76,"bipedal"), \
  46.             ("Hadrosaurus", 1.4,"bipedal"), \
  47.             ("Struthiomimus", 1.34, "bipedal"), \
  48.             ("Velociraptor", 2.72, "bipedal")
  49.  
  50. gravity = math.pow(9.8, 2)
  51.  
  52. leg_dict = {}
  53. stride_dict = {}
  54. stance_dict = {}
  55. diet_dict = {}
  56. dino_speed = {}
  57.  
  58. #Build dictionary set from first dataset
  59. for item in dataset1:
  60.     name, leg_length, diet = item
  61.     leg_dict[name] = leg_length
  62.     diet_dict[name] = diet
  63.  
  64. #Build dictionary set from second dataset
  65. for item2 in dataset2:
  66.     name, stride_length, stance = item2
  67.     stride_dict[name] = stride_length
  68.     stance_dict[name] = stance
  69.  
  70. #Build dictionary of dinosaur speed (could filter for bipedal here as well but dino_speed dict would be incomplete)
  71. for name, stance in stance_dict.items():
  72.     stride_length = stride_dict[name]
  73.     leg_length = leg_dict[name]
  74.     speed = ((stride_length / leg_length) - 1) * math.sqrt(leg_length * gravity)
  75.     dino_speed[name] = speed
  76.  
  77. #Sort by speed
  78. sorted_dinospeeds = sorted(dino_speed.items(), key=operator.itemgetter(1), reverse=True)
  79.  
  80. #filter for bipedal dinosaurs
  81. for name, speed in sorted_dinospeeds:
  82.     if stance_dict[name] is "bipedal":
  83.         print(name, speed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement