Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
0
02465students
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
02465material
02465students
Commits
74578260
Commit
74578260
authored
5 months ago
by
tuhe
Browse files
Options
Downloads
Patches
Plain Diff
Fruit homework fixes
parent
f889866a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
irlc/ex00/fruit_homework.py
+37
-25
37 additions, 25 deletions
irlc/ex00/fruit_homework.py
with
37 additions
and
25 deletions
irlc/ex00/fruit_homework.py
+
37
−
25
View file @
74578260
...
@@ -18,41 +18,53 @@ def mean_value(p_dict : dict) -> float:
...
@@ -18,41 +18,53 @@ def mean_value(p_dict : dict) -> float:
Hint: Look at the .items() method and the build-in sum(my_list) method.
"""
Hint: Look at the .items() method and the build-in sum(my_list) method.
"""
# TODO: 1 lines missing.
# TODO: 1 lines missing.
raise
NotImplementedError
(
"
Implement function body
"
)
raise
NotImplementedError
(
"
Implement function body
"
)
#
# def fruits_ordered(order_dict):
class
BasicFruitShop
:
"""
This is a simple class that represents a fruit-shop.
"""
This is a simple class that represents a fruit-shop.
You should give it a dictionary of prices when you initialize it.
"""
You should give it a dictionary of prices when you initialize it.
"""
def
__init__
(
self
,
name
:
str
,
prices
:
dict
):
"""
prices is a dictionary of the form {fruit_name: cost}. For instance
"""
prices is a dictionary of the form {fruit_name: cost}. For instance
prices = {
'
apple
'
: 5,
'
orange
'
: 6}
"""
prices = {
'
apple
'
: 5,
'
orange
'
: 6}
"""
self
.
name
=
name
self
.
prices
=
prices
def
cost
(
self
,
fruit
:
str
)
->
float
:
"""
Return the cost in pounds of the fruit with name
'
fruit
'
. It uses the self.prices variable
"""
Return the cost in pounds of the fruit with name
'
fruit
'
. It uses the self.prices variable
to get the price.
to get the price.
You don
'
t need to do exception handling here.
"""
You don
'
t need to do exception handling here.
"""
# """
# TODO: 1 lines missing.
# order_dict = {'apple': 5, 'pear': 2, ...} where the numbers are the quantity ordered.
raise
NotImplementedError
(
"
Return cost of fruit as a floating point number
"
)
#
# Hints: Dictionary comprehension like:
# > for fruit, pounds in order_dict.items()
if
__name__
==
'
__main__
'
:
# > self.getCostPerPound(fruit) allows you to get cost of a fruit
"
This code runs when you invoke the script from the command line (but not otherwise)
"
# > the total is sum of {pounds} * {cost_per_pound}
# """
# """
# order_dict: dictionary {'apple': 3, ...} of fruits and the pounds ordered
# fruitShops: List of OnlineFruitShops
#
# Hints:
# > Remember there is a s.price_of_order method
# > Use this method to first make a list containing the cost of the order at each fruit shop
# > List has form [cost1, cost2], then find the index of the smallest value (the list has an index-function)
# > return fruitShops[lowest_index].
# """
"""
Quesion 1: Lists and basic data types
"""
"""
Quesion 1: Lists and basic data types
"""
print
(
"
add(2,5) function should return 7, and it returned
"
,
add
(
2
,
5
))
animals
=
[
"
cat
"
,
"
giraffe
"
,
"
wolf
"
]
print
(
"
The nice animals are
"
,
misterfy
(
animals
))
"""
"""
This problem represents the probabilities of a loaded die as a dictionary such that
This problem represents the probabilities of a loaded die as a dictionary such that
> p(roll=3) = p_dict[3] = 0.15.
> p(roll=3) = p_dict[3] = 0.15.
"""
"""
p_die
=
{
1
:
0.20
,
2
:
0.10
,
3
:
0.15
,
4
:
0.05
,
5
:
0.10
,
6
:
0.40
}
print
(
"
Mean roll of die, sum_{i=1}^6 i * p(i) =
"
,
mean_value
(
p_die
))
"""
Part B: A simple class
"""
"""
Part B: A simple class
"""
# """ Part C: Class inheritance """
price1
=
{
"
apple
"
:
4
,
"
pear
"
:
8
,
'
orange
'
:
10
}
# """ Part C: Using classes """
shop1
=
BasicFruitShop
(
"
Alis Funky Fruits
"
,
price1
)
# TODO: 60 lines missing.
raise
NotImplementedError
(
"
Implement function body
"
)
price2
=
{
'
banana
'
:
9
,
"
apple
"
:
5
,
"
pear
"
:
7
,
'
orange
'
:
11
}
# print("For the order", order, " the best shop is", shop_smart(order, shops).name)
shop2
=
BasicFruitShop
(
"
Hansen Fruit Emporium
"
,
price2
)
fruit
=
"
apple
"
print
(
"
The cost of
"
,
fruit
,
"
in
"
,
shop1
.
name
,
"
is
"
,
shop1
.
cost
(
fruit
))
print
(
"
The cost of
"
,
fruit
,
"
in
"
,
shop2
.
name
,
"
is
"
,
shop2
.
cost
(
fruit
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment