LPP_alu0100966589 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.9.12
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ </div>
80
+
81
+ <h2>Defined Under Namespace</h2>
82
+ <p class="children">
83
+
84
+
85
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="TDD.html" title="TDD (module)">TDD</a></span>
86
+
87
+
88
+
89
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span>, <span class='object_link'><a href="CarbohydrateRich.html" title="CarbohydrateRich (class)">CarbohydrateRich</a></span>, <span class='object_link'><a href="Dll.html" title="Dll (class)">Dll</a></span>, <span class='object_link'><a href="DllNode.html" title="DllNode (class)">DllNode</a></span>, <span class='object_link'><a href="EggMilkAndDerivatives.html" title="EggMilkAndDerivatives (class)">EggMilkAndDerivatives</a></span>, <span class='object_link'><a href="Fish.html" title="Fish (class)">Fish</a></span>, <span class='object_link'><a href="Food.html" title="Food (class)">Food</a></span>, <span class='object_link'><a href="Fruits.html" title="Fruits (class)">Fruits</a></span>, <span class='object_link'><a href="MeatAndDerivatives.html" title="MeatAndDerivatives (class)">MeatAndDerivatives</a></span>, <span class='object_link'><a href="OilsAndSweets.html" title="OilsAndSweets (class)">OilsAndSweets</a></span>, <span class='object_link'><a href="Vegetables.html" title="Vegetables (class)">Vegetables</a></span>
90
+
91
+
92
+ </p>
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+ </div>
103
+
104
+ <div id="footer">
105
+ Generated on Tue Nov 28 21:26:18 2017 by
106
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
+ 0.9.12 (ruby-2.3.0).
108
+ </div>
109
+
110
+ </div>
111
+ </body>
112
+ </html>
@@ -0,0 +1,12 @@
1
+ require "TDD/version"
2
+ require "TDD/Food"
3
+ require "TDD/DllNode"
4
+ require "TDD/Dll"
5
+ require "TDD/FoodPyramidClasses"
6
+ require "TDD/sort.rb"
7
+ require "TDD/Plate.rb"
8
+
9
+
10
+ module TDD
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,80 @@
1
+ class Dll
2
+ include Enumerable
3
+
4
+ # Getter for the head node
5
+ # @return [DllNode] The first node on the list or nil if there is none
6
+ attr_reader :head
7
+
8
+ # Getter for the tail node
9
+ # @return [DllNode] The last node on the list or nil if there is none
10
+ attr_reader :tail
11
+
12
+ # Initializes to an empty list
13
+ def initialize
14
+ @head = nil
15
+ @tail = nil
16
+ end
17
+
18
+ # Insert a new value, having it be the first on the list
19
+ # @param value What you want to be inserted to the list
20
+ def insert_head(value)
21
+ if( @head == nil )
22
+ @head = DllNode.new( nil, value, nil )
23
+ @tail = @head
24
+ else
25
+ aux = DllNode.new( nil, value, @head )
26
+ @head.prev = aux
27
+ @head = aux
28
+ end
29
+ end
30
+
31
+ # Insert a new value, having it be the last on the list
32
+ # @param value What you want to be inserted to the list
33
+ def insert_tail(value)
34
+ if( @tail == nil )
35
+ @head = DllNode.new( nil, value, nil )
36
+ @tail = @head
37
+ else
38
+ aux = DllNode.new( @tail, value, nil )
39
+ @tail.next = aux
40
+ @tail = aux
41
+ end
42
+ end
43
+
44
+ # Deletes the first element on the list
45
+ def extract_head
46
+ if( @head != nil )
47
+ if( @head.next == nil )
48
+ @head = nil
49
+ @tail = nil
50
+ else
51
+ @head = @head.next
52
+ @head.prev.next = nil
53
+ @head.prev = nil
54
+ end
55
+ end
56
+ end
57
+
58
+ # Deletes the last element on the list
59
+ def extract_tail
60
+ if( @tail != nil )
61
+ if( @tail.prev == nil)
62
+ @tail = nil
63
+ @head = nil
64
+ else
65
+ @tail = @tail.prev
66
+ @tail.next.prev = nil
67
+ @tail.next = nil
68
+ end
69
+ end
70
+ end
71
+
72
+ # Iterates over the list and yields for each item. The yield passes the value of each element to the block
73
+ def each
74
+ curr_node = @head
75
+ while (curr_node != nil)
76
+ yield curr_node.value
77
+ curr_node = curr_node.next
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ # Struct that represents a node of a Double Linked List.
2
+ # @note "prev" and "next" are nodes, the previous and next one respectively. Value can be of any type
3
+ DllNode = Struct.new(:prev, :value, :next)
@@ -0,0 +1,110 @@
1
+ class Food
2
+ # Names of the nutrients
3
+ @@nutrients_name = ['proteins', 'carbohydrates', 'fats']
4
+ # Energy value (in Kcal) that each nutrient type gives
5
+ @@nutients_energy_value = [4, 4, 9]
6
+
7
+ # Getter for the nutrients name
8
+ # @return [Array<String>] Array with the names of the nutrients
9
+ def self.nutrients_name
10
+ @@nutrients_name
11
+ end
12
+
13
+
14
+ include Comparable
15
+
16
+ # @return [String] Name of the food
17
+ attr_reader :name
18
+
19
+ # @return [Array<Float>] Quantity in grams of each nutrient
20
+ attr_reader :nutrient_quantities
21
+
22
+ # @return [Float] Total energetic value of the food
23
+ attr_reader :energetic_value
24
+
25
+ # @return [Float | nil] Glycemic index for this food if it is calculated, nil otherwise
26
+ attr_reader :glycemic_index
27
+
28
+ # @param name [String] Name of the food
29
+ # @param nutrient_quantities [Array<FixNum>] An array with the values for the quantities of nutrients 0:grams of proteins, 1:grams of carbohydrates, 2:grams of fats
30
+ def initialize( name, nutrient_quantities )
31
+ raise( ArgumentError, 'Invalid number of nutrients values' ) unless (nutrient_quantities.length == @@nutrients_name.length)
32
+ @name = name.downcase
33
+
34
+ # How much of each nutrient this food has
35
+ @nutrient_quantities = nutrient_quantities
36
+
37
+ # Total energetic value of this food instance
38
+ @energetic_value = 0
39
+ @nutrient_quantities.length.times do |i|
40
+ @energetic_value += @nutrient_quantities[i] * @@nutients_energy_value[i]
41
+ end
42
+
43
+ # Setting the glycemic index to a default value
44
+ @glycemic_index = nil
45
+ end
46
+
47
+ # Given the name of a nutrient, #quantity_of_nutrient returns the quantity (in grams) of that nutrient
48
+ # @param n [String] The name of the nutrient. The value has to be one of "proteins", "carbohydrates", "fats"
49
+ # @return [Float] The quantity (in grams) of the nutrient requested that the food has
50
+ def quantity_of_nutrient( n )
51
+ # index method returns nil if the element doesn't exist
52
+ index = @@nutrients_name.index( n )
53
+ raise( ArgumentError, 'Invalid nutrient name' ) unless index
54
+
55
+ return @nutrient_quantities[ index ]
56
+ end
57
+
58
+ # to_s method
59
+ # @return [String] The format is "<food_name>: <protein_quantity>g, <carbohydrates_quantity>g, <fat_quantity>g"
60
+ def to_s
61
+ result = @name + ': '
62
+ (@nutrient_quantities.length - 1).times do |i|
63
+ result += "#{@nutrient_quantities[i]}g of #{@@nutrients_name[i]}, "
64
+ end
65
+ result += "#{@nutrient_quantities[-1]}g of #{@@nutrients_name[-1]}"
66
+ end
67
+
68
+ # <=> operator, defined so instances could be comparable
69
+ # @note We consider two foods to be equal if their energetic value is the samew
70
+ # @return [Fixnum]
71
+ def <=>(o)
72
+ return self.energetic_value <=> o.energetic_value
73
+ end
74
+
75
+ # Time interval between metrics (in minutes)
76
+ DELTA_TIME = 5.0
77
+
78
+ # Calculates the incremental area under the curve for this food given the glucose levels in blood for a person.
79
+ # @return [Float] Area under the curve of the metrics
80
+ # @param food_metrics [Array<Float>] Glucose in blood metrics of a person, taken each DELTA_TIME minutes for a given food
81
+ def iauc(food_metrics)
82
+ raise ArgumentError.new('Need at least 2 values to calculate the first trapezoid area') unless (food_metrics.length > 1)
83
+
84
+ DELTA_TIME/2.0 * (food_metrics.drop(1).reduce(0.0, :+) + food_metrics.take(food_metrics.length - 1).reduce(0.0, :+) - 2.0 * food_metrics[0] * food_metrics.length)
85
+ end
86
+
87
+ # Calculates the glycemic index of a person for the given food
88
+ # @return [Float] Glycemic index of a person for the given food
89
+ # @param food_metrics (see #iauc)
90
+ # @param glucose_metrics [Array<Float>] Glucose in blood metrics of a person, taken each DELTA_TIME minutes for pure glucose
91
+ def individual_gi(food_metrics, glucose_metrics)
92
+ raise ArgumentError.new("Sizes don't match") unless food_metrics.length == glucose_metrics.length
93
+
94
+ iauc(food_metrics) / iauc(glucose_metrics) * 100.0
95
+ end
96
+
97
+ # Calculates the glycemic index for the food given data of various people
98
+ # @return [Food] Returns selfs so other methods could be chained
99
+ # @param [Array<Hash<Symbol, Array<Float>>>] People metrics, each hash in the array is the data for a person, the hash has to have :food_metrics and :glucose_metrics keys which values are the arrays corresponding to the glucose in blood metric after the food and the pure glucose respectively.
100
+ def gi(people_metrics)
101
+ people_igs = []
102
+ people_metrics.each do |person_metrics|
103
+ people_igs << individual_gi(person_metrics[:food_metrics], person_metrics[:glucose_metrics])
104
+ end
105
+
106
+ @glycemic_index = people_igs.reduce(0.0, :+) / people_igs.length
107
+ return self
108
+ end
109
+
110
+ end
@@ -0,0 +1,20 @@
1
+ class EggMilkAndDerivatives < Food
2
+ end
3
+
4
+ class MeatAndDerivatives < Food
5
+ end
6
+
7
+ class Fish < Food
8
+ end
9
+
10
+ class OilsAndSweets < Food
11
+ end
12
+
13
+ class CarbohydrateRich < Food
14
+ end
15
+
16
+ class Vegetables < Food
17
+ end
18
+
19
+ class Fruits < Food
20
+ end
@@ -0,0 +1,124 @@
1
+ class Plate
2
+ attr_reader :name, :ingredient_quantities, :ingredients
3
+
4
+ @@food = {
5
+ 'fried egg' => EggMilkAndDerivatives.new('fried egg', [14.1, 0.0, 19.5]),
6
+ 'milk' => EggMilkAndDerivatives.new('milk', [3.3, 4.8, 3.2]),
7
+ 'yogurt' => EggMilkAndDerivatives.new('yogurt', [3.8, 4.9, 3.8]),
8
+ 'pork' => MeatAndDerivatives.new('pork', [21.5, 0.0, 6.3]),
9
+ 'beef' => MeatAndDerivatives.new('beef', [21.1, 0.0, 3.1]),
10
+ 'chicken' => MeatAndDerivatives.new('chicken', [20.6, 0.0, 5.6]),
11
+ 'cod' => Fish.new('cod', [17.7, 0.0, 0.4]),
12
+ 'tuna' => Fish.new('tuna', [21.5, 0.0, 15.5]),
13
+ 'salmon' => Fish.new('salmon', [19.9, 0.0, 13.6]),
14
+ 'olive oil' => OilsAndSweets.new('olive oil', [0.0, 0.2, 99.6]),
15
+ 'butter' => OilsAndSweets.new('butter', [0.7, 0.0, 83.2]),
16
+ 'chocolate' => OilsAndSweets.new('chocolate', [5.3, 47.0, 30.0]),
17
+ 'sugar' => CarbohydrateRich.new('sugar', [0.0, 99.8, 0.0]),
18
+ 'rice' => CarbohydrateRich.new('rice', [6.8, 77.7, 0.6]),
19
+ 'lentils' => CarbohydrateRich.new('lentils', [23.5, 52.0, 1.4]),
20
+ 'potatoe' => CarbohydrateRich.new('potatoe', [2.0, 15.4, 0.1]),
21
+ 'tomato' => Vegetables.new('tomato', [1.0, 3.5, 0.2]),
22
+ 'onion' => Vegetables.new('onion', [1.3, 5.8, 0.3]),
23
+ 'pumpkin' => Vegetables.new('pumpkin', [1.1, 4.8, 0.1]),
24
+ 'apple' => Fruits.new('apple', [0.3, 12.4, 0.4]),
25
+ 'banana' => Fruits.new('banana', [1.2, 21.4, 0.2]),
26
+ 'pear' => Fruits.new('pear', [0.5, 12.7, 0.3])
27
+ }
28
+
29
+ # Getter for the food hash
30
+ # @return [Hash<String, Food>] Hash mapping names to food instances
31
+ def self.food
32
+ @@food
33
+ end
34
+
35
+ def initialize(name, &block)
36
+ @name = name
37
+ @ingredients = []
38
+ @ingredient_quantities = []
39
+
40
+ if block_given?
41
+ if block.arity == 1
42
+ yield self
43
+ else
44
+ instance_eval(&block)
45
+ end
46
+ end
47
+ end
48
+
49
+ def add_ingredient(name, quantity)
50
+ ingredient = @@food[name.strip.downcase]
51
+ raise( ArgumentError, "Invalid ingredient: #{name}" ) unless !ingredient.nil?
52
+
53
+ # Add the ingredient to the list
54
+ @ingredients << ingredient
55
+
56
+ # Little convenient lambda to not repeat code below
57
+ # Given a key of quantity, it checks that the value is Numeric.
58
+ # If it is numeric, its value times the multiplier is added to
59
+ # the @ingredient_quantities array
60
+ check_and_add_amount = ->(key, multiplier = 1.0) {
61
+ amount = quantity[key]
62
+ raise( ArgumentError, "Invalid amount type: '#{amount}' must be of type Numeric" ) unless amount.is_a? Numeric
63
+ @ingredient_quantities << amount * multiplier
64
+ }
65
+
66
+ # Check what metric is used for the quantity and add
67
+ if quantity.has_key?(:grams)
68
+ check_and_add_amount.call(:grams, 1.0)
69
+
70
+ elsif quantity.has_key?(:ounces)
71
+ check_and_add_amount.call(:ounces, 28.35)
72
+
73
+ elsif quantity.has_key?(:liters)
74
+ check_and_add_amount.call(:liters, 1.0)
75
+
76
+ elsif quantity.has_key?(:mililiters)
77
+ check_and_add_amount.call(:mililiters, 1.0 / 1000.0)
78
+
79
+ elsif quantity.has_key?(:portion)
80
+ # Get the string and convert it to an array
81
+ value = quantity[:portion].split(" ")
82
+
83
+ # Get the quantity and the string
84
+ amount = value[0].to_f
85
+ str = value[1..-1].join(" ").downcase
86
+
87
+ case str
88
+ when "cup", "cups"
89
+ amount *= 1.0
90
+ when "teaspoon", "teaspoons"
91
+ amount *= 0.3
92
+ when "pinch"
93
+ amount *= 0.1
94
+ when "piece", "pieces"
95
+ amount *= 1.0
96
+ else raise( ArgumentError, "Unknown metric: #{str}" )
97
+ end
98
+
99
+ @ingredient_quantities << amount
100
+
101
+ else raise( ArgumentError, "Invalid amount metric in definition of ingredient #{name}" )
102
+ end
103
+ end
104
+
105
+ def method_missing(methodName, *args, &block)
106
+ add_ingredient(*args, &block)
107
+ end
108
+
109
+ def to_s
110
+ result = "\n#{@name}\n===\nNutritional composition\n"
111
+ totalEnergeticValue = 0
112
+
113
+ @ingredients.length.times do |i|
114
+ n = @ingredients[i].nutrient_quantities
115
+ energeticValue = @ingredients[i].energetic_value * @ingredient_quantities[i]
116
+
117
+ totalEnergeticValue += energeticValue
118
+ result += "#{@ingredients[i].name}\t\t\t#{n[0]}\t#{n[1]}\t#{n[2]}\t#{energeticValue}\n"
119
+ end
120
+
121
+ result += "total energetic value\t\t\t\t#{totalEnergeticValue}\n"
122
+
123
+ end
124
+ end