-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzles.py
More file actions
62 lines (52 loc) · 1.67 KB
/
Puzzles.py
File metadata and controls
62 lines (52 loc) · 1.67 KB
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
# problems pulled from codingbat.com
# and format adapted from google python class:
# http://code.google.com/edu/languages/google-python-class
#==================SUM~28==============/
# Problem statement:
# Given an array of ints,
# return true if the sum of all the 2's
# in the array is exactly 8.
# Example input -> output:
# sum28([2, 3, 2, 2, 4, 2]) -> true
# sum28([2, 3, 2, 2, 4, 2, 2]) -> false
# sum28([1, 2, 3, 4]) -> false
def sum28(nums):
# +++your code here+++
sum = 0
for int in nums
if int == 2
sum += int
return sum == 8
#=================ONLY~14==================/
# Problem statement:
# Given an array of ints, return true if every element is a 1 or a 4.
# Example input -> output:
# only14([1, 4, 1, 4]) -> true
# only14([1, 4, 2, 4]) -> false
# only14([1, 1]) -> true
def only14(nums):
# +++your code here+++
return
# Provided simple test() function in main() to print
# what each function returns vs. what it's supposed to return
def test(got, expected):
if got == expected:
prefix = ' OK '
else:
prefix = ' X '
print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))
# Provided main() calls the above functions with a ew winputs
# using test() to check if each result is correct or not
def main():
print 'sum28'
test(sum28([2, 3, 2, 2, 4, 2]), True)
test(sum28([1, 2, 3, 4]), False)
test(sum28([2]), False)
print
print 'only14'
test(only14([1, 4, 1, 4]), True)
test(only14([1, 4, 2, 4]), True)
test(only14([1, 1, 1, 5]), True)
# Standard boilerplate to call the main() function
if __name__ == '__main__':
main()