-
Notifications
You must be signed in to change notification settings - Fork 0
/
062_Smallest value of an array.py
67 lines (45 loc) · 1.75 KB
/
062_Smallest value of an array.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
"""
Codewars Coding Challenge
Day 62/366
Level 7kyu
Smallest value of an array
Write a function that can return the smallest value of an array or the index of that value. The function's 2nd parameter will tell whether it should return the value or the index.
Assume the first parameter will always be an array filled with at least 1 number and no duplicates. Assume the second parameter will be a string holding one of two values: 'value' and 'index'.
min([1,2,3,4,5], 'value') // => 1
min([1,2,3,4,5], 'index') // => 0
def find_smallest(numbers, to_return):
return 0
https://www.codewars.com/kata/544a54fd18b8e06d240005c0/train/python
"""
# My Solution
def find_smallest(numbers, to_return):
if to_return == "value":
return min(numbers)
else:
return numbers.index(min(numbers))
# Test
print(find_smallest([1,2,3,4], "value"))
print(find_smallest([4,3,5,2], "index"))
"""
Sample Tests
import codewars_test as test
from solution import find_smallest
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(find_smallest([5,4,3,2,1],"value"),1)
test.assert_equals(find_smallest([5,4,3,2,1],"index"),4)
test.assert_equals(find_smallest([ 8, 0, 9],"index"),1)
test.assert_equals(find_smallest([ 8, 0, 9],"value"),0)
test.assert_equals(find_smallest([ 1, 1, 0, 0, 1, 1],"value"),0)
test.assert_equals(find_smallest([ 1, 1, 0, 0, 1, 1],"index"),2)
test.assert_equals(find_smallest([0], 'value'), 0)
test.assert_equals(find_smallest([0], 'index'), 0)
"""
"""
Solutions From Codewars
=1=
def find_smallest(numbers,to_return):
return min(numbers) if to_return == 'value' else numbers.index(min(numbers))
"""