# In exercise 1, add up the numbers provided in the list S.
# This must be done using a for-loop, and the sum should be returned by the function.
def exercise1(S):
total_sum = 0
for num in S:
total_sum += num
return total_sum
# In exercise 2, add up the numbers provided in the list S using at most two lines of code
# The sum should be returned by the function.
def exercise2(S):
return sum(S)
## Exercise 3: In exercise 3, create a dictionary to store the values for the following list of points
## when input to the function "f(x,y) = 8x + 3y". For example, if your dictionary is called "f",
## you should have an entry "f[(9,8)] = 8" and an entry "f[(1,1)] = 11", and so on
## The dictionary should be returned by the function.## Points to include:
#- (0, 0)
#- (1,1)
#- (2, 2)
#- (3, 3)
#- (4, 4)
#- (5, 5)
def exercise3():
f = lambda x, y: 8*x + 3*y
result = {(x, y): f(x, y) for x in range(6) for y in range(6)}
return result
if __name__ == '__main__':
S = [1, 2, 3, 4, 5]
print("\nExercise 1:")
ex_sum = exercise1(S)
print(ex_sum)
print("\nExercise 2:")
ex_sum = exercise2(S)
print(ex_sum)
print("\nExercise 3:")
f = exercise3()
for key, val in f.items():
print("\tInput:", key, "Output:", val)
Associated Context | |
---|---|
Type | Code Snippet ( .py ) |
Associated Tags | List S Summing numbers Function return numpy Key-value pairs Looping through list Iteration Exercise 1 Input and output Array Manipulation Lambda function Python Programming For-Loop Function Exercise 2 Dictionary creation Dictionary Creation Function call Data Structures List of points |
💡 Smart Description | The code creates a dictionary to store the values for each point in an array S using at most two lines of code. It also includes functions that take input and output, which can be used as function parameters or arguments The code snippet includes three exercises. Exercise 1 calculates the sum of numbers in a list using a for-loop. Exercise 2 calculates the sum of numbers in a list using the built-in sum() function. Exercise 3 creates a |
🔎 Suggested Searches | function to calculate sum of two points in a list How to create a dictionary for each point using Python function to compute values from the input and output arrays Algorithm to find all possible elements within a list with an entry that is used by python Code to generate unique numbers based on its value Python code to sum numbers in a list using for-loop Python code to sum numbers in a list using two lines Python code to create a dictionary from a list of points Python code to calculate values for a given function Python code to iterate through a dictionary and print key-value pairs |
Related Links | https://www.tutorialspoint.com/python/python_for_loop.htm https://www.geeksforgeeks.org/python-programming-examples/#loops https://www.w3schools.com/python/python_for_loops.asp https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/ https://www.tutorialgateway.org/python-for-loop/ https://www.w3schools.com/python/ref_func_sum.asp https://realpython.com/iterate-through-dictionary-python/ https://www.geeksforgeeks.org/ https://www.geeksforgeeks.org/python-functions/ https://www.geeksforgeeks.org/python-string/ https://www.pythoncentral.io/how-to-sort-a-list-tuple-or-object-with-sorted-in-python/ https://www.datacamp.com/tutorial/loops-python-tutorial https://www.programiz.com/python-programming/examples/sum-natural-number https://www.learnpython.org/en/Loops |
Related People | David Merwin |
Sensitive Information | No Sensitive Information Detected |
Shareable Link | https://davidmerwin.pieces.cloud/?p=b3a04fb9b9 |
The code snippet includes three exercises. Exercise 1 calculates the sum of numbers in a list using a for-loop. Exercise 2 calculates the sum of numbers in a list using the built-in sum() function. Exercise 3 creates a
Exercise Functions D-Wave Python Prep.md
Exercise Functions
Preview:
In exercise 1, add up the numbers provided in the list S.
This must be done using a for-loop, and the sum should be returned by the function.
def exercise1(S):
total_sum = 0
for num in S:
total_sum += num
return total_sum
In exercise 2, add up the numbers provided in the list S using at most two lines of code
The sum should be returned by the function.
def exercise2(S):
return sum(S)
Exercise 3: In exercise 3, create a dictionary to store the values for the following list of points
when input to the function "f(x,y) = 8x + 3y". For example, if your dictionary is called "f",
you should have an entry "f[(9,8)] = 8" and an entry "f[(1,1)] = 11", and so on
The dictionary should be returned by the function.## Points to include:
#- (0, 0)
#- (1,1)
#- (2, 2)
#- (3, 3)
#- (4, 4)
#- (5, 5)
def exercise3():
f = lambda x, y: 8x + 3y
result = {(x, y): f(x, y) for x in range(6) for y in range(6)}
return result
if name == 'main':
S = [1, 2, 3, 4, 5]
@davidmerwin1992 @dwaveoceanbot