-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogic-2.py
178 lines (130 loc) · 4.37 KB
/
Logic-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 14:39:22 2019
@author: YaoJunyan
"""
'''
make_bricks
We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return True if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks
make_bricks(3, 1, 8) → True
make_bricks(3, 1, 9) → False
make_bricks(3, 2, 10) → True
'''
def make_bricks(small, big, goal):
#find answer from stackoverflow
if goal > big *5 +small:
return False
else:
return goal %5 <= small
'''lone_sum
Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.
lone_sum(1, 2, 3) → 6
lone_sum(3, 2, 3) → 2
lone_sum(3, 3, 3) → 0
'''
def lone_sum(a, b, c):
if a==b==c:
return 0
elif a== b:
return c
elif a==c:
return b
elif b==c:
return a
else:
return a+b+c
'''
lucky_sum
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.
lucky_sum(1, 2, 3) → 6
lucky_sum(1, 2, 13) → 3
lucky_sum(1, 13, 3) → 1
'''
def lucky_sum(a, b, c):
if a==13:
return 0
if b==13:
return a
if c==13:
return a+b
else:
return a+b+c
'''no_teen_sum
Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main no_teen_sum().
no_teen_sum(1, 2, 3) → 6
no_teen_sum(2, 13, 1) → 3
no_teen_sum(2, 1, 14) → 3
'''
def fix_teen(num):
if num in range(13,15):
return True
if num in range(17,20):
return True
else:
return False
def no_teen_sum(a, b, c):
if fix_teen(a) and fix_teen(b) and fix_teen(c):
return 0
if fix_teen(a):
if fix_teen(b):
return c
return b+c
if fix_teen(b):
if fix_teen(c):
return a
return a+c
if fix_teen(c):
if fix_teen(a):
return b
return a+b
else:
return a+b+c
'''
round_sum
For this problem, we'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. To avoid code repetition, write a separate helper "def round10(num):" and call it 3 times. Write the helper entirely below and at the same indent level as round_sum().
round_sum(16, 17, 18) → 60
round_sum(12, 13, 14) → 30
round_sum(6, 4, 4) → 10
'''
def round10(n):
result = None
if n %10 < 5:
result = round(n,-1)
else:
result= round(n+5,-1)
return int(result)
def round_sum(a, b, c):
return round10(a)+round10(b)+round10(c)
'''close_far
Given three ints, a b c, return True if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: abs(num) computes the absolute value of a number.
close_far(1, 2, 10) → True
close_far(1, 2, 3) → False
close_far(4, 1, 3) → True
'''
def close_far(a, b, c):
if abs(a-b)<=1 and abs(b-c)>=2 and abs(a-c)>=2:
return True
elif abs(a-c) <=1 and abs(b-c)>=2 and abs(a-b)>=2:
return True
else:
return False
'''make_chocolate
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.
make_chocolate(4, 1, 9) → 4
make_chocolate(4, 1, 10) → -1
make_chocolate(4, 1, 7) → 2
'''
def make_chocolate(small, big, goal):
if goal-5*big>small:
return -1
else:
if goal>=5*big:
return (goal-5*big)
else:
numbbig=goal//5
numbsmall=goal%5
if numbsmall>small:
return -1
else:
return numbsmall