Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Python/random_ints.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import random

def random_ints():
# Your code here!

# initialize an empty list to store the numbers
numbers = []
# initialize a variable to store the current number
number = 0
# loop until the number is 6
while number != 6:
# generate a random number in the range [1,10]
number = int(random.random()*10) + 1
# append the number to the list
numbers.append(number)
return numbers

def test():
N = 10000
Expand Down
21 changes: 18 additions & 3 deletions Python/rm_smallest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
# take in argument d
def rm_smallest(d):
# Your code here!
return 0;
# if the dictionary is empty, return the original dictionary
if not d:
return d

# find the minimum value in the dictionary
min_value = min(d.values())

# find the key(s) that correspond to the minimum value
min_keys = [k for k, v in d.items() if v == min_value]

# remove the key-value pair(s) from the dictionary
for k in min_keys:
d.pop(k)

# return the modified dictionary
return d

def test():
assert 'a' in rm_smallest({'a':1,'b':-10}).keys()
assert not 'b' in rm_smallest({'a':1,'b':-10}).keys()
assert not 'a' in rm_smallest({'a':1,'b':5,'c':3}).keys()
rm_smallest({})
assert rm_smallest({}) == {}
print("Success!")

if __name__ == "__main__":
Expand Down
9 changes: 6 additions & 3 deletions Python/square_root.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import math

# takes a single argument, n.
def square_root(n):
# Your code here!
return -1;
if isinstance(n, (int, float)):
if n >= 0:
return math.sqrt(n)
return -1


def test():
assert square_root(4) == 2
Expand Down
11 changes: 9 additions & 2 deletions Python/sum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
def sum(lst, n):
# Your code here!
return False
# initialize the sum to zero
total = 0

# loop over the list and add each element to the total
for num in lst:
total += num

# check if the sum equals the given number n and return the appropriate boolean value
return total == n

def test():
assert sum([-1, 1], 0)
Expand Down